Game Development Reference
In-Depth Information
empty
update
,
on_collision(with)
and
on_damage(amount)
methods for it
to work:
08-ai/entities/components/ai_input.rb
1
class
AiInput
<
Component
2
UPDATE_RATE
= 200
# ms
3
4
def
initialize
(object_pool)
5
@object_pool
=
object_pool
6
super
(
nil
)
7
@last_update
=
Gosu
.
milliseconds
8
end
9
10
def
control
(obj)
11
self
.
object
=
obj
12
@vision
=
AiVision
.
new(obj,
@object_pool
,
13
rand
(
700.
.
1200
))
14
@gun
=
AiGun
.
new(obj,
@vision
)
15
@motion
=
TankMotionFSM
.
new(obj,
@vision
,
@gun
)
16
end
17
18
def
on_collision
(with)
19
@motion
.
on_collision(with)
20
end
21
22
def
on_damage
(amount)
23
@motion
.
on_damage(amount)
24
end
25
26
def
update
27
returnif
object
.
health
.
dead?
28
@gun
.
adjust_angle
29
now
=
Gosu
.
milliseconds
30
returnif
now
-
@last_update
<
UPDATE_RATE
31
@last_update
=
now
32
@vision
.
update
33
@gun
.
update
34
@motion
.
update
35
end
36
end
It adjust gun angle all the time, but does updates at
UPDATE_RATE
to save CPU power.
AI is usually one of the most CPU intensive things in games, so it's a common practice to
execute it less often. Refreshing enemy brains 5 per second is enough to make them deadly.