Add a static ::GetInstance() method so framework objects can use the component registry
Close #143
This commit is contained in:
parent
4a8752b3d5
commit
894be2d5e2
@ -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>
|
||||
|
@ -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);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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;
|
||||
};
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user