Browse Source

EntityComponentManager Singleton, Component Types

- EntityComponentManager now has singleton functionality. I'm experiencing lots of issues with it when used with Unreal's hot reloading, so I'll be modifying it soon
- Components now properly set their types. Component Types are now stored in an enum
combatComponentRefactor
Macoy Madson 6 years ago
parent
commit
6a46950bad
  1. 1
      src/entityComponentSystem/ComponentManager.hpp
  2. 16
      src/entityComponentSystem/ComponentTypes.hpp
  3. 24
      src/entityComponentSystem/EntityComponentManager.cpp
  4. 5
      src/entityComponentSystem/EntityComponentManager.hpp
  5. 4
      src/entityComponentSystem/EntityTypes.hpp
  6. 37
      src/game/agent/AgentComponentManager.cpp
  7. 22
      src/game/agent/AgentComponentManager.hpp
  8. 2
      src/game/agent/PlanComponentManager.cpp
  9. 3
      src/unitTesting/EntityComponentSystem_test.cpp

1
src/entityComponentSystem/ComponentManager.hpp

@ -1,6 +1,7 @@
#pragma once
#include "EntityTypes.hpp"
#include "ComponentTypes.hpp"
namespace gv
{

16
src/entityComponentSystem/ComponentTypes.hpp

@ -0,0 +1,16 @@
#pragma once
namespace gv
{
enum class ComponentType : unsigned int
{
None = 0,
Test,
Movement,
Agent,
Plan,
ComponentType_count
};
};

24
src/entityComponentSystem/EntityComponentManager.cpp

@ -1,16 +1,24 @@
#include "EntityComponentManager.hpp"
#include <cassert>
namespace gv
{
Entity EntityComponentManager::NextNewEntity = 1;
EntityComponentManager *EntityComponentManager::Singleton = nullptr;
EntityComponentManager::EntityComponentManager()
{
// Should not create more than one ECM! Commented due to Unreal's hotreloading :(
// assert(!Singleton);
Singleton = this;
}
EntityComponentManager::~EntityComponentManager()
{
DestroyAllEntities();
Singleton = nullptr;
}
// Sets the ComponentManager for a ComponentType. Returns false if there is already a manager
@ -20,10 +28,12 @@ bool EntityComponentManager::AddComponentManagerOfType(ComponentType type,
{
if (manager)
{
// Make sure there isn't already a ComponentManager for the type
EntityComponentManager::ComponentManagerMapIterator findIt = ComponentManagers.find(type);
if (findIt == ComponentManagers.end())
ComponentManagers[type] = manager;
// Make sure there isn't already a ComponentManager for the type
assert(findIt == ComponentManagers.end());
ComponentManagers[type] = manager;
}
return false;
@ -113,4 +123,12 @@ void EntityComponentManager::DestroyAllEntities()
ActiveEntities.clear(); // this should be empty anyways
EntitiesPendingDestruction.clear();
}
EntityComponentManager *EntityComponentManager::GetSingleton()
{
// If failed, someone is requesting ECM before one has been initialized! Commented due to
// Unreal's hotreloading :(
// assert(Singleton);
return Singleton;
}
}

5
src/entityComponentSystem/EntityComponentManager.hpp

@ -3,6 +3,7 @@
#include <map>
#include "EntityTypes.hpp"
#include "ComponentTypes.hpp"
#include "ComponentManager.hpp"
namespace gv
@ -23,6 +24,8 @@ private:
typedef std::map<ComponentType, ComponentManager *> ComponentManagerMap;
typedef ComponentManagerMap::iterator ComponentManagerMapIterator;
static EntityComponentManager* Singleton;
ComponentManagerMap ComponentManagers;
EntityList ActiveEntities;
@ -67,5 +70,7 @@ public:
// Destroys all entities that were created by this EntityComponentManager (i.e. all entities in
// the ActiveEntities list)
void DestroyAllEntities();
static EntityComponentManager* GetSingleton();
};
};

4
src/entityComponentSystem/EntityTypes.hpp

@ -30,8 +30,4 @@ void EntityListRemoveUniqueEntitiesInSuspect(const EntityList& list, EntityList&
// Linear search for entity. I'll eventually add a binary search function if it can be assumed that
// the list is sorted
bool EntityListFindEntity(EntityList& list, Entity entity);
// This should probably become an enum at some point.
typedef unsigned int ComponentType;
};

37
src/game/agent/AgentComponentManager.cpp

@ -3,12 +3,14 @@
#include "../../util/Logging.hpp"
#include "../../entityComponentSystem/PooledComponentManager.hpp"
#include "../../entityComponentSystem/ComponentTypes.hpp"
#include "../../ai/htn/HTNTaskDb.hpp"
namespace gv
{
AgentComponentManager::AgentComponentManager() : gv::PooledComponentManager<AgentComponentData>(100)
{
Type = gv::ComponentType::Agent;
}
AgentComponentManager::~AgentComponentManager()
@ -167,6 +169,8 @@ void AgentComponentManager::SubscribeEntitiesInternal(const EntityList& subscrib
{
if (!currentComponent)
continue;
currentComponent->data.ConsciousState = AgentConsciousState::Conscious;
}
LOGD_IF(DebugPrint) << "AgentComponentManager: Subscribed " << subscribers.size()
@ -187,4 +191,37 @@ void AgentComponentManager::UnsubscribeEntitiesInternal(const EntityList& unsubs
LOGD_IF(unsubscribers.size()) << "AgentComponentManager: Unsubscribed " << unsubscribers.size()
<< " entities";
}
void AgentComponentManager::GetAgentConsciousStates(const EntityList& entities,
AgentConsciousStateList& stateListOut)
{
for (const Entity& entity : entities)
{
bool foundEntity = false;
// TODO: Adding true iterator support to pool will drastically help damning this to hell
gv::PooledComponentManager<AgentComponentData>::FragmentedPoolIterator it =
gv::PooledComponentManager<AgentComponentData>::NULL_POOL_ITERATOR;
for (gv::PooledComponent<AgentComponentData>* currentComponent = ActivePoolBegin(it);
currentComponent != nullptr &&
it != gv::PooledComponentManager<AgentComponentData>::NULL_POOL_ITERATOR;
currentComponent = GetNextActivePooledComponent(it))
{
if (!currentComponent)
continue;
Entity currentEntity = currentComponent->entity;
if (currentEntity == entity)
{
stateListOut.push_back(currentComponent->data.ConsciousState);
foundEntity = true;
break;
}
}
if (!foundEntity)
stateListOut.push_back(AgentConsciousState::None);
}
}
}

22
src/game/agent/AgentComponentManager.hpp

@ -1,6 +1,9 @@
#pragma once
#include <vector>
#include "../../entityComponentSystem/PooledComponentManager.hpp"
#include "../../entityComponentSystem/EntityTypes.hpp"
#include "PlanComponentManager.hpp"
#include "Needs.hpp"
@ -31,9 +34,26 @@ struct AgentGoal
};
typedef std::vector<AgentGoal> AgentGoalList;
// An agent can only be one of these at a time
enum class AgentConsciousState
{
None = 0,
Conscious,
Unconscious,
Sleeping,
Dead,
AgentState_count
};
typedef std::vector<AgentConsciousState> AgentConsciousStateList;
struct AgentComponentData
{
bool IsAlive = true;
AgentConsciousState ConsciousState;
NeedList Needs;
AgentGoalList Goals;
};
@ -65,5 +85,7 @@ public:
void Initialize(PlanComponentManager* newPlanComponentManager);
virtual void Update(float deltaSeconds);
void GetAgentConsciousStates(const EntityList& entities, AgentConsciousStateList& stateListOut);
};
};

2
src/game/agent/PlanComponentManager.cpp

@ -3,11 +3,13 @@
#include "../../util/Logging.hpp"
#include "../../entityComponentSystem/PooledComponentManager.hpp"
#include "../../entityComponentSystem/ComponentTypes.hpp"
namespace gv
{
PlanComponentManager::PlanComponentManager() : gv::PooledComponentManager<PlanComponentData>(100)
{
Type = ComponentType::Plan;
}
PlanComponentManager::~PlanComponentManager()

3
src/unitTesting/EntityComponentSystem_test.cpp

@ -3,6 +3,7 @@
#include "../entityComponentSystem/EntityTypes.hpp"
#include "../entityComponentSystem/EntityComponentManager.hpp"
#include "../entityComponentSystem/ComponentTypes.hpp"
#include "../entityComponentSystem/ComponentManager.hpp"
#include "../entityComponentSystem/PooledComponentManager.hpp"
@ -34,7 +35,7 @@ void TestEntityCreationAndDestruction()
public:
TestComponentManager()
{
Type = 1;
Type = ComponentType::Test;
}
virtual ~TestComponentManager()
{

Loading…
Cancel
Save