aboutsummaryrefslogtreecommitdiff
path: root/src/game/game.cpp
blob: ee3a2b812375b3403b1aacb77a8ecf55b908973e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "game.hpp"

#include <utility>

Game::Game(std::shared_ptr<IScene> scene, std::shared_ptr<IInputHandler> input_handler,
		   std::shared_ptr<CursorController> cursor_controller)
	: _scene(std::move(scene)),
	  _input_handler(std::move(input_handler)),
	  _cursor_controller(std::move(cursor_controller))
{
}

void Game::run()
{
	_scene->enter();

	_input_handler->enter_raw_mode();

	auto scene = _scene;
	auto input_handler = _input_handler;

	_input_handler->attach('q',
						   [&input_handler, &scene]()
						   {
							   input_handler->leave_raw_mode();
							   scene->leave();
							   exit(EXIT_SUCCESS);
						   });

	auto cursor_controller = _cursor_controller;

	_input_handler->attach('k',
						   [&cursor_controller]()
						   {
							   cursor_controller->move<Direction::UP>(1U);
						   });

	_input_handler->attach('j',
						   [&cursor_controller]()
						   {
							   cursor_controller->move<Direction::DOWN>(1U);
						   });

	_input_handler->attach('h',
						   [&cursor_controller]()
						   {
							   cursor_controller->move<Direction::LEFT>(1U);
						   });

	_input_handler->attach('l',
						   [&cursor_controller]()
						   {
							   cursor_controller->move<Direction::RIGHT>(1U);
						   });

	_input_handler->listen();

	_scene->leave();
}