Game Development Reference
In-Depth Information
If it's hard to understand how this works, get back to “Game Coordinate System” chapter
and let it sink in.
Implementing World Map
We will start analyzing game entities with
Map
.
03-prototype/entities/map.rb
1
require
'perlin_noise'
2
require
'gosu_texture_packer'
3
4
class
Map
5
MAP_WIDTH
= 100
MAP_HEIGHT
= 100
6
TILE_SIZE
= 128
7
8
9
def
initialize
10
load_tiles
11
@map
=
generate_map
12
end
13
14
def
find_spawn_point
15
while
true
16
x
=
rand
(
0.
.
MAP_WIDTH
*
TILE_SIZE
)
17
y
=
rand
(
0.
.
MAP_HEIGHT
*
TILE_SIZE
)
18
if
can_move_to?(x, y)
19
return
[
x, y
]
20
else
21
puts
"Invalid spawn point:
#{
[
x, y
]
}
"
22
end
23
end
24
end
25
26
def
can_move_to?
(x, y)
27
tile
=
tile_at(x, y)
28
tile
&&
tile
!=
@water
29
end
30
31
def
draw
(camera)
32
@map
.
each
do
|
x, row
|
33
row
.
each
do
|
y, val
|
34
tile
=
@map
[
x
][
y
]
35
map_x
=
x
*
TILE_SIZE
36
map_y
=
y
*
TILE_SIZE
37
if
camera
.
can_view?(map_x, map_y, tile)