Game Development Reference
In-Depth Information
# ...
def
inflict_damage
(amount, cause)
if
@health
> 0
@health_updated
=
true
if
object
.
respond_to?(
:input
)
object
.
input
.
stats
.
add_damage(amount)
# Don't count damage to trees and boxes
if
cause
.
respond_to?(
:input
)
&&
cause
!=
object
cause
.
input
.
stats
.
add_damage_dealt(amount)
end
end
@health
= [
@health
-
amount
.
to_i,
0].
max
after_death(cause)
if
dead?
end
end
# ...
end
# 12-stats/entities/components/tank_health.rb
class
TankHealth
<
Health
# ...
def
after_death
(cause)
# ...
object
.
input
.
stats
.
add_death
kill
=
object
!=
cause ?
1
:
-1
cause
.
input
.
stats
.
add_kill(kill)
# ...
end
# ...
end
Tracking Damage From Chain Reactions
There is one more way to cause damage. When you shoot a tree, box or barrel, it explodes,
probably triggering a chain reaction of explosions around it. If those explosions kill
somebody, it would only be fair to account that kill for the tank that triggered this chain
reaction.
To solve this, simply pass the cause of death to the
Explosion
that gets triggered
afterwards.
# 12-stats/entities/components/health.rb
class
Health
<
Component
# ...
def
after_death
(cause)