Permalink
Browse files

getting directions working for bomb throwing

  • Loading branch information...
1 parent 9acca70 commit 5aeb11f09bc03079d348a7c64b79ee37e76e044f @ryanb committed Jan 3, 2010
@@ -1,27 +1,25 @@
module RubyWarrior
module Abilities
class Base
- DIRECTIONS = {
- :forward => [1, 0],
- :right => [0, 1],
- :backward => [-1, 0],
- :left => [0, -1]
- }
-
def initialize(unit)
@unit = unit
end
- def offset(direction, amount = 1)
- DIRECTIONS[direction].map { |i| i*amount }
+ def offset(direction, forward = 1, right = 0)
+ case direction
+ when :forward then [forward, -right]
+ when :backward then [-forward, right]
+ when :right then [right, forward]
+ when :left then [-right, -forward]
+ end
end
- def space(direction, amount = 1)
- @unit.position.relative_space(*offset(direction, amount))
+ def space(direction, forward = 1, right = 0)
+ @unit.position.relative_space(*offset(direction, forward, right))
end
- def unit(direction, amount = 1)
- space(direction, amount).unit
+ def unit(direction, forward = 1, right = 0)
+ space(direction, forward, right).unit
end
def damage(receiver, amount)
@@ -7,11 +7,11 @@ def description
def perform(direction = :forward)
if @unit.position
- @unit.say "throws a bomb"
- receiver = @unit.position.relative_space(2, 0).unit
+ @unit.say "throws a bomb #{direction}"
+ receiver = space(direction, 2, 0).unit
damage(receiver, 10) if receiver
[[2, 1], [2, -1], [3, 0], [1, 0]].each do |x, y|
- receiver = @unit.position.relative_space(x, y).unit
+ receiver = space(direction, x, y).unit
damage(receiver, 1) if receiver
end
end
@@ -13,9 +13,17 @@
@ability.offset(:left).should == [0, -1]
end
+ it "should have offset for relative forward/right amounts" do
+ @ability.offset(:forward, 2).should == [2, 0]
+ @ability.offset(:forward, 2, 1).should == [2, -1]
+ @ability.offset(:right, 2, 1).should == [1, 2]
+ @ability.offset(:backward, 2, 1).should == [-2, 1]
+ @ability.offset(:left, 2, 1).should == [-1, -2]
+ end
+
it "should fetch unit at given direction with distance" do
- @ability.expects(:space).with(:right, 3).returns(stub(:unit => 'unit'))
- @ability.unit(:right, 3).should == 'unit'
+ @ability.expects(:space).with(:right, 3, 1).returns(stub(:unit => 'unit'))
+ @ability.unit(:right, 3, 1).should == 'unit'
end
it "should have no description" do
@@ -3,14 +3,14 @@
describe RubyWarrior::Abilities::Throw do
before(:each) do
@floor = RubyWarrior::Floor.new
- @floor.width = 2
+ @floor.width = 3
@floor.height = 3
@warrior = RubyWarrior::Units::Warrior.new
@floor.add(@warrior, 0, 0, :south)
@throw = RubyWarrior::Abilities::Throw.new(@warrior)
end
- it "should subtract 10 from units tow spaces away and 1 from units surrounding that space" do
+ it "should subtract 10 from unit two spaces forward and 1 from units surrounding that space" do
target_unit = RubyWarrior::Units::Base.new
target_unit.health = 15
second_unit = RubyWarrior::Units::Base.new
@@ -21,4 +21,16 @@
target_unit.health.should == 5
second_unit.health.should == 14
end
+
+ it "should subtract 10 from unit two spaces left and 1 from units surrounding that space" do
+ target_unit = RubyWarrior::Units::Base.new
+ target_unit.health = 15
+ second_unit = RubyWarrior::Units::Base.new
+ second_unit.health = 15
+ @floor.add(target_unit, 2, 0)
+ @floor.add(second_unit, 2, 1)
+ @throw.perform(:left)
+ target_unit.health.should == 5
+ second_unit.health.should == 14
+ end
end

0 comments on commit 5aeb11f

Please sign in to comment.