Game Development Reference
In-Depth Information
Make sure you spawn some AI controlled tanks in
PlayState
and try killing them now.
I bet they will eventually get you even while standing still. You can also make tanks spawn
below mouse cursor when you press
T
key:
class
PlayState
<
GameState
# ...
def
initialize
# ...
10.
times
do
|
i
|
Tank
.
new(
@object_pool
,
AiInput
.
new(
@object_pool
))
end
end
# ...
def
button_down
(
id
)
# ...
if
id
==
Gosu
::
KbT
t
=
Tank
.
new(
@object_pool
,
AiInput
.
new(
@object_pool
))
t
.
x, t
.
y
=
@camera
.
mouse_coords
end
# ...
end
# ...
end
Implementing Tank Motion States
This is the place where we will need Finite State Machine to get things right. We will
design it like this:
1.
TankMotionFSM
will decide which motion state tank should be in, considering
various parameters, e.g. existence of target or lack thereof, health, etc.
2.
There will be
TankMotionState
base class that will offer common methods
like
drive
,
wait
and
on_collision
.
3.
Concrete motion classes will implement
update
,
change_direction
and
other methods, that will fiddle with
Tank#throttle_down
and
Tank#direction
to make it move and turn.
We will begin with
TankMotionState
:
08-ai/entities/components/ai/tank_motion_state.rb
1
class
TankMotionState
2
def
initialize
(object, vision)