/* # _____ ____ ___ # | \/ ____| |___| # | | | \ | | #----------------------------------------------------------------------- # Copyright 2022, tyra - https://github.com/h4570/tyra # Licensed under Apache License 2.0 # Sandro Sobczyński */ #pragma once #include #include "./state.hpp" #include "./global_state_type.hpp" namespace Demo { template class StateManager { public: StateManager(const StateTypeT& t_initialState, const StateTypeT& t_exitState) { stateInitialized = false; currentState = t_initialState; exitState = t_exitState; } ~StateManager() { freeAll(); } const StateTypeT& getCurrentState() const { return currentState; } const StateTypeT& getExitState() const { return exitState; } bool finished() const { return currentState == exitState; } std::vector*>* getAll() { return &states; } State* get(const StateTypeT& stateType) { for (auto& state : states) { if (state->getState() == stateType) { return state; } } return nullptr; } void add(State* state) { states.push_back(state); } /** remove (without free) */ void remove(const State* state) { states.erase(std::remove(states.begin(), states.end(), state), states.end()); } /** free and remove */ void free(const State* state) { auto* found = get(state); if (!found) return; remove(found); delete found; } /** free and remove all */ void freeAll() { for (auto& state : states) { delete state; } states.clear(); } void update() { for (auto& state : states) { if (state->getState() == currentState) { if (state->wantFinish()) { currentState = state->onFinish(); stateInitialized = false; break; } if (!stateInitialized) { state->onStart(); stateInitialized = true; } state->update(); break; } } } private: bool stateInitialized; std::vector*> states; StateTypeT currentState, exitState; }; } // namespace Demo