class Map

Constants

MAP_HEIGHT
MAP_WIDTH
TILE_SIZE

Public Class Methods

bounding_box() click to toggle source
# 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
new(object_pool) click to toggle source
# 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

Public Instance Methods

can_move_to?(x, y) click to toggle source
# File lib/entities/map.rb, line 35
def can_move_to?(x, y)
  tile = tile_at(x, y)
  tile && tile != @water
end
draw(viewport) click to toggle source
# 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) click to toggle source
# 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_point() click to toggle source
# File lib/entities/map.rb, line 31
def spawn_point
  @spawn_points[(@spawn_points_pointer += 1) % @spawn_points.size]
end
spawn_points(max) click to toggle source
# 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

Private Instance Methods

choose_tile(val) click to toggle source
# File lib/entities/map.rb, line 159
def choose_tile(val)
  case val
  when 0.0..0.3 # 30% chance
    @water
  when 0.3..0.5 # 20% chance, water edges
    @sand
  else # 50% chance
    @grass
  end
end
find_spawn_point() click to toggle source
# File lib/entities/map.rb, line 172
def find_spawn_point
  while true
    x = rand(0..MAP_WIDTH * TILE_SIZE)
    y = rand(0..MAP_HEIGHT * TILE_SIZE)
    if can_move_to?(x, y) &&
        @object_pool.nearby_point(x, y, 150).empty?
      return [x, y]
    end
  end
end
generate_boxes() click to toggle source
# File lib/entities/map.rb, line 125
def generate_boxes
  boxes = 0
  target_boxes = rand(50..200)
  while boxes < target_boxes do
    x = rand(0..MAP_WIDTH * TILE_SIZE)
    y = rand(0..MAP_HEIGHT * TILE_SIZE)
    if tile_at(x, y) != @water
      Box.new(@object_pool, x, y)
      boxes += 1
    end
  end
end
generate_map() click to toggle source
# File lib/entities/map.rb, line 91
def generate_map
  noises = Perlin::Noise.new(2)
  contrast = Perlin::Curve.contrast(
    Perlin::Curve::CUBIC, 2)
  map = {}
  MAP_WIDTH.times do |x|
    map[x] = {}
    MAP_HEIGHT.times do |y|
      n = noises[x * 0.1, y * 0.1]
      n = contrast.call(n)
      map[x][y] = choose_tile(n)
    end
  end
  map
end
generate_powerups() click to toggle source
# File lib/entities/map.rb, line 138
def generate_powerups
  pups = 0
  target_pups = rand(20..30)
  while pups < target_pups do
    x = rand(0..MAP_WIDTH * TILE_SIZE)
    y = rand(0..MAP_HEIGHT * TILE_SIZE)
    if tile_at(x, y) != @water &&
        @object_pool.nearby_point(x, y, 150).empty?
      random_powerup.new(@object_pool, x, y)
      pups += 1
    end
  end
end
generate_trees() click to toggle source
# File lib/entities/map.rb, line 107
def generate_trees
  noises = Perlin::Noise.new(2)
  contrast = Perlin::Curve.contrast(
    Perlin::Curve::CUBIC, 2)
  trees = 0
  target_trees = rand(1500..1500)
  while trees < target_trees do
    x = rand(0..MAP_WIDTH * TILE_SIZE)
    y = rand(0..MAP_HEIGHT * TILE_SIZE)
    n = noises[x * 0.001, y * 0.001]
    n = contrast.call(n)
    if tile_at(x, y) == @grass && n > 0.5
      Tree.new(@object_pool, x, y, n * 2 - 1)
      trees += 1
    end
  end
end
load_tiles() click to toggle source
# File lib/entities/map.rb, line 81
def load_tiles
  tiles = Gosu::Image.load_tiles(
    $window, Utils.media_path('ground.png'),
    128, 128, true)
  @sand = tiles[0]
  @grass = tiles[8]
  @water = Gosu::Image.new(
    $window, Utils.media_path('water.png'), true)
end
random_powerup() click to toggle source
# File lib/entities/map.rb, line 152
def random_powerup
  [HealthPowerup,
   RepairPowerup,
   FireRatePowerup,
   TankSpeedPowerup].sample
end
tile_at(x, y) click to toggle source
# File lib/entities/map.rb, line 74
def tile_at(x, y)
  t_x = ((x / TILE_SIZE) % TILE_SIZE).floor
  t_y = ((y / TILE_SIZE) % TILE_SIZE).floor
  row = @map[t_x]
  row ? row[t_y] : @water
end