GameStateManager.cxx 877 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include "GameStateManager.hxx"
  2. GameStateManager::GameStateManager()
  3. :states_{{GameStates::Loading}}
  4. {
  5. loading_.on_enter();
  6. }
  7. GameStateManager::~GameStateManager()
  8. {
  9. while (!states_.empty()) {
  10. pop_state();
  11. }
  12. }
  13. GameState* GameStateManager::current()
  14. {
  15. if (states_.empty())
  16. return nullptr;
  17. switch (states_.top()) {
  18. default:
  19. return nullptr; // TODO: handle all game states
  20. case GameStates::Loading:
  21. return &loading_;
  22. case GameStates::Splash:
  23. return &splash_;
  24. }
  25. }
  26. void GameStateManager::push_state(GameStates const new_state)
  27. {
  28. states_.push(new_state);
  29. current()->on_enter();
  30. }
  31. void GameStateManager::pop_state()
  32. {
  33. auto const state = current();
  34. if (state!=nullptr) {
  35. state->on_leave();
  36. states_.pop();
  37. }
  38. }
  39. void GameStateManager::replace_state(GameStates const new_state)
  40. {
  41. pop_state();
  42. push_state(new_state);
  43. }