Implemented entity cloning

This commit is contained in:
2014-04-27 07:48:16 +02:00
parent 956e7cab7b
commit 04cafc53e2
19 changed files with 107 additions and 16 deletions
+2
View File
@@ -7,6 +7,8 @@
struct Component struct Component
{ {
EntityID Entity; EntityID Entity;
virtual Component* Clone() const = 0;
}; };
class ComponentFactory : public Factory<Component*> { }; class ComponentFactory : public Factory<Component*> { };
+2
View File
@@ -14,6 +14,8 @@ struct Box : Component
float Width; float Width;
float Height; float Height;
float Depth; float Depth;
virtual Box* Clone() const override { return new Box(*this); }
}; };
} }
+2
View File
@@ -13,6 +13,8 @@ struct Camera : Component
float FOV; float FOV;
float NearClip; float NearClip;
float FarClip; float FarClip;
virtual Camera* Clone() const override { return new Camera(*this); }
}; };
} }
+2
View File
@@ -13,6 +13,8 @@ struct DirectionalLight : Component
float MaxRange; float MaxRange;
float SpecularIntensity; float SpecularIntensity;
Color Color; Color Color;
virtual DirectionalLight* Clone() const override { return new DirectionalLight(*this); }
}; };
} }
+2
View File
@@ -9,6 +9,8 @@ struct FreeSteering : Component
{ {
FreeSteering() : Speed(35) { } FreeSteering() : Speed(35) { }
float Speed; float Speed;
virtual FreeSteering* Clone() const override { return new FreeSteering(*this); }
}; };
} }
+2
View File
@@ -18,6 +18,8 @@ struct Input : Component
std::array<int, GLFW_MOUSE_BUTTON_LAST+1> LastMouseState; std::array<int, GLFW_MOUSE_BUTTON_LAST+1> LastMouseState;
float dX, dY; float dX, dY;
float WheelDelta; float WheelDelta;
virtual Input* Clone() const override { return new Input(*this); }
}; };
} }
+2
View File
@@ -16,6 +16,8 @@ struct Model : Component
Color Color; Color Color;
bool Visible; bool Visible;
bool ShadowCaster; bool ShadowCaster;
virtual Model* Clone() const override { return new Model(*this); }
}; };
} }
+2
View File
@@ -16,6 +16,8 @@ namespace Components
double LifeTime; double LifeTime;
std::vector<glm::vec3> VelocitySpectrum; std::vector<glm::vec3> VelocitySpectrum;
std::vector<glm::vec3> AngularVelocitySpectrum; std::vector<glm::vec3> AngularVelocitySpectrum;
virtual Particle* Clone() const override { return new Particle(*this); }
}; };
} }
+2
View File
@@ -24,6 +24,8 @@ struct ParticleEmitter : Component
std::vector<glm::vec3> VelocitySpectrum; std::vector<glm::vec3> VelocitySpectrum;
std::vector<glm::vec3> AngularVelocitySpectrum; std::vector<glm::vec3> AngularVelocitySpectrum;
virtual ParticleEmitter* Clone() const override { return new ParticleEmitter(*this); }
private: private:
double TimeSinceLastSpawn; double TimeSinceLastSpawn;
}; };
+2
View File
@@ -12,6 +12,8 @@ struct Physics : Component
: Mass(0.f) { } : Mass(0.f) { }
float Mass; float Mass;
virtual Physics* Clone() const override { return new Physics(*this); }
}; };
} }
+2
View File
@@ -16,6 +16,8 @@ struct PointLight : Component
float constantAttenuation, linearAttenuation, quadraticAttenuation; float constantAttenuation, linearAttenuation, quadraticAttenuation;
float spotExponent; float spotExponent;
Color color; Color color;
virtual PointLight* Clone() const override { return new PointLight(*this); }
}; };
} }
+2
View File
@@ -17,6 +17,8 @@ struct SoundEmitter : Component
float Pitch; float Pitch;
bool Loop; bool Loop;
std::string Path; std::string Path;
virtual SoundEmitter* Clone() const override { return new SoundEmitter(*this); }
}; };
} }
+2
View File
@@ -12,6 +12,8 @@ struct Sphere : Component
: Radius(1.f){ } : Radius(1.f){ }
float Radius; float Radius;
virtual Sphere* Clone() const override { return new Sphere(*this); }
}; };
} }
+2
View File
@@ -13,6 +13,8 @@ struct Sprite : Component
{ {
std::string SpriteFile; std::string SpriteFile;
Color Color; Color Color;
virtual Sprite* Clone() const override { return new Sprite(*this); }
}; };
} }
+6 -1
View File
@@ -6,7 +6,12 @@
namespace Components namespace Components
{ {
struct Template : Component { }; struct Template
: public Component
{
virtual Template* Clone() const override { return nullptr; }
};
} }
#endif // !Components_Template_h__ #endif // !Components_Template_h__
+3 -1
View File
@@ -6,7 +6,7 @@
namespace Components namespace Components
{ {
struct Transform : Component struct Transform : public Component
{ {
Transform() Transform()
: Scale(glm::vec3(1.f)) { } : Scale(glm::vec3(1.f)) { }
@@ -15,6 +15,8 @@ struct Transform : Component
glm::quat Orientation; glm::quat Orientation;
glm::vec3 Velocity; glm::vec3 Velocity;
glm::vec3 Scale; glm::vec3 Scale;
virtual Transform* Clone() const override { return new Transform(*this); }
}; };
} }
+54 -1
View File
@@ -93,6 +93,7 @@ void World::ProcessEntityRemovals()
for (auto entity : m_EntitiesToRemove) for (auto entity : m_EntitiesToRemove)
{ {
m_EntityParents.erase(entity); m_EntityParents.erase(entity);
m_EntityChildren.erase(entity);
// Remove components // Remove components
for (auto pair : m_EntityComponents[entity]) for (auto pair : m_EntityComponents[entity])
{ {
@@ -116,7 +117,8 @@ void World::ProcessEntityRemovals()
EntityID World::CreateEntity(EntityID parent /*= 0*/) EntityID World::CreateEntity(EntityID parent /*= 0*/)
{ {
EntityID newEntity = GenerateEntityID(); EntityID newEntity = GenerateEntityID();
m_EntityParents.insert(std::pair<EntityID, EntityID>(newEntity, parent)); m_EntityParents[newEntity] = parent;
m_EntityChildren[parent].push_back(newEntity);
return newEntity; return newEntity;
} }
@@ -148,7 +150,58 @@ std::shared_ptr<Component> World::AddComponent(EntityID entity, std::string comp
return AddComponent<Component>(entity, componentType); return AddComponent<Component>(entity, componentType);
} }
void World::AddComponent(EntityID entity, std::string componentType, std::shared_ptr<Component> component)
{
component->Entity = entity;
m_ComponentsOfType[componentType].push_back(component);
m_EntityComponents[entity][componentType] = component;
for (auto pair : m_Systems)
{
auto system = pair.second;
system->OnComponentCreated(componentType, component);
}
}
void World::AddSystem(std::string systemType) void World::AddSystem(std::string systemType)
{ {
m_Systems[systemType] = std::shared_ptr<System>(m_SystemFactory.Create(systemType)); m_Systems[systemType] = std::shared_ptr<System>(m_SystemFactory.Create(systemType));
} }
EntityID World::CloneEntity(EntityID entity, EntityID parent /* = 0 */)
{
int clone = CreateEntity(parent);
for (auto pair : m_EntityComponents[entity])
{
auto type = pair.first;
auto component = std::shared_ptr<Component>(pair.second->Clone());
if (component != nullptr)
{
AddComponent(clone, type, component);
}
}
auto itChildren = m_EntityChildren.find(entity);
if (itChildren != m_EntityChildren.end())
{
for (EntityID child : itChildren->second)
{
CloneEntity(child, clone);
}
}
return clone;
}
std::list<EntityID> World::GetEntityChildren(EntityID entity)
{
auto it = m_EntityChildren.find(entity);
if (it == m_EntityChildren.end())
{
return std::list<EntityID>();
}
else
{
return it->second;
}
}
+10 -11
View File
@@ -34,6 +34,7 @@ public:
std::shared_ptr<T> GetSystem(std::string systemType); std::shared_ptr<T> GetSystem(std::string systemType);
EntityID CreateEntity(EntityID parent = 0); EntityID CreateEntity(EntityID parent = 0);
EntityID CloneEntity(EntityID entity, EntityID parent = 0);
void RemoveEntity(EntityID entity); void RemoveEntity(EntityID entity);
@@ -41,6 +42,7 @@ public:
EntityID GetEntityParent(EntityID entity); EntityID GetEntityParent(EntityID entity);
EntityID GetEntityBaseParent(EntityID entity); EntityID GetEntityBaseParent(EntityID entity);
std::list<EntityID> GetEntityChildren(EntityID entity);
template <class T> template <class T>
T GetProperty(EntityID entity, std::string property) T GetProperty(EntityID entity, std::string property)
@@ -83,13 +85,16 @@ protected:
EntityID m_LastEntityID; EntityID m_LastEntityID;
std::stack<EntityID> m_RecycledEntityIDs; std::stack<EntityID> m_RecycledEntityIDs;
// A bottom to top tree. A map of child entities to parent entities.
std::unordered_map<EntityID, EntityID> m_EntityParents;
std::unordered_map<EntityID, std::unordered_map<std::string, boost::any>> m_EntityProperties;
std::unordered_map<EntityID, EntityID> m_EntityParents; // child -> parent
std::unordered_map<EntityID, std::list<EntityID>> m_EntityChildren; // parent -> child
std::unordered_map<EntityID, std::unordered_map<std::string, boost::any>> m_EntityProperties;
std::unordered_map<std::string, std::list<std::shared_ptr<Component>>> m_ComponentsOfType; std::unordered_map<std::string, std::list<std::shared_ptr<Component>>> m_ComponentsOfType;
std::unordered_map<EntityID, std::map<std::string, std::shared_ptr<Component>>> m_EntityComponents; std::unordered_map<EntityID, std::map<std::string, std::shared_ptr<Component>>> m_EntityComponents;
// Internal: Add a component to an entity
void AddComponent(EntityID entity, std::string componentType, std::shared_ptr<Component> component);
std::list<EntityID> m_EntitiesToRemove; std::list<EntityID> m_EntitiesToRemove;
void ProcessEntityRemovals(); void ProcessEntityRemovals();
@@ -121,14 +126,8 @@ std::shared_ptr<T> World::AddComponent(EntityID entity, std::string componentTyp
return nullptr; return nullptr;
} }
component->Entity = entity; AddComponent(entity, componentType, component);
m_ComponentsOfType[componentType].push_back(component);
m_EntityComponents[entity][componentType] = component;
for (auto pair : m_Systems)
{
auto system = pair.second;
system->OnComponentCreated(componentType, component);
}
return component; return component;
} }
@@ -215,14 +215,18 @@
<ClInclude Include="..\..\src\Sound.h"> <ClInclude Include="..\..\src\Sound.h">
<Filter>Audio</Filter> <Filter>Audio</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\src\Components\Box.h" />
<ClInclude Include="..\..\src\Components\Sphere.h" />
<ClInclude Include="..\..\src\Systems\ParticleSystem.h"> <ClInclude Include="..\..\src\Systems\ParticleSystem.h">
<Filter>Particle System\Systems</Filter> <Filter>Particle System\Systems</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\src\Components\Particle.h"> <ClInclude Include="..\..\src\Components\Particle.h">
<Filter>Particle System\Components</Filter> <Filter>Particle System\Components</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\src\Components\Box.h">
<Filter>Physics\Components</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Components\Sphere.h">
<Filter>Physics\Components</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="..\..\src\Shaders\AABB.frag.glsl"> <None Include="..\..\src\Shaders\AABB.frag.glsl">