Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

adding golem unit with form ability to create golem

  • Loading branch information...
commit 3b7d8d2778d8df9c2d760cf6f8a4583f40e0f3ed 1 parent 5c6e632
@ryanb authored
View
2  lib/ruby_warrior.rb
@@ -25,6 +25,7 @@
require 'ruby_warrior/units/thick_sludge'
require 'ruby_warrior/units/captive'
require 'ruby_warrior/units/wizard'
+require 'ruby_warrior/units/golem'
require 'ruby_warrior/abilities/base'
require 'ruby_warrior/abilities/walk'
@@ -43,3 +44,4 @@
require 'ruby_warrior/abilities/direction_of'
require 'ruby_warrior/abilities/explode'
require 'ruby_warrior/abilities/detonate'
+require 'ruby_warrior/abilities/form'
View
25 lib/ruby_warrior/abilities/form.rb
@@ -0,0 +1,25 @@
+module RubyWarrior
+ module Abilities
+ class Form < Base
+ def description
+ "Forms a golem in given direction taking half of invoker's health. The passed block is executed for each golem turn."
+ end
+
+ def perform(direction = :forward, &block)
+ verify_direction(direction)
+ if space(direction).empty?
+ x, y = @unit.position.translate_offset(*offset(direction))
+ health = (@unit.health/2.0).floor
+ golem = @unit.base_golem
+ golem.max_health = health
+ golem.turn = block
+ @unit.health -= health
+ @unit.position.floor.add(golem, x, y, @unit.position.direction)
+ @unit.say "forms a golem #{direction} and gives half of his health (#{health})"
+ else
+ @unit.say "fails to form golem because something is blocking the way."
+ end
+ end
+ end
+ end
+end
View
2  lib/ruby_warrior/position.rb
@@ -70,8 +70,6 @@ def relative_direction(direction)
RELATIVE_DIRECTIONS[offset]
end
- private
-
def translate_offset(forward, right)
case direction
when :north then [@x + right, @y - forward]
View
20 lib/ruby_warrior/units/golem.rb
@@ -0,0 +1,20 @@
+module RubyWarrior
+ module Units
+ class Golem < Base
+ attr_writer :turn
+ attr_accessor :max_health
+
+ def play_turn(turn)
+ @turn.call(turn) if @turn
+ end
+
+ def attack_power
+ 3
+ end
+
+ def character
+ "G"
+ end
+ end
+ end
+end
View
11 lib/ruby_warrior/units/warrior.rb
@@ -6,6 +6,7 @@ class Warrior < Base
def initialize
@score = 0 # TODO make score dynamic
+ @golem_abilities = []
end
def play_turn(turn)
@@ -53,6 +54,16 @@ def perform_turn
say "does nothing" if @current_turn.action.nil?
super
end
+
+ def add_golem_abilities(*abilities)
+ @golem_abilities += abilities
+ end
+
+ def base_golem
+ golem = Golem.new
+ golem.add_abilities *@golem_abilities
+ golem
+ end
end
end
end
View
48 spec/ruby_warrior/abilities/form_spec.rb
@@ -0,0 +1,48 @@
+require File.dirname(__FILE__) + '/../../spec_helper'
+
+describe RubyWarrior::Abilities::Form do
+ before(:each) do
+ @floor = RubyWarrior::Floor.new
+ @floor.width = 2
+ @floor.height = 3
+ @warrior = RubyWarrior::Units::Warrior.new
+ @floor.add(@warrior, 0, 0, :east)
+ @form = RubyWarrior::Abilities::Form.new(@warrior)
+ end
+
+ it "should form a golem in front of warrior" do
+ @form.perform
+ @floor.get(1, 0).should be_kind_of(RubyWarrior::Units::Golem)
+ end
+
+ it "should form a golem in given direction" do
+ @form.perform(:right)
+ @floor.get(0, 1).should be_kind_of(RubyWarrior::Units::Golem)
+ end
+
+ it "should not form golem if space isn't empty" do
+ @floor.add(RubyWarrior::Units::Base.new, 1, 0)
+ @form.perform(:forward)
+ @form.perform(:left)
+ @floor.units.size.should == 2
+ end
+
+ it "should reduce warrior's health by half (rounding down) and give it to the golem" do
+ @warrior.health = 19
+ @form.perform
+ @warrior.health.should == 10
+ @floor.get(1, 0).max_health.should == 9
+ end
+
+ it "should add passed block as golem's turn block" do
+ @passed = nil
+ @form.perform(:forward) { |turn| @passed = turn }
+ @floor.get(1, 0).play_turn(:turn)
+ @passed.should == :turn
+ end
+
+ it "should start in same direction as warrior" do
+ @form.perform(:right)
+ @floor.get(0, 1).position.direction.should == :east
+ end
+end
View
28 spec/ruby_warrior/units/golem_spec.rb
@@ -0,0 +1,28 @@
+require File.dirname(__FILE__) + '/../../spec_helper'
+
+describe RubyWarrior::Units::Golem do
+ before(:each) do
+ @golem = RubyWarrior::Units::Golem.new
+ end
+
+ it "should execute turn proc when playing turn" do
+ proc = Object.new
+ proc.expects(:call).with(:turn)
+ @golem.turn = proc
+ @golem.play_turn(:turn)
+ end
+
+ it "should set max health and update current health" do
+ @golem.max_health = 10
+ @golem.max_health.should == 10
+ @golem.health.should == 10
+ end
+
+ it "should have attack power of 3" do
+ @golem.attack_power.should == 3
+ end
+
+ it "should appear as G on map" do
+ @golem.character.should == "G"
+ end
+end
View
5 spec/ruby_warrior/units/warrior_spec.rb
@@ -57,4 +57,9 @@ def turn(warrior)
it "should appear as @ on map" do
@warrior.character.should == "@"
end
+
+ it "should be able to add golem abilities which are used on base golem" do
+ @warrior.add_golem_abilities :walk!
+ @warrior.base_golem.abilities.keys.should == [:walk!]
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.