Class: Creature

Inherits:
Object
  • Object
show all
Defined in:
lib/battlefield.rb

Overview

If you want to make a new enemy, you have two ways to do it:

  1. Directly initialize it

enemy = Creature.new health

  1. Create a new class

class Goblin def initialize; @health = 5; end end

Common ancestor of any battle-able objects

Direct Known Subclasses

ArmouredCreature

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(health) ⇒ Creature

Initializes a Creature. Parameter health sets the Creature’s health.



17
# File 'lib/battlefield.rb', line 17

def initialize(health); @health = health; end

Instance Attribute Details

#healthObject (readonly)

Returns the value of attribute health.



15
16
17
# File 'lib/battlefield.rb', line 15

def health
  @health
end

Instance Method Details

#damage(amt) ⇒ Object

Damage function



19
# File 'lib/battlefield.rb', line 19

def damage(amt); @health -= amt; end

#drollObject

Roll twice



23
# File 'lib/battlefield.rb', line 23

def droll; self.roll + self.roll; end

#heal(amt) ⇒ Object

The 1st argument is your attack power. The other is the enemy’s. self heals c1 - c2 health.



25
26
27
# File 'lib/battlefield.rb', line 25

def heal(amt) 
  @health += c1 - c2
end

#rollObject

Rolling function - higher # = more power.



21
# File 'lib/battlefield.rb', line 21

def roll; (1..6).to_a.sample; end