Skip to content

Commit 6fc4e65

Browse files
committed
reorg into "Game" layout for actions and such
1 parent 10919ab commit 6fc4e65

File tree

2 files changed

+79
-48
lines changed

2 files changed

+79
-48
lines changed

src/game_components.hpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,51 +4,62 @@
44
#include <functional>
55
#include <chrono>
66
#include <map>
7+
#include <optional>
8+
#include <variant>
79

810
#include "color.hpp"
911
#include "vector2d.hpp"
1012

1113
namespace lefticus::my_awesome_game
1214
{
1315

16+
struct Game;
17+
1418
enum struct Direction { North, South, East, West };
1519

1620
struct Location
1721
{
18-
std::function<void()> action;
19-
std::function<void(Vector2D_Span<Color> &, std::chrono::milliseconds, Point)> draw;
20-
std::function<bool(std::chrono::milliseconds, Point, Direction)> can_enter;
22+
std::function<void(Game &, Point, Direction)> action;
23+
std::function<void(Vector2D_Span<Color> &, const Game &, Point)> draw;
24+
std::function<bool(const Game &, Point, Direction)> can_enter;
2125
};
2226

2327
struct Character
2428
{
2529
Point map_location{};
26-
std::function<void(Vector2D_Span<Color> &, std::chrono::milliseconds, Point)> draw;
30+
std::function<void(Vector2D_Span<Color> &, const Game &, Point)> draw;
2731
};
2832

2933

3034
struct Game_Map
3135
{
3236
explicit Game_Map(const Size size) : locations{ size } {}
3337
Vector2D<Location> locations;
34-
[[nodiscard]] bool can_enter_from(std::chrono::milliseconds game_clock, Point location, Direction from) const
38+
[[nodiscard]] bool can_enter_from(const Game &game, Point location, Direction from) const
3539
{
3640
const auto &map_location = locations.at(location);
3741
if (map_location.can_enter) {
38-
return map_location.can_enter(game_clock, location, from);
42+
return map_location.can_enter(game, location, from);
3943
} else {
4044
return true;
4145
}
4246
}
4347
};
4448

49+
using Variable = std::variant<double, std::int64_t, std::string>;
50+
4551
struct Game
4652
{
47-
53+
4854
std::map<std::string, Game_Map> maps;
49-
Character character;
55+
Character player;
5056
std::function<void (Game &)> start_game;
5157

58+
std::map<std::string, Variable> variables;
59+
std::vector<std::string> display_variables;
60+
std::string current_map;
61+
std::chrono::milliseconds clock;
62+
Size tile_size;
5263
};
5364

5465
}

src/main.cpp

Lines changed: 60 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ Game_Map make_map()// NOLINT congetive complexity
2727
Game_Map map{ Size{ 10, 10 } };// NOLINT magic numbers
2828

2929
auto solid_draw = []([[maybe_unused]] Vector2D_Span<Color> &pixels,
30-
[[maybe_unused]] std::chrono::milliseconds game_clock,
30+
[[maybe_unused]] const Game &game,
3131
[[maybe_unused]] Point map_location) {
3232
for (std::size_t cur_x = 0; cur_x < pixels.size().width; ++cur_x) {
3333
for (std::size_t cur_y = 0; cur_y < pixels.size().height; ++cur_y) {
3434
if (cur_x == 0 || cur_y == 0 || cur_x == pixels.size().width - 1 || cur_y == pixels.size().height - 1) {
3535
pixels.at(Point{ cur_x, cur_y }) = Color{ 128, 128, 128, 255 };// NOLINT Magic numbers
3636
} else {
37-
switch ((game_clock.count() / 1000) % 2) {// NOLINT Magic numbers
37+
switch ((game.clock.count() / 1000) % 2) {// NOLINT Magic numbers
3838
case 0:
3939
pixels.at(Point{ cur_x, cur_y }) = Color{ 255, 255, 255, 255 };// NOLINT Magic numbers
4040
break;
@@ -47,10 +47,10 @@ Game_Map make_map()// NOLINT congetive complexity
4747
}
4848
};
4949

50-
auto cannot_enter = [](std::chrono::milliseconds, Point, Direction) -> bool { return false; };
50+
auto cannot_enter = [](const Game &, Point, Direction) -> bool { return false; };
5151

5252
auto empty_draw = []([[maybe_unused]] Vector2D_Span<Color> &pixels,
53-
[[maybe_unused]] std::chrono::milliseconds game_clock,
53+
[[maybe_unused]] const Game &game,
5454
[[maybe_unused]] Point map_location) {
5555
for (std::size_t cur_x = 0; cur_x < pixels.size().width; ++cur_x) {
5656
for (std::size_t cur_y = 0; cur_y < pixels.size().height; ++cur_y) {
@@ -60,31 +60,54 @@ Game_Map make_map()// NOLINT congetive complexity
6060
};
6161

6262
for (std::size_t cur_x = 0; cur_x < map.locations.size().width; ++cur_x) {
63-
for (std::size_t cur_y = 0; cur_y < map.locations.size().height; ++cur_y) { map.locations.at(Point{ cur_x, cur_y }).draw = empty_draw; }
63+
for (std::size_t cur_y = 0; cur_y < map.locations.size().height; ++cur_y) {
64+
map.locations.at(Point{ cur_x, cur_y }).draw = empty_draw;
65+
}
6466
}
6567

6668
map.locations.at(Point{ 2, 3 }).draw = solid_draw;
6769
map.locations.at(Point{ 2, 3 }).can_enter = cannot_enter;
6870
map.locations.at(Point{ 1, 4 }).draw = solid_draw;
6971
map.locations.at(Point{ 1, 4 }).can_enter = cannot_enter;
7072
map.locations.at(Point{ 0, 2 }).draw = solid_draw;
71-
map.locations.at(Point{ 0, 2 }).can_enter = [](std::chrono::milliseconds, Point, Direction direction) {
73+
map.locations.at(Point{ 0, 2 }).can_enter = [](const Game &, Point, Direction direction) {
7274
return direction == Direction::South;
7375
};
7476

7577

7678
return map;
7779
}
7880

79-
void draw(Bitmap &viewport,
80-
Size tile_size,
81-
Point map_center,
82-
std::chrono::milliseconds time,
83-
const Game_Map &map,
84-
const Character &character)
81+
Game make_game()
82+
{
83+
Game retval;
84+
retval.maps.emplace("main", make_map());
85+
retval.current_map = "main";
86+
retval.tile_size = Size{ 8, 8 };// NOLINT Magic Number
87+
88+
Character player;
89+
player.draw = [player_bitmap = load_png("player.png")]([[maybe_unused]] Vector2D_Span<Color> &pixels,
90+
[[maybe_unused]] const Game &game,
91+
[[maybe_unused]] Point map_location) {
92+
// with with a fully saturated red at 50% alpha
93+
for (std::size_t cur_x = 0; cur_x < pixels.size().width; ++cur_x) {
94+
for (std::size_t cur_y = 0; cur_y < pixels.size().height; ++cur_y) {
95+
pixels.at(Point{ cur_x, cur_y }) += player_bitmap.at(Point{ cur_x, cur_y });
96+
}
97+
}
98+
};
99+
100+
101+
retval.player = player;
102+
103+
return retval;
104+
}
105+
106+
107+
void draw(Bitmap &viewport, Point map_center, const Game &game, const Game_Map &map)
85108
{
86-
const auto num_wide = viewport.pixels.size().width / tile_size.width;
87-
const auto num_high = viewport.pixels.size().width / tile_size.height;
109+
const auto num_wide = viewport.pixels.size().width / game.tile_size.width;
110+
const auto num_high = viewport.pixels.size().width / game.tile_size.height;
88111

89112
const auto x_offset = num_wide / 2;
90113
const auto y_offset = num_high / 2;
@@ -102,27 +125,34 @@ void draw(Bitmap &viewport,
102125

103126
for (std::size_t cur_x = 0; cur_x < num_wide; ++cur_x) {
104127
for (std::size_t cur_y = 0; cur_y < num_high; ++cur_y) {
105-
auto span = Vector2D_Span<Color>(Point{ cur_x * tile_size.width, cur_y * tile_size.width }, tile_size, viewport.pixels);
128+
auto span = Vector2D_Span<Color>(
129+
Point{ cur_x * game.tile_size.width, cur_y * game.tile_size.width }, game.tile_size, viewport.pixels);
106130
const auto map_location = Point{ cur_x, cur_y } + upper_left_map_location;
107-
map.locations.at(map_location).draw(span, time, map_location);
131+
map.locations.at(map_location).draw(span, game, map_location);
108132
}
109133
}
110134

111-
const auto character_relative_location = character.map_location - upper_left_map_location;
135+
const auto character_relative_location = game.player.map_location - upper_left_map_location;
136+
137+
const auto character_location = Point{ character_relative_location.x * game.tile_size.width,
138+
character_relative_location.y * game.tile_size.height };
112139

113-
const auto character_location =
114-
Point{ character_relative_location.x * tile_size.width, character_relative_location.y * tile_size.height };
140+
auto character_span = Vector2D_Span<Color>(character_location, game.tile_size, viewport.pixels);
115141

116-
auto character_span = Vector2D_Span<Color>(character_location, tile_size, viewport.pixels);
142+
game.player.draw(character_span, game, game.player.map_location);
143+
}
117144

118-
character.draw(character_span, time, character.map_location);
145+
void draw(Bitmap &viewport, const Game &game)
146+
{
147+
if (game.maps.contains(game.current_map)) {
148+
draw(viewport, game.player.map_location, game, game.maps.at(game.current_map));
149+
}
119150
}
120151

121152
void game_iteration_canvas()
122153
{
123-
const auto map = make_map();
154+
auto game = make_game();
124155

125-
static constexpr auto Tile_Size = Size{ 8, 8 };
126156

127157
// this should probably have a `bitmap` helper function that does what you expect
128158
// similar to the other parts of FTXUI
@@ -132,19 +162,6 @@ void game_iteration_canvas()
132162
double fps = 0;
133163
auto start_time = std::chrono::steady_clock::now();
134164

135-
const auto player_bitmap = load_png("player.png");
136-
137-
Character player;
138-
player.draw = [&]([[maybe_unused]] Vector2D_Span<Color> &pixels,
139-
[[maybe_unused]] std::chrono::milliseconds game_clock,
140-
[[maybe_unused]] Point map_location) {
141-
// with with a fully saturated red at 50% alpha
142-
for (std::size_t cur_x = 0; cur_x < pixels.size().width; ++cur_x) {
143-
for (std::size_t cur_y = 0; cur_y < pixels.size().height; ++cur_y) {
144-
pixels.at(Point{ cur_x, cur_y }) += player_bitmap.at(Point{ cur_x, cur_y });
145-
}
146-
}
147-
};
148165

149166
ftxui::Event last_event;
150167
ftxui::Event current_event;
@@ -161,9 +178,11 @@ void game_iteration_canvas()
161178
const auto game_clock =
162179
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start_time);
163180

181+
game.clock = game_clock;
182+
164183
[&] {
165184
if (current_event != last_event) {
166-
auto location = player.map_location;
185+
auto location = game.player.map_location;
167186
Direction from{};
168187

169188
if (current_event == ftxui::Event::ArrowUp) {
@@ -182,13 +201,14 @@ void game_iteration_canvas()
182201
return;
183202
}
184203

185-
if (map.can_enter_from(game_clock, location, from)) { player.map_location = location; }
204+
if (game.maps.at(game.current_map).can_enter_from(game, location, from)) {
205+
game.player.map_location = location;
206+
}
186207
}
187208
}();
188209

189210

190-
// just draw the map
191-
draw(*bm, Tile_Size, player.map_location, game_clock, map, player);
211+
draw(*bm, game);
192212
};
193213

194214
auto screen = ftxui::ScreenInteractive::TerminalOutput();

0 commit comments

Comments
 (0)