CoriEngine
Loading...
Searching...
No Matches
Scene.cpp
Go to the documentation of this file.
1#include "Scene.hpp"
6
7namespace Cori {
8 namespace World {
9 std::shared_ptr<Scene> Scene::Create(std::string name) {
10 return std::shared_ptr<Scene>(new Scene(std::move(name)));
11 }
12
13 Scene::Scene(std::string name) : m_Name(std::move(name)) {
15 m_ActiveCamera.BindCameraComponent(&GetContextComponent<Components::Scene::Camera>());
17 }
18
20 m_Registry.clear();
22 }
23
25 entt::entity entity = m_Registry.create();
26 Entity e = entt::handle{m_Registry, entity};
27 e.AddComponent<Internal::SceneID>(m_SceneID);
28 return e;
29 }
30
31 std::expected<void, Core::CoriError<>> Scene::AddEntityToCache(const Entity entity, const Utility::StringHash32 key) {
32 if (m_EntityCache.contains(key)) {
33 return std::unexpected(Core::CoriError("Entry with the given key already exists, make sure you're not reusing the key. It's also possible (but very unlikely), that you got a hash collision, try to change the key a bit."));
34 }
35
36 m_EntityCache.insert({ key, entity.GetRawEntity() });
37 CORI_CORE_DEBUG_TAGGED({ Logger::Tags::World::Self, Logger::Tags::World::Scene::Self }, "Entity '{}' added to scene local cache, key: '{}'", entity.GetDebugData(), key);
38 return{};
39 }
40
41 std::expected<Entity, Core::CoriError<>> Scene::GetEntityFromCache(const Utility::StringHash32 key) {
42 if (!m_EntityCache.contains(key)) {
43 return std::unexpected(Core::CoriError("No entity with the specified key found in scene local cache."));
44 }
45 if (!m_Registry.valid(m_EntityCache.at(key))) {
46 m_EntityCache.erase(key);
47 return std::unexpected(Core::CoriError("Entity at the specified key is invalid, removing this entry."));
48 }
49 return Entity{ { m_Registry, m_EntityCache.at(key) } };
50 }
51
53 if (m_EntityCache.contains(key)) {
54 const Entity entity = entt::handle{ m_Registry, m_EntityCache.at(key) };
55 m_EntityCache.erase(key);
56 CORI_CORE_DEBUG_TAGGED({ Logger::Tags::World::Self, Logger::Tags::World::Scene::Self }, "Entity '{}' removed from scene local cache, key: '{}'", entity.GetDebugData(), key);
57 }
58 CORI_CORE_WARN_TAGGED({ Logger::Tags::World::Self, Logger::Tags::World::Scene::Self }, "Trying to remove an Entity from scene local cache, provided key: '{}', but cache has no such entry.", key);
59 }
60
62 if (!entity.IsValid()) { return; }
64 entity.UnlinkFromParent();
65 }
66 const auto& uuidComp = entity.GetComponents<Components::Entity::UUID>();
67 m_UUIDToEntity.erase(uuidComp.m_UUID);
68 m_Registry.destroy(entity.GetRawEntity());
69 }
70
71 void Scene::OnUpdate([[maybe_unused]] Core::GameTimer& gameTimer) {
73
74 for (auto type : m_SystemPriority | std::views::values) {
75 m_RegisteredSystems[type]->OnUpdate(gameTimer);
76 }
77
78 Graphics::Renderer2D::SubmitScene(this);
79 }
80
82 for (auto type : m_SystemPriority | std::views::values) {
83 m_RegisteredSystems[type]->OnTickUpdate(gameTimer);
84 }
85 }
86
88 for (auto type : m_SystemPriority | std::views::values) {
89 m_RegisteredSystems[type]->OnImGuiRender(gameTimer);
90 }
91 }
92
96
100
102 return true;
103 }
104
106 return true;
107 }
108 }
109}
#define CORI_CORE_DEBUG_TAGGED(...)
Definition Logger.hpp:1026
#define CORI_CORE_WARN_TAGGED(...)
Definition Logger.hpp:1038
#define CORI_CORE_INFO_TAGGED(...)
Definition Logger.hpp:1027
#define CORI_PROFILE_FUNCTION()
Definition Profiler.hpp:9
Custom error class mainly used in std::expected.
Definition Error.hpp:27
A GameTimer is responsible for managing everything that is connected with time, ticks,...
Definition Time.hpp:8
static void BeginScene(const World::Components::Scene::Camera &camera)
Entities are the essential part of WorldSystem.
Definition Entity.hpp:25
void UnlinkFromParent()
Unlinks the entity from its parent if it has one.
Definition Entity.cpp:143
bool IsValid() const
Checks if the actual entity behind this handle is valid.
Definition Entity.hpp:161
bool HasComponents() const
Checks if the entity has all the specified components.
Definition Entity.hpp:122
T & AddComponent(Args &&... args)
Adds a component to the entity.
Definition Entity.hpp:39
std::string GetDebugData(bool showUUID=false) const
Gets the debuting string for logging.
Definition Entity.cpp:65
decltype(auto) GetComponents()
Retries the references to the requested components of the entity.
Definition Entity.hpp:74
entt::entity GetRawEntity() const
Gets a raw entt::entity if you need to interact with entt directly.
Definition Entity.hpp:280
std::expected< void, Core::CoriError<> > AddEntityToCache(const Entity entity, const Utility::StringHash32 key)
Definition Scene.cpp:31
T & AddContextComponent(Args &&... args)
Definition Scene.hpp:83
static std::shared_ptr< Scene > Create(std::string name)
Definition Scene.cpp:9
entt::registry m_Registry
Definition Scene.hpp:193
void OnTickUpdate(Core::GameTimer &gameTimer)
Definition Scene.cpp:81
T & GetContextComponent()
Definition Scene.hpp:88
void RemoveEntityFromCache(const Utility::StringHash32 key)
Definition Scene.cpp:52
Entity CreateBlankEntity()
Definition Scene.cpp:24
void BeginRender()
Definition Scene.cpp:93
void DestroyEntity(Entity entity)
Definition Scene.cpp:61
void OnUpdate(Core::GameTimer &gameTimer)
Definition Scene.cpp:71
std::expected< Entity, Core::CoriError<> > GetEntityFromCache(const Utility::StringHash32 key)
Definition Scene.cpp:41
void OnImGuiRender(Core::GameTimer &gameTimer)
Definition Scene.cpp:87
entt::hashed_string::hash_type StringHash32
Anything connected to WorldSystem (ECS) is in this namespace.
Global engine namespace.
static constexpr char Self[]
Definition Logger.hpp:122
static constexpr char Self[]
Definition Logger.hpp:119
Every Entity has a hierarchy component by default, it's essential for entity hierarchy system.
Every Entity by default has a UUID component, mostly unused for now.