diff --git a/src/EventBroker.h b/src/EventBroker.h index c11fe4a..f8877e8 100644 --- a/src/EventBroker.h +++ b/src/EventBroker.h @@ -6,6 +6,10 @@ #include #include +#define EVENT_SUBSCRIBE_MEMBER(relay, handler) \ + relay = decltype(relay)(std::bind(handler, this, std::placeholders::_1)); \ + EventBroker->Subscribe(relay); + struct Event { protected: diff --git a/src/Events/InputCommand.h b/src/Events/InputCommand.h index 34a22c9..bd18f6a 100644 --- a/src/Events/InputCommand.h +++ b/src/Events/InputCommand.h @@ -1,6 +1,8 @@ #ifndef Events_InputCommand_h__ #define Events_InputCommand_h__ +#include + #include "EventBroker.h" namespace Events @@ -10,6 +12,7 @@ struct InputCommand : Event { unsigned int PlayerID; std::string Command; + boost::any Value; }; } diff --git a/src/Events/InputCommandValue.h b/src/Events/InputCommandValue.h deleted file mode 100644 index 87e3702..0000000 --- a/src/Events/InputCommandValue.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef Events_InputCommandValue_h__ -#define Events_InputCommandValue_h__ - -#include "EventBroker.h" -#include "Events/InputCommand.h" - -namespace Events -{ - -template -struct InputCommandValue : public InputCommand -{ - T Value; -}; - -} - -#endif // Events_InputCommandValue_h__ \ No newline at end of file diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index c92f868..b8dcf9d 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -8,6 +8,14 @@ void GameWorld::Initialize() m_ResourceManager.Preload("Model", "Models/Placeholders/PhysicsTest/Plane.obj"); m_ResourceManager.Preload("Model", "Models/Placeholders/PhysicsTest/ArrowCube.obj"); + BindKey(GLFW_KEY_W, "+forward"); + BindKey(GLFW_KEY_S, "+backward"); + BindKey(GLFW_KEY_A, "+left"); + BindKey(GLFW_KEY_D, "+right"); + BindMouseButton(GLFW_MOUSE_BUTTON_1, "+attack"); + BindMouseButton(GLFW_MOUSE_BUTTON_2, "+attack2"); + BindMouseButton(GLFW_MOUSE_BUTTON_3, "+attack3"); + RegisterComponents(); { @@ -42,7 +50,7 @@ void GameWorld::Initialize() CommitEntity(ground); } - { + /*{ auto TankTest = CreateEntity(); auto transform = AddComponent(TankTest, "Transform"); transform->Position = glm::vec3(1.5f, 0.7f, 5.f); @@ -85,7 +93,7 @@ void GameWorld::Initialize() auto physics = AddComponent(ball, "Physics"); physics->Mass = 1; CommitEntity(ball); - } + }*/ { auto entity = CreateEntity(); @@ -138,4 +146,20 @@ void GameWorld::AddSystems() AddSystem("SoundSystem"); AddSystem("PhysicsSystem"); AddSystem("RenderSystem"); -} \ No newline at end of file +} + +void GameWorld::BindKey(int keyCode, std::string command) +{ + Events::BindKey e; + e.KeyCode = keyCode; + e.Command = command; + m_EventBroker->Publish(e); +} + +void GameWorld::BindMouseButton(int button, std::string command) +{ + Events::BindMouseButton e; + e.Button = button; + e.Command = command; + m_EventBroker->Publish(e); +} diff --git a/src/GameWorld.h b/src/GameWorld.h index 17901c3..355c9b1 100755 --- a/src/GameWorld.h +++ b/src/GameWorld.h @@ -47,6 +47,9 @@ public: private: std::shared_ptr m_Renderer; + + void BindKey(int keyCode, std::string command); + void BindMouseButton(int button, std::string command); }; #endif // GameWorld_h__ diff --git a/src/InputController.h b/src/InputController.h new file mode 100644 index 0000000..535e4f4 --- /dev/null +++ b/src/InputController.h @@ -0,0 +1,30 @@ +#ifndef InputController_h__ +#define InputController_h__ + +#include + +#include "EventBroker.h" +#include "Events/InputCommand.h" + +class InputController +{ +public: + InputController(std::shared_ptr<::EventBroker> eventBroker) + : EventBroker(EventBroker) { Initialize(); } + + virtual void Initialize() + { + EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &InputController::OnCommand) + } + + virtual bool OnCommand(const Events::InputCommand &event) { } + +protected: + std::shared_ptr<::EventBroker> EventBroker; + +private: + EventRelay m_EInputCommand; + +}; + +#endif // InputController_h__ diff --git a/src/Systems/InputSystem.cpp b/src/Systems/InputSystem.cpp index f8b1d59..3c9fdfd 100755 --- a/src/Systems/InputSystem.cpp +++ b/src/Systems/InputSystem.cpp @@ -10,32 +10,12 @@ void Systems::InputSystem::RegisterComponents(ComponentFactory* cf) void Systems::InputSystem::Initialize() { // Subscribe to events - m_EKeyDown = decltype(m_EKeyDown)(std::bind(&Systems::InputSystem::OnKeyDown, this, std::placeholders::_1)); - EventBroker->Subscribe(m_EKeyDown); - m_EKeyUp = decltype(m_EKeyUp)(std::bind(&Systems::InputSystem::OnKeyUp, this, std::placeholders::_1)); - EventBroker->Subscribe(m_EKeyUp); - m_EMousePress = decltype(m_EMousePress)(std::bind(&Systems::InputSystem::OnMousePress, this, std::placeholders::_1)); - EventBroker->Subscribe(m_EMousePress); - m_EMouseRelease = decltype(m_EMouseRelease)(std::bind(&Systems::InputSystem::OnMouseRelease, this, std::placeholders::_1)); - EventBroker->Subscribe(m_EMouseRelease); - m_EBindKey = decltype(m_EBindKey)(std::bind(&Systems::InputSystem::OnBindKey, this, std::placeholders::_1)); - EventBroker->Subscribe(m_EBindKey); - m_EBindMouseButton = decltype(m_EBindMouseButton)(std::bind(&Systems::InputSystem::OnBindMouseButton, this, std::placeholders::_1)); - EventBroker->Subscribe(m_EBindMouseButton); - - { - Events::BindKey e; - e.KeyCode = GLFW_KEY_W; - e.Command = "+forward"; - EventBroker->Publish(e); - } - - { - Events::BindMouseButton e; - e.Button = GLFW_MOUSE_BUTTON_1; - e.Command = "+attack"; - EventBroker->Publish(e); - } + EVENT_SUBSCRIBE_MEMBER(m_EKeyDown, &Systems::InputSystem::OnKeyDown) + EVENT_SUBSCRIBE_MEMBER(m_EKeyUp, &Systems::InputSystem::OnKeyUp) + EVENT_SUBSCRIBE_MEMBER(m_EMousePress, &Systems::InputSystem::OnMousePress) + EVENT_SUBSCRIBE_MEMBER(m_EMouseRelease, &Systems::InputSystem::OnMouseRelease) + EVENT_SUBSCRIBE_MEMBER(m_EBindKey, &Systems::InputSystem::OnBindKey) + EVENT_SUBSCRIBE_MEMBER(m_EBindMouseButton, &Systems::InputSystem::OnBindMouseButton) } void Systems::InputSystem::Update(double dt) diff --git a/src/Systems/InputSystem.h b/src/Systems/InputSystem.h index d0766e5..bc1142a 100755 --- a/src/Systems/InputSystem.h +++ b/src/Systems/InputSystem.h @@ -14,7 +14,6 @@ #include "Events/BindKey.h" #include "Events/BindMouseButton.h" #include "Events/InputCommand.h" -#include "Events/InputCommandValue.h" namespace Systems { diff --git a/vs11/Returngeance/Returngeance.vcxproj b/vs11/Returngeance/Returngeance.vcxproj index 2b7c917..88d37ff 100755 --- a/vs11/Returngeance/Returngeance.vcxproj +++ b/vs11/Returngeance/Returngeance.vcxproj @@ -142,7 +142,6 @@ - @@ -153,6 +152,7 @@ + diff --git a/vs11/Returngeance/Returngeance.vcxproj.filters b/vs11/Returngeance/Returngeance.vcxproj.filters index 4da5b38..f76cb42 100755 --- a/vs11/Returngeance/Returngeance.vcxproj.filters +++ b/vs11/Returngeance/Returngeance.vcxproj.filters @@ -261,12 +261,12 @@ Input\Events - - Input\Events - Input\Events + + Input +