Game Development Reference
In-Depth Information
Listing 8-39. Resetting the Finite State Machine
void Reset()
{
if(m_CurrentState != null)
{
m_CurrentState.Exit();
}
m_CurrentState = m_DefaultState;
for(int i = 0;i < m_NumberStates;i++)
{
m_States[i].Init();
}
if(m_CurrentState != null)
{
m_CurrentState.Enter();
}
}
The AddState() function adds a tank state State to the finite state machine. The function first checks
to see if there is room for more states and, if there is, adds them to the m_States array, increases the
number of states in the machine, and returns true. False is returned otherwise. (See Listing 8-40.)
Listing 8-40. Adding a State to the Finite State Machine
boolean AddState(StateTank State)
{
boolean result = false;
if (m_NumberStates < MAX_STATES)
{
m_States[m_NumberStates] = State;
m_NumberStates++;
result = true;
}
return result;
}
The TransitionState() function searches through all the states in the machine and tries to match
the Goal input state id FSM_StatesTank enumeration with the id from each of the states. If a match is
found, then m_GoalState is set to that state and true is returned. Otherwise, false is returned.
(See Listing 8-41.)
Listing 8-41. Transitioning Between States
boolean TransitionState(FSM_StatesTank Goal)
{
if(m_NumberStates == 0)
{
return false;
}
 
Search WWH ::




Custom Search