From 80f028edf2ebf9d69f30c4f06ea6441bce75b695 Mon Sep 17 00:00:00 2001 From: stiffly Date: Fri, 11 Dec 2015 13:45:19 +0100 Subject: [PATCH] Added a very basic PlayerSystem, and a PlayerComponent. --- include/Engine/Core/System.h | 7 ++-- include/Engine/Core/SystemPipeline.h | 4 +-- include/Game/Game.h | 1 + include/Game/PlayerSystem.h | 34 ++++++++++++++++++ include/Game/RaptorCopterSystem.h | 2 +- resources/Schema/Components/Player.xml | 4 +++ resources/Schema/Components/Player.xsd | 14 ++++++++ resources/Schema/Entities/Test.xml | 10 ++++++ src/Game/Game.cpp | 3 +- src/Game/PlayerSystem.cpp | 49 ++++++++++++++++++++++++++ 10 files changed, 121 insertions(+), 7 deletions(-) create mode 100644 include/Game/PlayerSystem.h create mode 100644 resources/Schema/Components/Player.xml create mode 100644 resources/Schema/Components/Player.xsd create mode 100644 src/Game/PlayerSystem.cpp diff --git a/include/Engine/Core/System.h b/include/Engine/Core/System.h index 8e8c71b9..a79e025f 100644 --- a/include/Engine/Core/System.h +++ b/include/Engine/Core/System.h @@ -10,16 +10,17 @@ class System friend class SystemPipeline; public: - System(const EventBroker* eventBroker, std::string componentType) + System(EventBroker* eventBroker, std::string componentType) : m_EventBroker(eventBroker) , m_ComponentType(componentType) { } - + + virtual void Initialize(); virtual void Update(World* world, ComponentWrapper& component, double dt) = 0; private: - const EventBroker* m_EventBroker; std::string m_ComponentType; + EventBroker* m_EventBroker; }; #endif \ No newline at end of file diff --git a/include/Engine/Core/SystemPipeline.h b/include/Engine/Core/SystemPipeline.h index 24f98121..e4b8bb1f 100644 --- a/include/Engine/Core/SystemPipeline.h +++ b/include/Engine/Core/SystemPipeline.h @@ -9,7 +9,7 @@ class SystemPipeline { public: - SystemPipeline(const EventBroker* eventBroker) + SystemPipeline(EventBroker* eventBroker) : m_EventBroker(eventBroker) { } ~SystemPipeline() @@ -51,7 +51,7 @@ public: } private: - const EventBroker* m_EventBroker; + EventBroker* m_EventBroker; std::unordered_map> m_Systems; }; diff --git a/include/Game/Game.h b/include/Game/Game.h index 500ecf3a..bc66d62f 100644 --- a/include/Game/Game.h +++ b/include/Game/Game.h @@ -13,6 +13,7 @@ #include "Core/EntityXMLFile.h" #include "Core/SystemPipeline.h" #include "RaptorCopterSystem.h" +#include "PlayerSystem.h" class Game { diff --git a/include/Game/PlayerSystem.h b/include/Game/PlayerSystem.h new file mode 100644 index 00000000..c6a9f04b --- /dev/null +++ b/include/Game/PlayerSystem.h @@ -0,0 +1,34 @@ +#ifndef PlayerSystem_h__ +#define PlayerSystem_h__ + +#include +#include + +#include "Common.h" +#include "Core/System.h" +#include "Core/EventBroker.h" +#include "Core/EKeyDown.h" +#include "Core/EKeyUp.h" + +class PlayerSystem : public System +{ +public: + PlayerSystem(EventBroker* eventBroker) + : System(eventBroker, "Player") + { } + + void Initialize(); + virtual void Update(World* world, ComponentWrapper& player, double dt) override; + +private: + float m_Speed; + glm::vec3 m_Direction; + EventBroker* m_EventBroker; + + EventRelay m_EKeyDown; + bool OnKeyDown(const Events::KeyDown &event); + EventRelay m_EKeyUp; + bool OnKeyUp(const Events::KeyUp &event); +}; + +#endif \ No newline at end of file diff --git a/include/Game/RaptorCopterSystem.h b/include/Game/RaptorCopterSystem.h index 55647004..a25ca3df 100644 --- a/include/Game/RaptorCopterSystem.h +++ b/include/Game/RaptorCopterSystem.h @@ -4,7 +4,7 @@ class RaptorCopterSystem : public System { public: - RaptorCopterSystem(const EventBroker* eventBroker) + RaptorCopterSystem(EventBroker* eventBroker) : System(eventBroker, "RaptorCopter") { } diff --git a/resources/Schema/Components/Player.xml b/resources/Schema/Components/Player.xml new file mode 100644 index 00000000..d2e5f3a7 --- /dev/null +++ b/resources/Schema/Components/Player.xml @@ -0,0 +1,4 @@ + + 0 + + \ No newline at end of file diff --git a/resources/Schema/Components/Player.xsd b/resources/Schema/Components/Player.xsd new file mode 100644 index 00000000..61f19646 --- /dev/null +++ b/resources/Schema/Components/Player.xsd @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/resources/Schema/Entities/Test.xml b/resources/Schema/Entities/Test.xml index 2c3ea18e..885ff3d2 100644 --- a/resources/Schema/Entities/Test.xml +++ b/resources/Schema/Entities/Test.xml @@ -31,6 +31,16 @@ + + + + + + + Models/Core/UnitCube.obj + + + diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index bff6aebc..0fc913de 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -44,7 +44,8 @@ Game::Game(int argc, char* argv[]) // Create system pipeline m_SystemPipeline = new SystemPipeline(m_EventBroker); - m_SystemPipeline->AddSystem(); + m_SystemPipeline->AddSystem(); + m_SystemPipeline->AddSystem(); m_LastTime = glfwGetTime(); diff --git a/src/Game/PlayerSystem.cpp b/src/Game/PlayerSystem.cpp new file mode 100644 index 00000000..b8dadaee --- /dev/null +++ b/src/Game/PlayerSystem.cpp @@ -0,0 +1,49 @@ +#include "PlayerSystem.h" + +void PlayerSystem::Initialize() +{ + EVENT_SUBSCRIBE_MEMBER(m_EKeyDown, &PlayerSystem::OnKeyDown); + EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, &PlayerSystem::OnKeyUp); +} + +void PlayerSystem::Update(World * world, ComponentWrapper & player, double dt) +{ + m_EventBroker->Process(); + ComponentWrapper& transform = world->GetComponent(player.EntityID, "Transform"); + (glm::vec3&)player["Velocity"] = m_Speed * float(dt) * m_Direction; + (glm::vec3&)transform["Position"] += (glm::vec3)player["Velocity"]; +} + +bool PlayerSystem::OnKeyDown(const Events::KeyDown & event) +{ + if (event.KeyCode == GLFW_KEY_P) { + m_Direction.z == -1; + } + if (event.KeyCode == GLFW_KEY_LEFT) { + m_Direction.x == -1; + } + if (event.KeyCode == GLFW_KEY_DOWN) { + m_Direction.z == 1; + } + if (event.KeyCode == GLFW_KEY_RIGHT) { + m_Direction.x == 1; + } + return true; +} + +bool PlayerSystem::OnKeyUp(const Events::KeyUp & event) +{ + if (event.KeyCode == GLFW_KEY_UP) { + m_Direction.z == 0; + } + if (event.KeyCode == GLFW_KEY_LEFT) { + m_Direction.x == 0; + } + if (event.KeyCode == GLFW_KEY_DOWN) { + m_Direction.z == 0; + } + if (event.KeyCode == GLFW_KEY_RIGHT) { + m_Direction.x == 0; + } + return false; +}