-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameStateMachine.cpp
62 lines (53 loc) · 1.11 KB
/
GameStateMachine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "GameStateMachine.h"
void GameStateMachine::pushState(GameState* pState)
{
//push & enter the state
m_gameStates.push_back(pState);
m_gameStates.back()->onEnter();
}
void GameStateMachine::popState()
{
if(!(m_gameStates.empty()) &&
m_gameStates.back()->onExit())
{
//free the GameState point in vector.
delete m_gameStates.back();
//lose it from vector
m_gameStates.pop_back();
}
}
void GameStateMachine::changeState(GameState* pState)
{
if(!m_gameStates.empty())
{
//Trying to enter state x from state x?
if(m_gameStates.back()->getStateID() == pState->getStateID())
{
return ;// don't do anything, end function.
}
//if we can exit, pop it and free the pointer.
if(m_gameStates.back()->onExit())
{
delete m_gameStates.back();
m_gameStates.pop_back();
}
}
//push back our new state.
m_gameStates.push_back(pState);
//enter that state
m_gameStates.back()->onEnter();
}
void GameStateMachine::update()
{
if(!m_gameStates.empty())
{
m_gameStates.back()->update();
}
}
void GameStateMachine::render()
{
if(!m_gameStates.empty())
{
m_gameStates.back()->render();
}
}