Class: Ruby2D::Tileset

Inherits:
Object
  • Object
show all
Includes:
Renderable
Defined in:
lib/ruby2d/tileset.rb

Constant Summary collapse

DEFAULT_COLOR =
Color.new([1.0, 1.0, 1.0, 1.0])

Instance Attribute Summary

Attributes included from Renderable

#color, #height, #width, #x, #y, #z

Instance Method Summary collapse

Methods included from Renderable

#add, #contains?, #remove

Constructor Details

#initialize(path, opts = {}) ⇒ Tileset

Returns a new instance of Tileset.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ruby2d/tileset.rb', line 9

def initialize(path, opts = {})
  @path = path

  # Initialize the tileset texture
  @texture = Texture.new(*Image.load_image(@path))
  @width = opts[:width] || @texture.width
  @height = opts[:height] || @texture.height
  @z = opts[:z] || 0

  @tiles = []
  @defined_tiles = {}
  @padding = opts[:padding] || 0
  @spacing = opts[:spacing] || 0
  @tile_width = opts[:tile_width]
  @tile_height = opts[:tile_height]
  @scale = opts[:scale] || 1

  unless opts[:show] == false then add end
end

Instance Method Details

#clear_tilesObject



48
49
50
# File 'lib/ruby2d/tileset.rb', line 48

def clear_tiles
  @tiles = []
end

#define_tile(name, x, y, rotate: 0, flip: nil) ⇒ Object



29
30
31
# File 'lib/ruby2d/tileset.rb', line 29

def define_tile(name, x, y, rotate: 0, flip: nil)
  @defined_tiles[name] = { x: x, y: y , rotate: rotate, flip: flip }
end

#drawObject



52
53
54
55
56
# File 'lib/ruby2d/tileset.rb', line 52

def draw
  Window.render_ready_check

  render
end

#set_tile(name, coordinates) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ruby2d/tileset.rb', line 33

def set_tile(name, coordinates)
  tile = @defined_tiles.fetch(name)

  coordinates.each do |coordinate|
    @tiles.push({
      tile_x: tile.fetch(:x), 
      tile_y: tile.fetch(:y), 
      tile_rotate: tile.fetch(:rotate),
      tile_flip: tile.fetch(:flip),
      x: coordinate.fetch(:x),
      y: coordinate.fetch(:y)
    })
  end
end