spajus/gosu-tiled

View on GitHub
lib/gosu_tiled/tilesets.rb

Summary

Maintainability
A
0 mins
Test Coverage
module Gosu
  module Tiled
    class Tilesets
      def initialize(data, data_dir, retro: false, tileable: true)
        @root_dir = data_dir
        @data = data
        @tilesets = {}
        @data.each do |t|
          tileset = Gosu::Image.load_tiles(
            File.join(data_dir, t['image']), t['tilewidth'], t['tileheight'], retro: retro, tileable: tileable
          )
          @tilesets[t['firstgid']] = {
            'data' => t,
            'tiles' => tileset
          }
        end
      end

      def size
        @data.size
      end

      def get(index)
        return empty_tile if index == 0 || index.nil?
        key = closest_key(index)
        @tilesets[key]['tiles'][index - key]
      end

      def properties(index)
        return {} if index == 0
        key = closest_key(index)
        props = @tilesets[key]['data']['tileproperties']
        return {} unless props
        props[(index - key).to_s]
      end

      private

      def empty_tile
        @empty_tile ||= EmptyTile.new
      end

      def closest_key(index)
        @tilesets.keys.select { |k| k <= index }.max
      end
    end
  end
end