From e11760a5e4a9a300b3266d319fc50c75bbafd401 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Thu, 27 Jun 2024 14:26:51 +0300 Subject: [PATCH] add entity.despawn(...) --- src/logic/LevelController.cpp | 1 + src/logic/scripting/lua/libentity.cpp | 16 ++++++++++++++++ src/objects/Entities.cpp | 10 ++++++++++ src/objects/Entities.hpp | 16 +++++++++++++--- 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/logic/LevelController.cpp b/src/logic/LevelController.cpp index 551beb71..f42bc926 100644 --- a/src/logic/LevelController.cpp +++ b/src/logic/LevelController.cpp @@ -40,6 +40,7 @@ void LevelController::update(float delta, bool input, bool pause) { level->objects.end() ); + level->entities->clean(); if (!pause) { // update all objects that needed for (const auto& obj : level->objects) { diff --git a/src/logic/scripting/lua/libentity.cpp b/src/logic/scripting/lua/libentity.cpp index 1a7cf027..fbc19c1c 100644 --- a/src/logic/scripting/lua/libentity.cpp +++ b/src/logic/scripting/lua/libentity.cpp @@ -31,6 +31,13 @@ static int l_spawn(lua::State* L) { return lua::pushinteger(L, id); } +static int l_despawn(lua::State* L) { + if (auto entity = get_entity(L, 1)) { + entity->destroy(); + } + return 0; +} + static int l_get_pos(lua::State* L) { if (auto entity = get_entity(L, 1)) { return lua::pushvec3_arr(L, entity->getTransform().pos); @@ -87,8 +94,16 @@ static int l_set_enabled(lua::State* L) { return 0; } +static int l_get_size(lua::State* L) { + if (auto entity = get_entity(L, 1)) { + return lua::pushvec3(L, entity->getRigidbody().hitbox.halfsize * 2.0f); + } + return 0; +} + const luaL_Reg entitylib [] = { {"spawn", lua::wrap}, + {"despawn", lua::wrap}, {NULL, NULL} }; @@ -105,5 +120,6 @@ const luaL_Reg rigidbodylib [] = { {"set_enabled", lua::wrap}, {"get_vel", lua::wrap}, {"set_vel", lua::wrap}, + {"get_size", lua::wrap}, {NULL, NULL} }; diff --git a/src/objects/Entities.cpp b/src/objects/Entities.cpp index 9261db2a..ce1cc171 100644 --- a/src/objects/Entities.cpp +++ b/src/objects/Entities.cpp @@ -33,6 +33,16 @@ entityid_t Entities::spawn(EntityDef& def, glm::vec3 pos) { return id; } +void Entities::clean() { + for (auto it = entities.begin(); it != entities.end(); ++it) { + if (registry.valid(it->second)) { + ++it; + } else { + it = entities.erase(it); + } + } +} + void Entities::updatePhysics(float delta){ auto view = registry.view(); auto physics = level->physics.get(); diff --git a/src/objects/Entities.hpp b/src/objects/Entities.hpp index 376462df..5d684a8b 100644 --- a/src/objects/Entities.hpp +++ b/src/objects/Entities.hpp @@ -36,11 +36,16 @@ class Rig; struct EntityDef; class Entity { + entityid_t id; entt::registry& registry; const entt::entity entity; public: - Entity(entt::registry& registry, const entt::entity entity) - : registry(registry), entity(entity) {} + Entity(entityid_t id, entt::registry& registry, const entt::entity entity) + : id(id), registry(registry), entity(entity) {} + + entityid_t getID() const { + return id; + } bool isValid() const { return registry.valid(entity); @@ -57,6 +62,10 @@ public: entityid_t getUID() const { return registry.get(entity).uid; } + + void destroy() { + registry.destroy(entity); + } }; class Entities { @@ -66,6 +75,7 @@ class Entities { entityid_t nextID = 1; public: Entities(Level* level); + void clean(); void updatePhysics(float delta); void renderDebug(LineBatch& batch); @@ -76,7 +86,7 @@ public: std::optional get(entityid_t id) { const auto& found = entities.find(id); if (found != entities.end()) { - return Entity(registry, found->second); + return Entity(id, registry, found->second); } return std::nullopt; }