Class: Ruby2D::Tileset

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

Overview

Tilesets are images containing multiple unique tiles. These tiles can be drawn to the screen multiple times in interesting combinations to produce things like backgrounds or draw larger objects.

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, tile_width: 32, tile_height: 32, atlas: nil, width: nil, height: nil, z: 0, padding: 0, spacing: 0, scale: 1, show: true) ⇒ Tileset

Setup a Tileset with an image.

Parameters:

  • path (#to_s)

    The location of the file to load as an image.

  • width (Numeric) (defaults to: nil)

    The width of image, or default is width from image file

  • height (Numeric) (defaults to: nil)

    The height of image, or default is height from image file

  • z (Numeric) (defaults to: 0)
  • padding (Numeric) (defaults to: 0)
  • spacing (Numeric) (defaults to: 0)
  • tile_width (Numeric) (defaults to: 32)

    Width of each tile in pixels

  • tile_height (Numeric) (defaults to: 32)

    Height of each tile in pixels

  • scale (Numeric) (defaults to: 1)

    Default is 1

  • show (Boolean) (defaults to: true)

    If true the image is added to Window automatically.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ruby2d/tileset.rb', line 25

def initialize(path, tile_width: 32, tile_height: 32, atlas: nil,
               width: nil, height: nil, z: 0,
               padding: 0, spacing: 0,
               scale: 1, show: true)
  @path = path.to_s

  # Initialize the tileset texture
  # Consider input pixmap atlas if supplied to load image file
  @texture = Image.load_image_as_texture @path, atlas: atlas
  @width = width || @texture.width
  @height = height || @texture.height
  @z = z

  @tiles = []
  @tile_definitions = {}
  @padding = padding
  @spacing = spacing
  @tile_width = tile_width
  @tile_height = tile_height
  @scale = scale

  _calculate_scaled_sizes

  add if show
end

Instance Method Details

#clear_tilesObject

Removes all stamped tiles so nothing is drawn.



93
94
95
# File 'lib/ruby2d/tileset.rb', line 93

def clear_tiles
  @tiles = []
end

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

Define and name a tile in the tileset image by its position. The actual tiles to be drawn are declared using #set_tile

Parameters:

  • name (String)

    A unique name for the tile in the tileset

  • x (Numeric)

    Column position of the tile

  • y (Numeric)

    Row position of the tile

  • rotate (Numeric) (defaults to: 0)

    Angle of the title when drawn, default is 0

  • flip (nil, :vertical, :horizontal, :both) (defaults to: nil)

    Direction to flip the tile if desired



59
60
61
# File 'lib/ruby2d/tileset.rb', line 59

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

#drawObject



97
98
99
100
101
# File 'lib/ruby2d/tileset.rb', line 97

def draw
  Window.render_ready_check

  render
end

#set_tile(name, coordinates) ⇒ Object

Select and “stamp” or set/place a tile to be drawn

Parameters:

  • name (String)

    The name of the tile defined using #define_tile

  • coordinates (Array<{"x", "y" => Numeric}>)

    one or more {x:, y:} coordinates to draw the tile



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ruby2d/tileset.rb', line 66

def set_tile(name, coordinates)
  tile_def = @tile_definitions.fetch(name)
  crop = _calculate_tile_crop(tile_def)

  coordinates.each do |coordinate|
    # Use Vertices object for tile placement so we can use them
    # directly when drawing the textures instead of making them for
    # every tile placement for each draw, and re-use the crop from
    # the tile definition
    vertices = Vertices.new(
      coordinate.fetch(:x), coordinate.fetch(:y),
      @scaled_tile_width, @scaled_tile_height,
      tile_def.fetch(:rotate),
      crop: crop,
      flip: tile_def.fetch(:flip)
    )
    # Remember the referenced tile for if we ever want to recalculate
    # them all due to change to scale etc (currently n/a since
    # scale is immutable)
    @tiles.push({
                  tile_def: tile_def,
                  vertices: vertices
                })
  end
end