Game Development Reference
In-Depth Information
Inflicting Damage With Bullets
There are two ways to inflict damage - directly and indirectly. When bullet hits enemy
tank (collides with tank bounding box), we should inflict direct damage. It can be done in
BulletPhysics#check_hit method that we already had:
class BulletPhysics < Component
# ...
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)
# Direct hit - extra damage
obj . health . inflict_damage( 20 )
object . target_x = x
object . target_y = y
return
end
end
end
# ...
end
Finally, Explosion itself should inflict additional damage to anything that are nearby.
The effect will be diminishing and it will be determined by object distance.
class Explosion < GameObject
# ...
def initialize (object_pool, x, y)
# ...
inflict_damage
end
private
def inflict_damage
object_pool . nearby( self , 100 ) . each do | obj |
if obj . class == Tank
obj . health . inflict_damage(
Math . sqrt( 3 * 100 - Utils . distance_between(
obj . x, obj . y, x, y)))
end
end
end
end
Search WWH ::




Custom Search