Game Development Reference
In-Depth Information
Implementing Play State
Before we start implementing actual gameplay, we need to think what game entities we
will be building. We will need a
Map
that will hold our tiles and provide world coordinate
system. We will also need a
Camera
that will know how to float around and zoom. There
will be
Bullet
s flying around, and each bullet will eventually cause an
Explosion
.
Having all that taken care of,
PlayState
should look pretty simple:
03-prototype/states/play_state.rb
1
require_relative
'../entities/map'
2
require_relative
'../entities/tank'
3
require_relative
'../entities/camera'
4
require_relative
'../entities/bullet'
5
require_relative
'../entities/explosion'
6
class
PlayState
<
GameState
7
8
def
initialize
9
@map
=
Map
.
new
10
@tank
=
Tank
.
new(
@map
)
11
@camera
=
Camera
.
new(
@tank
)
12
@bullets
= []
13
@explosions
= []
14
end
15
16
def
update
17
bullet
=
@tank
.
update(
@camera
)
@bullets
<<
bullet
if
bullet
18
@bullets
.
map(
&
:update
)
19
@bullets
.
reject!(
&
:done?
)
20
@camera
.
update
21
$window
.
caption
=
'Tanks Prototype. '
<<
22
"[FPS:
#{
Gosu
.
fps
}
. Tank @
#{
@tank
.
x
.
round
}
:
#{
@tank
.
y
.
round
}
]"
23
24
end
25
26
def
draw
27
cam_x
=
@camera
.
x
cam_y
=
@camera
.
y
28
off_x
=
$window
.
width
/ 2 -
cam_x
29
off_y
=
$window
.
height
/ 2 -
cam_y
30
$window
.
translate(off_x, off_y)
do
31
zoom
=
@camera
.
zoom
32
$window
.
scale(zoom, zoom, cam_x, cam_y)
do
33
@map
.
draw(
@camera
)
34
@tank
.
draw
35