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
{
EntityID Entity;
virtual Component* Clone() const = 0;
};
class ComponentFactory : public Factory<Component*> { };
+2
View File
@@ -14,6 +14,8 @@ struct Box : Component
float Width;
float Height;
float Depth;
virtual Box* Clone() const override { return new Box(*this); }
};
}
+2
View File
@@ -13,6 +13,8 @@ struct Camera : Component
float FOV;
float NearClip;
float FarClip;
virtual Camera* Clone() const override { return new Camera(*this); }
};
}
+2
View File
@@ -13,6 +13,8 @@ struct DirectionalLight : Component
float MaxRange;
float SpecularIntensity;
Color Color;
virtual DirectionalLight* Clone() const override { return new DirectionalLight(*this); }
};
}
+2
View File
@@ -9,6 +9,8 @@ struct FreeSteering : Component
{
FreeSteering() : Speed(35) { }
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;
float dX, dY;
float WheelDelta;
virtual Input* Clone() const override { return new Input(*this); }
};
}
+2
View File
@@ -16,6 +16,8 @@ struct Model : Component
Color Color;
bool Visible;
bool ShadowCaster;
virtual Model* Clone() const override { return new Model(*this); }
};
}
+2
View File
@@ -16,6 +16,8 @@ namespace Components
double LifeTime;
std::vector<glm::vec3> VelocitySpectrum;
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> AngularVelocitySpectrum;
virtual ParticleEmitter* Clone() const override { return new ParticleEmitter(*this); }
private:
double TimeSinceLastSpawn;
};
+2
View File
@@ -12,6 +12,8 @@ struct Physics : Component
: Mass(0.f) { }
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 spotExponent;
Color color;
virtual PointLight* Clone() const override { return new PointLight(*this); }
};
}
+2
View File
@@ -17,6 +17,8 @@ struct SoundEmitter : Component
float Pitch;
bool Loop;
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){ }
float Radius;
virtual Sphere* Clone() const override { return new Sphere(*this); }
};
}
+2
View File
@@ -13,6 +13,8 @@ struct Sprite : Component
{
std::string SpriteFile;
Color Color;
virtual Sprite* Clone() const override { return new Sprite(*this); }
};
}
+6 -1
View File
@@ -6,7 +6,12 @@
namespace Components
{
struct Template : Component { };
struct Template
: public Component
{
virtual Template* Clone() const override { return nullptr; }
};
}
#endif // !Components_Template_h__
+3 -1
View File
@@ -6,7 +6,7 @@
namespace Components
{
struct Transform : Component
struct Transform : public Component
{
Transform()
: Scale(glm::vec3(1.f)) { }
@@ -15,6 +15,8 @@ struct Transform : Component
glm::quat Orientation;
glm::vec3 Velocity;
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)
{
m_EntityParents.erase(entity);
m_EntityChildren.erase(entity);
// Remove components
for (auto pair : m_EntityComponents[entity])
{
@@ -116,7 +117,8 @@ void World::ProcessEntityRemovals()
EntityID World::CreateEntity(EntityID parent /*= 0*/)
{
EntityID newEntity = GenerateEntityID();
m_EntityParents.insert(std::pair<EntityID, EntityID>(newEntity, parent));
m_EntityParents[newEntity] = parent;
m_EntityChildren[parent].push_back(newEntity);
return newEntity;
}
@@ -148,7 +150,58 @@ std::shared_ptr<Component> World::AddComponent(EntityID entity, std::string comp
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)
{
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);
EntityID CreateEntity(EntityID parent = 0);
EntityID CloneEntity(EntityID entity, EntityID parent = 0);
void RemoveEntity(EntityID entity);
@@ -41,6 +42,7 @@ public:
EntityID GetEntityParent(EntityID entity);
EntityID GetEntityBaseParent(EntityID entity);
std::list<EntityID> GetEntityChildren(EntityID entity);
template <class T>
T GetProperty(EntityID entity, std::string property)
@@ -83,13 +85,16 @@ protected:
EntityID m_LastEntityID;
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<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;
void ProcessEntityRemovals();
@@ -121,14 +126,8 @@ std::shared_ptr<T> World::AddComponent(EntityID entity, std::string componentTyp
return nullptr;
}
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);
}
AddComponent(entity, componentType, component);
return component;
}
@@ -215,14 +215,18 @@
<ClInclude Include="..\..\src\Sound.h">
<Filter>Audio</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Components\Box.h" />
<ClInclude Include="..\..\src\Components\Sphere.h" />
<ClInclude Include="..\..\src\Systems\ParticleSystem.h">
<Filter>Particle System\Systems</Filter>
</ClInclude>
<ClInclude Include="..\..\src\Components\Particle.h">
<Filter>Particle System\Components</Filter>
</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>
<None Include="..\..\src\Shaders\AABB.frag.glsl">