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

View File

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