Skip to content

Commit

Permalink
Added a helper function for getting GameLoopBaseState ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
dbeef committed Dec 17, 2023
1 parent 6d00f71 commit df85e5c
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 89 deletions.
10 changes: 8 additions & 2 deletions src/game-loop/include/prefabs/ui/CheatConsole.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@

namespace prefabs
{
struct CheatConsoleComponent
class CheatConsoleComponent
{
GameLoop::State state{GameLoop::State::CURRENT};
public:
bool is_state_change_requested() const { return _state_change_requested; }
void request_state_change(GameLoop::State requested_state) { _requested_state = requested_state; _state_change_requested = true; }
GameLoop::State get_requested_state() const { return _requested_state;}
private:
bool _state_change_requested = false;
GameLoop::State _requested_state{GameLoop::State::CURRENT};
};

struct CheatConsole
Expand Down
2 changes: 2 additions & 0 deletions src/game-loop/interface/game-loop/GameLoop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class GameLoop

GameLoop(const std::shared_ptr<Viewport>&);
std::function<bool(uint32_t delta_time_ms)>& get();
GameLoopBaseState* get_game_loop_state_ptr(State);

private:

friend class GameLoopBaseState;
Expand Down
20 changes: 20 additions & 0 deletions src/game-loop/src/game-loop/GameLoop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,23 @@ GameLoop::GameLoop(const std::shared_ptr<Viewport>& viewport)
return _exit;
};
}

GameLoopBaseState *GameLoop::get_game_loop_state_ptr(GameLoop::State state) {
switch (state) {
case State::MAIN_MENU:
return &_states.main_menu;
case State::PLAYING:
return &_states.playing;
case State::STARTED:
return &_states.started;
case State::LEVEL_SUMMARY:
return &_states.level_summary;
case State::SCORES:
return &_states.scores;
case State::SANDBOX:
return &_states.sandbox;
case State::CURRENT:
return _states.current;
default: assert(false);
}
}
32 changes: 5 additions & 27 deletions src/game-loop/src/game-loop/GameLoopMainMenuState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,39 +105,17 @@ GameLoopBaseState *GameLoopMainMenuState::update(GameLoop& game_loop, uint32_t d
}
}


auto& cheat_console = registry.get<prefabs::CheatConsoleComponent>(_cheat_console);
switch (cheat_console.state)
{
case GameLoop::State::SCORES:
{
cheat_console.state = GameLoop::State::CURRENT;
return &game_loop._states.scores;
}
case GameLoop::State::MAIN_MENU:
{
cheat_console.state = GameLoop::State::CURRENT;
return &game_loop._states.main_menu;
}
case GameLoop::State::PLAYING:
{
cheat_console.state = GameLoop::State::CURRENT;
return &game_loop._states.playing;
}
case GameLoop::State::SANDBOX:
{
cheat_console.state = GameLoop::State::CURRENT;
return &game_loop._states.sandbox;
}
default: {}
}

if (position.y_center <= EXIT_LEVEL_Y)
{
log_info("Quitting using rope.");
game_loop._exit = true;
}

auto& cheat_console = registry.get<prefabs::CheatConsoleComponent>(_cheat_console);
if (cheat_console.is_state_change_requested()) {
return game_loop.get_game_loop_state_ptr(cheat_console.get_requested_state());
}

return this;
}

Expand Down
20 changes: 2 additions & 18 deletions src/game-loop/src/game-loop/GameLoopPlayingState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,24 +114,8 @@ GameLoopBaseState *GameLoopPlayingState::update(GameLoop& game_loop, uint32_t de
game_loop._level_summary_tracker->update(delta_time_ms);

auto& cheat_console = registry.get<prefabs::CheatConsoleComponent>(_cheat_console);
switch (cheat_console.state)
{
case GameLoop::State::SCORES:
{
cheat_console.state = GameLoop::State::CURRENT;
return &game_loop._states.scores;
}
case GameLoop::State::MAIN_MENU:
{
cheat_console.state = GameLoop::State::CURRENT;
return &game_loop._states.main_menu;
}
case GameLoop::State::PLAYING:
{
cheat_console.state = GameLoop::State::CURRENT;
return &game_loop._states.playing;
}
default: {}
if (cheat_console.is_state_change_requested()) {
return game_loop.get_game_loop_state_ptr(cheat_console.get_requested_state());
}

return this;
Expand Down
21 changes: 2 additions & 19 deletions src/game-loop/src/game-loop/GameLoopSandboxState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,26 +83,9 @@ GameLoopBaseState *GameLoopSandboxState::update(GameLoop& game_loop, uint32_t de
return &game_loop._states.main_menu;
}


auto& cheat_console = registry.get<prefabs::CheatConsoleComponent>(_cheat_console);
switch (cheat_console.state)
{
case GameLoop::State::SCORES:
{
cheat_console.state = GameLoop::State::CURRENT;
return &game_loop._states.scores;
}
case GameLoop::State::MAIN_MENU:
{
cheat_console.state = GameLoop::State::CURRENT;
return &game_loop._states.main_menu;
}
case GameLoop::State::PLAYING:
{
cheat_console.state = GameLoop::State::CURRENT;
return &game_loop._states.playing;
}
default: {}
if (cheat_console.is_state_change_requested()) {
return game_loop.get_game_loop_state_ptr(cheat_console.get_requested_state());
}

return this;
Expand Down
21 changes: 2 additions & 19 deletions src/game-loop/src/game-loop/GameLoopScoresState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,26 +75,9 @@ GameLoopBaseState *GameLoopScoresState::update(GameLoop& game_loop, uint32_t del
return &game_loop._states.main_menu;
}


auto& cheat_console = registry.get<prefabs::CheatConsoleComponent>(_cheat_console);
switch (cheat_console.state)
{
case GameLoop::State::SCORES:
{
cheat_console.state = GameLoop::State::CURRENT;
return &game_loop._states.scores;
}
case GameLoop::State::MAIN_MENU:
{
cheat_console.state = GameLoop::State::CURRENT;
return &game_loop._states.main_menu;
}
case GameLoop::State::PLAYING:
{
cheat_console.state = GameLoop::State::CURRENT;
return &game_loop._states.playing;
}
default: {}
if (cheat_console.is_state_change_requested()) {
return game_loop.get_game_loop_state_ptr(cheat_console.get_requested_state());
}

return this;
Expand Down
8 changes: 4 additions & 4 deletions src/game-loop/src/prefabs/ui/CheatConsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,22 +237,22 @@ namespace {
if (upper_cased == "ENTER SCORES") {
auto& registry = EntityRegistry::instance().get_registry();
auto& cheat_console_component = registry.get<prefabs::CheatConsoleComponent>(_self);
cheat_console_component.state = GameLoop::State::SCORES;
cheat_console_component.request_state_change(GameLoop::State::SCORES);
return std::make_pair(true, "entering scores");
} else if (upper_cased == "ENTER MAIN_MENU") {
auto& registry = EntityRegistry::instance().get_registry();
auto& cheat_console_component = registry.get<prefabs::CheatConsoleComponent>(_self);
cheat_console_component.state = GameLoop::State::MAIN_MENU;
cheat_console_component.request_state_change(GameLoop::State::MAIN_MENU);
return std::make_pair(true, "entering main_menu");
} else if (upper_cased == "ENTER SANDBOX") {
auto& registry = EntityRegistry::instance().get_registry();
auto& cheat_console_component = registry.get<prefabs::CheatConsoleComponent>(_self);
cheat_console_component.state = GameLoop::State::SANDBOX;
cheat_console_component.request_state_change(GameLoop::State::SANDBOX);
return std::make_pair(true, "entering sanbox");
} else if (upper_cased == "ENTER PLAYING") {
auto& registry = EntityRegistry::instance().get_registry();
auto& cheat_console_component = registry.get<prefabs::CheatConsoleComponent>(_self);
cheat_console_component.state = GameLoop::State::PLAYING;
cheat_console_component.request_state_change(GameLoop::State::PLAYING);
return std::make_pair(true, "entering playing");
} else {
return std::make_pair(false, "");
Expand Down

0 comments on commit df85e5c

Please sign in to comment.