Skip to content

Commit

Permalink
adding bind ability to keep a unit from moving, but makes him look li…
Browse files Browse the repository at this point in the history
…ke a captive
  • Loading branch information
ryanb committed Sep 6, 2008
1 parent a173d74 commit 1ddb687
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/ruby_warrior.rb
Expand Up @@ -30,3 +30,4 @@
require 'ruby_warrior/abilities/rescue'
require 'ruby_warrior/abilities/pivot'
require 'ruby_warrior/abilities/distance'
require 'ruby_warrior/abilities/bind'
19 changes: 19 additions & 0 deletions 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
9 changes: 6 additions & 3 deletions lib/ruby_warrior/abilities/rescue.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/ruby_warrior/space.rb
Expand Up @@ -17,7 +17,7 @@ def enemy?
end

def captive?
unit.kind_of? Units::Captive
unit.bound?
end

def empty?
Expand Down
14 changes: 14 additions & 0 deletions lib/ruby_warrior/units/base.rb
Expand Up @@ -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"
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions 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
Expand Down
19 changes: 19 additions & 0 deletions 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
14 changes: 13 additions & 1 deletion spec/ruby_warrior/abilities/rescue_spec.rb
Expand Up @@ -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))
Expand All @@ -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
14 changes: 14 additions & 0 deletions spec/ruby_warrior/space_spec.rb
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions spec/ruby_warrior/units/base_spec.rb
Expand Up @@ -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
4 changes: 4 additions & 0 deletions spec/ruby_warrior/units/captive_spec.rb
Expand Up @@ -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

0 comments on commit 1ddb687

Please sign in to comment.