From 0feb020be80bb6079cbf455cc64bac72dbe01f91 Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Fri, 13 Sep 2019 01:03:47 +0200 Subject: [PATCH] Added remove_entity --- ecs-lua/src/ecs-lua.cpp | 1 + ecs/include/ecs.h | 2 ++ ecs/src/ecs.cpp | 10 ++++++++++ 3 files changed, 13 insertions(+) diff --git a/ecs-lua/src/ecs-lua.cpp b/ecs-lua/src/ecs-lua.cpp index 8413f30..36a5962 100644 --- a/ecs-lua/src/ecs-lua.cpp +++ b/ecs-lua/src/ecs-lua.cpp @@ -57,6 +57,7 @@ namespace ecs::lua { // @todo Allow to construct with given uuid return thiz->create_entity(); }, + "remove_entity", &Manager::remove_entity, "has_entity", [] (Manager* thiz, std::string uuid) { // @todo Check if valid return thiz->has_entity(uuids::uuid::from_string(uuid).value()); diff --git a/ecs/include/ecs.h b/ecs/include/ecs.h index 40b1f24..d7a27f3 100644 --- a/ecs/include/ecs.h +++ b/ecs/include/ecs.h @@ -198,6 +198,8 @@ namespace ecs { Entity* create_entity(uuids::uuid uuid); Entity* create_entity(); + void remove_entity(ecs::Entity* entity); + bool has_entity(uuids::uuid uuid) { // @todo c++20 has .contains() return _entities.count(uuid); diff --git a/ecs/src/ecs.cpp b/ecs/src/ecs.cpp index cee45f3..a23c1a1 100644 --- a/ecs/src/ecs.cpp +++ b/ecs/src/ecs.cpp @@ -77,6 +77,16 @@ namespace ecs { return create_entity(uuid); } + void Manager::remove_entity(ecs::Entity* entity) { + auto it = _entities.find(entity->uuid); + if (it == _entities.end()) { + throw std::runtime_error("Entity with uuid does not exists"); + } + + delete it->second; + _entities.erase(it); + } + Entity* Manager::create_entity(uuids::uuid uuid) { Entity* entity = new Entity(uuid); _entities.insert({uuid, entity});