Game Development Reference
In-Depth Information
attr_reader
:stats
def
initialize
(
name
, object_pool)
# ...
@stats
=
Stats
.
new(
name
)
end
def
on_damage
(amount)
# ...
@stats
.
add_damage(amount)
end
end
That itch to extract a base class from
PlayerInput
and
AiInput
is getting stronger,
but we will have to resist the urge, for now.
Tracking Kills, Deaths and Damage
To begin tracking kills, we need to know whom does every bullet belong to.
Bullet
already has
source
attribute, which contains the tank that fired it, there will be no trouble
to find out who was the shooter when bullet gets a direct hit. But how about explosions?
Bullets that hit the ground nearby a tank deals indirect damage from the explosion.
Solution is simple, we need to pass the
source
of the
Bullet
to the
Explosion
when
it's being initialized.
class
Bullet
<
GameObject
# ...
def
explode
Explosion
.
new(object_pool,
@x
,
@y
,
@source
)
# ...
end
# ...
end
Making Damage Personal
Now that we have the source of every
Bullet
and
Explosion
they trigger, we can
start passing the cause of damage to
Health#inflict_damage
and incrementing the
appropriate stats.
# 12-stats/entities/components/health.rb
class
Health
<
Component