Game Development Reference
In-Depth Information
distance
=
Utils
.
distance_between(
obj
.
x, obj
.
y, object
.
x, object
.
y)
obj
!=
object
&&
distance
<
max_distance
end
end
end
Back to
TankPhysics
:
class
TankPhysics
<
Component
# ...
def
can_move_to?
(x, y)
old_x, old_y
=
object
.
x, object
.
y
object
.
x
=
x
object
.
y
=
y
return
false
unless
@map
.
can_move_to?(x, y)
@object_pool
.
nearby(object,
100
)
.
each
do
|
obj
|
if
collides_with_poly?(obj
.
box)
# Allow to get unstuck
old_distance
=
Utils
.
distance_between(
obj
.
x, obj
.
y, old_x, old_y)
new_distance
=
Utils
.
distance_between(
obj
.
x, obj
.
y, x, y)
return
false
if
new_distance
<
old_distance
end
end
true
ensure
object
.
x
=
old_x
object
.
y
=
old_y
end
# ...
private
def
collides_with_poly?
(poly)
if
poly
poly
.
each_slice(
2
)
do
|
x, y
|
return
true
if
Utils
.
point_in_poly(x, y,
*
box)
end
box
.
each_slice(
2
)
do
|
x, y
|
return
true
if
Utils
.
point_in_poly(x, y,
*
poly)
end
end
false
end