Class: Gemwarrior::Battle

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

Constant Summary collapse

ERROR_ATTACK_OPTION_INVALID =

CONSTANTS ERRORS

'That will not do anything against the monster.'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Battle

Returns a new instance of Battle.



12
13
14
15
16
# File 'lib/gemwarrior/battle.rb', line 12

def initialize(options)
  self.world    = options.fetch(:world)
  self.player   = options.fetch(:player)
  self.monster  = options.fetch(:monster)
end

Instance Attribute Details

#monsterObject

Returns the value of attribute monster.



10
11
12
# File 'lib/gemwarrior/battle.rb', line 10

def monster
  @monster
end

#playerObject

Returns the value of attribute player.



10
11
12
# File 'lib/gemwarrior/battle.rb', line 10

def player
  @player
end

#worldObject

Returns the value of attribute world.



10
11
12
# File 'lib/gemwarrior/battle.rb', line 10

def world
  @world
end

Instance Method Details

#startObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/gemwarrior/battle.rb', line 18

def start
  print_battle_line
  puts "You decide to attack the #{monster.name}!"
  puts "#{monster.name} cries out: \"#{monster.battlecry}\"".colorize(:yellow)

  # first strike!
  if monster_strikes_first?
    puts "#{monster.name} strikes first!".colorize(:yellow)
    monster_attacks_player
  end
  
  # main battle loop
  loop do
    if monster_dead?
      monster_death
      return
    elsif player_dead?
      player_death
    end

    puts
    puts "[Fight/Attack][Look][Run]".colorize(:color => :white, :background => :green)
    puts "PLAYER  :: #{player.hp_cur.to_s.rjust(3)} HP\n"
    puts "MONSTER :: #{monster.hp_cur.to_s.rjust(3)} HP\n"
    
    if player_near_death?
      puts "You are almost dead!\n".colorize(:yellow)
    end
    if monster_near_death?
      puts "#{monster.name} is almost dead!\n".colorize(:yellow)
    end
    
    puts 'What do you do?'
    cmd = gets.chomp.downcase
    
    # player action
    case cmd
    when 'fight', 'f', 'attack', 'a'
      puts "You attack #{monster.name}#{player.cur_weapon_name}!"
      dmg = calculate_damage_to(monster)
      if dmg > 0
        take_damage(monster, dmg)
        if monster_dead?
          monster_death
          return
        end
      else
        puts "You miss entirely!".colorize(:yellow)
      end
    when 'look', 'l'
      puts "#{monster.name}: #{monster.description}"
      puts "Its got some distinguishing features, too: face is #{monster.face}, hands are #{monster.hands}, and general mood is #{monster.mood}."
    when 'run', 'r'
      if player_escape?
        monster.hp_cur = monster.hp_max
        puts "You successfully elude #{monster.name}!".colorize(:green)
        print_escape_text
        return
      else
        puts "You were not able to run away! :-(".colorize(:yellow)
      end
    else
      puts ERROR_ATTACK_OPTION_INVALID
      next
    end
    
    # monster action
    monster_attacks_player
  end
end