-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathGameStateMachine.cpp
76 lines (68 loc) · 1.67 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "GameStateMachine.h"
//add a game state without removing previous state
void GameStateMachine::pushState(GameState* pState)
{
//push the state in the vector
m_gameStates.push_back(pState);
//call its onEnter function
m_gameStates.back()->onEnter();
}
//remove a game state without adding another
void GameStateMachine::popState()
{
//check if a state is present
if (!m_gameStates.empty())
{
//call its onExit function
if (m_gameStates.back()->onExit())
{
//remove it
delete m_gameStates.back();
m_gameStates.pop_back();
}
}
}
//add a state after removing previous state
void GameStateMachine::changeState(GameState* pState)
{
//check if a state is present
if (!m_gameStates.empty())
{
//commenting out this part to modify the FSM, trying to fix pointer error
//UPDATE : 07-Mar-2018 (This part was creating read access violation error for other states)
//FIX : The states are removed and re-added even if they are already present in the FSM
/*
//if it already exits don't do anything
if (m_gameStates.back()->getStateID() == pState->getStateID())
{
return; //do nothing
}
*/
//otherwise remove the previous state
if (m_gameStates.back()->onExit())
{
delete m_gameStates.back();
m_gameStates.pop_back();
}
}
//push back our new state
m_gameStates.push_back(pState);
//initialise it by calling its onEnter function
m_gameStates.back()->onEnter();
}
//function to update the current state
void GameStateMachine::update()
{
if (!m_gameStates.empty())
{
m_gameStates.back()->update();
}
}
//function to render the state
void GameStateMachine::render()
{
if (!m_gameStates.empty())
{
m_gameStates.back()->render();
}
}