Game Development Reference
In-Depth Information
38 tile . draw(map_x, map_y, 0 )
39 end
40 end
41 end
42 end
43
44
private
45
46 def tile_at (x, y)
47 t_x = ((x / TILE_SIZE ) % TILE_SIZE ) . floor
48 t_y = ((y / TILE_SIZE ) % TILE_SIZE ) . floor
49 row = @map [ t_x ]
50 row [ t_y ] if row
51 end
52
53 def load_tiles
54
tiles = Gosu :: Image . load_tiles(
$window , Game . media_path( 'ground.png' ),
55
128 , 128 , true )
56
@sand = tiles [0]
57
@grass = tiles [8]
58
@water = Gosu :: Image . new(
59
$window , Game . media_path( 'water.png' ), true )
60
61 end
62
63 def generate_map
64 noises = Perlin :: Noise . new( 2 )
65 contrast = Perlin :: Curve . contrast(
66 Perlin :: Curve :: CUBIC , 2 )
67 map = {}
68 MAP_WIDTH . times do | x |
69 map [ x ] = {}
70 MAP_HEIGHT . times do | y |
71 n = noises [ x * 0.1 , y * 0.1]
72 n = contrast . call(n)
73 map [ x ][ y ] = choose_tile(n)
74 end
75 end
76 map
77 end
78
79 def choose_tile (val)
80 case val
81 when 0.0. . 0.3 # 30% chance
82 @water
83 when 0.3. . 0.45 # 15% chance, water edges
Search WWH ::




Custom Search