Game Development Reference
In-Depth Information
How To Put It All Together
In our games we will want to separate game coordinates from viewport and screen as
much as possible. Basically, we will program ourselves a “camera man” who will be busy
following the action, zooming in and out, perhaps changing the view angle now and then.
Let's implement a prototype that will allow us to navigate and zoom around a big map. We
will only draw objects that are visible in viewport. Some math will be unavoidable, but in
most cases it's pretty basic - that's the beauty of 2D games:
02-warmup/coordinate_system.rb
1
require
'gosu'
2
3
class
WorldMap
4
attr_accessor
:on_screen
,
:off_screen
5
6
def
initialize
(width, height)
7
@images
=
{}
8
(
0.
.width)
.
step(
50
)
do
|
x
|
9
@images
[
x
] =
{}
10
(
0.
.height)
.
step(
50
)
do
|
y
|
11
img
=
Gosu
::
Image
.
from_text(
12
$window
,
"
#{
x
}
:
#{
y
}
"
,
13
Gosu
.
default_font_name,
15
)
14
@images
[
x
][
y
] =
img
15
end
16
end
17
end
18
19
def
draw
(camera)
20
@on_screen
=
@off_screen
= 0
21
@images
.
each
do
|
x, row
|
22
row
.
each
do
|
y, val
|
23
if
camera
.
can_view?(x, y, val)
24
val
.
draw(x, y,
0
)
25
@on_screen
+= 1
26
else
27
@off_screen
+= 1
28
end
29
end
30
end
31
end
32
end
33
34
class
Camera