From a232559bb19775fb7820249b75d2a435f7f57d1e Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Tue, 6 May 2014 17:07:38 +0200 Subject: [PATCH] FreeSteeringSystem converted to take input commands --- src/GameWorld.cpp | 10 +- src/InputController.h | 11 +- src/Systems/FreeSteeringSystem.cpp | 162 ++++++++++++++++++++--------- src/Systems/FreeSteeringSystem.h | 28 ++++- src/Systems/InputSystem.h | 8 +- 5 files changed, 155 insertions(+), 64 deletions(-) diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index b8dcf9d..48a65a5 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -12,6 +12,10 @@ void GameWorld::Initialize() BindKey(GLFW_KEY_S, "+backward"); BindKey(GLFW_KEY_A, "+left"); BindKey(GLFW_KEY_D, "+right"); + BindKey(GLFW_KEY_SPACE, "+up"); + BindKey(GLFW_KEY_LEFT_CONTROL, "+down"); + BindKey(GLFW_KEY_LEFT_ALT, "+slow"); + BindKey(GLFW_KEY_LEFT_SHIFT, "+fast"); BindMouseButton(GLFW_MOUSE_BUTTON_1, "+attack"); BindMouseButton(GLFW_MOUSE_BUTTON_2, "+attack2"); BindMouseButton(GLFW_MOUSE_BUTTON_3, "+attack3"); @@ -50,7 +54,7 @@ void GameWorld::Initialize() CommitEntity(ground); } - /*{ + { auto TankTest = CreateEntity(); auto transform = AddComponent(TankTest, "Transform"); transform->Position = glm::vec3(1.5f, 0.7f, 5.f); @@ -60,7 +64,7 @@ void GameWorld::Initialize() CommitEntity(TankTest); } - for(int i = 0; i < 83; i++) + /*for(int i = 0; i < 83; i++) { auto light = CreateEntity(); auto transform = AddComponent(light, "Transform"); @@ -122,7 +126,7 @@ void GameWorld::RegisterSystems() { m_SystemFactory.Register("TransformSystem", [this]() { return new Systems::TransformSystem(this, m_EventBroker); }); //m_SystemFactory.Register("LevelGenerationSystem", [this]() { return new Systems::LevelGenerationSystem(this); }); - m_SystemFactory.Register("InputSystem", [this]() { return new Systems::InputSystem(this, m_EventBroker, m_Renderer); }); + m_SystemFactory.Register("InputSystem", [this]() { return new Systems::InputSystem(this, m_EventBroker); }); m_SystemFactory.Register("DebugSystem", [this]() { return new Systems::DebugSystem(this, m_EventBroker); }); //m_SystemFactory.Register("CollisionSystem", [this]() { return new Systems::CollisionSystem(this); }); ////m_SystemFactory.Register("ParticleSystem", [this]() { return new Systems::ParticleSystem(this); }); diff --git a/src/InputController.h b/src/InputController.h index 535e4f4..7afd5d5 100644 --- a/src/InputController.h +++ b/src/InputController.h @@ -5,26 +5,29 @@ #include "EventBroker.h" #include "Events/InputCommand.h" +#include "Events/MouseMove.h" class InputController { public: InputController(std::shared_ptr<::EventBroker> eventBroker) - : EventBroker(EventBroker) { Initialize(); } + : EventBroker(eventBroker) { Initialize(); } virtual void Initialize() { - EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &InputController::OnCommand) + EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &InputController::OnCommand); + EVENT_SUBSCRIBE_MEMBER(m_EMouseMove, &InputController::OnMouseMove); } - virtual bool OnCommand(const Events::InputCommand &event) { } + virtual bool OnCommand(const Events::InputCommand &event) { return false; } + virtual bool OnMouseMove(const Events::MouseMove &event) { return false; } protected: std::shared_ptr<::EventBroker> EventBroker; private: EventRelay m_EInputCommand; - + EventRelay m_EMouseMove; }; #endif // InputController_h__ diff --git a/src/Systems/FreeSteeringSystem.cpp b/src/Systems/FreeSteeringSystem.cpp index 407200b..f1ed04e 100755 --- a/src/Systems/FreeSteeringSystem.cpp +++ b/src/Systems/FreeSteeringSystem.cpp @@ -7,65 +7,127 @@ void Systems::FreeSteeringSystem::RegisterComponents(ComponentFactory* cf) cf->Register("FreeSteering", []() { return new Components::FreeSteering(); }); } +void Systems::FreeSteeringSystem::Initialize() +{ + m_InputController = std::unique_ptr(new FreeSteeringInputController(EventBroker)); +} + void Systems::FreeSteeringSystem::Update(double dt) { - + } void Systems::FreeSteeringSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) { auto steering = m_World->GetComponent(entity, "FreeSteering"); - auto input = m_World->GetComponent(entity, "Input"); - if (steering && input) + if (steering) { auto transform = m_World->GetComponent(entity, "Transform"); - glm::vec3 Camera_Right = glm::vec3(glm::vec4(1, 0, 0, 0) * transform->Orientation); - glm::vec3 Camera_Forward = glm::vec3(glm::vec4(0, 0, 1, 0) * transform->Orientation); - - float speed = steering->Speed; - if (input->KeyState[GLFW_KEY_LEFT_SHIFT]) - { - speed *= 4.0f; - } - if (input->KeyState[GLFW_KEY_LEFT_ALT]) - { - speed /= 4.0f; - } - if (input->KeyState[GLFW_KEY_A]) - { - transform->Position -= Camera_Right * (float)dt * speed; - } - else if (input->KeyState[GLFW_KEY_D]) - { - transform->Position += Camera_Right * (float)dt * speed; - } - if (input->KeyState[GLFW_KEY_W]) - { - transform->Position -= Camera_Forward * (float)dt * speed; - } - if (input->KeyState[GLFW_KEY_S]) - { - transform->Position += Camera_Forward * (float)dt * speed; - } - if (input->KeyState[GLFW_KEY_SPACE]) - { - transform->Position += glm::vec3(0, 1, 0) * (float)dt * speed; - } - if (input->KeyState[GLFW_KEY_LEFT_CONTROL]) - { - transform->Position -= glm::vec3(0, 1, 0) * (float)dt * speed; - } - - if (input->MouseState[GLFW_MOUSE_BUTTON_LEFT]) - { - // TOUCHING THIS CODE MIGHT CAUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS // spelling tobias :3 - //--------------------------------------------------------------------- - transform->Orientation = glm::angleAxis(input->dY / 300.f, glm::vec3(1, 0, 0)) * transform->Orientation; - - transform->Orientation = transform->Orientation * glm::angleAxis(input->dX / 300.f, glm::vec3(0, 1, 0)); - //--------------------------------------------------------------------- - // TOUCHING THIS CODE MIGHT CAUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS - } + glm::vec3 cameraRight = glm::vec3(glm::vec4(1, 0, 0, 0) * m_InputController->Orientation); + glm::vec3 cameraForward = glm::vec3(glm::vec4(0, 0, -1, 0) * m_InputController->Orientation); + glm::vec3 movement; + movement += cameraRight * m_InputController->Movement.x; + movement.y += m_InputController->Movement.y; + movement += cameraForward * -m_InputController->Movement.z; + transform->Position += movement * steering->Speed * m_InputController->SpeedMultiplier * (float)dt; + transform->Orientation = m_InputController->Orientation; } } + +bool Systems::FreeSteeringSystem::FreeSteeringInputController::OnCommand(const Events::InputCommand &event) +{ + // Movement + if (event.Command == "+forward") + { + Movement.z += -1.f; + } + else if (event.Command == "-forward") + { + Movement.z -= -1.f; + } + else if (event.Command == "+backward") + { + Movement.z += 1.f; + } + else if (event.Command == "-backward") + { + Movement.z -= 1.f; + } + else if (event.Command == "+right") + { + Movement.x += 1.f; + } + else if (event.Command == "-right") + { + Movement.x -= 1.f; + } + else if (event.Command == "+left") + { + Movement.x += -1.f; + } + else if (event.Command == "-left") + { + Movement.x -= -1.f; + } + else if (event.Command == "+up") + { + Movement.y += 1.f; + } + else if (event.Command == "-up") + { + Movement.y -= 1.f; + } + else if (event.Command == "+down") + { + Movement.y += -1.f; + } + else if (event.Command == "-down") + { + Movement.y -= -1.f; + } + + // Speed + else if (event.Command == "+fast") + { + SpeedMultiplier *= 4.f; + } + else if (event.Command == "-fast") + { + SpeedMultiplier /= 4.f; + } + else if (event.Command == "+slow") + { + SpeedMultiplier /= 4.f; + } + else if (event.Command == "-slow") + { + SpeedMultiplier *= 4.f; + } + + // Mouse click + else if (event.Command == "+attack") + { + OrientationActive = true; + } + else if (event.Command == "-attack") + { + OrientationActive = false; + } + + return true; +} + +bool Systems::FreeSteeringSystem::FreeSteeringInputController::OnMouseMove(const Events::MouseMove &event) +{ + if (OrientationActive) + { + // TOUCHING THIS CODE MIGHT CAUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS + //--------------------------------------------------------------------- + Orientation = glm::angleAxis(event.DeltaY / 300.f, glm::vec3(1, 0, 0)) * Orientation * glm::angleAxis(event.DeltaX / 300.f, glm::vec3(0, 1, 0)); + //--------------------------------------------------------------------- + // TOUCHING THIS CODE MIGHT CAUSE THE UNIVERSE TO IMPLODE, ALSO DRAGONS + } + + return true; +} diff --git a/src/Systems/FreeSteeringSystem.h b/src/Systems/FreeSteeringSystem.h index 49a65d9..4535cbb 100755 --- a/src/Systems/FreeSteeringSystem.h +++ b/src/Systems/FreeSteeringSystem.h @@ -2,11 +2,12 @@ #include "System.h" #include "Components/Transform.h" -#include "Components/Input.h" #include "Components/FreeSteering.h" +#include "InputController.h" namespace Systems { + class FreeSteeringSystem : public System { public: @@ -14,8 +15,33 @@ public: : System(world, eventBroker) { } void RegisterComponents(ComponentFactory* cf) override; + void Initialize() override; void Update(double dt) override; void UpdateEntity(double dt, EntityID entity, EntityID parent) override; + +private: + class FreeSteeringInputController; + + std::unique_ptr m_InputController; }; + +class FreeSteeringSystem::FreeSteeringInputController : InputController +{ +public: + FreeSteeringInputController(std::shared_ptr<::EventBroker> eventBroker) + : InputController(eventBroker) + , SpeedMultiplier(1.f) + , OrientationActive(false) { } + + glm::vec3 Movement; + glm::quat Orientation; + float SpeedMultiplier; + bool OrientationActive; + +protected: + virtual bool OnCommand(const Events::InputCommand &event); + virtual bool OnMouseMove(const Events::MouseMove &event); +}; + } \ No newline at end of file diff --git a/src/Systems/InputSystem.h b/src/Systems/InputSystem.h index bc1142a..c56cccf 100755 --- a/src/Systems/InputSystem.h +++ b/src/Systems/InputSystem.h @@ -5,7 +5,6 @@ #include #include "System.h" -#include "Renderer.h" #include "Components/Input.h" #include "Events/KeyUp.h" #include "Events/KeyDown.h" @@ -21,9 +20,8 @@ namespace Systems class InputSystem : public System { public: - InputSystem(World* world, std::shared_ptr<::EventBroker> eventBroker, std::shared_ptr renderer) - : System(world, eventBroker) - , m_Renderer(renderer) { } + InputSystem(World* world, std::shared_ptr<::EventBroker> eventBroker) + : System(world, eventBroker) { } void RegisterComponents(ComponentFactory* cf) override; void Initialize() override; @@ -31,8 +29,6 @@ public: void Update(double dt) override; private: - std::shared_ptr m_Renderer; - // Input binding tables std::unordered_map m_KeyBindings; // GLFW_KEY... -> command string std::unordered_map m_MouseButtonBindings; // GLFW_MOUSE_BUTTON... -> command string