Browse Source

Engine: Add Scene::findEntity() function

master
Riyyi 2 years ago
parent
commit
efae5b9adb
  1. 30
      src/inferno/scene/scene.cpp
  2. 4
      src/inferno/scene/scene.h

30
src/inferno/scene/scene.cpp

@ -4,6 +4,9 @@
* SPDX-License-Identifier: MIT
*/
#include <cstdint> // uint32_t
#include <limits> // 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<TagComponent>();
for (auto [entity, tag] : view.each()) {
if (tag.tag == name) {
return static_cast<uint32_t>(entity);
}
}
return std::numeric_limits<uint32_t>::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

4
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();

Loading…
Cancel
Save