Class: Goby::Map

Inherits:
Object
  • Object
show all
Defined in:
lib/goby/map/map.rb

Overview

A 2D arrangement of Tiles. The Player can move around on it.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: "Map", tiles: [[Tile.new]], music: nil) ⇒ Map

Returns a new instance of Map.

Parameters:

  • name (String) (defaults to: "Map")

    the name.

  • tiles ([Tile]) (defaults to: [[Tile.new]])

    the content of the map.



8
9
10
11
12
# File 'lib/goby/map/map.rb', line 8

def initialize(name: "Map", tiles: [[Tile.new]], music: nil)
  @name = name
  @tiles = tiles
  @music = music
end

Instance Attribute Details

#musicObject

Returns the value of attribute music.



41
42
43
# File 'lib/goby/map/map.rb', line 41

def music
  @music
end

#nameObject

Returns the value of attribute name.



41
42
43
# File 'lib/goby/map/map.rb', line 41

def name
  @name
end

#tilesObject

Returns the value of attribute tiles.



41
42
43
# File 'lib/goby/map/map.rb', line 41

def tiles
  @tiles
end

Instance Method Details

#==(rhs) ⇒ Object

Parameters:

  • rhs (Map)

    the Map on the right.



37
38
39
# File 'lib/goby/map/map.rb', line 37

def ==(rhs)
  return @name == rhs.name
end

#in_bounds(y, x) ⇒ Boolean

Returns true when @tiles[x] is an existing index of @tiles. Otherwise, returns false.

Parameters:

Returns:

  • (Boolean)

    the existence of the tile.



20
21
22
# File 'lib/goby/map/map.rb', line 20

def in_bounds(y, x)
  return (y.nonnegative? && y < @tiles.length && x.nonnegative? && x < @tiles[y].length)
end

#to_sObject

Prints the map in a nice format.



25
26
27
28
29
30
31
32
33
34
# File 'lib/goby/map/map.rb', line 25

def to_s
  output = ""
  @tiles.each do |row|
    row.each do |tile|
      output += (tile.graphic + " ")
    end
    output += "\n"
  end
  return output
end