Game Development Reference
In-Depth Information
PlayState
would
then
iterate
through
@object_pool.objects
and
invoke
update
and
draw
methods.
Now, let's begin by implementing base class for
GameObject
:
05-refactor/entities/game_object.rb
1
class
GameObject
2
def
initialize
(object_pool)
3
@components
= []
4
@object_pool
=
object_pool
5
@object_pool
.
objects
<<
self
6
end
7
8
def
components
9
@components
10
end
11
12
def
update
13
@components
.
map(
&
:update
)
14
end
15
16
def
draw
(viewport)
17
@components
.
each {
|
c
|
c
.
draw(viewport) }
18
end
19
20
def
removable?
21
@removable
22
end
23
24
def
mark_for_removal
25
@removable
=
true
26
end
27
28
protected
29
30
def
object_pool
31
@object_pool
32
end
33
end
When
GameObject
is initialized, it registers itself with
ObjectPool
and prepares
empty
@components
array.
Concrete
GameObject
classes
should
initialize
Components
so that array would not be empty.