Sha256: 37f58d42ccf8302d66ee1c4f0f712a9da85081d61dc46693201ca633e6927b90

Contents?: true

Size: 1.74 KB

Versions: 2

Compression:

Stored size: 1.74 KB

Contents

class TileMap
  attr_accessor :position

  def initialize(map_name:, tiles:, tile_size: nil, tile_width: nil, tile_height: nil)
    @tile_width = tile_width || tile_size
    @tile_height = tile_height || tile_size

    if(@tile_height.nil? or @tile_width.nil?)
      raise("Tile size is not properly defined. Either you set a `tile_size` or a `tile_width` and `tile_height`")
    end

    @map_name = map_name
    @tiles = tiles
    @position = Coordinates.zero

    @grid = TileMap.load_grid(@map_name)
  end

  def width
    @grid.max(&:length) * @tile_width
  end

  def height
    @grid.length * @tile_height
  end

  def spawn
    tile_position = Coordinates.zero

    @grid.each do |line|
      tile_position.x = 0

      line.each do |tile_index|
        if !tile_index.nil?
          actor = @tiles[tile_index].clone
          actor.position.x = @position.x + (tile_position.x * @tile_width)
          actor.position.y = @position.y + (tile_position.y * @tile_height)
        end

        tile_position.x += 1
      end

      tile_position.y += 1
    end
  end

  private

  class << self
    @@maps = {}

    def load_grid(map_name)
      File.readlines(TileMap.locate_map(map_name), chomp: true).map do |line|
        line.each_char.map do |char|
          char == " " ? nil : char.to_i
        end
      end
    end

    def locate_map(map_name)
      return @@maps[map_name] if @@maps[map_name]

      puts "Initialize map: '#{map_name}'"

      file_name = Dir.entries(base_path).find { |e| e =~ /^#{map_name}($|\.)/  }

      raise "Map file not found with name '#{map_name}' in #{base_path}" if file_name.nil?

      @@maps[map_name] = "#{base_path}/#{file_name}"

      return @@maps[map_name]
    end

    def base_path
      "#{Dir.pwd}/maps"
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fantasy-0.1.5.1 lib/fantasy/tile_map.rb
fantasy-0.1.5 lib/fantasy/tile_map.rb