class Gosu::Tiled::Layer

Public Class Methods

new(window, data, options) click to toggle source
# File lib/gosu_tiled/layer.rb, line 4
def initialize(window, data, options)
  @window = window
  @data = data
  @options = options
end

Public Instance Methods

draw(x, y, tilesets) click to toggle source
# File lib/gosu_tiled/layer.rb, line 18
def draw(x, y, tilesets)
  if type == 'tilelayer'
    draw_tiles(x, y, tilesets)
  elsif type == 'objectgroup'
    draw_objects(x, y, tilesets)
  end
end
screen_height_in_tiles() click to toggle source
# File lib/gosu_tiled/layer.rb, line 30
def screen_height_in_tiles
  (@window.height / tile_height.to_f).ceil
end
screen_width_in_tiles() click to toggle source
# File lib/gosu_tiled/layer.rb, line 26
def screen_width_in_tiles
  (@window.width / tile_width.to_f).ceil
end
type() click to toggle source
# File lib/gosu_tiled/layer.rb, line 14
def type
  @data['type']
end
visible?() click to toggle source
# File lib/gosu_tiled/layer.rb, line 10
def visible?
  @data['visible']
end

Private Instance Methods

draw_objects(x, y, tilesets) click to toggle source
# File lib/gosu_tiled/layer.rb, line 78
def draw_objects(x, y, tilesets)
  @data['objects'].each do |obj|
    obj_x = obj['x']
    obj_y = obj['y']
    if within_screen_range(obj_x, obj_y)
      tilesets.get(obj['gid']).draw(obj_x - x, obj_y - y - tile_height, 10)
    end
  end
end
draw_tiles(x, y, tilesets) click to toggle source
# File lib/gosu_tiled/layer.rb, line 44
def draw_tiles(x, y, tilesets)
  off_x = offset_x_in_tiles(x)
  off_y = offset_y_in_tiles(y)
  tile_range_x = (off_x..screen_width_in_tiles + off_x)
  tile_range_y = (off_y..screen_height_in_tiles + off_y)
  tile_range_x.each do |xx|
    tile_range_y.each do |yy|
      target_x = transpose_tile_x(xx, x)
      target_y = transpose_tile_y(yy, y)
      if within_map_range(x + target_x, y + target_y)
        tilesets.get(tile_at(xx, yy)).draw(target_x, target_y, 0)
      end
    end
  end
end
map_height() click to toggle source
# File lib/gosu_tiled/layer.rb, line 104
def map_height
  @options[:height]
end
map_width() click to toggle source
# File lib/gosu_tiled/layer.rb, line 100
def map_width
  @options[:width]
end
offset_x_in_tiles(x) click to toggle source
# File lib/gosu_tiled/layer.rb, line 36
def offset_x_in_tiles(x)
  x / tile_width
end
offset_y_in_tiles(y) click to toggle source
# File lib/gosu_tiled/layer.rb, line 40
def offset_y_in_tiles(y)
  y / tile_height
end
tile_at(x, y) click to toggle source
# File lib/gosu_tiled/layer.rb, line 88
def tile_at(x, y)
  @data['data'][y * @data['width'] + x]
end
tile_height() click to toggle source
# File lib/gosu_tiled/layer.rb, line 96
def tile_height
  @options[:tile_height]
end
tile_width() click to toggle source
# File lib/gosu_tiled/layer.rb, line 92
def tile_width
  @options[:tile_width]
end
transpose_tile_x(x, off_x) click to toggle source
# File lib/gosu_tiled/layer.rb, line 70
def transpose_tile_x(x, off_x)
  x * tile_width - off_x
end
transpose_tile_y(y, off_y) click to toggle source
# File lib/gosu_tiled/layer.rb, line 74
def transpose_tile_y(y, off_y)
  y * tile_height - off_y
end
within_map_range(x, y) click to toggle source
# File lib/gosu_tiled/layer.rb, line 60
def within_map_range(x, y)
  (0..map_width - 1).include?(x) && (0..map_height - 1).include?(y)
end
within_screen_range(x, y) click to toggle source
# File lib/gosu_tiled/layer.rb, line 64
def within_screen_range(x, y)
  range_x = (x - tile_width..@window.width + x + tile_width)
  range_y = (y..@window.height + y + tile_height)
  range_x.include?(x) && range_y.include?(y)
end