Skip to content

Commit 0baf1b1

Browse files
committed
Enact game actions on locations
1 parent 6fc4e65 commit 0baf1b1

File tree

5 files changed

+111
-83
lines changed

5 files changed

+111
-83
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ find_package(docopt CONFIG)
44
find_package(lodepng CONFIG)
55

66
# Generic test that uses conan libs
7-
add_executable(intro main.cpp color.hpp size.hpp point.hpp vector2d.hpp bitmap.hpp bitmap.cpp game_components.hpp)
7+
add_executable(intro main.cpp color.hpp size.hpp point.hpp vector2d.hpp bitmap.hpp bitmap.cpp game_components.hpp game.cpp game.hpp)
88
target_link_libraries(
99
intro
1010
PRIVATE project_options

src/game.cpp

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#include "game.hpp"
2+
#include "bitmap.hpp"
3+
#include "game_components.hpp"
4+
5+
6+
namespace lefticus::my_awesome_game {
7+
Game_Map make_map()// NOLINT cognitive complexity
8+
{
9+
Game_Map map{ Size{ 10, 10 } };// NOLINT magic numbers
10+
11+
auto solid_draw = []([[maybe_unused]] Vector2D_Span<Color> &pixels,
12+
[[maybe_unused]] const Game &game,
13+
[[maybe_unused]] Point map_location) {
14+
for (std::size_t cur_x = 0; cur_x < pixels.size().width; ++cur_x) {
15+
for (std::size_t cur_y = 0; cur_y < pixels.size().height; ++cur_y) {
16+
if (cur_x == 0 || cur_y == 0 || cur_x == pixels.size().width - 1 || cur_y == pixels.size().height - 1) {
17+
pixels.at(Point{ cur_x, cur_y }) = Color{ 128, 128, 128, 255 };// NOLINT Magic numbers
18+
} else {
19+
switch ((game.clock.count() / 1000) % 2) {// NOLINT Magic numbers
20+
case 0:
21+
pixels.at(Point{ cur_x, cur_y }) = Color{ 255, 255, 255, 255 };// NOLINT Magic numbers
22+
break;
23+
case 1:// NOLINT Magic Numbers
24+
pixels.at(Point{ cur_x, cur_y }) = Color{ 200, 240, 240, 255 };// NOLINT Magic numbers
25+
break;
26+
}
27+
}
28+
}
29+
}
30+
};
31+
32+
auto cannot_enter = [](const Game &, Point, Direction) -> bool { return false; };
33+
34+
auto empty_draw = []([[maybe_unused]] Vector2D_Span<Color> &pixels,
35+
[[maybe_unused]] const Game &game,
36+
[[maybe_unused]] Point map_location) {
37+
for (std::size_t cur_x = 0; cur_x < pixels.size().width; ++cur_x) {
38+
for (std::size_t cur_y = 0; cur_y < pixels.size().height; ++cur_y) {
39+
pixels.at(Point{ cur_x, cur_y }) = Color{ 10, 10, 10, 255 };// NOLINT Magic numbers
40+
}
41+
}
42+
};
43+
44+
for (std::size_t cur_x = 0; cur_x < map.locations.size().width; ++cur_x) {
45+
for (std::size_t cur_y = 0; cur_y < map.locations.size().height; ++cur_y) {
46+
map.locations.at(Point{ cur_x, cur_y }).draw = empty_draw;
47+
}
48+
}
49+
50+
map.locations.at(Point{ 2, 3 }).draw = solid_draw;
51+
map.locations.at(Point{ 2, 3 }).can_enter = cannot_enter;
52+
map.locations.at(Point{ 1, 4 }).draw = solid_draw;
53+
map.locations.at(Point{ 1, 4 }).can_enter = cannot_enter;
54+
map.locations.at(Point{ 0, 2 }).draw = solid_draw;
55+
map.locations.at(Point{ 0, 2 }).can_enter = [](const Game &, Point, Direction direction) {
56+
return direction == Direction::South;
57+
};
58+
59+
map.locations.at(Point{0,3}).action = [](Game &game, Point, Direction) {
60+
game.last_message = "There is a secret entrance to the north";
61+
};
62+
63+
64+
return map;
65+
}
66+
67+
Game make_game()
68+
{
69+
Game retval;
70+
retval.maps.emplace("main", make_map());
71+
retval.current_map = "main";
72+
retval.tile_size = Size{ 8, 8 };// NOLINT Magic Number
73+
74+
Character player;
75+
player.draw = [player_bitmap = load_png("player.png")]([[maybe_unused]] Vector2D_Span<Color> &pixels,
76+
[[maybe_unused]] const Game &game,
77+
[[maybe_unused]] Point map_location) {
78+
// with with a fully saturated red at 50% alpha
79+
for (std::size_t cur_x = 0; cur_x < pixels.size().width; ++cur_x) {
80+
for (std::size_t cur_y = 0; cur_y < pixels.size().height; ++cur_y) {
81+
pixels.at(Point{ cur_x, cur_y }) += player_bitmap.at(Point{ cur_x, cur_y });
82+
}
83+
}
84+
};
85+
86+
87+
retval.player = player;
88+
89+
return retval;
90+
}
91+
}// namespace lefticus::my_awesome_game

src/game.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef MY_AWESOME_GAME_GAME_HPP
2+
#define MY_AWESOME_GAME_GAME_HPP
3+
4+
namespace lefticus::my_awesome_game {
5+
struct Game;
6+
Game make_game();
7+
}
8+
9+
10+
11+
#endif// MY_AWESOME_GAME_GAME_HPP

src/game_components.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ struct Game
6060
std::string current_map;
6161
std::chrono::milliseconds clock;
6262
Size tile_size;
63+
64+
std::string last_message;
6365
};
6466

6567
}

src/main.cpp

Lines changed: 6 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "bitmap.hpp"
1313
#include "color.hpp"
14+
#include "game.hpp"
1415
#include "game_components.hpp"
1516
#include "point.hpp"
1617
#include "size.hpp"
@@ -22,86 +23,6 @@
2223

2324

2425
namespace lefticus::my_awesome_game {
25-
Game_Map make_map()// NOLINT congetive complexity
26-
{
27-
Game_Map map{ Size{ 10, 10 } };// NOLINT magic numbers
28-
29-
auto solid_draw = []([[maybe_unused]] Vector2D_Span<Color> &pixels,
30-
[[maybe_unused]] const Game &game,
31-
[[maybe_unused]] Point map_location) {
32-
for (std::size_t cur_x = 0; cur_x < pixels.size().width; ++cur_x) {
33-
for (std::size_t cur_y = 0; cur_y < pixels.size().height; ++cur_y) {
34-
if (cur_x == 0 || cur_y == 0 || cur_x == pixels.size().width - 1 || cur_y == pixels.size().height - 1) {
35-
pixels.at(Point{ cur_x, cur_y }) = Color{ 128, 128, 128, 255 };// NOLINT Magic numbers
36-
} else {
37-
switch ((game.clock.count() / 1000) % 2) {// NOLINT Magic numbers
38-
case 0:
39-
pixels.at(Point{ cur_x, cur_y }) = Color{ 255, 255, 255, 255 };// NOLINT Magic numbers
40-
break;
41-
case 1:// NOLINT Magic Numbers
42-
pixels.at(Point{ cur_x, cur_y }) = Color{ 200, 240, 240, 255 };// NOLINT Magic numbers
43-
break;
44-
}
45-
}
46-
}
47-
}
48-
};
49-
50-
auto cannot_enter = [](const Game &, Point, Direction) -> bool { return false; };
51-
52-
auto empty_draw = []([[maybe_unused]] Vector2D_Span<Color> &pixels,
53-
[[maybe_unused]] const Game &game,
54-
[[maybe_unused]] Point map_location) {
55-
for (std::size_t cur_x = 0; cur_x < pixels.size().width; ++cur_x) {
56-
for (std::size_t cur_y = 0; cur_y < pixels.size().height; ++cur_y) {
57-
pixels.at(Point{ cur_x, cur_y }) = Color{ 10, 10, 10, 255 };// NOLINT Magic numbers
58-
}
59-
}
60-
};
61-
62-
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) {
64-
map.locations.at(Point{ cur_x, cur_y }).draw = empty_draw;
65-
}
66-
}
67-
68-
map.locations.at(Point{ 2, 3 }).draw = solid_draw;
69-
map.locations.at(Point{ 2, 3 }).can_enter = cannot_enter;
70-
map.locations.at(Point{ 1, 4 }).draw = solid_draw;
71-
map.locations.at(Point{ 1, 4 }).can_enter = cannot_enter;
72-
map.locations.at(Point{ 0, 2 }).draw = solid_draw;
73-
map.locations.at(Point{ 0, 2 }).can_enter = [](const Game &, Point, Direction direction) {
74-
return direction == Direction::South;
75-
};
76-
77-
78-
return map;
79-
}
80-
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-
}
10526

10627

10728
void draw(Bitmap &viewport, Point map_center, const Game &game, const Game_Map &map)
@@ -203,6 +124,9 @@ void game_iteration_canvas()
203124

204125
if (game.maps.at(game.current_map).can_enter_from(game, location, from)) {
205126
game.player.map_location = location;
127+
auto action = game.maps.at(game.current_map).locations.at(location).action;
128+
129+
if (action) { action(game, location, from); }
206130
}
207131
}
208132
}();
@@ -229,11 +153,11 @@ void game_iteration_canvas()
229153
last_time = new_time;
230154

231155
// now actually draw the game elements
232-
return ftxui::hbox({ bm | ftxui::border,
156+
return ftxui::vbox({ftxui::hbox({ bm | ftxui::border,
233157
ftxui::vbox({ ftxui::text("Frame: " + std::to_string(counter)),
234158
ftxui::text("FPS: " + std::to_string(fps)),
235159
ftxui::text("Character: " + last_character),
236-
small_bm | ftxui::border }) });
160+
small_bm | ftxui::border }) }), ftxui::text("Message: " + game.last_message)});
237161
};
238162

239163
auto container = ftxui::Container::Vertical({});

0 commit comments

Comments
 (0)