Skip to content

Commit 3b7d8d2

Browse files
committed
adding golem unit with form ability to create golem
1 parent 5c6e632 commit 3b7d8d2

File tree

8 files changed

+139
-2
lines changed

8 files changed

+139
-2
lines changed

lib/ruby_warrior.rb

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
require 'ruby_warrior/units/thick_sludge'
2626
require 'ruby_warrior/units/captive'
2727
require 'ruby_warrior/units/wizard'
28+
require 'ruby_warrior/units/golem'
2829

2930
require 'ruby_warrior/abilities/base'
3031
require 'ruby_warrior/abilities/walk'
@@ -43,3 +44,4 @@
4344
require 'ruby_warrior/abilities/direction_of'
4445
require 'ruby_warrior/abilities/explode'
4546
require 'ruby_warrior/abilities/detonate'
47+
require 'ruby_warrior/abilities/form'

lib/ruby_warrior/abilities/form.rb

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module RubyWarrior
2+
module Abilities
3+
class Form < Base
4+
def description
5+
"Forms a golem in given direction taking half of invoker's health. The passed block is executed for each golem turn."
6+
end
7+
8+
def perform(direction = :forward, &block)
9+
verify_direction(direction)
10+
if space(direction).empty?
11+
x, y = @unit.position.translate_offset(*offset(direction))
12+
health = (@unit.health/2.0).floor
13+
golem = @unit.base_golem
14+
golem.max_health = health
15+
golem.turn = block
16+
@unit.health -= health
17+
@unit.position.floor.add(golem, x, y, @unit.position.direction)
18+
@unit.say "forms a golem #{direction} and gives half of his health (#{health})"
19+
else
20+
@unit.say "fails to form golem because something is blocking the way."
21+
end
22+
end
23+
end
24+
end
25+
end

lib/ruby_warrior/position.rb

-2
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ def relative_direction(direction)
7070
RELATIVE_DIRECTIONS[offset]
7171
end
7272

73-
private
74-
7573
def translate_offset(forward, right)
7674
case direction
7775
when :north then [@x + right, @y - forward]

lib/ruby_warrior/units/golem.rb

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module RubyWarrior
2+
module Units
3+
class Golem < Base
4+
attr_writer :turn
5+
attr_accessor :max_health
6+
7+
def play_turn(turn)
8+
@turn.call(turn) if @turn
9+
end
10+
11+
def attack_power
12+
3
13+
end
14+
15+
def character
16+
"G"
17+
end
18+
end
19+
end
20+
end

lib/ruby_warrior/units/warrior.rb

+11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class Warrior < Base
66

77
def initialize
88
@score = 0 # TODO make score dynamic
9+
@golem_abilities = []
910
end
1011

1112
def play_turn(turn)
@@ -53,6 +54,16 @@ def perform_turn
5354
say "does nothing" if @current_turn.action.nil?
5455
super
5556
end
57+
58+
def add_golem_abilities(*abilities)
59+
@golem_abilities += abilities
60+
end
61+
62+
def base_golem
63+
golem = Golem.new
64+
golem.add_abilities *@golem_abilities
65+
golem
66+
end
5667
end
5768
end
5869
end
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
require File.dirname(__FILE__) + '/../../spec_helper'
2+
3+
describe RubyWarrior::Abilities::Form do
4+
before(:each) do
5+
@floor = RubyWarrior::Floor.new
6+
@floor.width = 2
7+
@floor.height = 3
8+
@warrior = RubyWarrior::Units::Warrior.new
9+
@floor.add(@warrior, 0, 0, :east)
10+
@form = RubyWarrior::Abilities::Form.new(@warrior)
11+
end
12+
13+
it "should form a golem in front of warrior" do
14+
@form.perform
15+
@floor.get(1, 0).should be_kind_of(RubyWarrior::Units::Golem)
16+
end
17+
18+
it "should form a golem in given direction" do
19+
@form.perform(:right)
20+
@floor.get(0, 1).should be_kind_of(RubyWarrior::Units::Golem)
21+
end
22+
23+
it "should not form golem if space isn't empty" do
24+
@floor.add(RubyWarrior::Units::Base.new, 1, 0)
25+
@form.perform(:forward)
26+
@form.perform(:left)
27+
@floor.units.size.should == 2
28+
end
29+
30+
it "should reduce warrior's health by half (rounding down) and give it to the golem" do
31+
@warrior.health = 19
32+
@form.perform
33+
@warrior.health.should == 10
34+
@floor.get(1, 0).max_health.should == 9
35+
end
36+
37+
it "should add passed block as golem's turn block" do
38+
@passed = nil
39+
@form.perform(:forward) { |turn| @passed = turn }
40+
@floor.get(1, 0).play_turn(:turn)
41+
@passed.should == :turn
42+
end
43+
44+
it "should start in same direction as warrior" do
45+
@form.perform(:right)
46+
@floor.get(0, 1).position.direction.should == :east
47+
end
48+
end

spec/ruby_warrior/units/golem_spec.rb

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
require File.dirname(__FILE__) + '/../../spec_helper'
2+
3+
describe RubyWarrior::Units::Golem do
4+
before(:each) do
5+
@golem = RubyWarrior::Units::Golem.new
6+
end
7+
8+
it "should execute turn proc when playing turn" do
9+
proc = Object.new
10+
proc.expects(:call).with(:turn)
11+
@golem.turn = proc
12+
@golem.play_turn(:turn)
13+
end
14+
15+
it "should set max health and update current health" do
16+
@golem.max_health = 10
17+
@golem.max_health.should == 10
18+
@golem.health.should == 10
19+
end
20+
21+
it "should have attack power of 3" do
22+
@golem.attack_power.should == 3
23+
end
24+
25+
it "should appear as G on map" do
26+
@golem.character.should == "G"
27+
end
28+
end

spec/ruby_warrior/units/warrior_spec.rb

+5
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,9 @@ def turn(warrior)
5757
it "should appear as @ on map" do
5858
@warrior.character.should == "@"
5959
end
60+
61+
it "should be able to add golem abilities which are used on base golem" do
62+
@warrior.add_golem_abilities :walk!
63+
@warrior.base_golem.abilities.keys.should == [:walk!]
64+
end
6065
end

0 commit comments

Comments
 (0)