GameStateManager.cxx 829 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. }
  23. }
  24. void GameStateManager::push_state(GameStates const new_state)
  25. {
  26. states_.push(new_state);
  27. current()->on_enter();
  28. }
  29. void GameStateManager::pop_state()
  30. {
  31. auto const state = current();
  32. if (state!=nullptr) {
  33. state->on_leave();
  34. states_.pop();
  35. }
  36. }
  37. void GameStateManager::replace_state(GameStates const new_state)
  38. {
  39. pop_state();
  40. push_state(new_state);
  41. }