Class: Zarta::Player

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

Overview

The Bloated Beast. The Mother of Methods. The Function Fornicator.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dungeon) ⇒ Player

Returns a new instance of Player.



21
22
23
24
25
26
27
28
29
30
# File 'lib/zarta/player.rb', line 21

def initialize(dungeon)
  @name    = 'Testy McTestface'
  @health  = [100, 100]
  @level   = 1
  @xp      = 0
  @dungeon = dungeon
  @weapon  = Zarta::Weapon.new(@dungeon)
  @prompt  = TTY::Prompt.new
  @pastel  = Pastel.new
end

Instance Attribute Details

#healthObject

The player’s health in an array

current health / max health


10
11
12
# File 'lib/zarta/player.rb', line 10

def health
  @health
end

#levelObject

The player’s current experience level



13
14
15
# File 'lib/zarta/player.rb', line 13

def level
  @level
end

#nameObject

The player’s name



6
7
8
# File 'lib/zarta/player.rb', line 6

def name
  @name
end

#weaponObject

The player’s current weapon



19
20
21
# File 'lib/zarta/player.rb', line 19

def weapon
  @weapon
end

#xpObject

The player’s accumulated experience points



16
17
18
# File 'lib/zarta/player.rb', line 16

def xp
  @xp
end

Instance Method Details

#deathObject



141
142
143
144
145
146
# File 'lib/zarta/player.rb', line 141

def death
  puts 'You have been killed!'
  puts @pastel.bright_red.bold('GAME OVER')
  gets
  exit[0]
end

#enemy_killedObject



148
149
150
151
152
153
154
155
156
# File 'lib/zarta/player.rb', line 148

def enemy_killed
  xp_gained = gain_xp
  puts "You have slain the #{@enemy_c}!"
  puts "You gain #{@pastel.bright_blue.bold(xp_gained)} Experience."
  level_up if @xp >= @level * NEXT_LEVEL_XP
  @enemy.dealt_with = true
  player_wins if @enemy.name == 'BOSS!'
  gets
end

#fightObject



88
89
90
91
92
93
# File 'lib/zarta/player.rb', line 88

def fight
  Zarta::HUD.new(@dungeon)
  player_turn
  enemy_killed && return if @enemy.dealt_with
  @enemy.enemy_turn
end

#fleeObject



112
113
114
115
116
117
118
# File 'lib/zarta/player.rb', line 112

def flee
  return unless @prompt.yes?('Are you sure?')
  puts "You try to get away from the #{@enemy_c}"
  flee_hit
  @enemy.dealt_with = true
  gets
end

#flee_damageObject



129
130
131
132
133
134
# File 'lib/zarta/player.rb', line 129

def flee_damage
  enemy_hit = @enemy.enemy_damage
  take_damage(enemy_hit)
  puts "The #{@enemy_c} hits you as you try to flee!"
  puts "You take #{@pastel.red.bold(enemy_hit)} hits you as you flee!"
end

#flee_hitObject



120
121
122
123
124
125
126
127
# File 'lib/zarta/player.rb', line 120

def flee_hit
  base = @dungeon.level + @level
  level_difference = @enemy.level - @level
  flee_chance = (rand(base) + level_difference) * FLEE_CHANCE
  flee_damage && return if rand(base) <= flee_chance
  puts 'You are successful!'
  @enemy.dealt_with = true
end

#gain_xpObject



165
166
167
168
169
# File 'lib/zarta/player.rb', line 165

def gain_xp
  xp_gained = @enemy.level + @level + @dungeon.level
  @xp += xp_gained
  xp_gained
end

#handle_enemyObject



67
68
69
70
71
72
73
74
# File 'lib/zarta/player.rb', line 67

def handle_enemy
  @enemy = @dungeon.room.enemy
  @enemy_c = @pastel.magenta.bold(@enemy.name)
  puts "There is a #{@enemy_c} in here!"

  # Loop until the enemy is dealt with.
  prompt_enemy until @enemy.dealt_with
end

#handle_weaponObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/zarta/player.rb', line 32

def handle_weapon
  @room_weapon = @dungeon.room.weapon
  @room_weapon_c = @pastel.cyan.bold(@room_weapon.name)
  @weapon_handled = false
  Zarta::HUD.new(@dungeon)
  puts "You see a #{@room_weapon_c} in this room."

  # Repeat until they pick it up or leave it
  prompt_weapon until @weapon_handled
end

#health_increaseObject



179
180
181
182
183
184
185
186
# File 'lib/zarta/player.rb', line 179

def health_increase
  base_increase = (@level - 1) + @dungeon.level
  increase = rand(base_increase..base_increase * HEALTH_INCREASE)
  @health[1] += increase
  @health[0] = @health[1]
  puts 'Your health is replenished!'
  puts "You gain #{@pastel.bright_green.bold(increase)} to max health."
end

#leave_weaponObject



62
63
64
65
# File 'lib/zarta/player.rb', line 62

def leave_weapon
  return unless @prompt.yes?('Are you sure?')
  @weapon_handled = true
end

#level_upObject



171
172
173
174
175
176
177
# File 'lib/zarta/player.rb', line 171

def level_up
  @xp -= @level * NEXT_LEVEL_XP
  @level += 1
  puts @pastel.bright_blue.bold('You gain a level!')
  puts "You are now level #{@pastel.bright_blue.bold(@level)}"
  health_increase
end

#pickup_weaponObject



54
55
56
57
58
59
60
# File 'lib/zarta/player.rb', line 54

def pickup_weapon
  puts 'Your current weapon will be replaced.'
  return unless @prompt.yes?('Are you sure?')
  @weapon = @room_weapon
  @weapon_handled = true
  Zarta::HUD.new(@dungeon)
end

#player_damageObject

Another Turing-level algorithm for determining how hard the player hits. It would be cool to generate critical hits in here.



106
107
108
109
110
# File 'lib/zarta/player.rb', line 106

def player_damage
  base_damage = @weapon.damage + rand(@weapon.damage)
  hit = base_damage + rand(@level) + @level
  hit.round
end

#player_turnObject



95
96
97
98
99
100
101
102
# File 'lib/zarta/player.rb', line 95

def player_turn
  Zarta::HUD.new(@dungeon)
  hit = player_damage
  @enemy.take_damage(hit)
  puts "You attack the #{@enemy_c}!"
  puts "You hit for #{@pastel.bright_yellow.bold(hit)} damage."
  gets
end

#player_winsObject

I’ve never made this method call, so I’ll just assume that it works…



159
160
161
162
163
# File 'lib/zarta/player.rb', line 159

def player_wins
  puts 'Congrats, you won the game!'
  gets
  exit[0]
end

#prompt_enemyObject



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/zarta/player.rb', line 76

def prompt_enemy
  enemy_choice = @prompt.select('What do you want to do?') do |menu|
    menu.choice 'Fight!'
    menu.choice 'Flee!'
    menu.choice 'Look!'
  end

  fight if enemy_choice == 'Fight!'
  flee if enemy_choice == 'Flee!'
  @enemy.inspect if enemy_choice == 'Look!'
end

#prompt_weaponObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/zarta/player.rb', line 43

def prompt_weapon
  weapon_choice = @prompt.select('What do you want to do?') do |menu|
    menu.choice 'Pick it up'
    menu.choice 'Look at it'
    menu.choice 'Leave it'
  end
  pickup_weapon if weapon_choice == 'Pick it up'
  @room_weapon.inspect_weapon if weapon_choice == 'Look at it'
  leave_weapon if weapon_choice == 'Leave it'
end

#take_damage(damage) ⇒ Object



136
137
138
139
# File 'lib/zarta/player.rb', line 136

def take_damage(damage)
  @health[0] -= damage
  death if @health[0] <= 0
end