Refactored factory and component system.

This commit is contained in:
2014-03-07 01:08:54 +01:00
parent 18197bf21d
commit e65db077d5
30 changed files with 313 additions and 284 deletions
+20 -2
View File
@@ -8,7 +8,11 @@
#include "Entity.h"
#include "Component.h"
#include "ComponentFactory.h"
#include "Factory.h"
#include "Components/Transform.h"
#include "Components/Input.h"
#include "Components/DirectionalLight.h"
#include "System.h"
#include "Systems/CollisionSystem.h"
@@ -18,6 +22,9 @@ public:
World();
~World();
virtual void RegisterComponents();
virtual void RegisterSystems();
EntityID CreateEntity(EntityID parent = 0);
void RemoveEntity(EntityID entity);
@@ -29,12 +36,22 @@ public:
template <class T>
std::shared_ptr<T> AddComponent(EntityID entity, std::string componentType)
{
std::shared_ptr<T> component = std::static_pointer_cast<T>(ComponentFactory::Instance()->Create(componentType));
std::shared_ptr<T> component = std::shared_ptr<T>(static_cast<T*>(m_ComponentFactory.Create(componentType)));
if (component == nullptr) {
LOG_ERROR("Failed to attach invalid component \"%s\" to entity #%i", componentType.c_str(), entity);
return nullptr;
}
m_ComponentsOfType[componentType].push_back(component);
m_EntityComponents[entity][componentType] = component;
return component;
}
std::shared_ptr<Component> AddComponent(EntityID entity, std::string componentType)
{
return AddComponent<Component>(entity, componentType);
}
template <class T>
std::shared_ptr<T> GetComponent(EntityID entity, std::string componentType)
{
@@ -56,6 +73,7 @@ private:
// A bottom to top tree. A map of child entities to parent entities.
std::unordered_map<EntityID, EntityID> m_EntityParents;
ComponentFactory m_ComponentFactory;
std::unordered_map<std::string, std::vector<std::shared_ptr<Component>>> m_ComponentsOfType;
std::unordered_map<EntityID, std::map<std::string, std::shared_ptr<Component>>> m_EntityComponents;
std::vector<std::shared_ptr<System>> m_Systems;