Class: RubyWarrior::Units::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_warrior/units/base.rb

Direct Known Subclasses

Archer, Captive, Golem, Sludge, Warrior, Wizard

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#healthObject



18
19
20
# File 'lib/ruby_warrior/units/base.rb', line 18

def health
  @health ||= max_health
end

#positionObject

Returns the value of attribute position.



5
6
7
# File 'lib/ruby_warrior/units/base.rb', line 5

def position
  @position
end

Instance Method Details

#abilitiesObject



91
92
93
# File 'lib/ruby_warrior/units/base.rb', line 91

def abilities
  @abilities ||= {}
end

#add_abilities(*new_abilities) ⇒ Object



60
61
62
63
64
# File 'lib/ruby_warrior/units/base.rb', line 60

def add_abilities(*new_abilities)
  new_abilities.each do |ability|
    abilities[ability] = eval("Abilities::#{ability.to_s.sub('!', '').camelize}").new(self) # TODO use something similar to constantize here
  end
end

#alive?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/ruby_warrior/units/base.rb', line 34

def alive?
  !position.nil?
end

#attack_powerObject



7
8
9
# File 'lib/ruby_warrior/units/base.rb', line 7

def attack_power
  0
end

#bindObject



47
48
49
# File 'lib/ruby_warrior/units/base.rb', line 47

def bind
  @bound = true
end

#bound?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/ruby_warrior/units/base.rb', line 38

def bound?
  @bound
end

#characterObject



95
96
97
# File 'lib/ruby_warrior/units/base.rb', line 95

def character
  "?"
end

#earn_points(points) ⇒ Object



15
16
# File 'lib/ruby_warrior/units/base.rb', line 15

def earn_points(points)
end

#max_healthObject



11
12
13
# File 'lib/ruby_warrior/units/base.rb', line 11

def max_health
  0
end

#nameObject Also known as: to_s



55
56
57
# File 'lib/ruby_warrior/units/base.rb', line 55

def name
  self.class.name.split('::').last.titleize
end

#next_turnObject



66
67
68
# File 'lib/ruby_warrior/units/base.rb', line 66

def next_turn
  Turn.new(abilities)
end

#perform_turnObject



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ruby_warrior/units/base.rb', line 75

def perform_turn
  if @position
    abilities.values.each do |ability|
      ability.pass_turn
    end
    if @current_turn.action && !bound?
      name, *args = @current_turn.action
      abilities[name].perform(*args)
    end
  end
end

#play_turn(turn) ⇒ Object



87
88
89
# File 'lib/ruby_warrior/units/base.rb', line 87

def play_turn(turn)
  # to be overriden by subclass
end

#prepare_turnObject



70
71
72
73
# File 'lib/ruby_warrior/units/base.rb', line 70

def prepare_turn
  @current_turn = next_turn
  play_turn(@current_turn)
end

#say(msg) ⇒ Object



51
52
53
# File 'lib/ruby_warrior/units/base.rb', line 51

def say(msg)
  UI.puts_with_delay "#{name} #{msg}"
end

#take_damage(amount) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ruby_warrior/units/base.rb', line 22

def take_damage(amount)
  unbind if bound?
  if health
    self.health -= amount
    say "takes #{amount} damage, #{health} health power left"
    if health <= 0
      @position = nil
      say "dies"
    end
  end
end

#unbindObject



42
43
44
45
# File 'lib/ruby_warrior/units/base.rb', line 42

def unbind
  say "released from bonds"
  @bound = false
end