Game Development Reference
In-Depth Information
end
@camera
.
draw_crosshair
@radar
.
draw
end
# ...
end
Moving Objects In QuadTree
There is one more errand we now have to take care of. Everything works fine when things
are static, but when tanks and bullets move, we need to update them in our
QuadTree
.
That's why
ObjectPool
has
tree_remove
and
tree_insert
, which are called
from
GameObject#move
. From now on, the only way to change object's location will
be by using
GameObject#move
:
class
GameObject
attr_reader
:x
,
:y
,
:location
,
:components
def
initialize
(object_pool, x, y)
@x
,
@y
=
x, y
@location
= [
x, y
]
@components
= []
@object_pool
=
object_pool
@object_pool
.
add(
self
)
end
def
move
(new_x, new_y)
returnif
new_x
==
@x
&&
new_y
==
@y
@object_pool
.
tree_remove(
self
)
@x
=
new_x
@y
=
new_y
@location
= [
new_x, new_y
]
@object_pool
.
tree_insert(
self
)
end
# ...
end
At this point we have to go through all the game objects and change how they initialize
their base class and update
x
and
y
coordinates, but we won't cover that here. If in doubt,
refer to code at
10-partitioning
.
Finally, FPS is back to stable 60 and we can focus on gameplay again.