Skip to content

Commit e1c4876

Browse files
committed
moving timed bomb behavior into explode ability so it is not specific to Captives
1 parent 867655c commit e1c4876

File tree

11 files changed

+44
-36
lines changed

11 files changed

+44
-36
lines changed

lib/ruby_warrior/abilities/base.rb

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ def damage(receiver, amount)
2929

3030
def description
3131
end
32+
33+
def pass_turn
34+
# callback which is triggered every turn
35+
end
3236
end
3337
end
3438
end

lib/ruby_warrior/abilities/explode.rb

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module RubyWarrior
22
module Abilities
33
class Explode < Base
4+
attr_accessor :time
5+
46
def description
57
"Kills you and all surrounding units. You probably don't want to do this intentionally."
68
end
@@ -13,6 +15,14 @@ def perform
1315
end
1416
end
1517
end
18+
19+
def pass_turn
20+
if @time && @unit.position
21+
@unit.say "is ticking"
22+
@time -= 1
23+
perform if @time.zero?
24+
end
25+
end
1626
end
1727
end
1828
end

lib/ruby_warrior/space.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def stairs?
2929
end
3030

3131
def ticking?
32-
unit.respond_to?(:bomb_time) && unit.bomb_time
32+
unit && unit.abilities.include?(:explode!)
3333
end
3434

3535
def unit

lib/ruby_warrior/units/base.rb

+8-3
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,14 @@ def prepare_turn
7373
end
7474

7575
def perform_turn
76-
if @current_turn.action && @position && !bound?
77-
name, *args = @current_turn.action
78-
abilities[name].perform(*args)
76+
if @position
77+
abilities.values.each do |ability|
78+
ability.pass_turn
79+
end
80+
if @current_turn.action && !bound?
81+
name, *args = @current_turn.action
82+
abilities[name].perform(*args)
83+
end
7984
end
8085
end
8186

lib/ruby_warrior/units/captive.rb

-13
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
module RubyWarrior
22
module Units
33
class Captive < Base
4-
attr_accessor :bomb_time
5-
64
def initialize
7-
add_abilities :explode!
85
bind
96
end
107

@@ -15,16 +12,6 @@ def max_health
1512
def character
1613
"C"
1714
end
18-
19-
def play_turn(turn)
20-
if @bomb_time
21-
@bomb_time -= 1
22-
if @bomb_time.zero?
23-
@bound = false # unbind so it can perform an action
24-
turn.explode!
25-
end
26-
end
27-
end
2815
end
2916
end
3017
end

spec/ruby_warrior/abilities/explode_spec.rb

+10
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,14 @@
1919
@captive.health.should == -90
2020
unit.health.should == -80
2121
end
22+
23+
it "should explode when bomb time reaches zero" do
24+
@captive.health = 10
25+
@explode.time = 3
26+
@explode.pass_turn
27+
@explode.pass_turn
28+
@captive.health.should == 10
29+
@explode.pass_turn
30+
@captive.health.should == -90
31+
end
2232
end

spec/ruby_warrior/abilities/throw_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
it "should detonate an explosive if any unit has one" do
3838
captive = RubyWarrior::Units::Captive.new
3939
captive.health = 1
40-
captive.bomb_time = 3
40+
captive.add_abilities :explode!
4141
@floor.add(captive, 1, 2)
4242
@throw.perform
4343
captive.health.should == -99

spec/ruby_warrior/space_spec.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@
126126

127127
describe "with captive" do
128128
before(:each) do
129-
@floor.add(RubyWarrior::Units::Captive.new, 0, 0)
129+
@captive = RubyWarrior::Units::Captive.new
130+
@floor.add(@captive, 0, 0)
130131
@space = @floor.space(0, 0)
131132
end
132133

@@ -139,7 +140,7 @@
139140
end
140141

141142
it "should be ticking if captive has time bomb" do
142-
@space.unit.bomb_time = 10
143+
@captive.add_abilities :explode!
143144
@space.should be_ticking
144145
end
145146

+3-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
require File.dirname(__FILE__) + '/../../spec_helper'
22

3-
describe RubyWarrior::Units::ThickSludge do
3+
describe RubyWarrior::Units::Captive do
44
before(:each) do
55
@captive = RubyWarrior::Units::Captive.new
66
end
@@ -17,18 +17,7 @@
1717
@captive.should be_bound
1818
end
1919

20-
it "should explode when bomb time reaches 0 and unbind itself" do
21-
@captive.bomb_time = 3
22-
@captive.play_turn(stub)
23-
@captive.play_turn(stub)
24-
@captive.should be_bound
25-
turn = stub
26-
turn.expects(:explode!)
27-
@captive.play_turn(turn)
28-
@captive.should_not be_bound
29-
end
30-
31-
it "should have explode ability" do
32-
@captive.abilities.keys.should include(:explode!)
20+
it "should not have explode ability by default (this should be added when needed)" do
21+
@captive.abilities.should_not include(:explode!)
3322
end
3423
end

towers/intermediate/level_006.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@
1818
unit :sludge, 3, 1, :west
1919
unit :captive, 0, 0, :west
2020
unit :captive, 4, 1, :west do |u|
21-
u.bomb_time = 7
21+
u.add_abilities :explode!
22+
u.abilities[:explode!].time = 7
2223
end

towers/intermediate/level_007.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
unit :sludge, 1, 2, :north
2020
unit :sludge, 2, 1, :west
2121
unit :captive, 4, 1, :west do |u|
22-
u.bomb_time = 10
22+
u.add_abilities :explode!
23+
u.abilities[:explode!].time = 10
2324
end
2425
unit :captive, 2, 0, :west

0 commit comments

Comments
 (0)