Game Development Reference
In-Depth Information
33
end
34
end
It registers itself with
GameObject#components
, provides some protected methods to
access parent object and it's most often called properties -
x
and
y
.
Refactoring Explosion
Explosion
was probably the smallest class, so we will extract it's components first.
05-refactor/entities/explosion.rb
1
class
Explosion
<
GameObject
2
attr_accessor
:x
,
:y
3
4
def
initialize
(object_pool, x, y)
5
super
(object_pool)
6
@x
,
@y
=
x, y
7
ExplosionGraphics
.
new(
self
)
8
ExplosionSounds
.
play
9
end
10
end
It is much cleaner than before.
ExplosionGraphics
will be a
Component
that
handles animation, and
ExplosionSounds
will play a sound.
05-refactor/entities/components/explosion_graphics.rb
1
class
ExplosionGraphics
<
Component
2
FRAME_DELAY
= 16.66
# ms
3
4
def
initialize
(game_object)
5
super
6
@current_frame
= 0
7
end
8
9
def
draw
(viewport)
10
image
=
current_frame
image
.
draw(
11
x
-
image
.
width
/ 2 + 3
,
12
y
-
image
.
height
/ 2 - 35
,
13
20
)
14
15
end
16
17
def
update