Class: RubyWarrior::Floor

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_warrior/floor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFloor

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

#gridObject

Returns the value of attribute grid.



3
4
5
# File 'lib/ruby_warrior/floor.rb', line 3

def grid
  @grid
end

#heightObject

Returns the value of attribute height.



3
4
5
# File 'lib/ruby_warrior/floor.rb', line 3

def height
  @height
end

#stairs_locationObject (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

#widthObject

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

#characterObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ruby_warrior/floor.rb', line 48

def character
  rows = []
  rows << " " + ("-" * @width)
  @height.times do |y|
    row = "|"
    @width.times do |x|
      row << space(x, y).character
    end
    row << "|"
    rows << row
  end
  rows << " " + ("-" * @width)
  rows.join("\n") + "\n"
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_unitsObject



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

Returns:

  • (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_spaceObject



22
23
24
# File 'lib/ruby_warrior/floor.rb', line 22

def stairs_space
  space(*@stairs_location)
end

#unique_unitsObject



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

#unitsObject



26
27
28
# File 'lib/ruby_warrior/floor.rb', line 26

def units
  @units.reject { |u| u.position.nil? }
end