Game Development Reference
In-Depth Information
@object
.
physics
.
change_direction(
15
@gun
.
desired_gun_angle
-
16
@gun
.
desired_gun_angle
% 45
)
17
18
19
@changed_direction_at
=
Gosu
.
milliseconds
20
@will_keep_direction_for
=
turn_time
21
end
22
23
def
drive_time
24
10000
25
end
26
27
def
turn_time
28
rand
(
300.
.
600
)
29
end
30
end
Tank Fleeing State
Now, if our health is low, we will do the opposite of chasing. Gun will be pointing and
shooting at the opponent, but we want body to move away, so we won't get ourselves
killed. It is very similar to
TankChasingState
where
change_direction
adds
extra 180 degrees to the equation, but there is one more thing. Tank can only flee for
a while. Then it gets itself together and goes into final battle. That's why we provide
can_flee?
method that
TankMotionFSM
will consult with before entering fleeing
state.
We have implemented all the states, that means we are moments away from actually
playable prototype with tank bots running around and fighting with you and each other.
Wiring Tank Motion States Into Finite State Machine
Implementing
TankMotionFSM
after we have all motion states ready is surprisingly
easy:
08-ai/entities/components/ai/tank_motion_fsm.rb
1
class
TankMotionFSM
2
STATE_CHANGE_DELAY
= 500
3
4
def
initialize
(object, vision, gun)
5
@object
=
object
@vision
=
vision
6
@gun
=
gun
7