This commit is contained in:
Asuro
2025-02-23 02:07:26 +01:00
parent d7ed08eb08
commit 369b994755
2 changed files with 36 additions and 31 deletions

View File

@@ -58,7 +58,7 @@ namespace Game
{
for (uint32_t xx = 0; xx < 11; ++xx)
{
Cube* c = Cubes.New();
Cube* c = Cubes.Get(Cubes.New());
if (c)
{
c->TestX = xx;
@@ -66,8 +66,7 @@ namespace Game
}
}
}
Cube* floor = Cubes.New();
Cubes.New(); // Floor
}
if (Tests.Count == 0)
{
@@ -138,8 +137,10 @@ namespace Game
player.PlayerCamTransform.RotateLocal({player.WalkXRot, 0.0f, 0.0f});
}
Cubes.Update();
Tests.Update();
for (uint16_t i = 0; i < Cubes.Count; ++i)
{
Cubes.Get({i})->Update();
}
END_PERF(GetShared().Window.PerfCounters, PerfCounterType::GameLevelUpdate, GetShared().Window.FrameCounter);
}
@@ -203,10 +204,6 @@ namespace Game
EData.ModelHandle = 1;
EData.Transform.Position = {0.0f, 0.0f, 10.0f};
}
void TestEntity::Update()
{
EData.TestColor[0] = 0.0f;
}
} // namespace Game

View File

@@ -2,10 +2,17 @@
#include "../engine/Shared.h"
#include "Global.h"
#include "Log.h"
#include "Puzzle.h"
#include "rendering/Rendering.h"
#include <bgfx/bgfx.h>
#include <cstdint>
#define ENTITY_HANDLE(X) \
struct X \
{ \
uint16_t Idx = UINT16_MAX; \
};
namespace Game
{
struct EntityRenderData
@@ -18,6 +25,7 @@ namespace Game
void Render(const Model* models, const Material* materials);
};
ENTITY_HANDLE(CubeHandle);
struct Cube
{
int32_t TestX = -1;
@@ -28,15 +36,21 @@ namespace Game
void Update();
};
ENTITY_HANDLE(TestEntityHandle);
struct TestEntity
{
EntityRenderData EData;
void Setup();
void Update();
};
template <typename T, uint32_t C> class EntityManager
ENTITY_HANDLE(PuzzleTileEntityHandle);
struct PuzzleTileEntity
{
EntityRenderData EData;
};
template <typename T, typename HandleT, uint32_t C> class EntityManager
{
public:
uint16_t Count = 0;
@@ -59,48 +73,41 @@ namespace Game
return changed;
}
T* New()
HandleT New()
{
if (Data == nullptr)
{
Log("Accessed EntityManager before setup!");
return nullptr;
return {};
}
if (Count >= C)
{
Log("Too many entities!");
return nullptr;
return {};
}
Data[Count] = {};
Data[Count].Setup();
T* result = &Data[Count];
HandleT H;
H.Idx = Count;
++Count;
return result;
return H;
}
T* Get(uint16_t idx)
T* Get(HandleT handle)
{
if (idx > Count)
if (handle.Idx > Count)
{
Log("OOB Access!");
return nullptr;
}
return &Data[idx];
}
void Update()
{
for (uint32_t i = 0; i < Count; ++i)
{
Data[i].Update();
}
return &Data[handle.Idx];
}
void Render(const Model* models, const Material* materials)
{
for (int32_t i = 0; i < Count; ++i)
for (uint16_t i = 0; i < Count; ++i)
{
T* c = Get(i);
T* c = Get({i});
if (c) c->EData.Render(models, materials);
}
}
@@ -109,8 +116,9 @@ namespace Game
class Level
{
public:
EntityManager<Cube, 1024> Cubes;
EntityManager<TestEntity, 32> Tests;
EntityManager<Cube, CubeHandle, 1024> Cubes;
EntityManager<TestEntity, TestEntityHandle, 32> Tests;
EntityManager<PuzzleTileEntity, PuzzleTileEntityHandle, 1024> PuzzleTiles;
public:
void Setup(GameData& data);