preparing serialization of entities
This commit is contained in:
@@ -1,3 +1,25 @@
|
||||
#include <Entity/Entity.h>
|
||||
|
||||
#include <Core/Common/serialization.h>
|
||||
|
||||
DEFINE_ENTITY(Inert);
|
||||
|
||||
void serialize(archive& ar, NonNullPtr<Entity> entity)
|
||||
{
|
||||
serialize_elem(ar, entity->ID);
|
||||
|
||||
if (ar.loading)
|
||||
{
|
||||
serialize_elem(ar, entity->Kind->kind);
|
||||
}
|
||||
else
|
||||
{
|
||||
uint8 kind;
|
||||
serialize_elem(ar, kind);
|
||||
entity->Kind = kEntity_type_class_ptr[kind];
|
||||
}
|
||||
|
||||
serialize_elem(ar, entity->X);
|
||||
serialize_elem(ar, entity->Y);
|
||||
serialize_elem(ar, entity->Z);
|
||||
}
|
||||
|
||||
+34
-23
@@ -7,24 +7,41 @@
|
||||
#include <Engine/Class.h>
|
||||
|
||||
#define DECLARE_ENTITY() \
|
||||
Entity* Base; \
|
||||
static const Class* Kind;
|
||||
Entity* Base; \
|
||||
static Class* Kind;
|
||||
|
||||
// Will register the class globally at launch
|
||||
#define DEFINE_ENTITY(entity) \
|
||||
constexpr Class entityKind##entity = \
|
||||
Class entityKind##entity = \
|
||||
MakeClass(ConstString(#entity), (uint8)Entity_Type::entity, sizeof(entity), alignof(entity), nullptr); \
|
||||
const Class* entity::Kind = &entityKind##entity;
|
||||
Class* entity::Kind = &entityKind##entity;
|
||||
|
||||
#define DEFINE_ENTITY_SERIALIZED(entity, serialize_fct) \
|
||||
constexpr Class entityKind##entity = \
|
||||
Class entityKind##entity = \
|
||||
MakeClass(ConstString(#entity), (uint8)Entity_Type::entity, sizeof(entity), alignof(entity), serialize_fct); \
|
||||
const Class* entity::Kind = &entityKind##entity;
|
||||
Class* entity::Kind = &entityKind##entity;
|
||||
|
||||
struct EntityManager;
|
||||
using DerivedType = void*;
|
||||
using EntityID = uint64_t;
|
||||
|
||||
struct Entity final
|
||||
{
|
||||
EntityID ID = 0;
|
||||
Class* Kind = nullptr;
|
||||
DerivedType Derived = nullptr;
|
||||
float X = 0.0f;
|
||||
float Y = 0.0f;
|
||||
float Z = 0.0f;
|
||||
};
|
||||
|
||||
struct Inert
|
||||
{
|
||||
DECLARE_ENTITY()
|
||||
|
||||
index_t MeshInstance = indexMax;
|
||||
};
|
||||
|
||||
// clang-format off
|
||||
#define ENTITY_TYPE_LIST(X) X(Inert),
|
||||
|
||||
@@ -41,33 +58,25 @@ inline const char* kEntity_type_names[] = {
|
||||
ENTITY_TYPE_LIST(AS_STR)
|
||||
"Count"
|
||||
};
|
||||
#undef AS_STR
|
||||
|
||||
#define AS_CLASS(name) name::Kind
|
||||
inline Class* kEntity_type_class_ptr[]
|
||||
{
|
||||
ENTITY_TYPE_LIST(AS_CLASS)
|
||||
nullptr
|
||||
};
|
||||
#undef AS_CLASS
|
||||
|
||||
#define ENTITY(kind) ToUnderlying(Entity_Type::kind)
|
||||
// clang-format on
|
||||
|
||||
struct Entity final
|
||||
{
|
||||
EntityID ID = 0;
|
||||
const Class* Kind = nullptr;
|
||||
DerivedType Derived = nullptr;
|
||||
float X = 0.0f;
|
||||
float Y = 0.0f;
|
||||
float Z = 0.0f;
|
||||
};
|
||||
|
||||
// Can reinterpret cast to this to have the offset of Base and Kind for any entity
|
||||
struct entity_template
|
||||
{
|
||||
DECLARE_ENTITY();
|
||||
};
|
||||
|
||||
struct Inert
|
||||
{
|
||||
DECLARE_ENTITY()
|
||||
|
||||
index_t MeshInstance = indexMax;
|
||||
};
|
||||
|
||||
//
|
||||
template <typename EntityType>
|
||||
concept EntityConcept = requires(EntityType entity) {
|
||||
@@ -105,3 +114,5 @@ template <typename EntityType>
|
||||
Assert(IsA<EntityType>(entity));
|
||||
return static_cast<EntityType*>(entity->Derived);
|
||||
}
|
||||
|
||||
void serialize(archive& ar, NonNullPtr<Entity> entity);
|
||||
|
||||
@@ -28,7 +28,13 @@ void InitEntityManager(NonNullPtr<World> world)
|
||||
|
||||
void ShutdownEntityManager()
|
||||
{
|
||||
GetEntityManager().Entities.Destroy();
|
||||
auto& manager = GetEntityManager();
|
||||
for (typed_entity_array& type : manager.by_type)
|
||||
{
|
||||
ArenaRelease(type.arena);
|
||||
}
|
||||
|
||||
manager.Entities.Destroy();
|
||||
}
|
||||
|
||||
EntityManager& GetEntityManager()
|
||||
@@ -37,8 +43,6 @@ EntityManager& GetEntityManager()
|
||||
return *entityManager;
|
||||
}
|
||||
|
||||
void Serialize(NonNullPtr<EntityManager> entityManager) {}
|
||||
|
||||
entity_template* RegisterEntity(EntityManager& manager, Entity* base, DerivedType entity)
|
||||
{
|
||||
base->ID = EntityManager::ID++;
|
||||
@@ -61,6 +65,11 @@ entity_template* RegisterEntity(EntityManager& manager, Entity* base, DerivedTyp
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void RegisterBaseEntity(EntityManager& manager, Entity&& base)
|
||||
{
|
||||
GetEntityManager().Entities.PushBack(std::move(base));
|
||||
}
|
||||
|
||||
void UpdateEntityManager(EntityManager& manager)
|
||||
{
|
||||
// Todo : inert by definition dont move, but this is for test
|
||||
|
||||
@@ -27,6 +27,6 @@ struct EntityManager
|
||||
void InitEntityManager(NonNullPtr<World> world);
|
||||
void ShutdownEntityManager();
|
||||
EntityManager& GetEntityManager();
|
||||
void Serialize(NonNullPtr<EntityManager> entityManager);
|
||||
entity_template* RegisterEntity(EntityManager& manager, Entity* base, DerivedType entity);
|
||||
void RegisterBaseEntity(EntityManager& manager, Entity&& base);
|
||||
void UpdateEntityManager(EntityManager& manager);
|
||||
|
||||
Reference in New Issue
Block a user