aboutsummaryrefslogtreecommitdiff
path: root/src/game/game.cpp
blob: f788d1558fee21ec2fdd75481ab8c9ba22e97799 (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
60
61
62
#include "game.hpp"

#include "commands/insert_cell.hpp"
#include "commands/move_cursor.hpp"
#include "commands/quit.hpp"
#include "game/cursor_listener.hpp"

#include <fmt/core.h>
#include <iostream>

Game::Game(const std::shared_ptr<IWindow> &window, const std::shared_ptr<IScene> &scene,
		   const std::shared_ptr<ICursorController> &cursor_controller) noexcept
	: _window(window), _scene(scene), _cursor_controller(cursor_controller)
{
}

void Game::on_start() noexcept
{
	auto cursor_listener = std::make_shared<CursorListener>(_scene);

	_cursor_controller->subscribe(CursorEvent::POSITION_CHANGE, cursor_listener);

	const auto window_size = _window->size();

	const auto center_position =
		Vector2({.x = static_cast<Vector2::Value>(window_size.get_width()) / 2,
				 .y = static_cast<Vector2::Value>(window_size.get_height()) / 2});

	_cursor_controller->move_to(center_position);

	cursor_listener->update(center_position);
}

void Game::on_exit() const noexcept
{
	for (auto row : *_scene->get_matrix())
	{
		for (auto &col : row)
		{
			fmt::print("{}", col);
		}

		fmt::print("\n");
	}

	std::cout.flush();
}

std::unordered_map<char, std::shared_ptr<ICommand>>
Game::get_input_config() const noexcept
{
	return {{'q', std::make_shared<QuitCommand>()},
			{'i', std::make_shared<InsertCellCommand>(_cursor_controller, _scene)},
			{'k', std::make_shared<MoveCursorCommand>(Vector2::up(), _cursor_controller,
													  _window)},
			{'j', std::make_shared<MoveCursorCommand>(Vector2::down(), _cursor_controller,
													  _window)},
			{'h', std::make_shared<MoveCursorCommand>(Vector2::left(), _cursor_controller,
													  _window)},
			{'l', std::make_shared<MoveCursorCommand>(Vector2::right(),
													  _cursor_controller, _window)}};
}