]> git.tdb.fi Git - libs/game.git/blob - source/gameview/playerinput.cpp
Add infrastructure for receiving player input
[libs/game.git] / source / gameview / playerinput.cpp
1 #include "playerinput.h"
2 #include <msp/input/bindings.h>
3
4 using namespace std;
5
6 namespace Msp::GameView {
7
8 PlayerInput::PlayerInput(Game::Director &director, Graphics::Window &wnd):
9         event_source(director.get_event_bus()),
10         keyboard(wnd),
11         mouse(wnd)
12 {
13         kbm_hub.attach(keyboard);
14         kbm_hub.attach(mouse);
15
16         bindings = director.get_resources().get_all<Input::Bindings>();
17 }
18
19 void PlayerInput::init_players()
20 {
21         add_player(kbm_hub);
22 }
23
24 void PlayerInput::add_player(Input::Device &dev)
25 {
26         Player &player = players.emplace_back(&dev, scheme_factory());
27         for(Input::Bindings *b: bindings)
28                 if(b->is_compatible(dev))
29                 {
30                         b->apply_to(*player.controls, dev);
31                         break;
32                 }
33         event_source.emit<Events::PlayerArrived>(ref(*player.controls));
34 }
35
36 void PlayerInput::synthesize_initial_events(Game::EventObserver &observer)
37 {
38         for(const Player &p: players)
39                 event_source.emit_to<Events::PlayerArrived>(observer, ref(*p.controls));
40 }
41
42 } // namespace Msp::GameView