Improving entity manager and entity support for a simpler version, made some cleanup on the road to support serialization of world

This commit is contained in:
2026-08-31 23:05:20 -04:00
parent 4180622d6a
commit cb615091ca
17 changed files with 607 additions and 88 deletions
+40 -7
View File
@@ -1,6 +1,9 @@
#include <Entity/EntityManager.h>
#include <Entity/EntityManager.h>
#include <Core/Common/EnumUtils.h>
#include <Data/World.h>
#include <Entity/Entity.h>
#include <game.h>
#include <Graphics/MeshRenderer.h>
EntityID EntityManager::ID = 0;
@@ -12,7 +15,15 @@ void InitEntityManager(NonNullPtr<World> world)
newManager->Entities.Create(world->WorldArena JULIET_DEBUG_PARAM("Entities"));
newManager->Arena = ArenaAllocate({ .Name = "Entity Arena" });
ArenaParams by_type_params{ .ReserveSize = Kilobytes(1),
.CommitSize = Kilobytes(1),
.Name = "" JULIET_DEBUG_ONLY(, .CanReserveMore = false) };
for (uint8 i = 0; i < ENTITY(Count); ++i)
{
by_type_params.Name = kEntity_type_names[i];
newManager->by_type[i].arena = ArenaAllocate(by_type_params);
newManager->by_type[i].array = nullptr;
}
}
void ShutdownEntityManager()
@@ -26,18 +37,40 @@ EntityManager& GetEntityManager()
return *entityManager;
}
void RegisterEntity(EntityManager& /*manager*/, Entity* entity)
void Serialize(NonNullPtr<EntityManager> entityManager) {}
entity_template* RegisterEntity(EntityManager& manager, Entity* base, DerivedType entity)
{
entity->ID = EntityManager::ID++;
base->ID = EntityManager::ID++;
base->Derived = entity;
manager.Entities.PushBack(*base);
auto* ptr = (entity_template*)ArenaPushSize(manager.by_type[base->Kind->kind].arena, base->Kind->size_of, base->Kind->alignment,
false JULIET_DEBUG_PARAM(kEntity_type_names[base->Kind->kind]));
MemCopy(ptr, entity, base->Kind->size_of);
manager.by_type[base->Kind->kind].count += 1;
ptr->Base = manager.Entities.Back();
if (manager.by_type[base->Kind->kind].array == nullptr)
{
manager.by_type[base->Kind->kind].array = ptr;
}
return ptr;
}
void UpdateEntityManager(EntityManager& manager)
{
for (Entity& ent : manager.Entities)
// Todo : inert by definition dont move, but this is for test
auto& by_type = manager.by_type[ENTITY(Inert)];
for (index_t i = 0; i < by_type.count; ++i)
{
if (ent.MeshInstance != indexMax)
Inert* inert = reinterpret_cast<Inert*>(by_type.array) + i;
if (inert->MeshInstance != indexMax)
{
SetMeshInstanceTransform(ent.MeshInstance, MatrixTranslation(ent.X, ent.Y, 0.0f));
SetMeshInstanceTransform(inert->MeshInstance, MatrixTranslation(inert->Base->X, inert->Base->Y, inert->Base->Z));
}
}
}