Game Development Reference
In-Depth Information
Seeking Health Powerups After Heavy Damage
To
seek
health
when
damaged,
we
need
to
change
TankFleeingState#change_direction
:
class
TankFleeingState
<
TankMotionState
# ...
def
change_direction
closest_powerup
=
@vision
.
closest_powerup(
RepairPowerup
,
HealthPowerup
)
if
closest_powerup
angle
=
Utils
.
angle_between(
@object
.
x,
@object
.
y,
closest_powerup
.
x, closest_powerup
.
y)
@object
.
physics
.
change_direction(
angle
-
angle
% 45
)
else
# ... reverse from enemy
end
@changed_direction_at
=
Gosu
.
milliseconds
@will_keep_direction_for
=
turn_time
end
# ...
end
This small change tells AI to pick up health while fleeing. The interesting part is that when
tank picks up
RepairPowerup
, it's health gets fully restored and AI should switch back
to
TankFightingState
. This simple thing is a major improvement in AI behavior.
Evading Collisions And Getting Unstuck
While observing AI navigation, it was noticeable that tanks often got stuck, even in
simple situations, like driving into a tree and hitting it repeatedly for a dozen of seconds.
To reduce the number of such occasions, we will introduce
TankNavigatingState
,
which would help avoid collisions, and
TankStuckState
, which would be responsible
for driving out of dead ends as quickly as possible.
To implement these states, we need to have a way to tell if tank can go forward and a way
of getting a direction which is not blocked by other objects. Let's add a couple of methods
to
AiVision
:
class
AiVision
# ...