Skip to content

Commit 5aeb11f

Browse files
committed
getting directions working for bomb throwing
1 parent 9acca70 commit 5aeb11f

File tree

4 files changed

+38
-20
lines changed

4 files changed

+38
-20
lines changed

lib/ruby_warrior/abilities/base.rb

+11-13
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
module RubyWarrior
22
module Abilities
33
class Base
4-
DIRECTIONS = {
5-
:forward => [1, 0],
6-
:right => [0, 1],
7-
:backward => [-1, 0],
8-
:left => [0, -1]
9-
}
10-
114
def initialize(unit)
125
@unit = unit
136
end
147

15-
def offset(direction, amount = 1)
16-
DIRECTIONS[direction].map { |i| i*amount }
8+
def offset(direction, forward = 1, right = 0)
9+
case direction
10+
when :forward then [forward, -right]
11+
when :backward then [-forward, right]
12+
when :right then [right, forward]
13+
when :left then [-right, -forward]
14+
end
1715
end
1816

19-
def space(direction, amount = 1)
20-
@unit.position.relative_space(*offset(direction, amount))
17+
def space(direction, forward = 1, right = 0)
18+
@unit.position.relative_space(*offset(direction, forward, right))
2119
end
2220

23-
def unit(direction, amount = 1)
24-
space(direction, amount).unit
21+
def unit(direction, forward = 1, right = 0)
22+
space(direction, forward, right).unit
2523
end
2624

2725
def damage(receiver, amount)

lib/ruby_warrior/abilities/throw.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ def description
77

88
def perform(direction = :forward)
99
if @unit.position
10-
@unit.say "throws a bomb"
11-
receiver = @unit.position.relative_space(2, 0).unit
10+
@unit.say "throws a bomb #{direction}"
11+
receiver = space(direction, 2, 0).unit
1212
damage(receiver, 10) if receiver
1313
[[2, 1], [2, -1], [3, 0], [1, 0]].each do |x, y|
14-
receiver = @unit.position.relative_space(x, y).unit
14+
receiver = space(direction, x, y).unit
1515
damage(receiver, 1) if receiver
1616
end
1717
end

spec/ruby_warrior/abilities/base_spec.rb

+10-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,17 @@
1313
@ability.offset(:left).should == [0, -1]
1414
end
1515

16+
it "should have offset for relative forward/right amounts" do
17+
@ability.offset(:forward, 2).should == [2, 0]
18+
@ability.offset(:forward, 2, 1).should == [2, -1]
19+
@ability.offset(:right, 2, 1).should == [1, 2]
20+
@ability.offset(:backward, 2, 1).should == [-2, 1]
21+
@ability.offset(:left, 2, 1).should == [-1, -2]
22+
end
23+
1624
it "should fetch unit at given direction with distance" do
17-
@ability.expects(:space).with(:right, 3).returns(stub(:unit => 'unit'))
18-
@ability.unit(:right, 3).should == 'unit'
25+
@ability.expects(:space).with(:right, 3, 1).returns(stub(:unit => 'unit'))
26+
@ability.unit(:right, 3, 1).should == 'unit'
1927
end
2028

2129
it "should have no description" do

spec/ruby_warrior/abilities/throw_spec.rb

+14-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
describe RubyWarrior::Abilities::Throw do
44
before(:each) do
55
@floor = RubyWarrior::Floor.new
6-
@floor.width = 2
6+
@floor.width = 3
77
@floor.height = 3
88
@warrior = RubyWarrior::Units::Warrior.new
99
@floor.add(@warrior, 0, 0, :south)
1010
@throw = RubyWarrior::Abilities::Throw.new(@warrior)
1111
end
1212

13-
it "should subtract 10 from units tow spaces away and 1 from units surrounding that space" do
13+
it "should subtract 10 from unit two spaces forward and 1 from units surrounding that space" do
1414
target_unit = RubyWarrior::Units::Base.new
1515
target_unit.health = 15
1616
second_unit = RubyWarrior::Units::Base.new
@@ -21,4 +21,16 @@
2121
target_unit.health.should == 5
2222
second_unit.health.should == 14
2323
end
24+
25+
it "should subtract 10 from unit two spaces left and 1 from units surrounding that space" do
26+
target_unit = RubyWarrior::Units::Base.new
27+
target_unit.health = 15
28+
second_unit = RubyWarrior::Units::Base.new
29+
second_unit.health = 15
30+
@floor.add(target_unit, 2, 0)
31+
@floor.add(second_unit, 2, 1)
32+
@throw.perform(:left)
33+
target_unit.health.should == 5
34+
second_unit.health.should == 14
35+
end
2436
end

0 commit comments

Comments
 (0)