"Basic" entity component system. Almost.
This commit is contained in:
@@ -0,0 +1,14 @@
|
|||||||
|
#ifndef Component_h__
|
||||||
|
#define Component_h__
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
#include "Entity.h"
|
||||||
|
#include "ComponentFactory.h"
|
||||||
|
|
||||||
|
struct Component
|
||||||
|
{
|
||||||
|
EntityID Entity;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // Component_h__
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
#ifndef ComponentFactory_h__
|
||||||
|
#define ComponentFactory_h__
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <functional>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
#include "Component.h"
|
||||||
|
|
||||||
|
#define REGISTER_COMPONENT(NAME, TYPE) \
|
||||||
|
static ComponentRegistrar<TYPE> registrar(NAME);
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
class ComponentRegistrar
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ComponentRegistrar(std::string name)
|
||||||
|
{
|
||||||
|
ComponentFactory::Instance()->Register(name, [](void) -> std::shared_ptr<Component> { return new T() });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class ComponentFactory
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
static ComponentFactory* Instance()
|
||||||
|
{
|
||||||
|
static ComponentFactory factory;
|
||||||
|
return &factory;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Register(std::string name, std::function<std::shared_ptr<Component>(void)> factoryFunction)
|
||||||
|
{
|
||||||
|
m_FactoryFunctions[name] = factoryFunction;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<Component> Create(std::string name)
|
||||||
|
{
|
||||||
|
auto it = m_FactoryFunctions.find(name);
|
||||||
|
if (it != m_FactoryFunctions.end())
|
||||||
|
return it->second();
|
||||||
|
else
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::map<std::string, std::function<std::shared_ptr<Component>(void)>> m_FactoryFunctions;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // ComponentFactory_h__
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
#include "Component.h"
|
||||||
|
|
||||||
|
namespace Components
|
||||||
|
{
|
||||||
|
|
||||||
|
REGISTER_COMPONENT("Transform", Transform);
|
||||||
|
struct Transform : Component
|
||||||
|
{
|
||||||
|
float Position[3];
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
typedef unsigned int EntityID;
|
||||||
@@ -39,7 +39,7 @@
|
|||||||
</ImportGroup>
|
</ImportGroup>
|
||||||
<PropertyGroup Label="UserMacros" />
|
<PropertyGroup Label="UserMacros" />
|
||||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||||
<IncludePath>$(SolutionDir)\Libraries;$(SolutionDir)\Libraries\glew-1.10.0\include;$(SolutionDir)\Libraries\glfw-3.0.4\include;$(SolutionDir)\Libraries\glm-0.9.5.2;$(IncludePath)</IncludePath>
|
<IncludePath>$(ProjectDir);$(SolutionDir)\Libraries;$(SolutionDir)\Libraries\glew-1.10.0\include;$(SolutionDir)\Libraries\glfw-3.0.4\include;$(SolutionDir)\Libraries\glm-0.9.5.2;$(IncludePath)</IncludePath>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||||
<LibraryPath>$(SolutionDir)\Libraries\glew-1.10.0\lib\Debug\Win32;$(SolutionDir)\Libraries\glfw-3.0.4\lib\Debug;$(LibraryPath)</LibraryPath>
|
<LibraryPath>$(SolutionDir)\Libraries\glew-1.10.0\lib\Debug\Win32;$(SolutionDir)\Libraries\glfw-3.0.4\lib\Debug;$(LibraryPath)</LibraryPath>
|
||||||
@@ -48,7 +48,7 @@
|
|||||||
<TargetName>$(ProjectName)-$(Configuration)</TargetName>
|
<TargetName>$(ProjectName)-$(Configuration)</TargetName>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||||
<IncludePath>$(SolutionDir)\Libraries;$(SolutionDir)\Libraries\glew-1.10.0\include;$(SolutionDir)\Libraries\glfw-3.0.4\include;$(SolutionDir)\Libraries\glm-0.9.5.2;$(IncludePath)</IncludePath>
|
<IncludePath>$(ProjectDir);$(SolutionDir)\Libraries;$(SolutionDir)\Libraries\glew-1.10.0\include;$(SolutionDir)\Libraries\glfw-3.0.4\include;$(SolutionDir)\Libraries\glm-0.9.5.2;$(IncludePath)</IncludePath>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||||
<LibraryPath>$(SolutionDir)\Libraries\glew-1.10.0\lib\Release\Win32;$(SolutionDir)\Libraries\glfw-3.0.4\lib\Release;$(LibraryPath)</LibraryPath>
|
<LibraryPath>$(SolutionDir)\Libraries\glew-1.10.0\lib\Release\Win32;$(SolutionDir)\Libraries\glfw-3.0.4\lib\Release;$(LibraryPath)</LibraryPath>
|
||||||
@@ -90,8 +90,13 @@
|
|||||||
<ClCompile Include="main.cpp" />
|
<ClCompile Include="main.cpp" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClInclude Include="Component.h" />
|
||||||
|
<ClInclude Include="ComponentFactory.h" />
|
||||||
|
<ClInclude Include="Components\Transform.h" />
|
||||||
|
<ClInclude Include="Entity.h" />
|
||||||
<ClInclude Include="logging.h" />
|
<ClInclude Include="logging.h" />
|
||||||
<ClInclude Include="glerror.h" />
|
<ClInclude Include="glerror.h" />
|
||||||
|
<ClInclude Include="World.h" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
<ImportGroup Label="ExtensionTargets">
|
<ImportGroup Label="ExtensionTargets">
|
||||||
|
|||||||
@@ -13,6 +13,9 @@
|
|||||||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
||||||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||||
</Filter>
|
</Filter>
|
||||||
|
<Filter Include="Header Files\Components">
|
||||||
|
<UniqueIdentifier>{9af04489-86da-41b7-aaba-12d5b6653660}</UniqueIdentifier>
|
||||||
|
</Filter>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="main.cpp">
|
<ClCompile Include="main.cpp">
|
||||||
@@ -26,5 +29,20 @@
|
|||||||
<ClInclude Include="logging.h">
|
<ClInclude Include="logging.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="World.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="Component.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="Entity.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="Components\Transform.h">
|
||||||
|
<Filter>Header Files\Components</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="ComponentFactory.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
@@ -0,0 +1,81 @@
|
|||||||
|
#include <stack>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "Entity.h"
|
||||||
|
#include "Component.h"
|
||||||
|
#include "Components/Transform.h"
|
||||||
|
|
||||||
|
class World
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
World()
|
||||||
|
{
|
||||||
|
m_LastEntityID = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
EntityID CreateEntity(EntityID parent = 0)
|
||||||
|
{
|
||||||
|
EntityID newEntity = GenerateEntityID();
|
||||||
|
m_SceneGraph.insert(std::pair<EntityID, EntityID>(newEntity, parent));
|
||||||
|
return newEntity;
|
||||||
|
}
|
||||||
|
|
||||||
|
void RemoveEntity(EntityID entity)
|
||||||
|
{
|
||||||
|
m_SceneGraph.erase(entity);
|
||||||
|
RecycleEntityID(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ValidEntity(EntityID entity)
|
||||||
|
{
|
||||||
|
return m_SceneGraph.find(entity) != m_SceneGraph.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
EntityID GetEntityParent(EntityID entity)
|
||||||
|
{
|
||||||
|
auto it = m_SceneGraph.find(entity);
|
||||||
|
return it == m_SceneGraph.end() ? 0 : it->second;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<Component> AddComponent(EntityID entity, std::string componentType)
|
||||||
|
{
|
||||||
|
std::shared_ptr<Component> component = ComponentFactory::Instance()->Create(componentType);
|
||||||
|
m_ComponentsOfType[componentType].push_back(component);
|
||||||
|
m_EntityComponents[entity][componentType] = component;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*std::vector<EntityID> GetEntityChildren(EntityID entity)
|
||||||
|
{
|
||||||
|
std::vector<EntityID> children;
|
||||||
|
auto range = m_SceneGraph.equal_range(entity);
|
||||||
|
for (auto it = range.first; it != range.second; ++it)
|
||||||
|
children.push_back(it->second);
|
||||||
|
return children;
|
||||||
|
}*/
|
||||||
|
|
||||||
|
private:
|
||||||
|
EntityID m_LastEntityID;
|
||||||
|
std::stack<EntityID> m_RecycledEntityIDs;
|
||||||
|
// A map of child entities to parent entities
|
||||||
|
std::unordered_map<EntityID, EntityID> m_SceneGraph;
|
||||||
|
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;
|
||||||
|
|
||||||
|
EntityID GenerateEntityID()
|
||||||
|
{
|
||||||
|
if (!m_RecycledEntityIDs.empty()) {
|
||||||
|
EntityID id = m_RecycledEntityIDs.top();
|
||||||
|
m_RecycledEntityIDs.pop();
|
||||||
|
return id;
|
||||||
|
} else {
|
||||||
|
return ++m_LastEntityID;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void RecycleEntityID(EntityID id)
|
||||||
|
{
|
||||||
|
m_RecycledEntityIDs.push(id);
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user