Game Development Reference
In-Depth Information
8
if
trajectory_length
>
MAX_DIST
9
object
.
target_x, object
.
target_y
=
point_at_distance(
MAX_DIST
)
10
end
11
end
12
13
def
update
14
fly_speed
=
Utils
.
adjust_speed(object
.
speed)
15
fly_distance
=
(
Gosu
.
milliseconds
-
object
.
fired_at)
* 0.001 *
fly_speed
16
object
.
x, object
.
y
=
point_at_distance(fly_distance)
17
object
.
explode
if
arrived?
18
end
19
20
def
trajectory_length
21
d_x
=
object
.
target_x
-
x
22
d_y
=
object
.
target_y
-
y
23
Math
.
sqrt(d_x
*
d_x
+
d_y
*
d_y)
24
end
25
26
def
point_at_distance
(distance)
27
if
distance
>
trajectory_length
28
return
[
object
.
target_x, object
.
target_y
]
29
end
30
distance_factor
=
distance
.
to_f
/
trajectory_length
31
p_x
=
x
+
(object
.
target_x
-
x)
*
distance_factor
32
p_y
=
y
+
(object
.
target_y
-
y)
*
distance_factor
33
[
p_x, p_y
]
34
end
35
36
private
37
38
def
arrived?
39
x
==
object
.
target_x
&&
y
==
object
.
target_y
40
end
41
end
BulletPhysics
is where the most of
Bullet
ended up at. It does all the calculations
and triggers
Bullet#explode
when ready. When we will be implementing collision
detection, the implementation will go somewhere here.
05-refactor/entities/components/bullet_graphics.rb
1
class
BulletGraphics
<
Component
2
COLOR
=
Gosu
::
Color
::
BLACK
3
4
def
draw
(viewport)