Added UUIDs to entities

Added component add/remove/check/get methods for Entity
gui_api_redesign
Joey Pollack 4 years ago
parent d9f7a136e5
commit e237d0dcb5

@ -7,7 +7,8 @@ Build System:
✔ Modify .sh scripts to recognize the noeditor flag @done (1/25/2022, 3:59:23 PM) ✔ Modify .sh scripts to recognize the noeditor flag @done (1/25/2022, 3:59:23 PM)
Core: 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 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) ✔ 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) ✔ Create a LogListener that uses the colored text (replace the current stdout listener) @done(22-05-16 18:23)

@ -26,6 +26,7 @@
// Tools // Tools
#include "tools/map_editor/map_editor.h" #include "tools/map_editor/map_editor.h"
#include <world/components.h>
using namespace lunarium::gui; using namespace lunarium::gui;
@ -46,6 +47,7 @@ namespace editor
OpRes Editor::Initialize() OpRes Editor::Initialize()
{ {
LabelComponent test;
LogCat = Logger::RegisterCategory("EDITOR"); LogCat = Logger::RegisterCategory("EDITOR");
// Initialize internal data // Initialize internal data

@ -12,8 +12,21 @@
namespace lunarium namespace lunarium
{ {
std::mt19937_64 UUID::mt64(time(nullptr)); 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(); return mt64();
} }
// LUUID UUID::GetNullID()
// {
// return 0;
// }
} }

@ -17,7 +17,8 @@ namespace lunarium
class UUID class UUID
{ {
public: public:
static LUUID GetID(); static LUUID GetNewID();
//static LUUID GetNullID();
private: private:
static std::mt19937_64 mt64; static std::mt19937_64 mt64;

@ -17,7 +17,7 @@ namespace lunarium
{ {
struct LabelComponent struct LabelComponent
{ {
std::string Label std::string Label;
}; };
} }

@ -11,8 +11,13 @@
namespace lunarium namespace lunarium
{ {
Entity::Entity(entt::registry* r) 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)
{
} }
} }

@ -9,6 +9,8 @@
#ifndef LUNARIUM_ENTITY_H_ #ifndef LUNARIUM_ENTITY_H_
#define LUNARIUM_ENTITY_H_ #define LUNARIUM_ENTITY_H_
#include <utils/uuid.h>
#include <utils/logger.h>
#include <entt/entt.hpp> #include <entt/entt.hpp>
namespace lunarium namespace lunarium
@ -16,10 +18,54 @@ namespace lunarium
class Entity class Entity
{ {
public: public:
// TODO: Maybe change this to take the World instead of the registry
Entity(entt::registry* r); 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<typename T, typename... Args>
T& AddComponent(Args&&... args)
{
if (HasComponent<T>())
{
Log::Error(LogCategory::GAME_SYSTEM, "Entity already has component!");
return GetComponent<T>();
}
T& component = mpRegistry->emplace<T>(mHandle, std::forward<Args>(args)...);
//m_Scene->OnComponentAdded<T>(*this, component);
return component;
}
template<typename T>
T& GetComponent()
{
if (!HasComponent<T>())
{
Log::Error(LogCategory::GAME_SYSTEM, "Entity does not have component!");
}
return mpRegistry->get<T>(mHandle);
}
template<typename T>
bool HasComponent()
{
return mpRegistry->all_of<T>(mHandle);
}
template<typename T>
void RemoveComponent()
{
if (!HasComponent<T>())
{
Log::Error(LogCategory::GAME_SYSTEM, "Entity does not have component!");
}
m_Scene->m_Registry.remove<T>(mHandle);
}
private: private:
entt::entity mID; LUUID mUUID;
entt::entity mHandle;
entt::registry* mpRegistry; entt::registry* mpRegistry;
}; };
} }

Loading…
Cancel
Save