Game Development Reference
In-Depth Information
The
Update()
function updates the tank's artificial intelligence for the patrol/attack state.
(See Listing 8-37.)
Listing 8-37. Updating the Tank Patrol/Attack State
void Update()
{
// Steer Main Tank Body to Waypoint
SteerVehicleToWayPoint(m_WayPoint);
// Turn Tank Turret towards target and fire
if (m_Target != null)
{
TurnTurretTowardTarget(m_Target);
}
else
if (m_TargetObj != null)
{
TurnTurretTowardTarget(m_TargetObj.m_Orientation.GetPosition());
}
else
{
Log.e("STATETANKSTEERWAYPOINT" , "NO TARGET FOR TANK TO SHOOT AT!!!!");
}
if (m_FireWeapon)
{
if (m_NumberRoundsFired >= m_RoundsToFire)
{
m_NumberRoundsFired = 0;
m_FireWeapon = false;
}
else
{
// Find Time Elapsed Between firing sequences
long ElapsedTime = System.currentTimeMillis() - m_StartTimeFiring;
if (ElapsedTime > m_TimeIntervalBetweenFiring)
{
FireTurretWeapon();
}
}
}
}
The
Update()
function does the following:
1.
Sets the steering and acceleration for the tank by calling
SteerVehicleToWayPoint()
with the target waypoint.
Calls
TurnTurretTowardTarget()
with the target location, which turns the
tank's turret toward the target, if a target location is in
m_Target
.
2.