diff --git a/src/inferno/scene/scene.cpp b/src/inferno/scene/scene.cpp index fd9ad92..4ff843c 100644 --- a/src/inferno/scene/scene.cpp +++ b/src/inferno/scene/scene.cpp @@ -4,6 +4,9 @@ * SPDX-License-Identifier: MIT */ +#include // uint32_t +#include // std::numeric_limits + #include "ruc/file.h" #include "ruc/format/log.h" #include "ruc/json/json.h" @@ -127,12 +130,6 @@ uint32_t Scene::createEntity(const std::string& name) return entity; } -void Scene::destroyEntity(uint32_t entity) -{ - ScriptSystem::the().cleanup(entity); - m_registry->destroy(entt::entity { entity }); -} - uint32_t Scene::loadEntity(ruc::Json json) { uint32_t entity = createEntity((json.exists("name")) @@ -144,6 +141,25 @@ uint32_t Scene::loadEntity(ruc::Json json) return entity; } +uint32_t Scene::findEntity(std::string_view name) +{ + auto view = m_registry->view(); + + for (auto [entity, tag] : view.each()) { + if (tag.tag == name) { + return static_cast(entity); + } + } + + return std::numeric_limits::max(); +} + +void Scene::destroyEntity(uint32_t entity) +{ + ScriptSystem::the().cleanup(entity); + m_registry->destroy(entt::entity { entity }); +} + glm::mat4 Scene::cameraProjectionView() { return CameraSystem::the().projectionView(); @@ -151,7 +167,7 @@ glm::mat4 Scene::cameraProjectionView() void Scene::validEntity(uint32_t entity) const { - VERIFY(m_registry->valid(entt::entity { entity }), "Entity is not valid"); + VERIFY(m_registry->valid(entt::entity { entity }), "invalid entity '{}'", entity); } } // namespace Inferno diff --git a/src/inferno/scene/scene.h b/src/inferno/scene/scene.h index e88897f..ba34b15 100644 --- a/src/inferno/scene/scene.h +++ b/src/inferno/scene/scene.h @@ -27,9 +27,9 @@ public: void destroy(); uint32_t createEntity(const std::string& name = ""); - void destroyEntity(uint32_t entity); - uint32_t loadEntity(ruc::Json json); + uint32_t findEntity(std::string_view name); + void destroyEntity(uint32_t entity); glm::mat4 cameraProjectionView();