From e237d0dcb5ab5a183fae0a8905d515f83d8e48ea Mon Sep 17 00:00:00 2001 From: Joey Pollack Date: Tue, 31 May 2022 16:46:05 -0400 Subject: [PATCH] Added UUIDs to entities Added component add/remove/check/get methods for Entity --- docs/tasks/core.todo | 3 ++- src/run_modes/editor/editor.cpp | 2 ++ src/utils/uuid.cpp | 15 ++++++++++- src/utils/uuid.h | 3 ++- src/world/components.h | 2 +- src/world/entity.cpp | 9 +++++-- src/world/entity.h | 48 ++++++++++++++++++++++++++++++++- 7 files changed, 75 insertions(+), 7 deletions(-) diff --git a/docs/tasks/core.todo b/docs/tasks/core.todo index d581f51..2cadcfa 100644 --- a/docs/tasks/core.todo +++ b/docs/tasks/core.todo @@ -7,7 +7,8 @@ Build System: ✔ Modify .sh scripts to recognize the noeditor flag @done (1/25/2022, 3:59:23 PM) Core: - ☐ Wrap NFD in an API in the platform module @low + ✔ Wrap NFD in an API in the platform module @low @done(22-05-31 15:44) + Wrapper added to utils - not platform ☐ Add custom (64 bit?) UUID generator (based on Chreno's UUIDs) ✔ Add Terminal subsystem to allow for printing colored text in a cross-platform way @done(22-05-16 18:05) ✔ Create a LogListener that uses the colored text (replace the current stdout listener) @done(22-05-16 18:23) diff --git a/src/run_modes/editor/editor.cpp b/src/run_modes/editor/editor.cpp index ac6c47f..018cfae 100644 --- a/src/run_modes/editor/editor.cpp +++ b/src/run_modes/editor/editor.cpp @@ -26,6 +26,7 @@ // Tools #include "tools/map_editor/map_editor.h" +#include using namespace lunarium::gui; @@ -46,6 +47,7 @@ namespace editor OpRes Editor::Initialize() { + LabelComponent test; LogCat = Logger::RegisterCategory("EDITOR"); // Initialize internal data diff --git a/src/utils/uuid.cpp b/src/utils/uuid.cpp index 1c73668..44c7ad7 100644 --- a/src/utils/uuid.cpp +++ b/src/utils/uuid.cpp @@ -12,8 +12,21 @@ namespace lunarium { std::mt19937_64 UUID::mt64(time(nullptr)); - LUUID UUID::GetID() + LUUID UUID::GetNewID() { + // This is kind of hacky. Should probably try to remove the need for null ids. + // LUUID id = 0; + // while (0 == id) + // { + // id = mt64(); + // } + // return id; + return mt64(); } + + // LUUID UUID::GetNullID() + // { + // return 0; + // } } \ No newline at end of file diff --git a/src/utils/uuid.h b/src/utils/uuid.h index 8c6c9f9..bba255e 100644 --- a/src/utils/uuid.h +++ b/src/utils/uuid.h @@ -17,7 +17,8 @@ namespace lunarium class UUID { public: - static LUUID GetID(); + static LUUID GetNewID(); + //static LUUID GetNullID(); private: static std::mt19937_64 mt64; diff --git a/src/world/components.h b/src/world/components.h index f848800..2c8bd49 100644 --- a/src/world/components.h +++ b/src/world/components.h @@ -17,7 +17,7 @@ namespace lunarium { struct LabelComponent { - std::string Label + std::string Label; }; } diff --git a/src/world/entity.cpp b/src/world/entity.cpp index 1daa44a..05c88af 100644 --- a/src/world/entity.cpp +++ b/src/world/entity.cpp @@ -11,8 +11,13 @@ namespace lunarium { Entity::Entity(entt::registry* r) - : mID(entt::null), mpRegistry(r) + : mHandle(entt::null), mpRegistry(r) + { + mUUID = UUID::GetNewID(); + } + + Entity::Entity(entt::registry* r, LUUID uuid) + : mHandle(entt::null), mpRegistry(r), mUUID(uuid) { - } } \ No newline at end of file diff --git a/src/world/entity.h b/src/world/entity.h index 3b8b0c4..90ad22a 100644 --- a/src/world/entity.h +++ b/src/world/entity.h @@ -9,6 +9,8 @@ #ifndef LUNARIUM_ENTITY_H_ #define LUNARIUM_ENTITY_H_ +#include +#include #include namespace lunarium @@ -16,10 +18,54 @@ namespace lunarium class Entity { public: + // TODO: Maybe change this to take the World instead of the registry Entity(entt::registry* r); + Entity(entt::registry* r, LUUID uuid); + + // These component methods are based on methods from the Hazel engine, written by Cherno + // https://github.com/TheCherno/Hazel/blob/dev/Hazel/src/Hazel/Scene/Entity.h + template + T& AddComponent(Args&&... args) + { + if (HasComponent()) + { + Log::Error(LogCategory::GAME_SYSTEM, "Entity already has component!"); + return GetComponent(); + } + T& component = mpRegistry->emplace(mHandle, std::forward(args)...); + //m_Scene->OnComponentAdded(*this, component); + return component; + } + + template + T& GetComponent() + { + if (!HasComponent()) + { + Log::Error(LogCategory::GAME_SYSTEM, "Entity does not have component!"); + } + return mpRegistry->get(mHandle); + } + + template + bool HasComponent() + { + return mpRegistry->all_of(mHandle); + } + + template + void RemoveComponent() + { + if (!HasComponent()) + { + Log::Error(LogCategory::GAME_SYSTEM, "Entity does not have component!"); + } + m_Scene->m_Registry.remove(mHandle); + } private: - entt::entity mID; + LUUID mUUID; + entt::entity mHandle; entt::registry* mpRegistry; }; }