Class: Gemwarrior::Battle

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

Constant Summary collapse

ERROR_ATTACK_OPTION_INVALID =

CONSTANTS ERRORS

'That will not do anything against the monster.'
BEAST_MODE_ATTACK =
100
TEXT_ESCAPE =

MESSAGES

'POOF'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PlayerLevels

check_level, get_level_stats

Constructor Details

#initialize(options) ⇒ Battle

Returns a new instance of Battle.



20
21
22
23
24
25
# File 'lib/gemwarrior/battle.rb', line 20

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

Instance Attribute Details

#monsterObject

Returns the value of attribute monster.



18
19
20
# File 'lib/gemwarrior/battle.rb', line 18

def monster
  @monster
end

#playerObject

Returns the value of attribute player.



18
19
20
# File 'lib/gemwarrior/battle.rb', line 18

def player
  @player
end

#player_defendingObject

Returns the value of attribute player_defending.



18
19
20
# File 'lib/gemwarrior/battle.rb', line 18

def player_defending
  @player_defending
end

#worldObject

Returns the value of attribute world.



18
19
20
# File 'lib/gemwarrior/battle.rb', line 18

def world
  @world
end

Instance Method Details

#start(is_arena = nil, is_event = nil) ⇒ Object



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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/gemwarrior/battle.rb', line 27

def start(is_arena = nil, is_event = nil)
  print_battle_line

  if is_arena
    print 'Your opponent is now...'
    Animation::run({:phrase => "#{monster.name.upcase}!", :speed => :slow})
  elsif is_event
    puts "You are attacked by #{monster.name}!"
  else
    puts "You decide to attack #{monster.name}!"
  end

  puts "#{monster.name} cries out: \"#{monster.battlecry}\"".colorize(:yellow)

  # first strike!
  unless is_arena
    if monster_strikes_first?(is_event)
      puts "#{monster.name} strikes first!".colorize(:yellow)
      monster_attacks_player
    end
  end

  # main battle loop
  loop do
    if monster_dead?
      monster_death
      return
    elsif player_dead?
      player_death
    end

    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
    print "PLAYER  :: #{player.hp_cur.to_s.rjust(3)} HP"
    if world.debug_mode
      print " (LVL: #{player.level})"
    end
    print "\n"

    print "MONSTER :: "
    if world.debug_mode || PlayerLevels::get_level_stats(player.level)[:special_abilities].include?(:rocking_vision)
      print "#{monster.hp_cur.to_s.rjust(3)}"
    else
      print "???"
    end
    print " HP"
    if world.debug_mode
      print " (LVL: #{monster.level})"
    end
    print "\n"
    puts

    self.player_defending = false

    puts 'What do you do?'
    print '['.colorize(:yellow)
    print 'F'.colorize(:green)
    print 'ight/'.colorize(:yellow)
    print 'A'.colorize(:green)
    print 'ttack]['.colorize(:yellow)
    print 'D'.colorize(:green)
    print 'efend]['.colorize(:yellow)
    print 'L'.colorize(:green)
    print 'ook]['.colorize(:yellow)
    print 'R'.colorize(:green)
    print 'un]'.colorize(:yellow)
    print "\n"

    cmd = STDIN.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 'defend', 'd'
      puts 'You dig in and defend this round.'
      self.player_defending = true
    when 'look', 'l'
      print "#{monster.name}".colorize(:white)
      print " (#{monster.hp_cur}/#{monster.hp_max} HP): #{monster.description}\n"
      puts "It has some distinguishing features, too: face is #{monster.face}, hands are #{monster.hands}, and general mood is #{monster.mood}."
      if world.debug_mode
        puts "If defeated, will receive:"
        puts " >> XP   : #{monster.xp}"
        puts " >> ROX  : #{monster.rox}"
        puts " >> ITEMS: #{monster.inventory.list_contents}"
        next
      end
    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