Class: Ruby2D::Tileset
- Inherits:
-
Object
- Object
- Ruby2D::Tileset
- 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
Instance Attribute Summary
Attributes included from Renderable
#color, #height, #width, #x, #y, #z
Instance Method Summary collapse
-
#clear_tiles ⇒ Object
Removes all stamped tiles so nothing is drawn.
-
#define_tile(name, x, y, rotate: 0, flip: nil) ⇒ Object
Define and name a tile in the tileset image by its position.
- #draw ⇒ Object
-
#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
constructor
Setup a Tileset with an image.
-
#set_tile(name, coordinates) ⇒ Object
Select and “stamp” or set/place a tile to be drawn.
Methods included from Renderable
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.
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_tiles ⇒ Object
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
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 |
#draw ⇒ Object
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
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 |