Advertisement
Guest User

state_manager

a guest
Jul 16th, 2016
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. #pragma once
  2. #include <SFML\Graphics.hpp>
  3. #include "BaseState.h"
  4. #include "SharedContext.h"
  5.  
  6. using StateContainer = std::vector<std::pair<StateType, BaseState*>>;
  7. using TypeContainer  = std::vector<StateType>;
  8. using StateFactory   = std::unordered_map<StateType, std::function<BaseState*(void)>>;
  9.  
  10. class StateManager
  11. {
  12. public:
  13.     StateManager(SharedContext* context);
  14.     ~StateManager();
  15.  
  16.     void Update(const sf::Time& time);
  17.     void Draw();
  18.  
  19.     void ProcessRequests();
  20.  
  21.     SharedContext* GetContext();
  22.     bool HasState(const StateType& type);
  23.  
  24.     void SwitchTo(const StateType& type);
  25.     void Remove(const StateType type);
  26.  
  27. private:
  28.     void CreateState(const StateType& type);
  29.     void RemoveState(const StateType& type);
  30.  
  31.     template<class T>
  32.     void RegisterState(const StateType& type)
  33.     {
  34.         m_stateFactory[type] = [this]() -> BaseState*
  35.         {
  36.             return new T(this);
  37.         }
  38.     };
  39.  
  40.     SharedContext* m_context;
  41.     StateContainer m_states;
  42.     TypeContainer m_toRemove;
  43.     StateFactory m_stateFactory;
  44. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement