Class: RedBird::TileSet

Inherits:
Object
  • Object
show all
Defined in:
lib/red_bird/tile_set.rb

Overview

A TileSet is a set of Sprites. It allows you to reuse the same Sprite several times in the creation of a game scenario, making the level design more straightforward.

Author:

  • Frederico Linhares

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(texture_file, texture_palette, tile_width, tile_height) ⇒ TileSet

Returns a new instance of TileSet.

Parameters:

  • texture_file (String)

    this method uses the path as argument to create a RedBird::Texture.

  • tile_width (Integer)

    width of each tile.

  • tile_height (Integer)

    height of each tile.

Author:

  • Frederico Linhares



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/red_bird/tile_set.rb', line 24

def initialize(texture_file, texture_palette, tile_width, tile_height)
  @texture = RedBird::Texture.new(texture_file, texture_palette)

  @tile_width = tile_width
  @tile_height = tile_height

  horizontal_tiles = @texture.width / @tile_width
  vertical_tiles = @texture.height / @tile_height

  @tiles = Sprite.grid(
    @texture, 0, 0, horizontal_tiles, vertical_tiles, @tile_width,
    @tile_height)
end

Instance Attribute Details

#tile_heightInteger (readonly)

Returns height of each tile.

Returns:

  • (Integer)

    height of each tile.



16
17
18
19
20
21
22
23
24
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
50
51
52
53
54
55
# File 'lib/red_bird/tile_set.rb', line 16

class TileSet
  attr_reader :tiles, :tile_width, :tile_height

  # @param texture_file [String] this method uses the path as argument to
  #   create a {RedBird::Texture}.
  # @param tile_width [Integer] width of each tile.
  # @param tile_height [Integer] height of each tile.
  # @author Frederico Linhares
  def initialize(texture_file, texture_palette, tile_width, tile_height)
    @texture = RedBird::Texture.new(texture_file, texture_palette)

    @tile_width = tile_width
    @tile_height = tile_height

    horizontal_tiles = @texture.width / @tile_width
    vertical_tiles = @texture.height / @tile_height

    @tiles = Sprite.grid(
      @texture, 0, 0, horizontal_tiles, vertical_tiles, @tile_width,
      @tile_height)
  end

  # Load information from a yaml file and uses it create an instance of this
  # class.
  #
  # @param data_dir [String] path to the directory containing the yaml and
  #   other files referenced in the yaml.
  # @param tile_file [String] path to the yaml file.
  # @return [RedBird::TileSet]
  # @author Frederico Linhares
  def self.from_yml(data_dir, tile_file, palette)
    yaml_file = data_dir + tile_file

    data = YAML.load_file(yaml_file)
    texture_path = data_dir + data["texture"]

    return self.new(texture_path, palette, data["width"], data["height"])
  end

end

#tile_widthInteger (readonly)

Returns width of each tile.

Returns:

  • (Integer)

    width of each tile.



16
17
18
19
20
21
22
23
24
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
50
51
52
53
54
55
# File 'lib/red_bird/tile_set.rb', line 16

class TileSet
  attr_reader :tiles, :tile_width, :tile_height

  # @param texture_file [String] this method uses the path as argument to
  #   create a {RedBird::Texture}.
  # @param tile_width [Integer] width of each tile.
  # @param tile_height [Integer] height of each tile.
  # @author Frederico Linhares
  def initialize(texture_file, texture_palette, tile_width, tile_height)
    @texture = RedBird::Texture.new(texture_file, texture_palette)

    @tile_width = tile_width
    @tile_height = tile_height

    horizontal_tiles = @texture.width / @tile_width
    vertical_tiles = @texture.height / @tile_height

    @tiles = Sprite.grid(
      @texture, 0, 0, horizontal_tiles, vertical_tiles, @tile_width,
      @tile_height)
  end

  # Load information from a yaml file and uses it create an instance of this
  # class.
  #
  # @param data_dir [String] path to the directory containing the yaml and
  #   other files referenced in the yaml.
  # @param tile_file [String] path to the yaml file.
  # @return [RedBird::TileSet]
  # @author Frederico Linhares
  def self.from_yml(data_dir, tile_file, palette)
    yaml_file = data_dir + tile_file

    data = YAML.load_file(yaml_file)
    texture_path = data_dir + data["texture"]

    return self.new(texture_path, palette, data["width"], data["height"])
  end

end

#tilesArray<RedBird::TileSet::Sprite> (readonly)

Returns:

  • (Array<RedBird::TileSet::Sprite>)


16
17
18
19
20
21
22
23
24
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
50
51
52
53
54
55
# File 'lib/red_bird/tile_set.rb', line 16

class TileSet
  attr_reader :tiles, :tile_width, :tile_height

  # @param texture_file [String] this method uses the path as argument to
  #   create a {RedBird::Texture}.
  # @param tile_width [Integer] width of each tile.
  # @param tile_height [Integer] height of each tile.
  # @author Frederico Linhares
  def initialize(texture_file, texture_palette, tile_width, tile_height)
    @texture = RedBird::Texture.new(texture_file, texture_palette)

    @tile_width = tile_width
    @tile_height = tile_height

    horizontal_tiles = @texture.width / @tile_width
    vertical_tiles = @texture.height / @tile_height

    @tiles = Sprite.grid(
      @texture, 0, 0, horizontal_tiles, vertical_tiles, @tile_width,
      @tile_height)
  end

  # Load information from a yaml file and uses it create an instance of this
  # class.
  #
  # @param data_dir [String] path to the directory containing the yaml and
  #   other files referenced in the yaml.
  # @param tile_file [String] path to the yaml file.
  # @return [RedBird::TileSet]
  # @author Frederico Linhares
  def self.from_yml(data_dir, tile_file, palette)
    yaml_file = data_dir + tile_file

    data = YAML.load_file(yaml_file)
    texture_path = data_dir + data["texture"]

    return self.new(texture_path, palette, data["width"], data["height"])
  end

end

Class Method Details

.from_yml(data_dir, tile_file, palette) ⇒ RedBird::TileSet

Load information from a yaml file and uses it create an instance of this class.

Parameters:

  • data_dir (String)

    path to the directory containing the yaml and other files referenced in the yaml.

  • tile_file (String)

    path to the yaml file.

Returns:

Author:

  • Frederico Linhares



46
47
48
49
50
51
52
53
# File 'lib/red_bird/tile_set.rb', line 46

def self.from_yml(data_dir, tile_file, palette)
  yaml_file = data_dir + tile_file

  data = YAML.load_file(yaml_file)
  texture_path = data_dir + data["texture"]

  return self.new(texture_path, palette, data["width"], data["height"])
end