Merge pull request #9 from teamfisk/ecs-systems

ECS Systems
This commit is contained in:
2015-12-10 10:42:37 +01:00
13 changed files with 229 additions and 47 deletions
+1 -1
Submodule assets updated: 8a7ecf53f7...b37468222e
+25
View File
@@ -0,0 +1,25 @@
#ifndef System_h__
#define System_h__
#include "EventBroker.h"
#include "World.h"
#include "ComponentWrapper.h"
class System
{
friend class SystemPipeline;
public:
System(const EventBroker* eventBroker, std::string componentType)
: m_EventBroker(eventBroker)
, m_ComponentType(componentType)
{ }
virtual void Update(World* world, ComponentWrapper& component, double dt) = 0;
private:
const EventBroker* m_EventBroker;
std::string m_ComponentType;
};
#endif
+58
View File
@@ -0,0 +1,58 @@
#ifndef SystemPipeline_h__
#define SystemPipeline_h__
#include "../Common.h"
#include "EventBroker.h"
#include "System.h"
#include "World.h"
class SystemPipeline
{
public:
SystemPipeline(const EventBroker* eventBroker)
: m_EventBroker(eventBroker)
{ }
~SystemPipeline()
{
for (auto& pair : m_Systems) {
for (auto& system : pair.second) {
delete system;
}
}
}
template <typename T, typename... Arguments>
void AddSystem(Arguments... args)
{
System* system = new T(m_EventBroker, args...);
if (!system->m_ComponentType.empty()) {
m_Systems[system->m_ComponentType].push_back(system);
} else {
LOG_ERROR("Failed to add system \"%s\": Missing component type!", typeid(T).name());
delete system;
}
}
void Update(World* world, double dt)
{
for (auto& pair : m_Systems) {
const std::string& componentName = pair.first;
auto& systems = pair.second;
const ComponentPool* pool = world->GetComponents(componentName);
if (pool == nullptr) {
continue;
}
for (auto& component : *pool) {
for (auto& system : systems) {
system->Update(world, component, dt);
}
}
}
}
private:
const EventBroker* m_EventBroker;
std::unordered_map<std::string, std::vector<System*>> m_Systems;
};
#endif
@@ -22,6 +22,10 @@ private:
void FillLights(World* world, RenderQueue* renderQueue);
glm::mat4 ModelMatrix(World* world, EntityID entity);
glm::vec3 AbsolutePosition(World* world, EntityID entity);
glm::quat AbsoluteOrientation(World* world, EntityID entity);
glm::vec3 AbsoluteScale(World* world, EntityID entity);
};
#endif
+3
View File
@@ -11,6 +11,8 @@
#include "Rendering/RenderQueueFactory.h"
#include "Core/EKeyDown.h"
#include "Core/EntityXMLFile.h"
#include "Core/SystemPipeline.h"
#include "RaptorCopterSystem.h"
class Game
{
@@ -29,6 +31,7 @@ private:
InputManager* m_InputManager;
GUI::Frame* m_FrameStack;
World* m_World;
SystemPipeline* m_SystemPipeline;
RenderQueueFactory* m_RenderQueueFactory;
EventRelay<Game, Events::KeyUp> m_EKeyUp;
+16
View File
@@ -0,0 +1,16 @@
#include "Common.h"
#include "Core/System.h"
class RaptorCopterSystem : public System
{
public:
RaptorCopterSystem(const EventBroker* eventBroker)
: System(eventBroker, "RaptorCopter")
{ }
virtual void Update(World* world, ComponentWrapper& raptorCopter, double dt) override
{
ComponentWrapper& transform = world->GetComponent(raptorCopter.EntityID, "Transform");
(glm::vec3&)transform["Orientation"] += (float)(double)raptorCopter["Speed"] * (float)dt * (glm::vec3)raptorCopter["Axis"];
}
};
+1
View File
@@ -4,4 +4,5 @@
<xs:include schemaLocation="Components/Transform.xsd"/>
<xs:include schemaLocation="Components/Model.xsd"/>
<xs:include schemaLocation="Components/Test.xsd"/>
<xs:include schemaLocation="Components/RaptorCopter.xsd"/>
</xs:schema>
@@ -0,0 +1,4 @@
<c:RaptorCopter>
<Speed>0</Speed>
<Axis X="0" Y="0" Z="0"/>
</c:RaptorCopter>
@@ -0,0 +1,14 @@
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:t="types">
<xs:import schemaLocation="../Types.xsd" namespace="types"/>
<xs:element name="RaptorCopter">
<xs:complexType>
<xs:all>
<xs:element name="Speed" type="t:double" minOccurs="0"/>
<xs:element name="Axis" type="t:Vector" minOccurs="0"/>
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>
+56 -37
View File
@@ -37,57 +37,76 @@
<Position X="0" Y="-0"/>
<Scale X="1" Y="1" Z="1"/>
</c:Transform>
<c:Model>
<Resource>Models/Core/UnitRaptor.obj</Resource>
<Color R="1" G="0.4" B="0.8"/>
</c:Model>
<!--<c:Move>
<Speed>1</Speed>
<Direction X="-1"/>
<Rotation Y="3.14"/>
</c:Move>-->
</Components>
<Children>
<Entity>
<Components>
<c:Transform>
<Position X="-0.2" Y="0"/>
<Orientation X="0" Y="0" Z="0"/>
<Position X="0" Y="0"/>
<Orientation X="0.0" Y="0" Z="0.0"/>
</c:Transform>
<c:Model>
<Resource>Models/Core/UnitRaptor.obj</Resource>
<Color R="1" G="0.4" B="0.8"/>
</c:Model>
</Components>
<Children>
<Entity>
<Components>
<c:Transform>
<Position X="0" Y="0.4"/>
<Scale X="0.1" Y="0.4" Z="0.1"/>
</c:Transform>
<c:Model>
<Resource>Models/Core/UnitCube.obj</Resource>
<Color R="1" G="0.4" B="0.8"/>
</c:Model>
</Components>
</Entity>
<Entity>
<Components>
<c:Transform>
<Position X="0" Y="0.6"/>
<Scale X="1.7" Y="0.03" Z="0.1"/>
<Position X="0" Y="0"/>
<Orientation X="0" Y="0" Z="0"/>
</c:Transform>
<c:Model>
<Resource>Models/Core/UnitCube.obj</Resource>
<Color R="1" G="0.4" B="0.8"/>
</c:Model>
</Components>
</Entity>
<Entity>
<Components>
<c:Transform>
<Position X="0" Y="0.6"/>
<Scale X="1.7" Y="0.03" Z="0.1"/>
<Orientation X="0" Y="1.57" Z="0"/>
</c:Transform>
<c:Model>
<Resource>Models/Core/UnitCube.obj</Resource>
<Color R="1" G="0.4" B="0.8"/>
</c:Model>
<c:RaptorCopter>
<Speed>20</Speed>
<Axis Y="1"/>
</c:RaptorCopter>
</Components>
<Children>
<Entity>
<Components>
<c:Transform>
<Position X="0" Y="0.4"/>
<Scale X="0.1" Y="0.4" Z="0.1"/>
</c:Transform>
<c:Model>
<Resource>Models/Core/UnitCylinder.obj</Resource>
<Color R="1" G="0.4" B="0.8"/>
</c:Model>
</Components>
</Entity>
<Entity>
<Components>
<c:Transform>
<Position X="0" Y="0.6"/>
<Scale X="1.7" Y="0.03" Z="0.1"/>
<Orientation X="0" Y="0" Z="0"/>
</c:Transform>
<c:Model>
<Resource>Models/Core/UnitCube.obj</Resource>
<Color R="1" G="0.4" B="0.8"/>
</c:Model>
</Components>
</Entity>
<Entity>
<Components>
<c:Transform>
<Position X="0" Y="0.6"/>
<Scale X="1.7" Y="0.03" Z="0.1"/>
<Orientation X="0" Y="1.57" Z="0"/>
</c:Transform>
<c:Model>
<Resource>Models/Core/UnitCube.obj</Resource>
<Color R="1" G="0.4" B="0.8"/>
</c:Model>
</Components>
</Entity>
</Children>
</Entity>
</Children>
</Entity>
+1
View File
@@ -13,6 +13,7 @@
<xs:element ref="c:Transform" minOccurs="0"/>
<xs:element ref="c:Model" minOccurs="0"/>
<xs:element ref="c:Test" minOccurs="0"/>
<xs:element ref="c:RaptorCopter" minOccurs="0"/>
</xs:all>
</xs:complexType>
</xs:element>
+40 -9
View File
@@ -15,20 +15,51 @@ void RenderQueueFactory::Update(World* world)
glm::mat4 RenderQueueFactory::ModelMatrix(World* world, EntityID entity)
{
//should really return absolute model matrix based on parents position, scale and orientation
//GetAbsolutePosition(World* world, ComponentWrapper transformComponent)
glm::vec3 position = AbsolutePosition(world, entity);
glm::quat orientation = AbsoluteOrientation(world, entity);
glm::vec3 scale = AbsoluteScale(world, entity);
ComponentWrapper transformComponent = world->GetComponent(entity, "Transform");
glm::vec3 position = transformComponent["Position"];
glm::vec3 scale = transformComponent["Scale"];
glm::vec3 oritentation = transformComponent["Orientation"];
glm::mat4 modelMatrix = glm::translate(glm::mat4(), position) * glm::toMat4(orientation) * glm::scale(scale);
return modelMatrix;
}
glm::vec3 RenderQueueFactory::AbsolutePosition(World* world, EntityID entity)
{
glm::vec3 position;
do {
ComponentWrapper transform = world->GetComponent(entity, "Transform");
position += AbsoluteOrientation(world, entity) * (glm::vec3)transform["Position"];
entity = world->GetParent(entity);
} while (entity != 0);
return position;
}
glm::quat RenderQueueFactory::AbsoluteOrientation(World* world, EntityID entity)
{
glm::quat orientation;
do {
ComponentWrapper transform = world->GetComponent(entity, "Transform");
orientation = glm::quat((glm::vec3)transform["Orientation"]) * orientation;
entity = world->GetParent(entity);
} while (entity != 0);
return orientation;
}
glm::vec3 RenderQueueFactory::AbsoluteScale(World* world, EntityID entity)
{
ComponentWrapper transform = world->GetComponent(entity, "Transform");
glm::vec3 scale = (glm::vec3)transform["Scale"];
glm::mat4 modelMatrix = glm::translate(glm::mat4(), position) * glm::toMat4(glm::quat(oritentation)) * glm::scale(scale);
EntityID parent = world->GetParent(entity);
if (parent != 0) {
return ModelMatrix(world, parent) * modelMatrix;
return AbsoluteScale(world, parent) * scale;
} else {
return modelMatrix;
return scale;
}
}
+6
View File
@@ -41,6 +41,10 @@ Game::Game(int argc, char* argv[])
if (!mapToLoad.empty()) {
ResourceManager::Load<EntityXMLFile>(mapToLoad)->PopulateWorld(m_World);
}
// Create system pipeline
m_SystemPipeline = new SystemPipeline(m_EventBroker);
m_SystemPipeline->AddSystem<RaptorCopterSystem>();
m_LastTime = glfwGetTime();
@@ -64,6 +68,8 @@ void Game::Tick()
m_Renderer->Update(dt);
m_EventBroker->Swap();
// Iterate through systems and update world!
m_SystemPipeline->Update(m_World, dt);
testTick(dt);
m_RenderQueueFactory->Update(m_World);