Skip to content

Commit

Permalink
adding direction_of ability to determine direction towards any given …
Browse files Browse the repository at this point in the history
…space
  • Loading branch information
ryanb committed Sep 13, 2008
1 parent e7ae699 commit 934edcb
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 7 deletions.
1 change: 1 addition & 0 deletions lib/ruby_warrior.rb
Expand Up @@ -33,3 +33,4 @@
require 'ruby_warrior/abilities/bind'
require 'ruby_warrior/abilities/listen'
require 'ruby_warrior/abilities/direction_of_stairs'
require 'ruby_warrior/abilities/direction_of'
13 changes: 13 additions & 0 deletions lib/ruby_warrior/abilities/direction_of.rb
@@ -0,0 +1,13 @@
module RubyWarrior
module Abilities
class DirectionOf < Base
def description
"Pass a Space as an argument, and the direction (:left, :right, :forward, :backward) to that space will be returned."
end

def perform(space)
@unit.position.relative_direction_of(space)
end
end
end
end
2 changes: 1 addition & 1 deletion lib/ruby_warrior/abilities/direction_of_stairs.rb
Expand Up @@ -10,4 +10,4 @@ def perform
end
end
end
end
end
10 changes: 7 additions & 3 deletions lib/ruby_warrior/position.rb
Expand Up @@ -43,11 +43,15 @@ def distance_from_stairs
end

def relative_direction_of_stairs
relative_direction(direction_of_stairs)
relative_direction_of(@floor.stairs_space)
end

def direction_of_stairs
stairs_x, stairs_y = *@floor.stairs_location
def relative_direction_of(space)
relative_direction(direction_of(space))
end

def direction_of(space)
stairs_x, stairs_y = *space.location
if (@x - stairs_x).abs > (@y - stairs_y).abs
if stairs_x > @x
:west
Expand Down
6 changes: 5 additions & 1 deletion lib/ruby_warrior/space.rb
Expand Up @@ -25,13 +25,17 @@ def empty?
end

def stairs?
@floor.stairs_location == [@x, @y]
@floor.stairs_location == location
end

def unit
@floor.get(@x, @y)
end

def location
[@x, @y]
end

def to_map
if unit
unit.to_map
Expand Down
13 changes: 13 additions & 0 deletions spec/ruby_warrior/abilities/direction_of_spec.rb
@@ -0,0 +1,13 @@
require File.dirname(__FILE__) + '/../../spec_helper'

describe RubyWarrior::Abilities::DirectionOf do
before(:each) do
@position = stub
@distance = RubyWarrior::Abilities::DirectionOf.new(stub(:position => @position, :say => nil))
end

it "should return relative direction of given space" do
@position.stubs(:relative_direction_of).with(:space).returns(:left)
@distance.perform(:space).should == :left
end
end
7 changes: 5 additions & 2 deletions spec/ruby_warrior/position_spec.rb
Expand Up @@ -73,12 +73,15 @@
@position.distance_from_stairs.should == 2
end

it "should return direction and relative direction of stairs" do
it "should return relative direction of stairs" do
@floor.place_stairs(0, 0)
@position.direction_of_stairs.should == :north
@position.relative_direction_of_stairs.should == :forward
end

it "should return relative direction of given space" do
@position.relative_direction_of(@floor.space(0, 0)).should == :forward
end

it "should be able to determine relative direction" do
@position.relative_direction(:north).should == :forward
@position.rotate 1
Expand Down

0 comments on commit 934edcb

Please sign in to comment.