Added remove_entity

This commit is contained in:
Dreaded_X 2019-09-13 01:03:47 +02:00
parent ac463f0669
commit 0feb020be8
3 changed files with 13 additions and 0 deletions

View File

@ -57,6 +57,7 @@ namespace ecs::lua {
// @todo Allow to construct with given uuid // @todo Allow to construct with given uuid
return thiz->create_entity(); return thiz->create_entity();
}, },
"remove_entity", &Manager::remove_entity,
"has_entity", [] (Manager* thiz, std::string uuid) { "has_entity", [] (Manager* thiz, std::string uuid) {
// @todo Check if valid // @todo Check if valid
return thiz->has_entity(uuids::uuid::from_string(uuid).value()); return thiz->has_entity(uuids::uuid::from_string(uuid).value());

View File

@ -198,6 +198,8 @@ namespace ecs {
Entity* create_entity(uuids::uuid uuid); Entity* create_entity(uuids::uuid uuid);
Entity* create_entity(); Entity* create_entity();
void remove_entity(ecs::Entity* entity);
bool has_entity(uuids::uuid uuid) { bool has_entity(uuids::uuid uuid) {
// @todo c++20 has .contains() // @todo c++20 has .contains()
return _entities.count(uuid); return _entities.count(uuid);

View File

@ -77,6 +77,16 @@ namespace ecs {
return create_entity(uuid); 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* Manager::create_entity(uuids::uuid uuid) {
Entity* entity = new Entity(uuid); Entity* entity = new Entity(uuid);
_entities.insert({uuid, entity}); _entities.insert({uuid, entity});