Skip to content

Commit 8dc74c9

Browse files
committed
Provide both enter_action and exit_action
1 parent 0baf1b1 commit 8dc74c9

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

src/game.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,12 @@ Game_Map make_map()// NOLINT cognitive complexity
5656
return direction == Direction::South;
5757
};
5858

59-
map.locations.at(Point{0,3}).action = [](Game &game, Point, Direction) {
59+
map.locations.at(Point{0,3}).enter_action = [](Game &game, Point, Direction) {
6060
game.last_message = "There is a secret entrance to the north";
6161
};
62+
map.locations.at(Point{ 0, 3 }).exit_action = [](Game &game, Point, Direction) {
63+
game.last_message = "";
64+
};
6265

6366

6467
return map;

src/game_components.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ enum struct Direction { North, South, East, West };
1919

2020
struct Location
2121
{
22-
std::function<void(Game &, Point, Direction)> action;
22+
std::function<void(Game &, Point, Direction)> enter_action;
23+
std::function<void(Game &, Point, Direction)> exit_action;
2324
std::function<void(Vector2D_Span<Color> &, const Game &, Point)> draw;
2425
std::function<bool(const Game &, Point, Direction)> can_enter;
2526
};

src/main.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ void draw(Bitmap &viewport, const Game &game)
7070
}
7171
}
7272

73-
void game_iteration_canvas()
73+
void game_iteration_canvas()// NOLINT cognitive complexity
7474
{
7575
auto game = make_game();
7676

@@ -104,6 +104,8 @@ void game_iteration_canvas()
104104
[&] {
105105
if (current_event != last_event) {
106106
auto location = game.player.map_location;
107+
const auto last_location = location;
108+
107109
Direction from{};
108110

109111
if (current_event == ftxui::Event::ArrowUp) {
@@ -123,11 +125,15 @@ void game_iteration_canvas()
123125
}
124126

125127
if (game.maps.at(game.current_map).can_enter_from(game, location, from)) {
128+
auto exit_action = game.maps.at(game.current_map).locations.at(last_location).exit_action;
129+
if (exit_action) { exit_action(game, last_location, from); }
130+
126131
game.player.map_location = location;
127-
auto action = game.maps.at(game.current_map).locations.at(location).action;
128132

129-
if (action) { action(game, location, from); }
133+
auto enter_action = game.maps.at(game.current_map).locations.at(location).enter_action;
134+
if (enter_action) { enter_action(game, location, from); }
130135
}
136+
131137
}
132138
}();
133139

0 commit comments

Comments
 (0)