Permalink
Browse files

adding distance_of ability - closes #21

  • Loading branch information...
1 parent e1c4876 commit c44b6ba11344d7ec478a113409114b1b95437655 @ryanb committed Jan 3, 2010
View
2 lib/ruby_warrior.rb
@@ -36,7 +36,7 @@
require 'ruby_warrior/abilities/shoot'
require 'ruby_warrior/abilities/rescue'
require 'ruby_warrior/abilities/pivot'
-require 'ruby_warrior/abilities/distance'
+require 'ruby_warrior/abilities/distance_of'
require 'ruby_warrior/abilities/bind'
require 'ruby_warrior/abilities/listen'
require 'ruby_warrior/abilities/direction_of_stairs'
View
13 lib/ruby_warrior/abilities/distance.rb
@@ -1,13 +0,0 @@
-module RubyWarrior
- module Abilities
- class Distance < Base
- def description
- "Returns the number of spaces the stairs are away."
- end
-
- def perform
- @unit.position.distance_from_stairs
- end
- end
- end
-end
View
13 lib/ruby_warrior/abilities/distance_of.rb
@@ -0,0 +1,13 @@
+module RubyWarrior
+ module Abilities
+ class DistanceOf < Base
+ def description
+ "Pass a Space as an argument, and it will return an integer representing the distance to that space."
+ end
+
+ def perform(space)
+ @unit.position.distance_of(space)
+ end
+ end
+ end
+end
View
8 lib/ruby_warrior/position.rb
@@ -38,8 +38,12 @@ def move(forward, right = 0)
end
def distance_from_stairs
- stairs_x, stairs_y = *@floor.stairs_location
- (@x - stairs_x).abs + (@y - stairs_y).abs
+ distance_of(@floor.stairs_space)
+ end
+
+ def distance_of(space)
+ x, y = *space.location
+ (@x - x).abs + (@y - y).abs
end
def relative_direction_of_stairs
View
13 spec/ruby_warrior/abilities/distance_of_spec.rb
@@ -0,0 +1,13 @@
+require File.dirname(__FILE__) + '/../../spec_helper'
+
+describe RubyWarrior::Abilities::DistanceOf do
+ before(:each) do
+ @position = stub
+ @distance = RubyWarrior::Abilities::DistanceOf.new(stub(:position => @position, :say => nil))
+ end
+
+ it "should return distance from stairs" do
+ @position.stubs(:distance_of).with(:space).returns(5)
+ @distance.perform(:space).should == 5
+ end
+end
View
13 spec/ruby_warrior/abilities/distance_spec.rb
@@ -1,13 +0,0 @@
-require File.dirname(__FILE__) + '/../../spec_helper'
-
-describe RubyWarrior::Abilities::Distance do
- before(:each) do
- @position = stub
- @distance = RubyWarrior::Abilities::Distance.new(stub(:position => @position, :say => nil))
- end
-
- it "should return distance from stairs" do
- @position.stubs(:distance_from_stairs).returns(5)
- @distance.perform.should == 5
- end
-end
View
5 spec/ruby_warrior/position_spec.rb
@@ -100,4 +100,9 @@
it "should return a space at the same location as position" do
@position.space.location.should == [1, 2]
end
+
+ it "should return distance of given space" do
+ @position.distance_of(@floor.space(5, 3)).should == 5
+ @position.distance_of(@floor.space(4, 2)).should == 3
+ end
end

0 comments on commit c44b6ba

Please sign in to comment.