Game Development Reference
In-Depth Information
21
else
22
text
=
@health
.
to_s
23
font_size
= 18
24
end
25
@image
=
Gosu
::
Image
.
from_text(
26
$window
, text,
27
Gosu
.
default_font_name, font_size)
28
@health_updated
=
false
29
end
30
end
31
32
def
dead?
33
@health
< 1
34
end
35
36
def
inflict_damage
(amount)
37
if
@health
> 0
38
@health_updated
=
true
39
@health
= [
@health
-
amount
.
to_i,
0].
max
40
if
@health
< 1
41
Explosion
.
new(
@object_pool
, x, y)
42
end
43
end
44
end
45
46
def
draw
(viewport)
47
@image
.
draw(
x
-
@image
.
width
/ 2
,
48
y
-
object
.
graphics
.
height
/ 2 -
49
@image
.
height,
100
)
50
51
end
52
end
It hooks itself into the game right away, after we initialize it in
Tank
class:
class
Tank
<
GameObject
attr_accessor
:health
# ...
def
initialize
(object_pool, input)
# ...
@health
=
TankHealth
.
new(
self
, object_pool)
# ..
end
# ..
end