class Toy::UnitCollection

Attributes

all[R]

Public Class Methods

new(direction=Toy::Direction) click to toggle source
# File lib/toy/unit_collection.rb, line 7
def initialize(direction=Toy::Direction)
  @all = []
  @direction = direction
end

Public Instance Methods

add(unit) click to toggle source
# File lib/toy/unit_collection.rb, line 12
def add(unit)
  raise Toy::UnitCollection::Error::UnitError unless unit.is_a?(Unit)
  
  @all << unit
end
find_by_coordinates(x, y) click to toggle source
# File lib/toy/unit_collection.rb, line 18
def find_by_coordinates(x, y)
  @all.find { |unit| unit.x == x && unit.y == y }
end
find_by_direction_of(x, y, direction) click to toggle source
# File lib/toy/unit_collection.rb, line 42
def find_by_direction_of(x, y, direction)
  raise Toy::UnitCollection::Error::DirectionError unless Toy::Direction::DIRECTION.include?(direction)

  send("find_#{direction}_of", x, y)
end
find_by_object(object) click to toggle source
# File lib/toy/unit_collection.rb, line 22
def find_by_object(object)
  @all.find { |unit| unit.object == object }
end
find_east_of(x, y) click to toggle source
# File lib/toy/unit_collection.rb, line 30
def find_east_of(x, y)
  @all.find { |unit| unit.x == x.succ && unit.y == y }
end
find_north_of(x, y) click to toggle source
# File lib/toy/unit_collection.rb, line 26
def find_north_of(x, y)
  @all.find { |unit| unit.x == x && unit.y == y.succ }
end
find_south_of(x, y) click to toggle source
# File lib/toy/unit_collection.rb, line 34
def find_south_of(x, y)
  @all.find { |unit| unit.x == x && unit.y == y.pred }
end
find_west_of(x, y) click to toggle source
# File lib/toy/unit_collection.rb, line 38
def find_west_of(x, y)
  @all.find { |unit| unit.x == x.pred && unit.y == y }
end