Class: Map

Inherits:
Object
  • Object
show all
Defined in:
lib/entities/map.rb

Constant Summary collapse

MAP_WIDTH =
30
MAP_HEIGHT =
30
TILE_SIZE =
128

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object_pool) ⇒ Map

Returns a new instance of Map.



14
15
16
17
18
19
20
21
22
# File 'lib/entities/map.rb', line 14

def initialize(object_pool)
  load_tiles
  @object_pool = object_pool
  object_pool.map = self
  @map = generate_map
  generate_trees
  generate_boxes
  generate_powerups
end

Class Method Details

.bounding_boxObject



6
7
8
9
10
11
12
# File 'lib/entities/map.rb', line 6

def self.bounding_box
  center = [MAP_WIDTH * TILE_SIZE / 2,
            MAP_HEIGHT * TILE_SIZE / 2]
  half_dimension = [MAP_WIDTH * TILE_SIZE,
                    MAP_HEIGHT * TILE_SIZE]
  AxisAlignedBoundingBox.new(center, half_dimension)
end

Instance Method Details

#can_move_to?(x, y) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
# File 'lib/entities/map.rb', line 35

def can_move_to?(x, y)
  tile = tile_at(x, y)
  tile && tile != @water
end

#draw(viewport) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/entities/map.rb', line 50

def draw(viewport)
  viewport = viewport.map { |p| p / TILE_SIZE }
  x0, x1, y0, y1 = viewport.map(&:to_i)
  (x0-1..x1).each do |x|
    (y0-1..y1).each do |y|
      row = @map[x]
      map_x = x * TILE_SIZE
      map_y = y * TILE_SIZE
      if row
        tile = @map[x][y]
        if tile
          tile.draw(map_x, map_y, 0)
        else
          @water.draw(map_x, map_y, 0)
        end
      else
        @water.draw(map_x, map_y, 0)
      end
    end
  end
end

#movement_penalty(x, y) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/entities/map.rb', line 40

def movement_penalty(x, y)
  tile = tile_at(x, y)
  case tile
  when @sand
    0.33
  else
    0
  end
end

#spawn_pointObject



31
32
33
# File 'lib/entities/map.rb', line 31

def spawn_point
  @spawn_points[(@spawn_points_pointer += 1) % @spawn_points.size]
end

#spawn_points(max) ⇒ Object



24
25
26
27
28
29
# File 'lib/entities/map.rb', line 24

def spawn_points(max)
  @spawn_points = (0..max).map do
    find_spawn_point
  end
  @spawn_points_pointer = 0
end