Aspen
2D C++ Class-Based Object Oriented Game Engine
GameState.hpp
1 #ifndef __GAMESTATE_HPP
2 #define __GAMESTATE_HPP
3 
4 #include "Object.hpp"
5 
7 namespace Aspen
8 {
10 namespace GameState
11 {
14 class GameState : public Object::Object
15 {
17  std::string _state;
18 
19 public:
24  GameState(Object *parent = nullptr, std::string name = "GameState");
31  GameState(std::string state, Object *parent = nullptr, std::string name = "GameState");
32 
35  const std::string StateName();
38  void StateName(std::string state);
39 };
40 
43 {
44 public:
50  GameStateManager(Object *parent = nullptr, std::string name = "GameStateManager");
51 
57  template <typename T>
58  GameState *LoadState(bool active = false)
59  {
60  T *temp = new T(this);
61  GameState *state = dynamic_cast<GameState *>(temp);
62  if (state)
63  {
64  AddChild(state);
65  state->SetActive(active);
66  }
67  else
68  if (temp)
69  delete temp;
70  return state;
71  }
72 
76  GameState *GetState(unsigned i);
80  GameState *GetState(std::string name);
81 
83  void UnloadAllStates();
86  void UnloadState(unsigned i);
89  void UnloadState(GameState *state);
92  void UnloadState(std::string name);
93 
97  bool SetCurrentState(unsigned i);
101  bool SetCurrentState(GameState *state);
105  bool SetCurrentState(std::string name);
106 
109  bool ActivateState(unsigned i);
112  bool ActivateState(GameState *state);
115  bool ActivateState(std::string name);
116 
119  bool DeactivateState(unsigned i);
122  bool DeactivateState(GameState *state);
125  bool DeactivateState(std::string name);
126 };
127 } // namespace GameState
128 } // namespace Aspen
129 
130 #endif
void SetActive(bool active)
Sets active status of the object Triggers OnActivate or OnDeactivate appropriately.
GameStateManager(Object *parent=nullptr, std::string name="GameStateManager")
Constructor Derived classes should call this in their constructors' initialization list.
Object(Object *parent=nullptr, std::string name="Object")
Constructor Derived classes should call this in their constructors' initialization list.
GameState base class Inherit this to create your own states.
Definition: GameState.hpp:14
General base class Allows for parent/child relationship trees.
Definition: Object.hpp:33
void AddChild(Object *child)
Adds child to this Object's list of children This will also set child's _parent to this This will do ...
void UnloadAllStates()
Unloads all child states.
GameState(Object *parent=nullptr, std::string name="GameState")
Constructor.
bool ActivateState(unsigned i)
Activates the current state to the one at the given index.
GameStateManager class.
Definition: GameState.hpp:42
const std::string StateName()
Gets the state's name.
GameState * GetState(unsigned i)
Gets the state at the given index.
std::string _state
State name.
Definition: GameState.hpp:17
GameState * LoadState(bool active=false)
Loads the given GameState derived class as a new state.
Definition: GameState.hpp:58
Aspen engine namespace.
Definition: Audio.hpp:8
bool SetCurrentState(unsigned i)
Sets the current state to the one at the given index Deactivates all other states.
bool DeactivateState(unsigned i)
Deactivates the current state to the one at the given index.
void UnloadState(unsigned i)
Unloads the state with the given index.