Class: RubyWarrior::Floor
- Inherits:
-
Object
- Object
- RubyWarrior::Floor
- Defined in:
- lib/ruby_warrior/floor.rb
Instance Attribute Summary collapse
-
#grid ⇒ Object
Returns the value of attribute grid.
-
#height ⇒ Object
Returns the value of attribute height.
-
#stairs_location ⇒ Object
readonly
Returns the value of attribute stairs_location.
-
#width ⇒ Object
Returns the value of attribute width.
Instance Method Summary collapse
- #add(unit, x, y, direction = nil) ⇒ Object
- #get(x, y) ⇒ Object
-
#initialize ⇒ Floor
constructor
A new instance of Floor.
- #other_units ⇒ Object
- #out_of_bounds?(x, y) ⇒ Boolean
- #place_stairs(x, y) ⇒ Object
- #space(x, y) ⇒ Object
- #stairs_space ⇒ Object
- #to_map ⇒ Object
- #unique_units ⇒ Object
- #units ⇒ Object
Constructor Details
#initialize ⇒ Floor
Returns a new instance of Floor.
6 7 8 9 10 11 |
# File 'lib/ruby_warrior/floor.rb', line 6 def initialize @width = 0 @height = 0 @units = [] @stairs_location = [-1, -1] end |
Instance Attribute Details
#grid ⇒ Object
Returns the value of attribute grid.
3 4 5 |
# File 'lib/ruby_warrior/floor.rb', line 3 def grid @grid end |
#height ⇒ Object
Returns the value of attribute height.
3 4 5 |
# File 'lib/ruby_warrior/floor.rb', line 3 def height @height end |
#stairs_location ⇒ Object (readonly)
Returns the value of attribute stairs_location.
4 5 6 |
# File 'lib/ruby_warrior/floor.rb', line 4 def stairs_location @stairs_location end |
#width ⇒ Object
Returns the value of attribute width.
3 4 5 |
# File 'lib/ruby_warrior/floor.rb', line 3 def width @width end |
Instance Method Details
#add(unit, x, y, direction = nil) ⇒ Object
13 14 15 16 |
# File 'lib/ruby_warrior/floor.rb', line 13 def add(unit, x, y, direction = nil) @units << unit unit.position = Position.new(self, x, y, direction) end |
#get(x, y) ⇒ Object
34 35 36 37 38 |
# File 'lib/ruby_warrior/floor.rb', line 34 def get(x, y) units.detect do |unit| unit.position.at?(x, y) end end |
#other_units ⇒ Object
30 31 32 |
# File 'lib/ruby_warrior/floor.rb', line 30 def other_units units.reject { |u| u.kind_of? Units::Warrior } end |
#out_of_bounds?(x, y) ⇒ Boolean
44 45 46 |
# File 'lib/ruby_warrior/floor.rb', line 44 def out_of_bounds?(x, y) x < 0 || y < 0 || x > @width-1 || y > @height-1 end |
#place_stairs(x, y) ⇒ Object
18 19 20 |
# File 'lib/ruby_warrior/floor.rb', line 18 def place_stairs(x, y) @stairs_location = [x, y] end |
#space(x, y) ⇒ Object
40 41 42 |
# File 'lib/ruby_warrior/floor.rb', line 40 def space(x, y) Space.new(self, x, y) end |
#stairs_space ⇒ Object
22 23 24 |
# File 'lib/ruby_warrior/floor.rb', line 22 def stairs_space space(*@stairs_location) end |
#to_map ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/ruby_warrior/floor.rb', line 48 def to_map rows = [] rows << " " + ("-" * @width) @height.times do |y| row = "|" @width.times do |x| row << space(x, y).to_map end row << "|" rows << row end rows << " " + ("-" * @width) rows.join("\n") + "\n" end |
#unique_units ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/ruby_warrior/floor.rb', line 63 def unique_units unique_units = [] units.each do |unit| unique_units << unit unless unique_units.map { |u| u.class }.include?(unit.class) end unique_units end |
#units ⇒ Object
26 27 28 |
# File 'lib/ruby_warrior/floor.rb', line 26 def units @units.reject { |u| u.position.nil? } end |