]> git.tdb.fi Git - libs/game.git/blobdiff - source/gameview/playerinput.cpp
Add infrastructure for receiving player input
[libs/game.git] / source / gameview / playerinput.cpp
diff --git a/source/gameview/playerinput.cpp b/source/gameview/playerinput.cpp
new file mode 100644 (file)
index 0000000..5029ce5
--- /dev/null
@@ -0,0 +1,42 @@
+#include "playerinput.h"
+#include <msp/input/bindings.h>
+
+using namespace std;
+
+namespace Msp::GameView {
+
+PlayerInput::PlayerInput(Game::Director &director, Graphics::Window &wnd):
+       event_source(director.get_event_bus()),
+       keyboard(wnd),
+       mouse(wnd)
+{
+       kbm_hub.attach(keyboard);
+       kbm_hub.attach(mouse);
+
+       bindings = director.get_resources().get_all<Input::Bindings>();
+}
+
+void PlayerInput::init_players()
+{
+       add_player(kbm_hub);
+}
+
+void PlayerInput::add_player(Input::Device &dev)
+{
+       Player &player = players.emplace_back(&dev, scheme_factory());
+       for(Input::Bindings *b: bindings)
+               if(b->is_compatible(dev))
+               {
+                       b->apply_to(*player.controls, dev);
+                       break;
+               }
+       event_source.emit<Events::PlayerArrived>(ref(*player.controls));
+}
+
+void PlayerInput::synthesize_initial_events(Game::EventObserver &observer)
+{
+       for(const Player &p: players)
+               event_source.emit_to<Events::PlayerArrived>(observer, ref(*p.controls));
+}
+
+} // namespace Msp::GameView