Game Development Reference
In-Depth Information
65
66
def
point_at_distance
(distance)
67
return
[
@target_x
,
@target_y
]
if
distance
>
trajectory_length
68
distance_factor
=
distance
.
to_f
/
trajectory_length
69
p_x
=
@x
+
(
@target_x
-
@x
)
*
distance_factor
70
p_y
=
@y
+
(
@target_y
-
@y
)
*
distance_factor
71
[
p_x, p_y
]
72
end
73
end
Possibly the most interesting part of
Bullet
implementation is
point_at_distance
method. It returns coordinates of point that is between bullet source, which is point that
bullet was fired from, and it's target, which is the destination point. The returned point is
as far away from source point as
distance
tells it to.
After bullet has done flying, it explodes with fanfare. In our prototype
Explosion
is a
part of
Bullet
, because it's the only thing that triggers it. Therefore
Bullet
has two
stages of it's lifecycle. First it flies towards the target, then it's exploding. That brings us to
Explosion
:
03-prototype/entities/explosion.rb
1
class
Explosion
2
FRAME_DELAY
= 10
# ms
3
4
def
animation
5
@@animation
||=
Gosu
::
Image
.
load_tiles(
6
$window
,
Game
.
media_path(
'explosion.png'
),
128
,
128
,
false
)
7
8
end
9
10
def
sound
11
@@sound
||=
Gosu
::
Sample
.
new(
$window
,
Game
.
media_path(
'explosion.mp3'
))
12
13
end
14
15
def
initialize
(x, y)
16
sound
.
play
17
@x
,
@y
=
x, y
18
@current_frame
= 0
19
end
20
21
def
update
22
@current_frame
+= 1
if
frame_expired?