aboutsummaryrefslogtreecommitdiff
path: root/src/game/game.cpp
blob: 02a0f65703714508ff7b872f34c08e0b94b8fc21 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "game.hpp"

#include "commands/insert_cell.hpp"
#include "commands/move_cursor.hpp"
#include "commands/quit.hpp"
#include "commands/toggle_pause.hpp"

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

Game::Game(
	std::shared_ptr<IWindow> window,
	std::shared_ptr<IScene> scene,
	std::shared_ptr<ICursorController> cursor_controller,
	std::shared_ptr<IStatusLine> statusline,
	std::shared_ptr<IGenerationTracker> generation_tracker,
	std::shared_ptr<IStatusManager> status_manager,
	IStatusLineSubscriberAdapterFactory<Vector2>
		vector2_statusline_subscriber_adapter_factory
) noexcept
	: _window(std::move(window)),
	  _scene(std::move(scene)),
	  _cursor_controller(std::move(cursor_controller)),
	  _statusline(std::move(statusline)),
	  _generation_tracker(std::move(generation_tracker)),
	  _status_manager(std::move(status_manager)),
	  _vector2_statusline_subscriber_adapter_factory(
		  vector2_statusline_subscriber_adapter_factory
	  )
{
}

void Game::on_start() noexcept
{
	_statusline->initialize_background();

	_status_manager->set_section_title(StatusLineSection::A, "");
	_status_manager->set_section_title(StatusLineSection::B, "X: ");
	_status_manager->set_section_title(StatusLineSection::C, "Y: ");
	_status_manager->set_section_title(StatusLineSection::D, "Paused: ");
	_status_manager->set_section_title(StatusLineSection::E, "Generation: ");

	std::shared_ptr<ISubscriber<Vector2>> vector2_statusline_subscriber_adapter =
		_vector2_statusline_subscriber_adapter_factory(
			_status_manager,
			{ StatusLineSection::B, StatusLineSection::C }
		);

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

	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);

	_status_manager->set_section_body(
		StatusLineSection::B,
		fmt::format("{}", center_position.get_x())
	);

	_status_manager->set_section_body(
		StatusLineSection::C,
		fmt::format("{}", center_position.get_y())
	);

	_status_manager->set_section_body(
		StatusLineSection::D,
		_generation_tracker->get_is_paused() ? "yes" : "no"
	);

	_status_manager->set_section_body(StatusLineSection::E, "0");
}

void Game::on_update() noexcept {}

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();
}

auto Game::get_input_config() const noexcept
	-> std::unordered_map<char, std::shared_ptr<ICommand>>
{
	return {
		{ 'q', std::make_shared<QuitCommand>() },
		{ 'i', std::make_shared<InsertCellCommand>(_cursor_controller, _scene) },
		{ 'p',
		  std::make_shared<TogglePauseCommand>(_generation_tracker, _status_manager) },
		{ '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
		  ) }
	};
}