Class: RedBird::TileMap

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

Overview

A TileMap is a grid of Sprites; it works as a scenario. It has scrolling functionalities to control which portion of it is on the screen.

Author:

  • Frederico Linhares

Instance Attribute Summary collapse

Attributes inherited from Entity

#height, #pos_x, #pos_y, #priority, #width

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Entity

#cursor_down, #cursor_hover, #cursor_up, #tick

Constructor Details

#initialize(tile_set, pos_x, pos_y, width, height, num_hor_tiles, num_ver_tiles, tiles) ⇒ TileMap

Returns a new instance of TileMap.

Parameters:

  • tile_set (RedBird::TileSet)
  • pos_x (Integer)

    horizontal position of this map in the screen.

  • pos_y (Integer)

    vertical position of this map in the screen.

  • width (Integer)

    width of the region displayed in the screen.

  • height (Integer)

    height of the region displayed in the screen.

  • num_hor_tiles (Integer)

    total width in number of tiles.

  • num_ver_tiles (Integer)

    total height in number of tiles.

  • tiles (Array<Integer>)

    code of every tile to display, must be an one dimension array.

Author:

  • Frederico Linhares



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/red_bird/tile_map.rb', line 37

def initialize(tile_set, pos_x, pos_y, width, height, num_hor_tiles,
               num_ver_tiles, tiles)
  @tile_set = tile_set
  @pos_x = pos_x
  @pos_y = pos_y

  @width = width
  @height = height

  @ver_offset = 0
  @hor_offset = 0

  @priority = -1

  @tiles = tiles

  # Number of tiles that are going to be rendered to screen.
  @render_hor_tiles = @width / @tile_set.tile_width + 1
  @render_ver_tiles = @height / @tile_set.tile_height + 1

  @total_hor_tiles = num_hor_tiles
  @total_ver_tiles = num_ver_tiles

  @total_width = @total_hor_tiles * @tile_set.tile_width
  @total_height = @total_ver_tiles * @tile_set.tile_height
end

Instance Attribute Details

#hor_offsetInteger

Returns how much the tilemap scrolled from the left corner.

Returns:

  • (Integer)

    how much the tilemap scrolled from the left corner.



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
56
57
58
59
60
61
62
63
64
65
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/red_bird/tile_map.rb', line 23

class TileMap < Entity
  attr_accessor :hor_offset, :ver_offset
  attr_reader :tiles, :tile_set, :total_width, :total_height

  # @param tile_set [RedBird::TileSet]
  # @param pos_x [Integer] horizontal position of this map in the screen.
  # @param pos_y [Integer] vertical position of this map in the screen.
  # @param width [Integer] width of the region displayed in the screen.
  # @param height [Integer] height of the region displayed in the screen.
  # @param num_hor_tiles [Integer] total width in number of tiles.
  # @param num_ver_tiles [Integer] total height in number of tiles.
  # @param tiles [Array<Integer>] code of every tile to display, must be an
  #   one dimension array.
  # @author Frederico Linhares
  def initialize(tile_set, pos_x, pos_y, width, height, num_hor_tiles,
                 num_ver_tiles, tiles)
    @tile_set = tile_set
    @pos_x = pos_x
    @pos_y = pos_y

    @width = width
    @height = height

    @ver_offset = 0
    @hor_offset = 0

    @priority = -1

    @tiles = tiles

    # Number of tiles that are going to be rendered to screen.
    @render_hor_tiles = @width / @tile_set.tile_width + 1
    @render_ver_tiles = @height / @tile_set.tile_height + 1

    @total_hor_tiles = num_hor_tiles
    @total_ver_tiles = num_ver_tiles

    @total_width = @total_hor_tiles * @tile_set.tile_width
    @total_height = @total_ver_tiles * @tile_set.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 map_file [String] path to the yaml file.
  # @return [RedBird::TileMap]
  # @author Frederico Linhares
  def self.from_yml(data_dir, map_file, tile_set, pos_x, pos_y, width,
                    height)
    yaml_file = data_dir + map_file

    data = YAML.load_file(yaml_file)
    num_hor_tiles = data["num_hor_tiles"]
    num_ver_tiles = data["num_ver_tiles"]
    tiles = data["tiles"].unpack("S*")

    return self.new(
             tile_set, pos_x, pos_y, width, height, num_hor_tiles,
             num_ver_tiles, tiles)
  end

  # Gets the {RedBird::Sprite} at position x, y.
  #
  # @param x [Integer]
  # @param y [Integer]
  # @author Frederico Linhares
  def tile_xy(x, y)
    return @tile_set.tiles[@tiles[y_to_tile(y) + x]][:type]
  end

  # Renders this tilemap to the screen.
  #
  # @author Frederico Linhares
  def render
    first_hor_tile = @hor_offset / @tile_set.tile_width
    first_ver_tile = @ver_offset / @tile_set.tile_height

    (first_ver_tile..(first_ver_tile + @render_ver_tiles)).each do |y|
      (first_hor_tile..(first_hor_tile + @render_hor_tiles)).each do |x|
        unless @tiles[y_to_tile(y) + x].nil? then
          @tile_set.tiles[@tiles[y_to_tile(y) + x]].render_to_screen(
            x * @tile_set.tile_width - @hor_offset + @pos_x,
            y * @tile_set.tile_height - @ver_offset + @pos_y)
        end
      end
    end
  end

  private
  def y_to_tile(y)
    return y * @total_hor_tiles
  end
end

#tile_setRedBird::TileSet (readonly)

Returns tile set in use by this class.

Returns:



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
56
57
58
59
60
61
62
63
64
65
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/red_bird/tile_map.rb', line 23

class TileMap < Entity
  attr_accessor :hor_offset, :ver_offset
  attr_reader :tiles, :tile_set, :total_width, :total_height

  # @param tile_set [RedBird::TileSet]
  # @param pos_x [Integer] horizontal position of this map in the screen.
  # @param pos_y [Integer] vertical position of this map in the screen.
  # @param width [Integer] width of the region displayed in the screen.
  # @param height [Integer] height of the region displayed in the screen.
  # @param num_hor_tiles [Integer] total width in number of tiles.
  # @param num_ver_tiles [Integer] total height in number of tiles.
  # @param tiles [Array<Integer>] code of every tile to display, must be an
  #   one dimension array.
  # @author Frederico Linhares
  def initialize(tile_set, pos_x, pos_y, width, height, num_hor_tiles,
                 num_ver_tiles, tiles)
    @tile_set = tile_set
    @pos_x = pos_x
    @pos_y = pos_y

    @width = width
    @height = height

    @ver_offset = 0
    @hor_offset = 0

    @priority = -1

    @tiles = tiles

    # Number of tiles that are going to be rendered to screen.
    @render_hor_tiles = @width / @tile_set.tile_width + 1
    @render_ver_tiles = @height / @tile_set.tile_height + 1

    @total_hor_tiles = num_hor_tiles
    @total_ver_tiles = num_ver_tiles

    @total_width = @total_hor_tiles * @tile_set.tile_width
    @total_height = @total_ver_tiles * @tile_set.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 map_file [String] path to the yaml file.
  # @return [RedBird::TileMap]
  # @author Frederico Linhares
  def self.from_yml(data_dir, map_file, tile_set, pos_x, pos_y, width,
                    height)
    yaml_file = data_dir + map_file

    data = YAML.load_file(yaml_file)
    num_hor_tiles = data["num_hor_tiles"]
    num_ver_tiles = data["num_ver_tiles"]
    tiles = data["tiles"].unpack("S*")

    return self.new(
             tile_set, pos_x, pos_y, width, height, num_hor_tiles,
             num_ver_tiles, tiles)
  end

  # Gets the {RedBird::Sprite} at position x, y.
  #
  # @param x [Integer]
  # @param y [Integer]
  # @author Frederico Linhares
  def tile_xy(x, y)
    return @tile_set.tiles[@tiles[y_to_tile(y) + x]][:type]
  end

  # Renders this tilemap to the screen.
  #
  # @author Frederico Linhares
  def render
    first_hor_tile = @hor_offset / @tile_set.tile_width
    first_ver_tile = @ver_offset / @tile_set.tile_height

    (first_ver_tile..(first_ver_tile + @render_ver_tiles)).each do |y|
      (first_hor_tile..(first_hor_tile + @render_hor_tiles)).each do |x|
        unless @tiles[y_to_tile(y) + x].nil? then
          @tile_set.tiles[@tiles[y_to_tile(y) + x]].render_to_screen(
            x * @tile_set.tile_width - @hor_offset + @pos_x,
            y * @tile_set.tile_height - @ver_offset + @pos_y)
        end
      end
    end
  end

  private
  def y_to_tile(y)
    return y * @total_hor_tiles
  end
end

#tilesArray<Integer> (readonly)

Returns tiles indexes.

Returns:

  • (Array<Integer>)

    tiles indexes.



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
56
57
58
59
60
61
62
63
64
65
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/red_bird/tile_map.rb', line 23

class TileMap < Entity
  attr_accessor :hor_offset, :ver_offset
  attr_reader :tiles, :tile_set, :total_width, :total_height

  # @param tile_set [RedBird::TileSet]
  # @param pos_x [Integer] horizontal position of this map in the screen.
  # @param pos_y [Integer] vertical position of this map in the screen.
  # @param width [Integer] width of the region displayed in the screen.
  # @param height [Integer] height of the region displayed in the screen.
  # @param num_hor_tiles [Integer] total width in number of tiles.
  # @param num_ver_tiles [Integer] total height in number of tiles.
  # @param tiles [Array<Integer>] code of every tile to display, must be an
  #   one dimension array.
  # @author Frederico Linhares
  def initialize(tile_set, pos_x, pos_y, width, height, num_hor_tiles,
                 num_ver_tiles, tiles)
    @tile_set = tile_set
    @pos_x = pos_x
    @pos_y = pos_y

    @width = width
    @height = height

    @ver_offset = 0
    @hor_offset = 0

    @priority = -1

    @tiles = tiles

    # Number of tiles that are going to be rendered to screen.
    @render_hor_tiles = @width / @tile_set.tile_width + 1
    @render_ver_tiles = @height / @tile_set.tile_height + 1

    @total_hor_tiles = num_hor_tiles
    @total_ver_tiles = num_ver_tiles

    @total_width = @total_hor_tiles * @tile_set.tile_width
    @total_height = @total_ver_tiles * @tile_set.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 map_file [String] path to the yaml file.
  # @return [RedBird::TileMap]
  # @author Frederico Linhares
  def self.from_yml(data_dir, map_file, tile_set, pos_x, pos_y, width,
                    height)
    yaml_file = data_dir + map_file

    data = YAML.load_file(yaml_file)
    num_hor_tiles = data["num_hor_tiles"]
    num_ver_tiles = data["num_ver_tiles"]
    tiles = data["tiles"].unpack("S*")

    return self.new(
             tile_set, pos_x, pos_y, width, height, num_hor_tiles,
             num_ver_tiles, tiles)
  end

  # Gets the {RedBird::Sprite} at position x, y.
  #
  # @param x [Integer]
  # @param y [Integer]
  # @author Frederico Linhares
  def tile_xy(x, y)
    return @tile_set.tiles[@tiles[y_to_tile(y) + x]][:type]
  end

  # Renders this tilemap to the screen.
  #
  # @author Frederico Linhares
  def render
    first_hor_tile = @hor_offset / @tile_set.tile_width
    first_ver_tile = @ver_offset / @tile_set.tile_height

    (first_ver_tile..(first_ver_tile + @render_ver_tiles)).each do |y|
      (first_hor_tile..(first_hor_tile + @render_hor_tiles)).each do |x|
        unless @tiles[y_to_tile(y) + x].nil? then
          @tile_set.tiles[@tiles[y_to_tile(y) + x]].render_to_screen(
            x * @tile_set.tile_width - @hor_offset + @pos_x,
            y * @tile_set.tile_height - @ver_offset + @pos_y)
        end
      end
    end
  end

  private
  def y_to_tile(y)
    return y * @total_hor_tiles
  end
end

#total_heightInteger (readonly)

Returns height of the entire tilemap (not only what is displayed in the screen).

Returns:

  • (Integer)

    height of the entire tilemap (not only what is displayed in the screen).



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
56
57
58
59
60
61
62
63
64
65
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/red_bird/tile_map.rb', line 23

class TileMap < Entity
  attr_accessor :hor_offset, :ver_offset
  attr_reader :tiles, :tile_set, :total_width, :total_height

  # @param tile_set [RedBird::TileSet]
  # @param pos_x [Integer] horizontal position of this map in the screen.
  # @param pos_y [Integer] vertical position of this map in the screen.
  # @param width [Integer] width of the region displayed in the screen.
  # @param height [Integer] height of the region displayed in the screen.
  # @param num_hor_tiles [Integer] total width in number of tiles.
  # @param num_ver_tiles [Integer] total height in number of tiles.
  # @param tiles [Array<Integer>] code of every tile to display, must be an
  #   one dimension array.
  # @author Frederico Linhares
  def initialize(tile_set, pos_x, pos_y, width, height, num_hor_tiles,
                 num_ver_tiles, tiles)
    @tile_set = tile_set
    @pos_x = pos_x
    @pos_y = pos_y

    @width = width
    @height = height

    @ver_offset = 0
    @hor_offset = 0

    @priority = -1

    @tiles = tiles

    # Number of tiles that are going to be rendered to screen.
    @render_hor_tiles = @width / @tile_set.tile_width + 1
    @render_ver_tiles = @height / @tile_set.tile_height + 1

    @total_hor_tiles = num_hor_tiles
    @total_ver_tiles = num_ver_tiles

    @total_width = @total_hor_tiles * @tile_set.tile_width
    @total_height = @total_ver_tiles * @tile_set.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 map_file [String] path to the yaml file.
  # @return [RedBird::TileMap]
  # @author Frederico Linhares
  def self.from_yml(data_dir, map_file, tile_set, pos_x, pos_y, width,
                    height)
    yaml_file = data_dir + map_file

    data = YAML.load_file(yaml_file)
    num_hor_tiles = data["num_hor_tiles"]
    num_ver_tiles = data["num_ver_tiles"]
    tiles = data["tiles"].unpack("S*")

    return self.new(
             tile_set, pos_x, pos_y, width, height, num_hor_tiles,
             num_ver_tiles, tiles)
  end

  # Gets the {RedBird::Sprite} at position x, y.
  #
  # @param x [Integer]
  # @param y [Integer]
  # @author Frederico Linhares
  def tile_xy(x, y)
    return @tile_set.tiles[@tiles[y_to_tile(y) + x]][:type]
  end

  # Renders this tilemap to the screen.
  #
  # @author Frederico Linhares
  def render
    first_hor_tile = @hor_offset / @tile_set.tile_width
    first_ver_tile = @ver_offset / @tile_set.tile_height

    (first_ver_tile..(first_ver_tile + @render_ver_tiles)).each do |y|
      (first_hor_tile..(first_hor_tile + @render_hor_tiles)).each do |x|
        unless @tiles[y_to_tile(y) + x].nil? then
          @tile_set.tiles[@tiles[y_to_tile(y) + x]].render_to_screen(
            x * @tile_set.tile_width - @hor_offset + @pos_x,
            y * @tile_set.tile_height - @ver_offset + @pos_y)
        end
      end
    end
  end

  private
  def y_to_tile(y)
    return y * @total_hor_tiles
  end
end

#total_widthInteger (readonly)

Returns width of the entire tilemap (not only what is displayed in the screen).

Returns:

  • (Integer)

    width of the entire tilemap (not only what is displayed in the screen).



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
56
57
58
59
60
61
62
63
64
65
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/red_bird/tile_map.rb', line 23

class TileMap < Entity
  attr_accessor :hor_offset, :ver_offset
  attr_reader :tiles, :tile_set, :total_width, :total_height

  # @param tile_set [RedBird::TileSet]
  # @param pos_x [Integer] horizontal position of this map in the screen.
  # @param pos_y [Integer] vertical position of this map in the screen.
  # @param width [Integer] width of the region displayed in the screen.
  # @param height [Integer] height of the region displayed in the screen.
  # @param num_hor_tiles [Integer] total width in number of tiles.
  # @param num_ver_tiles [Integer] total height in number of tiles.
  # @param tiles [Array<Integer>] code of every tile to display, must be an
  #   one dimension array.
  # @author Frederico Linhares
  def initialize(tile_set, pos_x, pos_y, width, height, num_hor_tiles,
                 num_ver_tiles, tiles)
    @tile_set = tile_set
    @pos_x = pos_x
    @pos_y = pos_y

    @width = width
    @height = height

    @ver_offset = 0
    @hor_offset = 0

    @priority = -1

    @tiles = tiles

    # Number of tiles that are going to be rendered to screen.
    @render_hor_tiles = @width / @tile_set.tile_width + 1
    @render_ver_tiles = @height / @tile_set.tile_height + 1

    @total_hor_tiles = num_hor_tiles
    @total_ver_tiles = num_ver_tiles

    @total_width = @total_hor_tiles * @tile_set.tile_width
    @total_height = @total_ver_tiles * @tile_set.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 map_file [String] path to the yaml file.
  # @return [RedBird::TileMap]
  # @author Frederico Linhares
  def self.from_yml(data_dir, map_file, tile_set, pos_x, pos_y, width,
                    height)
    yaml_file = data_dir + map_file

    data = YAML.load_file(yaml_file)
    num_hor_tiles = data["num_hor_tiles"]
    num_ver_tiles = data["num_ver_tiles"]
    tiles = data["tiles"].unpack("S*")

    return self.new(
             tile_set, pos_x, pos_y, width, height, num_hor_tiles,
             num_ver_tiles, tiles)
  end

  # Gets the {RedBird::Sprite} at position x, y.
  #
  # @param x [Integer]
  # @param y [Integer]
  # @author Frederico Linhares
  def tile_xy(x, y)
    return @tile_set.tiles[@tiles[y_to_tile(y) + x]][:type]
  end

  # Renders this tilemap to the screen.
  #
  # @author Frederico Linhares
  def render
    first_hor_tile = @hor_offset / @tile_set.tile_width
    first_ver_tile = @ver_offset / @tile_set.tile_height

    (first_ver_tile..(first_ver_tile + @render_ver_tiles)).each do |y|
      (first_hor_tile..(first_hor_tile + @render_hor_tiles)).each do |x|
        unless @tiles[y_to_tile(y) + x].nil? then
          @tile_set.tiles[@tiles[y_to_tile(y) + x]].render_to_screen(
            x * @tile_set.tile_width - @hor_offset + @pos_x,
            y * @tile_set.tile_height - @ver_offset + @pos_y)
        end
      end
    end
  end

  private
  def y_to_tile(y)
    return y * @total_hor_tiles
  end
end

#ver_offsetInteger

Returns how much the tilemap scrolled from the top.

Returns:

  • (Integer)

    how much the tilemap scrolled from the top.



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
56
57
58
59
60
61
62
63
64
65
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/red_bird/tile_map.rb', line 23

class TileMap < Entity
  attr_accessor :hor_offset, :ver_offset
  attr_reader :tiles, :tile_set, :total_width, :total_height

  # @param tile_set [RedBird::TileSet]
  # @param pos_x [Integer] horizontal position of this map in the screen.
  # @param pos_y [Integer] vertical position of this map in the screen.
  # @param width [Integer] width of the region displayed in the screen.
  # @param height [Integer] height of the region displayed in the screen.
  # @param num_hor_tiles [Integer] total width in number of tiles.
  # @param num_ver_tiles [Integer] total height in number of tiles.
  # @param tiles [Array<Integer>] code of every tile to display, must be an
  #   one dimension array.
  # @author Frederico Linhares
  def initialize(tile_set, pos_x, pos_y, width, height, num_hor_tiles,
                 num_ver_tiles, tiles)
    @tile_set = tile_set
    @pos_x = pos_x
    @pos_y = pos_y

    @width = width
    @height = height

    @ver_offset = 0
    @hor_offset = 0

    @priority = -1

    @tiles = tiles

    # Number of tiles that are going to be rendered to screen.
    @render_hor_tiles = @width / @tile_set.tile_width + 1
    @render_ver_tiles = @height / @tile_set.tile_height + 1

    @total_hor_tiles = num_hor_tiles
    @total_ver_tiles = num_ver_tiles

    @total_width = @total_hor_tiles * @tile_set.tile_width
    @total_height = @total_ver_tiles * @tile_set.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 map_file [String] path to the yaml file.
  # @return [RedBird::TileMap]
  # @author Frederico Linhares
  def self.from_yml(data_dir, map_file, tile_set, pos_x, pos_y, width,
                    height)
    yaml_file = data_dir + map_file

    data = YAML.load_file(yaml_file)
    num_hor_tiles = data["num_hor_tiles"]
    num_ver_tiles = data["num_ver_tiles"]
    tiles = data["tiles"].unpack("S*")

    return self.new(
             tile_set, pos_x, pos_y, width, height, num_hor_tiles,
             num_ver_tiles, tiles)
  end

  # Gets the {RedBird::Sprite} at position x, y.
  #
  # @param x [Integer]
  # @param y [Integer]
  # @author Frederico Linhares
  def tile_xy(x, y)
    return @tile_set.tiles[@tiles[y_to_tile(y) + x]][:type]
  end

  # Renders this tilemap to the screen.
  #
  # @author Frederico Linhares
  def render
    first_hor_tile = @hor_offset / @tile_set.tile_width
    first_ver_tile = @ver_offset / @tile_set.tile_height

    (first_ver_tile..(first_ver_tile + @render_ver_tiles)).each do |y|
      (first_hor_tile..(first_hor_tile + @render_hor_tiles)).each do |x|
        unless @tiles[y_to_tile(y) + x].nil? then
          @tile_set.tiles[@tiles[y_to_tile(y) + x]].render_to_screen(
            x * @tile_set.tile_width - @hor_offset + @pos_x,
            y * @tile_set.tile_height - @ver_offset + @pos_y)
        end
      end
    end
  end

  private
  def y_to_tile(y)
    return y * @total_hor_tiles
  end
end

Class Method Details

.from_yml(data_dir, map_file, tile_set, pos_x, pos_y, width, height) ⇒ RedBird::TileMap

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.

  • map_file (String)

    path to the yaml file.

Returns:

Author:

  • Frederico Linhares



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/red_bird/tile_map.rb', line 72

def self.from_yml(data_dir, map_file, tile_set, pos_x, pos_y, width,
                  height)
  yaml_file = data_dir + map_file

  data = YAML.load_file(yaml_file)
  num_hor_tiles = data["num_hor_tiles"]
  num_ver_tiles = data["num_ver_tiles"]
  tiles = data["tiles"].unpack("S*")

  return self.new(
           tile_set, pos_x, pos_y, width, height, num_hor_tiles,
           num_ver_tiles, tiles)
end

Instance Method Details

#renderObject

Renders this tilemap to the screen.

Author:

  • Frederico Linhares



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/red_bird/tile_map.rb', line 98

def render
  first_hor_tile = @hor_offset / @tile_set.tile_width
  first_ver_tile = @ver_offset / @tile_set.tile_height

  (first_ver_tile..(first_ver_tile + @render_ver_tiles)).each do |y|
    (first_hor_tile..(first_hor_tile + @render_hor_tiles)).each do |x|
      unless @tiles[y_to_tile(y) + x].nil? then
        @tile_set.tiles[@tiles[y_to_tile(y) + x]].render_to_screen(
          x * @tile_set.tile_width - @hor_offset + @pos_x,
          y * @tile_set.tile_height - @ver_offset + @pos_y)
      end
    end
  end
end

#tile_xy(x, y) ⇒ Object

Gets the Sprite at position x, y.

Parameters:

  • x (Integer)
  • y (Integer)

Author:

  • Frederico Linhares



91
92
93
# File 'lib/red_bird/tile_map.rb', line 91

def tile_xy(x, y)
  return @tile_set.tiles[@tiles[y_to_tile(y) + x]][:type]
end