Game Development Reference
In-Depth Information
Let's examine the main idea behind this gem. Here is a slightly simplified version that does
handles everything in under 20 lines of code:
02-warmup/tileset.rb
1
require
'json'
2
class
Tileset
3
def
initialize
(window, json)
4
@json
=
JSON
.
parse(
File
.
read(json))
image_file
=
File
.
join(
5
File
.
dirname(json),
@json
[
'meta'
][
'image'
]
)
6
@main_image
=
Gosu
::
Image
.
new(
7
@window
, image_file,
true
)
8
9
end
10
11
def
frame
(
name
)
12
f
=
@json
[
'frames'
][
name
][
'frame'
]
@main_image
.
subimage(
13
f
[
'x'
]
, f
[
'y'
]
, f
[
'w'
]
, f
[
'h'
]
)
14
15
end
16
end
If by now you are familiar with
Gosu documentation
, you will wonder what the hell
accidentally
discovered it
while digging through Gosu source code.
I'm lucky this function existed, because I was ready to bring out the heavy artillery and use
RMagick
to extract those tiles. We will probably need RMagick at some point of time later,
but it's better to avoid dependencies as long as possible.
Combining Tiles Into A Map
With tileset loading issue out of the way, we can finally get back to drawing that cool map
of ours.
The following program will fill the screen with random tiles.
02-warmup/random_map.rb
1
require
'gosu'
2
require
'gosu_texture_packer'
3
4
def
media_path
(file)
5
File
.
join(
File
.
dirname(
File
.
dirname(
__FILE__
)),
'media'
, file)
6