Game Development Reference
In-Depth Information
# ...
end
It's probably not the most elegant solution you could come up with, but
can_move_to?
temporarily changes
Tank
location to make a collision test, and then reverts old
coordinates just before returning the result. Now our tanks stop with banging sound when
they hit each other.
Tanks colliding
Catching Bullets
Right now bullets fly right through our tanks, and we want them to collide. It's a pretty
simple change, which mostly affects
BulletPhysics
class:
# 06-physics/entities/components/bullet_physics.rb
class
BulletPhysics
<
Component
# ...
def
update
# ...
check_hit
object
.
explode
if
arrived?
end
# ...
private
def
check_hit
@object_pool
.
nearby(object,
50
)
.
each
do
|
obj
|
nextif
obj
==
object
.
source
# Don't hit source tank
if
Utils
.
point_in_poly(x, y,
*
obj
.
box)
object
.
target_x
=
x
object
.
target_y
=
y
return
end