Skip to content

Commit ea80309

Browse files
committed
More game updates
1 parent da69394 commit ea80309

File tree

7 files changed

+238
-52
lines changed

7 files changed

+238
-52
lines changed

resources/tiled/maps.tiled-session

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"scale": 12.904166666666667,
3737
"selectedLayer": 0,
3838
"viewCenter": {
39-
"x": 28.052954472069743,
39+
"x": 27.975460122699385,
4040
"y": 32.00516628995803
4141
}
4242
}

resources/tiled/tiles/Map.tmj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@
5858
114, 115, 141, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 141, 115, 116,
5959
114, 115, 116, 195, 196, 196, 196, 196, 197, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 114, 115, 116,
6060
114, 115, 116, 221, 222, 223, 222, 223, 223, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 114, 115, 116,
61-
114, 115, 116, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 114, 115, 116,
62-
114, 115, 116, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 212, 262, 214, 190, 190, 190, 190, 190, 190, 190, 190, 114, 115, 116,
63-
114, 115, 116, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 212, 262, 213, 263, 138, 261, 262, 214, 190, 190, 190, 190, 190, 190, 114, 115, 116,
64-
114, 115, 116, 190, 190, 190, 190, 190, 190, 190, 190, 190, 212, 263, 209, 265, 210, 265, 211, 138, 261, 214, 190, 190, 190, 190, 190, 114, 115, 116,
61+
114, 115, 116, 190, 190, 190, 190, 190, 190, 190, 190, 190, 215, 216, 216, 346, 217, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 114, 115, 116,
62+
114, 115, 116, 190, 190, 190, 190, 190, 190, 190, 190, 190, 267, 268, 295, 293, 269, 212, 214, 190, 190, 190, 190, 190, 190, 190, 190, 114, 115, 116,
63+
114, 115, 116, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 267, 269, 212, 263, 261, 262, 214, 190, 190, 190, 190, 190, 190, 114, 115, 116,
64+
114, 115, 116, 190, 190, 190, 190, 190, 190, 190, 190, 190, 212, 213, 214, 190, 264, 265, 211, 138, 261, 214, 190, 190, 190, 190, 190, 114, 115, 116,
6565
114, 115, 116, 190, 190, 190, 190, 190, 190, 190, 190, 212, 263, 209, 266, 190, 190, 190, 238, 138, 138, 235, 190, 190, 190, 190, 190, 114, 115, 116,
6666
114, 115, 116, 190, 190, 190, 190, 190, 212, 262, 262, 263, 209, 266, 190, 190, 190, 190, 264, 265, 210, 266, 190, 190, 190, 190, 190, 114, 115, 116,
6767
114, 115, 116, 190, 190, 190, 190, 190, 264, 265, 210, 265, 266, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 190, 140, 115, 116,

src/game.cpp

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,19 @@ Game_Map make_map()
99
{
1010
auto map = load_tiled_map("/home/jason/my_awesome_game/resources/tiled/tiles/Map.tmj");
1111

12-
map.locations.at(Point{4,5}).can_enter // NOLINT magic numbers
13-
= [](const Game &, Point, Direction){ return true; };
14-
map.locations.at(Point{4,5}).enter_action // NOLINT magic numbers
15-
= [](Game &game, Point, Direction){ game.current_map = "store"; game.player.map_location = Point{6,6}; }; // NOLINT magic numbers
12+
map.locations.at(Point{ 4, 5 }).can_enter// NOLINT magic numbers
13+
= [](const Game &, Point, Direction) { return true; };
14+
map.locations.at(Point{ 4, 5 }).enter_action// NOLINT magic numbers
15+
= [](Game &game, Point, Direction) {
16+
game.current_map = "store";
17+
game.player.map_location = Point{ 6, 6 };// NOLINT magic numbers
18+
};
1619

20+
map.locations.at(Point{ 4, 6 }).enter_action// NOLINT magic numbers
21+
= [](Game &game, Point, Direction) { game.last_message = "A store"; };
22+
23+
map.locations.at(Point{ 4, 6 }).exit_action// NOLINT magic numbers
24+
= [](Game &game, Point, Direction) { game.last_message = ""; };
1725

1826
return map;
1927
}
@@ -22,6 +30,21 @@ Game_Map make_store()
2230
{
2331
auto map = load_tiled_map("/home/jason/my_awesome_game/resources/tiled/tiles/Store.tmj");
2432

33+
map.locations.at(Point{ 7, 6 }).enter_action// NOLINT magic numbers
34+
= [](Game &game, Point, Direction) {
35+
game.current_map = "main";
36+
game.player.map_location = Point{ 4, 6 };// NOLINT magic numbers
37+
};
38+
39+
map.locations.at(Point{ 3, 3 }).enter_action = [](Game &game, Point, Direction) {
40+
game.set_menu(
41+
Menu{ { "Ask about town", "This is the quiet town of 'Quad Corners'. The economy has been down for the last few years. People have been moving away. It's a bit depressing, really." },
42+
set_flag("Tell about present", "Ah yes, the Xstation6, all of the kids want one. You'll probably have a hard time finding one of those around here.", variable{"xstation"}),
43+
check_flag("Ask about Xstation6", "I think bob has one.", variable{"xstation"}),
44+
45+
exit_menu() });
46+
};
47+
2548
return map;
2649
}
2750

@@ -33,19 +56,19 @@ Game make_game()
3356
retval.current_map = "main";
3457
retval.tile_size = Size{ 8, 8 };// NOLINT Magic Number
3558

36-
retval.variables["Task"] = "Exit game";
37-
retval.display_variables.emplace_back("Task");
59+
retval.variables["Cash"] = 50;// NOLINT
60+
retval.variables["xstation"] = false;
61+
retval.display_variables.emplace_back("Cash");
3862

3963

4064
Character player;
41-
player.map_location = { 14, 17 }; // NOLINT Magic Number
65+
player.map_location = { 14, 17 };// NOLINT Magic Number
4266
player.draw =
4367
[](Vector2D_Span<Color> &pixels, [[maybe_unused]] const Game &game, [[maybe_unused]] Point map_location) {
44-
45-
const auto &tile = game.maps.at("main").tile_sets.front().at(98); // NOLINT magic number
68+
const auto &tile = game.maps.at("main").tile_sets.front().at(98);// NOLINT magic number
4669
for (std::size_t cur_x = 0; cur_x < pixels.size().width; ++cur_x) {
4770
for (std::size_t cur_y = 0; cur_y < pixels.size().height; ++cur_y) {
48-
pixels.at(Point{cur_x, cur_y}) += tile.at(Point{cur_x, cur_y});
71+
pixels.at(Point{ cur_x, cur_y }) += tile.at(Point{ cur_x, cur_y });
4972
}
5073
}
5174
};
@@ -54,10 +77,10 @@ Game make_game()
5477
retval.player = player;
5578

5679
retval.popup_message =
57-
"Welcome to 'Learning C++ With Game Hacking Lesson 02'\n\n"
58-
"Your job, again, is to get into the special square in the bottom right corner of the map, and you'll need to "
59-
"modify the code.\n\n"
60-
"We need to talk about 'magic numbers' in this code, but you'll have the fun of changing some colors!";
80+
"Welcome to Quad Corners.\n\nThis town is your last stop on your delivery run. Unfortunately shortly after you "
81+
"arrived, some heavy rains came and washed out the road.\n\nTomorrow is your kid's birthday, and you still don't "
82+
"have a present!\n\nYou're not a slacker, you were planning to get it on the way home. Now you have to see if you "
83+
"can find it here, instead.";
6184

6285
return retval;
6386
}

src/game_components.cpp

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ Game_Map load_tiled_map(const std::filesystem::path &map_json)// NOLINT cofnitiv
106106

107107

108108
for (const auto &[point, tile_data] : points) {
109-
map.locations.at(point).draw = [tiles = tile_data](Vector2D_Span<Color> &pixels, const Game &game, Point, Layer layer) {
109+
map.locations.at(point).draw = [tiles = tile_data](
110+
Vector2D_Span<Color> &pixels, const Game &game, Point, Layer layer) {
110111
const auto &tile_sets = game.get_current_map().tile_sets;
111112
bool first_tile = true;
112113
for (const auto &tile : tiles) {
@@ -134,11 +135,48 @@ Game_Map load_tiled_map(const std::filesystem::path &map_json)// NOLINT cofnitiv
134135
map.locations.at(point).can_enter = [tiles = tile_data](const Game &game, Point, Direction) {
135136
const auto &tile_sets = game.get_current_map().tile_sets;
136137
return std::ranges::all_of(tiles, [&](const auto &tile) {
137-
return tile.foreground || tile.background || tile.tileid == 0 || tile_sets[0].properties.at(tile.tileid).passable;
138+
return tile.foreground || tile.background || tile.tileid == 0
139+
|| tile_sets[0].properties.at(tile.tileid).passable;
138140
});
139141
};
140142
}
141143

142144
return map;
143145
}
146+
147+
Menu::MenuItem::MenuItem(std::string text_,
148+
std::function<void(Game &)> action_,
149+
std::function<bool(const Game &)> visible_)
150+
: text{ std::move(text_) }, action{ std::move(action_) }, visible{ std::move(visible_) }
151+
{}
152+
153+
Menu::MenuItem::MenuItem(std::string text_, std::string message_, std::function<bool(const Game &)> visible_)
154+
: MenuItem(
155+
std::move(text_),
156+
[message = std::move(message_)](Game &game) { game.popup_message = message; },
157+
std::move(visible_))
158+
{}
159+
160+
Menu::MenuItem exit_menu()
161+
{
162+
return { "Exit", [](Game &game) { game.clear_menu(); } };
163+
}
164+
165+
Menu::MenuItem set_flag(std::string text, std::string message, variable var)
166+
{
167+
return { std::move(text), [message = std::move(message), var = std::move(var)](Game &game) {
168+
game.popup_message = message;
169+
if (game.variables[var.name] != Variable{ true }) {
170+
game.variables[var.name] = true;
171+
game.set_menu(game.get_menu());
172+
}
173+
} };
174+
}
175+
176+
Menu::MenuItem check_flag(std::string text, std::string message, variable var)
177+
{
178+
return { std::move(text), std::move(message), std::move(var) == true };
179+
}
180+
181+
144182
}// namespace lefticus::awesome_game

src/game_components.hpp

Lines changed: 131 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,65 @@ struct Game_Map
5555

5656
Game_Map load_tiled_map(const std::filesystem::path &map_json);
5757

58+
59+
using Variable = std::variant<double, std::int64_t, std::string, bool>;
60+
61+
template<typename Comparitor> struct Variable_Comparison
62+
{
63+
Comparitor comparitor;
64+
65+
bool operator()(const Game &game) const { return comparitor(game); }
66+
};
67+
68+
template<typename T> Variable_Comparison(T t) -> Variable_Comparison<T>;
69+
70+
71+
template<typename LHS, typename RHS> auto operator&&(Variable_Comparison<LHS> lhs, Variable_Comparison<RHS> rhs)
72+
{
73+
return Variable_Comparison{ [lhs = std::move(lhs.comparitor), rhs = std::move(rhs.comparitor)](
74+
const Game &game) { return lhs(game) && rhs(game); } };
75+
}
76+
77+
template<typename LHS, typename RHS> auto operator||(Variable_Comparison<LHS> lhs, Variable_Comparison<RHS> rhs)
78+
{
79+
return Variable_Comparison{ [lhs = std::move(lhs.comparitor), rhs = std::move(rhs.comparitor)](
80+
const Game &game) { return lhs(game) || rhs(game); } };
81+
}
82+
83+
struct variable
84+
{
85+
std::string name;
86+
};
87+
88+
89+
90+
91+
inline std::string to_string(const Variable &variable)
92+
{
93+
return std::visit([](const auto &value) { return fmt::format("{}", value); }, variable);
94+
}
95+
5896
struct Menu
5997
{
6098
struct MenuItem
6199
{
62100
std::string text;
63101
std::function<void(Game &)> action;
102+
std::function<bool(const Game &)> visible;
103+
MenuItem(std::string text_, std::string message_, std::function<bool (const Game &)> = {});
104+
105+
template<typename Compare>
106+
MenuItem(std::string text_, std::string message_, Variable_Comparison<Compare> comp)
107+
: MenuItem(std::move(text_), std::move(message_), std::function<bool(const Game &)>(std::move(comp.comparitor)))
108+
{
109+
}
110+
111+
MenuItem(std::string text_, std::function<void(Game &)> action_, std::function<bool (const Game &)> visible_ = {});
112+
113+
template<typename Compare>
114+
MenuItem(std::string text_, std::function<void(Game &)> action_, Variable_Comparison<Compare> comp)
115+
: MenuItem(std::move(text_), std::move(action_), std::function<bool(const Game &)>(std::move(comp.comparitor)))
116+
{}
64117
};
65118

66119
std::vector<MenuItem> items;
@@ -70,13 +123,6 @@ struct Menu
70123
explicit Menu(std::initializer_list<MenuItem> items_) : items{ items_ } {}
71124
};
72125

73-
using Variable = std::variant<double, std::int64_t, std::string, bool>;
74-
75-
inline std::string to_string(const Variable &variable)
76-
{
77-
return std::visit([](const auto &value) { return fmt::format("{}", value); }, variable);
78-
}
79-
80126
struct Game
81127
{
82128

@@ -132,6 +178,84 @@ struct Game
132178
bool menu_is_new = false;
133179
};
134180

181+
template<typename Value> auto operator==(variable var, Value value)
182+
{
183+
return Variable_Comparison{ [name = std::move(var.name), value = Variable{ std::move(value) }](
184+
const Game &game) { return game.variables.at(name) == value; } };
185+
}
186+
187+
template<typename Value> auto operator==(Value value, variable var)
188+
{
189+
return Variable_Comparison{ [name = std::move(var.name), value = Variable{ std::move(value) }](
190+
const Game &game) { return value == game.variables.at(name); } };
191+
}
192+
193+
194+
template<typename Value> auto operator!=(variable var, Value value)
195+
{
196+
return Variable_Comparison{ [name = std::move(var.name), value = Variable{ std::move(value) }](
197+
const Game &game) { return game.variables.at(name) != value; } };
198+
}
199+
200+
template<typename Value> auto operator!=(Value value, variable var)
201+
{
202+
return Variable_Comparison{ [name = std::move(var.name), value = Variable{ std::move(value) }](
203+
const Game &game) { return value != game.variables.at(name); } };
204+
}
205+
206+
template<typename Value> auto operator<(variable var, Value value)
207+
{
208+
return Variable_Comparison{ [name = std::move(var.name), value = Variable{ std::move(value) }](
209+
const Game &game) { return game.variables.at(name) < value; } };
210+
}
211+
212+
template<typename Value> auto operator<(Value value, variable var)
213+
{
214+
return Variable_Comparison{ [name = std::move(var.name), value = Variable{ std::move(value) }](
215+
const Game &game) { return value < game.variables.at(name); } };
216+
}
217+
218+
219+
template<typename Value> auto operator<=(variable var, Value value)
220+
{
221+
return Variable_Comparison{ [name = std::move(var.name), value = Variable{ std::move(value) }](
222+
const Game &game) { return game.variables.at(name) <= value; } };
223+
}
224+
225+
template<typename Value> auto operator<=(Value value, variable var)
226+
{
227+
return Variable_Comparison{ [name = std::move(var.name), value = Variable{ std::move(value) }](
228+
const Game &game) { return value <= game.variables.at(name); } };
229+
}
230+
231+
template<typename Value> auto operator>(variable var, Value value)
232+
{
233+
return Variable_Comparison{ [name = std::move(var.name), value = Variable{ std::move(value) }](
234+
const Game &game) { return game.variables.at(name) > value; } };
235+
}
236+
237+
template<typename Value> auto operator>(Value value, variable var)
238+
{
239+
return Variable_Comparison{ [name = std::move(var.name), value = Variable{ std::move(value) }](
240+
const Game &game) { return value > game.variables.at(name); } };
241+
}
242+
243+
template<typename Value> auto operator>=(variable var, Value value)
244+
{
245+
return Variable_Comparison{ [name = std::move(var.name), value = Variable{ std::move(value) }](
246+
const Game &game) { return game.variables.at(name) >= value; } };
247+
}
248+
249+
template<typename Value> auto operator>=(Value value, variable var)
250+
{
251+
return Variable_Comparison{ [name = std::move(var.name), value = Variable{ std::move(value) }](
252+
const Game &game) { return value >= game.variables.at(name); } };
253+
}
254+
255+
Menu::MenuItem exit_menu();
256+
Menu::MenuItem set_flag(std::string text, std::string message, variable var);
257+
Menu::MenuItem check_flag(std::string text, std::string message, variable var);
258+
135259
}// namespace lefticus::awesome_game
136260

137261
#endif// AWESOME_GAME_GAME_COMPONENTS_HPP

src/game_hacking_lesson_00.cpp

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,32 @@ Game_Map make_map()// NOLINT cognitive complexity
99
Game_Map map{ Size{ 10, 10 } };// NOLINT magic numbers
1010

1111

12-
auto empty_draw =
13-
[](Vector2D_Span<Color> &pixels, [[maybe_unused]] const Game &game, [[maybe_unused]] Point map_location, Layer layer) {
14-
if (layer == Layer::Foreground) {
15-
return;
16-
}
17-
// just a grey
18-
fill(pixels, Color{ 25, 25, 25, 255 });// NOLINT magic number
19-
};
12+
auto empty_draw = [](Vector2D_Span<Color> &pixels,
13+
[[maybe_unused]] const Game &game,
14+
[[maybe_unused]] Point map_location,
15+
Layer layer) {
16+
if (layer == Layer::Foreground) { return; }
17+
// just a grey
18+
fill(pixels, Color{ 25, 25, 25, 255 });// NOLINT magic number
19+
};
2020

2121
auto cannot_enter = [](const Game &, Point, Direction) -> bool { return false; };
2222

23-
auto water_draw =
24-
[](Vector2D_Span<Color> &pixels, [[maybe_unused]] const Game &game, [[maybe_unused]] Point map_location, Layer layer) {
25-
if (layer == Layer::Foreground) { return; }
26-
fill(pixels, Color{ 0, 0, 250, 255 });// NOLINT magic number
27-
};
23+
auto water_draw = [](Vector2D_Span<Color> &pixels,
24+
[[maybe_unused]] const Game &game,
25+
[[maybe_unused]] Point map_location,
26+
Layer layer) {
27+
if (layer == Layer::Foreground) { return; }
28+
fill(pixels, Color{ 0, 0, 250, 255 });// NOLINT magic number
29+
};
2830

2931

3032
auto wall_draw = []([[maybe_unused]] Vector2D_Span<Color> &pixels,
3133
[[maybe_unused]] const Game &game,
3234
[[maybe_unused]] Point map_location,
3335
Layer layer) {
34-
if (layer == Layer::Foreground) {
35-
return;
36-
}
37-
36+
if (layer == Layer::Foreground) { return; }
37+
3838
static constexpr auto wall_color = Color{ 100, 100, 100, 128 };
3939

4040
// We fill in the wall with a color, the color alternates by the second
@@ -134,10 +134,8 @@ Game_Map make_map()// NOLINT cognitive complexity
134134
map.locations.at(special_location).enter_action = [](Game &game, Point, Direction) {
135135
game.last_message = "You found the secret room! Now change the call to `play_game` to start lesson 01";
136136
Menu menu;
137-
menu.items.push_back(
138-
Menu::MenuItem{ "Continue Game", [](Game &menu_action_game) { menu_action_game.clear_menu(); } });
139-
menu.items.push_back(
140-
Menu::MenuItem{ "Exit Game", [](Game &menu_action_game) { menu_action_game.exit_game = true; } });
137+
menu.items.emplace_back("Continue Game", [](Game &menu_action_game) { menu_action_game.clear_menu(); });
138+
menu.items.emplace_back("Exit Game", [](Game &menu_action_game) { menu_action_game.exit_game = true; });
141139
game.set_menu(menu);
142140
};
143141

0 commit comments

Comments
 (0)