Game Development Reference
In-Depth Information
3. Calls
TurnTurretTowardTarget()
with
m_TargetObj
's position, in order to turn
the tank's turret toward that target, if there is no location in
m_Target
and
m_TargetObj
is not null.
4. If
m_FireWeapon
is true, then either of the following occurs:
a.
m_FireWeapon
resets to false, if the number of rounds to fire for this burst is equal to
the number of rounds that are required to be fired.
b. The weapon is fired by calling
FireTurretWeapon()
, if the required time delay has
passed since the
m_FireWeapon
was set to true.
The
CheckTransitions()
function checks for a transition to a different state, based on the game
conditions. If the tank is within
m_WayPointRadius
distance of the target waypoint, the current
waypoint is saved in the
m_LastWayPoint
variable and the state returned by the function is the
process command state. If the tank is not within
m_WayPointRadius
distance of the target waypoint,
there is no change in state, and the patrol/attack state is returned. (See Listing 8-38.)
Listing 8-38. Checking the Transitions
FSM_StatesTank CheckTransitions()
{
Object3d AIVehicle = GetParent().GetAIVehicle().GetMainBody();
Vector3 VehiclePos = AIVehicle.m_Orientation.GetPosition();
Vector3 Distance = Vector3.Subtract(VehiclePos,m_WayPoint);
float D = Distance.Length();
if (D <= m_WayPointRadius)
{
m_LastWayPoint.Set(m_WayPoint.x, m_WayPoint.y, m_WayPoint.z);
return FSM_StatesTank.FSM_STATE_PROCESS_COMMAND;
}
else
{
return FSM_StatesTank.FSM_STATE_STEER_WAYPOINT;
}
}
Creating the Tank Finite State Machine
The Finite State Machine class for the tank is the FSMDriver class. The finite state machine actually
executes the tank AI by calling the appropriate functions in each of the tank states.
The way the FSMDriver class is used is that you
Add new tank states with the
AddState()
function.
1.
Set the default state that the machine starts in with the
SetDefaultState()
command.
2.