class Object
Public Instance Methods
Armour(protection = 0)
click to toggle source
Initializes a new Armour
object. If there are no arguments, protection is set to 0.
# File lib/battlefield.rb, line 132 def Armour(protection = 0); Armour.new protection; end
Creature(health = 1, armour = Armour(), weapon = Weapon())
click to toggle source
Initializes a new Creature
. If no arguments are given, health is set to 1, armour is set to Armour(0), and weapon is set to Weapon(0).
# File lib/battlefield.rb, line 134 def Creature(health = 1, armour = Armour(), weapon = Weapon()); Creature.new health, armour, weapon; end
Hero(armour = Armour(), weapon = Weapon())
click to toggle source
Alias for Hero.new
. Remember to include the ()s if you give no arguments or you will get an error!
# File lib/battlefield.rb, line 136 def Hero(armour = Armour(), weapon = Weapon()); Hero.new(armour); end
Weapon(power = 0)
click to toggle source
Initializes a new Weapon
. If there are no arguments, power is set to 0.
# File lib/battlefield.rb, line 130 def Weapon(power = 0); Weapon.new power; end
battle(arg = Hash.new)
click to toggle source
Strongly recommended keys -> :hero, :enemy Recommended keys -> :heal
:hero should be a kind of Hero
. This is your fighter.
:enemy should be a kind of Creature
. This is the enemy. If it is also a kind of Hero
, use it in :hero instead.
:heal can be set to:
1. false(default) - allows no one to heal during the battle 2. 'hero' - allows the hero to heal during the battle 3. 'enemy' - allows the enemy to heal during the battle 4.'both' - allows the hero and enemy to both heal during the battle
This is also the name of the executable. To use the executable, treat it as the method battle.
end ¶ ↑
# File lib/battlefield.rb, line 97 def battle(arg = Hash.new) :hero = Hero() unless defined? :hero :enemy = Creature() unless defined? :enemy :heal = false unless defined? :enemy raise(BattleError, "You must specify which party to heal.") unless :heal == "both" || :heal == "hero" || :heal == "enemy" || :heal == false until :hero.health <= 0 || :enemy.health <= 0 roll1, roll2 = :hero.droll, :enemy.droll # You hit enemy and it takes your roll amount if roll1 > roll2 if :heal == "hero" or :heal == "both" :hero.heal roll1, roll2 puts "You gained #{roll1 - roll2} health" sleep 1 end puts "The enemy took #{roll1} damage" :hero.attack :enemy, roll1 sleep 1 # Enemy hits you and you take its roll amount elsif roll2 > roll1 if :heal == "enemy" or :heal == "both" :enemy.heal roll2, roll1 puts "The enemy gained #{roll2 - roll1} health" sleep(1) end puts "You took #{roll2} damage" :enemy.attack :hero, roll2 sleep 1 end puts "\nYou: #{:hero.health} #{:enemy}: #{:enemy.health}\n" sleep 2 end end