Class: Zarta::HUD

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

Overview

Resfreshes the top of the screen when I need it to. I spawn instances of this guy a lot.

Instance Method Summary collapse

Constructor Details

#initialize(dungeon) ⇒ HUD

Returns a new instance of HUD.



147
148
149
150
151
152
153
# File 'lib/zarta/main.rb', line 147

def initialize(dungeon)
  @dungeon  = dungeon
  @player   = @dungeon.player
  @pastel   = Pastel.new

  hud_table
end

Instance Method Details

#beginning(word) ⇒ Object

Checks if a word is an ‘an’ word or an ‘a’ word. stackoverflow.com/a/18463759/1576860



217
218
219
220
221
# File 'lib/zarta/main.rb', line 217

def beginning(word)
  # That weird looking thing below is interpolated as an array, so it would
  # come out looking like: [a, e, i, o ,u]
  %w(a e i o u).include?(word[0]) ? 'an' : 'a'
end

#build_table_rowsObject

Pulled this and the functions it calls below out here to make it a little clearer and to be able to make changes without breaking my brain.



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

def build_table_rows
  t = []
  t << [@player.name, "LVL: #{@player.level}"]
  t << [display_health, display_xp]
  t << [display_weapon, display_dungeon_level]
  t << [current_score, high_score]
end

#current_scoreObject



199
200
201
# File 'lib/zarta/main.rb', line 199

def current_score
  "Current Score: #{@dungeon.score}"
end

#display_dungeon_levelObject



195
196
197
# File 'lib/zarta/main.rb', line 195

def display_dungeon_level
  "Dungeon Level:  #{@dungeon.level}/#{@dungeon.max_level}"
end

#display_healthObject



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

def display_health
  current_health = @player.health[0]
  max_health = @player.health[1]
  hud_health = "HP: #{current_health}/#{max_health}"

  return @pastel.red(hud_health) if current_health < max_health / 2
  @pastel.green(hud_health)
end

#display_weaponObject



191
192
193
# File 'lib/zarta/main.rb', line 191

def display_weapon
  "Weapon: #{@player.weapon.name} (#{@player.weapon.damage})"
end

#display_xpObject



187
188
189
# File 'lib/zarta/main.rb', line 187

def display_xp
  "EXP: #{@player.xp}/#{@player.level * 10}"
end

#high_scoreObject



203
204
205
# File 'lib/zarta/main.rb', line 203

def high_score
  "High Score: #{@dungeon.high_score_player} - #{@dungeon.high_score}"
end

#hud_tableObject

Lays out the top of the screen in a nice way for me. It would be great if I could get the whole game to display in a bordered table, but alas, the gem I’m using can’t do it, and I appear to lack the technichal knowhow.



158
159
160
161
162
163
164
165
166
# File 'lib/zarta/main.rb', line 158

def hud_table
  table = Terminal::Table.new
  table.title = @pastel.bright_red(@dungeon.name)
  table.style = { width: 80, padding_left: 3, border_x: '=' }
  table.rows = build_table_rows
  system 'clear'
  puts table
  puts room_description
end

#room_descriptionObject

Pretty straightforward but I have plans for the room description generation to be far more interesting. So check back here sometime in the next millenium.



210
211
212
213
# File 'lib/zarta/main.rb', line 210

def room_description
  word_start = beginning(@dungeon.room.description)
  puts "You are in #{word_start} #{@dungeon.room.description} room."
end