Add a static ::GetInstance() method so framework objects can use the component registry

Close #143
This commit is contained in:
S David 2023-08-11 14:17:08 -04:00
parent 4a8752b3d5
commit 894be2d5e2
4 changed files with 27 additions and 19 deletions

View File

@ -40,13 +40,11 @@ class EntityManager : INonCopyable
using TypeList = std::list<TypeInfo>;
public:
#ifdef UNIT_TEST
static EntityManager& GetInstance()
{
static EntityManager instance;
return instance;
}
#endif
virtual ~EntityManager() {}
template<typename TComponent>

View File

@ -33,6 +33,16 @@ class ComponentStore : public EntityManager
return instance;
}
static void Init()
{
auto& instance = GetInstance();
instance.RegisterPool(instance.positions);
instance.RegisterPool(instance.properties);
instance.RegisterPool(instance.views);
instance.RegisterPool(instance.players);
}
ComponentDictionary<PositionAttribute> positions;
ComponentDictionary<PlayerBehavior> players;
ComponentDictionary<RenderedCircle> views;
@ -42,12 +52,6 @@ class ComponentStore : public EntityManager
ComponentStore()
: EntityManager(), positions(), players(), views(), properties()
{
using base = EntityManager;
base::RegisterPool(positions);
base::RegisterPool(players);
base::RegisterPool(views);
base::RegisterPool(properties);
}
};
}

View File

@ -30,16 +30,14 @@ using namespace QW::GameEngine;
using mutex_lock = std::unique_lock<std::mutex>;
auto& component_pools = ComponentStore::GetInstance();
QuartzWarriors::QuartzWarriors() : IGame(), is_running(false), ticks(0)
QuartzWarriors::QuartzWarriors()
: IGame()
, is_running(false)
, ticks(0)
, component_pools(ComponentStore::GetInstance())
{
this->renderer = System::Video::new_renderer();
// Initialize player 1's data structs (Entity id = 0)
component_pools.views.emplace(0, 0);
component_pools.positions.emplace(0, 0);
component_pools.players.emplace(0, 0);
this->renderer = System::Video::new_renderer();
}
QuartzWarriors::~QuartzWarriors()
@ -52,6 +50,13 @@ QuartzWarriors::~QuartzWarriors()
void
QuartzWarriors::Init(int argc, char* argv[])
{
component_pools.Init();
// Initialize player 1's data structs
component_pools.views.emplace(playerId, playerId);
component_pools.positions.emplace(playerId, playerId);
component_pools.players.emplace(playerId, playerId);
component_pools.properties.emplace(playerId, playerId);
this->is_running = true;

View File

@ -16,9 +16,10 @@
#include "framework/core/IGame.hpp"
#include "framework/core/components/Metadata.hpp"
#include "framework/core/input/EventDispatcher.hpp"
#include "framework/system/sdl.hpp"
#include "game/ComponentStore.hpp"
namespace QW {
using namespace Core;
@ -50,9 +51,9 @@ class QuartzWarriors : public Core::IGame
bool is_running;
Uint32 ticks;
System::Video::rendering_ctx renderer;
EventDispatcher event_dispatcher;
System::Video::rendering_ctx renderer;
QW::GameEngine::ComponentStore& component_pools;
std::mutex data_mutex;
};
}