Game Development Reference
In-Depth Information
1
class
Box
<
GameObject
2
attr_reader
:x
,
:y
,
:health
,
:graphics
,
:angle
3
4
def
initialize
(object_pool, x, y)
5
super
(object_pool)
6
@x
,
@y
=
x, y
7
@graphics
=
BoxGraphics
.
new(
self
)
8
@health
=
Health
.
new(
self
, object_pool,
10
,
true
)
9
@angle
=
rand
(
-15.
.
15
)
10
end
11
12
def
on_collision
(object)
13
returnunless
object
.
physics
.
speed
> 1.0
14
@x
,
@y
=
Utils
.
point_at_distance(
@x
,
@y
, object
.
direction,
2
)
15
@box
=
nil
16
end
17
18
def
box
19
return
@box
if
@box
20
w
=
@graphics
.
width
/ 2
21
h
=
@graphics
.
height
/ 2
22
# Bounding box adjusted to trim shadows
23
@box
= [
x
-
w
+ 4
, y
-
h
+ 8
,
24
x
+
w , y
-
h
+ 8
,
25
x
+
w , y
+
h,
26
x
-
w
+ 4
, y
+
h
]
27
@box
=
Utils
.
rotate(
@angle
,
@x
,
@y
,
*
@box
)
28
end
29
end
It will be generated with slight random angle, to preserve realistic shadows but give an
impression of chaotic placement. Tanks will also be able to push boxes a little on collision,
but only when going fast enough.
Health
component is the same one that
Tree
has, but
initialized with less health and explosive flag is
true
, so the box will blow up after one
hit and deal extra damage to the surroundings.
BoxGraphics
is nothing fancy, it just loads random sprite upon initialization:
09-polishing/entities/components/box_graphics.rb
1
class
BoxGraphics
<
Component
2
def
initialize
(object)
3
super
(object)
4
load_sprite
5
end