Game Development Reference
In-Depth Information
5.
Gun will have a “desired” angle that it will be automatically adjusting to, according
to it's speed.
Here is the implementation:
08-ai/entities/components/ai/gun.rb
1
class
AiGun
2
DECISION_DELAY
= 1000
attr_reader
:target
,
:desired_gun_angle
3
4
5
def
initialize
(object, vision)
6
@object
=
object
7
@vision
=
vision
8
@desired_gun_angle
=
rand
(
0.
.
360
)
9
@retarget_speed
=
rand
(
1.
.
5
)
10
@accuracy
=
rand
(
0.
.
10
)
11
@aggressiveness
=
rand
(
1.
.
5
)
12
end
13
14
def
adjust_angle
15
adjust_desired_angle
16
adjust_gun_angle
17
end
18
19
def
update
20
if
@vision
.
in_sight
.
any?
21
if
@vision
.
closest_tank
!=
@target
22
change_target(
@vision
.
closest_tank)
23
end
24
else
25
@target
=
nil
26
end
27
28
if
@target
29
if
(
0.
.
10 -
rand
(
0.
.
@accuracy
))
.
include?(
30
(
@desired_gun_angle
-
@object
.
gun_angle)
.
abs
.
round)
31
distance
=
distance_to_target
32
if
distance
- 50 <=
BulletPhysics
::
MAX_DIST
33
target_x, target_y
=
Utils
.
point_at_distance(
34
@object
.
x,
@object
.
y,
@object
.
gun_angle,
35
distance
+ 10 -
rand
(
0.
.
@accuracy
))
36
if
can_make_new_decision?
&&
@object
.
can_shoot?
&&
37
should_shoot?
38
@object
.
shoot(target_x, target_y)
39
end
40
end