POC for entities.
This commit is contained in:
70
Game/Entity/Entity.h
Normal file
70
Game/Entity/Entity.h
Normal file
@@ -0,0 +1,70 @@
|
||||
#pragma once
|
||||
|
||||
#include <concepts>
|
||||
#include <Core/Common/CoreUtils.h>
|
||||
#include <Core/Memory/Allocator.h>
|
||||
#include <Engine/Class.h>
|
||||
#include <Entity/EntityManager.h>
|
||||
#include <type_traits>
|
||||
|
||||
// Add any new fields into the concept below
|
||||
#define DECLARE_ENTITY(entity) \
|
||||
Entity* Base; \
|
||||
static const Juliet::Class* Kind;
|
||||
|
||||
// Will register the class globally at launch
|
||||
#define DEFINE_ENTITY(entity) \
|
||||
constexpr Juliet::Class entityKind##entity(#entity, sizeof(#entity) / sizeof(char)); \
|
||||
const Juliet::Class* entity::Kind = &entityKind##entity;
|
||||
|
||||
namespace Game
|
||||
{
|
||||
using DerivedType = void*;
|
||||
|
||||
struct Entity final
|
||||
{
|
||||
EntityID ID;
|
||||
const Juliet::Class* Kind;
|
||||
DerivedType Derived;
|
||||
float X, Y;
|
||||
};
|
||||
|
||||
template <typename EntityType>
|
||||
concept EntityConcept = requires(EntityType entity)
|
||||
{
|
||||
requires std::same_as<decltype(entity.Kind), const Juliet::Class*>;
|
||||
requires std::same_as<decltype(entity.Base), Entity*>;
|
||||
};
|
||||
|
||||
template <typename EntityType>
|
||||
requires EntityConcept<EntityType>
|
||||
bool IsA(const Entity* entity)
|
||||
{
|
||||
return entity->Kind == EntityType::Kind;
|
||||
}
|
||||
|
||||
template <typename EntityType>
|
||||
requires EntityConcept<EntityType>
|
||||
EntityType* MakeEntity(EntityManager& manager, float x, float y)
|
||||
{
|
||||
EntityType* result = static_cast<EntityType*>(Juliet::Calloc(1, sizeof(EntityType)));
|
||||
Entity* base = result->Base = static_cast<Entity*>(Juliet::Calloc(1, sizeof(Entity)));
|
||||
base->X = x;
|
||||
base->Y = y;
|
||||
base->Derived = result;
|
||||
base->Kind = EntityType::Kind;
|
||||
|
||||
RegisterEntity(manager, base);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename EntityType>
|
||||
requires EntityConcept<EntityType>
|
||||
EntityType* DownCast(Entity* entity)
|
||||
{
|
||||
Assert(IsA<EntityType>(entity));
|
||||
return static_cast<EntityType*>(entity->Derived);
|
||||
}
|
||||
|
||||
} // namespace Game
|
||||
25
Game/Entity/EntityManager.cpp
Normal file
25
Game/Entity/EntityManager.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#include <Entity/EntityManager.h>
|
||||
|
||||
#include <Entity/Entity.h>
|
||||
|
||||
namespace Game
|
||||
{
|
||||
namespace
|
||||
{
|
||||
EntityManager Manager;
|
||||
}
|
||||
|
||||
EntityID EntityManager::ID = 0;
|
||||
|
||||
void InitEntityManager() {}
|
||||
|
||||
EntityManager& GetEntityManager()
|
||||
{
|
||||
return Manager;
|
||||
}
|
||||
|
||||
void RegisterEntity(EntityManager& manager, Entity* entity)
|
||||
{
|
||||
entity->ID = EntityManager::ID++;
|
||||
}
|
||||
} // namespace Game
|
||||
19
Game/Entity/EntityManager.h
Normal file
19
Game/Entity/EntityManager.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <Core/Common/CoreTypes.h>
|
||||
|
||||
namespace Game
|
||||
{
|
||||
using EntityID = uint64_t;
|
||||
|
||||
struct Entity;
|
||||
struct EntityManager
|
||||
{
|
||||
static EntityID ID;
|
||||
// May be this should contains the allocator for each entity types
|
||||
};
|
||||
|
||||
void InitEntityManager();\
|
||||
EntityManager& GetEntityManager();
|
||||
void RegisterEntity(EntityManager& manager, Entity* entity);
|
||||
}
|
||||
Reference in New Issue
Block a user