diff --git a/src/game/Level.cpp b/src/game/Level.cpp index 120fa64..6cdddd0 100644 --- a/src/game/Level.cpp +++ b/src/game/Level.cpp @@ -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 diff --git a/src/game/Level.h b/src/game/Level.h index 01b3f02..73bdae7 100644 --- a/src/game/Level.h +++ b/src/game/Level.h @@ -2,10 +2,17 @@ #include "../engine/Shared.h" #include "Global.h" #include "Log.h" +#include "Puzzle.h" #include "rendering/Rendering.h" #include #include +#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 class EntityManager + ENTITY_HANDLE(PuzzleTileEntityHandle); + struct PuzzleTileEntity + { + EntityRenderData EData; + }; + + template 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 Cubes; - EntityManager Tests; + EntityManager Cubes; + EntityManager Tests; + EntityManager PuzzleTiles; public: void Setup(GameData& data);