Class: RubyWarrior::Position

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

Constant Summary collapse

DIRECTIONS =
[:north, :east, :south, :west]
RELATIVE_DIRECTIONS =
[:forward, :right, :backward, :left]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(floor, x, y, direction = nil) ⇒ Position

Returns a new instance of Position.



7
8
9
10
11
12
# File 'lib/ruby_warrior/position.rb', line 7

def initialize(floor, x, y, direction = nil)
  @floor = floor
  @x = x
  @y = y
  @direction_index = DIRECTIONS.index(direction || :north)
end

Instance Attribute Details

#floorObject (readonly)

Returns the value of attribute floor.



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

def floor
  @floor
end

Instance Method Details

#at?(x, y) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/ruby_warrior/position.rb', line 14

def at?(x, y)
  @x == x && @y == y
end

#directionObject



18
19
20
# File 'lib/ruby_warrior/position.rb', line 18

def direction
  DIRECTIONS[@direction_index]
end

#direction_of(space) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/ruby_warrior/position.rb', line 57

def direction_of(space)
  space_x, space_y = *space.location
  if (@x - space_x).abs > (@y - space_y).abs
    space_x > @x ? :east : :west
  else
    space_y > @y ? :south : :north
  end
end

#distance_from_stairsObject



40
41
42
# File 'lib/ruby_warrior/position.rb', line 40

def distance_from_stairs
  distance_of(@floor.stairs_space)
end

#distance_of(space) ⇒ Object



44
45
46
47
# File 'lib/ruby_warrior/position.rb', line 44

def distance_of(space)
  x, y = *space.location
  (@x - x).abs + (@y - y).abs
end

#move(forward, right = 0) ⇒ Object



36
37
38
# File 'lib/ruby_warrior/position.rb', line 36

def move(forward, right = 0)
  @x, @y = *translate_offset(forward, right)
end

#relative_direction(direction) ⇒ Object



66
67
68
69
70
71
# File 'lib/ruby_warrior/position.rb', line 66

def relative_direction(direction)
  offset = DIRECTIONS.index(direction) - @direction_index
  offset -= 4 while offset > 3
  offset += 4 while offset < 0
  RELATIVE_DIRECTIONS[offset]
end

#relative_direction_of(space) ⇒ Object



53
54
55
# File 'lib/ruby_warrior/position.rb', line 53

def relative_direction_of(space)
  relative_direction(direction_of(space))
end

#relative_direction_of_stairsObject



49
50
51
# File 'lib/ruby_warrior/position.rb', line 49

def relative_direction_of_stairs
  relative_direction_of(@floor.stairs_space)
end

#relative_space(forward, right = 0) ⇒ Object



28
29
30
# File 'lib/ruby_warrior/position.rb', line 28

def relative_space(forward, right = 0)
  @floor.space(*translate_offset(forward, right))
end

#rotate(amount) ⇒ Object



22
23
24
25
26
# File 'lib/ruby_warrior/position.rb', line 22

def rotate(amount)
  @direction_index += amount
  @direction_index -= 4 while @direction_index > 3
  @direction_index += 4 while @direction_index < 0
end

#spaceObject



32
33
34
# File 'lib/ruby_warrior/position.rb', line 32

def space
  @floor.space(@x, @y)
end

#translate_offset(forward, right) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/ruby_warrior/position.rb', line 73

def translate_offset(forward, right)
  case direction
  when :north then [@x + right, @y - forward]
  when :east then [@x + forward, @y + right]
  when :south then [@x - right, @y + forward]
  when :west then [@x - forward, @y - right]
  end
end