diff --git a/Escape-the-Dawn/Components/Input.h b/Escape-the-Dawn/Components/Input.h index ca7dfe6..d278aa7 100644 --- a/Escape-the-Dawn/Components/Input.h +++ b/Escape-the-Dawn/Components/Input.h @@ -1,16 +1,21 @@ #ifndef Components_Input_h__ #define Components_Input_h__ -#include "Component.h" +#include + #include +#include "Component.h" + namespace Components { struct Input : Component { - int KeyState[GLFW_KEY_LAST]; - int MouseState[GLFW_MOUSE_BUTTON_LAST]; + std::array KeyState; + std::array LastKeyState; + std::array MouseState; + std::array LastMouseState; float dX, dY; }; diff --git a/Escape-the-Dawn/System.h b/Escape-the-Dawn/System.h index 4ea04a1..f747476 100644 --- a/Escape-the-Dawn/System.h +++ b/Escape-the-Dawn/System.h @@ -11,7 +11,10 @@ public: System(World* world) : m_World(world) { } virtual ~System() { } - virtual void Update(double dt, EntityID entity, EntityID parent) = 0; + // Called once per system every tick + virtual void Update(double dt) { } + // Called once for every entity in the world every tick + virtual void Update(double dt, EntityID entity, EntityID parent) { } protected: World* m_World; diff --git a/Escape-the-Dawn/Systems/InputSystem.cpp b/Escape-the-Dawn/Systems/InputSystem.cpp index c0d6822..278a2d0 100644 --- a/Escape-the-Dawn/Systems/InputSystem.cpp +++ b/Escape-the-Dawn/Systems/InputSystem.cpp @@ -1,18 +1,40 @@ #include "InputSystem.h" #include "World.h" +void Systems::InputSystem::Update(double dt) +{ + m_LastKeyState = m_CurrentKeyState; + m_LastMouseState = m_CurrentMouseState; + + // Keyboard input + for (int i = 0; i <= GLFW_KEY_LAST; ++i) { + m_CurrentKeyState[i] = glfwGetKey(m_Renderer->GetWindow(), i); + } + + // Mouse buttons + for (int i = 0; i <= GLFW_MOUSE_BUTTON_LAST; ++i) { + m_CurrentKeyState[i] = glfwGetMouseButton(m_Renderer->GetWindow(), i); + } + + // Cursor position + double xpos, ypos; + glfwGetCursorPos(m_Renderer->GetWindow(), &xpos, &ypos); + m_CurrentMouseDeltaX = xpos - m_LastMouseX; + m_CurrentMouseDeltaY = ypos - m_LastMouseY; + m_LastMouseX = xpos; + m_LastMouseY = ypos; +} + void Systems::InputSystem::Update(double dt, EntityID entity, EntityID parent) { auto input = m_World->GetComponent(entity, "Input"); if (input == nullptr) return; - for (int i = 0; i <= GLFW_KEY_LAST; ++i) { - input->KeyState[i] = glfwGetKey(m_Renderer->GetWindow(), i); - } - - for (int i = 0; i <= GLFW_MOUSE_BUTTON_LAST; ++i) { - input->MouseState[i] = glfwGetMouseButton(m_Renderer->GetWindow(), i); - } + input->KeyState = m_CurrentKeyState; + input->LastKeyState = m_LastKeyState; + input->MouseState = m_CurrentMouseState; + input->LastMouseState = m_LastMouseState; + input->dX = m_CurrentMouseDeltaX; + input->dY = m_CurrentMouseDeltaY; } - diff --git a/Escape-the-Dawn/Systems/InputSystem.h b/Escape-the-Dawn/Systems/InputSystem.h index ff51ec2..2c0e521 100644 --- a/Escape-the-Dawn/Systems/InputSystem.h +++ b/Escape-the-Dawn/Systems/InputSystem.h @@ -1,6 +1,8 @@ #ifndef InputSystem_h__ #define InputSystem_h__ +#include + #include "System.h" #include "Renderer.h" #include "Components/Input.h" @@ -14,10 +16,17 @@ public: InputSystem(World* world, Renderer* renderer) : System(world), m_Renderer(renderer) { } + void Update(double dt) override; void Update(double dt, EntityID entity, EntityID parent) override; private: Renderer* m_Renderer; + + std::array m_CurrentKeyState; + std::array m_LastKeyState; + std::array m_CurrentMouseState; + std::array m_LastMouseState; + float m_CurrentMouseDeltaX, m_CurrentMouseDeltaY; float m_LastMouseX, m_LastMouseY; }; diff --git a/Escape-the-Dawn/World.cpp b/Escape-the-Dawn/World.cpp index 43dd8d8..ae5a7d8 100644 --- a/Escape-the-Dawn/World.cpp +++ b/Escape-the-Dawn/World.cpp @@ -32,6 +32,7 @@ void World::RecursiveUpdate(std::shared_ptr system, double dt, EntityID void World::Update(double dt) { for (auto system : m_Systems) { + system->Update(dt); RecursiveUpdate(system, dt, 0); } } diff --git a/Escape-the-Dawn/World.h b/Escape-the-Dawn/World.h index 5ff403d..874cee4 100644 --- a/Escape-the-Dawn/World.h +++ b/Escape-the-Dawn/World.h @@ -64,8 +64,8 @@ public: void AddSystem(System* system); - // Recursively update through the scene graph void Update(double dt); + // Recursively update through the scene graph void RecursiveUpdate(std::shared_ptr system, double dt, EntityID parentEntity); private: