From 1ddb687e7d65e7971b848aea3d0a9d0b20532e36 Mon Sep 17 00:00:00 2001 From: Ryan Bates Date: Fri, 5 Sep 2008 21:22:03 -0700 Subject: [PATCH] adding bind ability to keep a unit from moving, but makes him look like a captive --- lib/ruby_warrior.rb | 1 + lib/ruby_warrior/abilities/bind.rb | 19 +++++++++++++++++++ lib/ruby_warrior/abilities/rescue.rb | 9 ++++++--- lib/ruby_warrior/space.rb | 2 +- lib/ruby_warrior/units/base.rb | 14 ++++++++++++++ lib/ruby_warrior/units/captive.rb | 4 ++++ spec/ruby_warrior/abilities/bind_spec.rb | 19 +++++++++++++++++++ spec/ruby_warrior/abilities/rescue_spec.rb | 14 +++++++++++++- spec/ruby_warrior/space_spec.rb | 14 ++++++++++++++ spec/ruby_warrior/units/base_spec.rb | 14 ++++++++++++++ spec/ruby_warrior/units/captive_spec.rb | 4 ++++ 11 files changed, 109 insertions(+), 5 deletions(-) create mode 100644 lib/ruby_warrior/abilities/bind.rb create mode 100644 spec/ruby_warrior/abilities/bind_spec.rb diff --git a/lib/ruby_warrior.rb b/lib/ruby_warrior.rb index 206272f9..8f5dbc09 100644 --- a/lib/ruby_warrior.rb +++ b/lib/ruby_warrior.rb @@ -30,3 +30,4 @@ require 'ruby_warrior/abilities/rescue' require 'ruby_warrior/abilities/pivot' require 'ruby_warrior/abilities/distance' +require 'ruby_warrior/abilities/bind' diff --git a/lib/ruby_warrior/abilities/bind.rb b/lib/ruby_warrior/abilities/bind.rb new file mode 100644 index 00000000..6bde1d89 --- /dev/null +++ b/lib/ruby_warrior/abilities/bind.rb @@ -0,0 +1,19 @@ +module RubyWarrior + module Abilities + class Bind < Base + def description + "Bind unit in given direction to keep him from moving (forward by default)." + end + + def perform(direction = :forward) + receiver = unit(direction) + if receiver + @unit.say "binds #{receiver}" + receiver.bind + else + @unit.say "binds nothing" + end + end + end + end +end diff --git a/lib/ruby_warrior/abilities/rescue.rb b/lib/ruby_warrior/abilities/rescue.rb index b3ed6544..7ac04ae5 100644 --- a/lib/ruby_warrior/abilities/rescue.rb +++ b/lib/ruby_warrior/abilities/rescue.rb @@ -7,9 +7,12 @@ def description def perform(direction = :forward) if space(direction).captive? - @unit.say "rescues captive" - unit(direction).position = nil - @unit.earn_points(20) + recipient = unit(direction) + recipient.unbind + if recipient.kind_of? Units::Captive + recipient.position = nil + @unit.earn_points(20) + end end end end diff --git a/lib/ruby_warrior/space.rb b/lib/ruby_warrior/space.rb index bb9a8d48..bd20df8f 100644 --- a/lib/ruby_warrior/space.rb +++ b/lib/ruby_warrior/space.rb @@ -17,7 +17,7 @@ def enemy? end def captive? - unit.kind_of? Units::Captive + unit.bound? end def empty? diff --git a/lib/ruby_warrior/units/base.rb b/lib/ruby_warrior/units/base.rb index db05c907..71d0b545 100644 --- a/lib/ruby_warrior/units/base.rb +++ b/lib/ruby_warrior/units/base.rb @@ -20,6 +20,7 @@ def health end def take_damage(amount) + unbind if bound? if health self.health -= amount say "takes #{amount} damage, #{health} health power left" @@ -34,6 +35,19 @@ def alive? !position.nil? end + def bound? + @bound + end + + def unbind + say "released from bonds" + @bound = false + end + + def bind + @bound = true + end + def say(msg) UI.puts_with_delay "#{name} #{msg}" end diff --git a/lib/ruby_warrior/units/captive.rb b/lib/ruby_warrior/units/captive.rb index 3b83020d..58209960 100644 --- a/lib/ruby_warrior/units/captive.rb +++ b/lib/ruby_warrior/units/captive.rb @@ -1,6 +1,10 @@ module RubyWarrior module Units class Captive < Base + def initialize + bind + end + def max_health 1 end diff --git a/spec/ruby_warrior/abilities/bind_spec.rb b/spec/ruby_warrior/abilities/bind_spec.rb new file mode 100644 index 00000000..239dae13 --- /dev/null +++ b/spec/ruby_warrior/abilities/bind_spec.rb @@ -0,0 +1,19 @@ +require File.dirname(__FILE__) + '/../../spec_helper' + +describe RubyWarrior::Abilities::Bind do + before(:each) do + @bind = RubyWarrior::Abilities::Bind.new(stub(:say => nil)) + end + + it "should bind recipient" do + receiver = RubyWarrior::Units::Base.new + @bind.stubs(:unit).returns(receiver) + @bind.perform + receiver.should be_bound + end + + it "should do nothing if no recipient" do + @bind.stubs(:unit).returns(nil) + lambda { @bind.perform }.should_not raise_error + end +end diff --git a/spec/ruby_warrior/abilities/rescue_spec.rb b/spec/ruby_warrior/abilities/rescue_spec.rb index 67ce6434..a4a154ac 100644 --- a/spec/ruby_warrior/abilities/rescue_spec.rb +++ b/spec/ruby_warrior/abilities/rescue_spec.rb @@ -16,7 +16,7 @@ captive.position.should be_nil end - it "should do nothing to other unit" do + it "should do nothing to other unit if not bound" do unit = RubyWarrior::Units::Base.new unit.position = stub @rescue.expects(:space).with(:forward).returns(stub(:captive? => false)) @@ -25,4 +25,16 @@ @rescue.perform unit.position.should_not be_nil end + + it "should release other unit when bound" do + unit = RubyWarrior::Units::Base.new + unit.bind + unit.position = stub + @rescue.expects(:space).with(:forward).returns(stub(:captive? => true)) + @rescue.expects(:unit).with(:forward).returns(unit) + @warrior.expects(:earn_points).never + @rescue.perform + unit.should_not be_bound + unit.position.should_not be_nil + end end diff --git a/spec/ruby_warrior/space_spec.rb b/spec/ruby_warrior/space_spec.rb index f6b6b64c..040f57bd 100644 --- a/spec/ruby_warrior/space_spec.rb +++ b/spec/ruby_warrior/space_spec.rb @@ -88,6 +88,20 @@ it "should not be empty" do @space.should_not be_empty end + + describe "bound" do + before(:each) do + @space.unit.bind + end + + it "should be captive" do + @space.should be_captive + end + + it "should not look like enemy" do + @space.should_not be_enemy + end + end end describe "with captive" do diff --git a/spec/ruby_warrior/units/base_spec.rb b/spec/ruby_warrior/units/base_spec.rb index 64b5a4e1..8589f697 100644 --- a/spec/ruby_warrior/units/base_spec.rb +++ b/spec/ruby_warrior/units/base_spec.rb @@ -105,4 +105,18 @@ it "should appear as question mark on map" do @unit.to_map.should == "?" end + + it "should be released from bonds when taking damage" do + @unit.stubs(:max_health).returns(10) + @unit.bind + @unit.should be_bound + @unit.take_damage(2) + @unit.should_not be_bound + end + + it "should be released from bonds when calling release" do + @unit.bind + @unit.unbind + @unit.should_not be_bound + end end diff --git a/spec/ruby_warrior/units/captive_spec.rb b/spec/ruby_warrior/units/captive_spec.rb index 40e05011..a6cffac3 100644 --- a/spec/ruby_warrior/units/captive_spec.rb +++ b/spec/ruby_warrior/units/captive_spec.rb @@ -12,4 +12,8 @@ it "should appear as C on map" do @captive.to_map.should == "C" end + + it "should be bound by default" do + @captive.should be_bound + end end