class MISSIONGAME::UI

Public Instance Methods

ask(question, filter = nil) click to toggle source

Ask user a question. A regular expression filter can be applied.

# File lib/ui.rb, line 95
def ask(question, filter = nil)
  if filter
    match = false
    answer = nil
    while match == false
      print UI_ARROW.red + question.light_white + ' '
      answer = gets.chomp
      if answer.match(filter)
        return answer
      else
        print 'Sorry, please try again.'.red
        new_line
        new_line
      end
    end
  else
    print "\u2712 ".red + question.light_white + ' '
    input = gets.chomp
    return input
  end
end
cannot_travel_combat() click to toggle source
# File lib/ui.rb, line 183
def cannot_travel_combat
  puts "You're in a war with the vampires! Fight for your life to proceed or else your're being feasted on!"
end
clear() click to toggle source

Clear the screen

# File lib/ui.rb, line 20
def clear
  print "\e[H\e[2J"
end
display_map(args) click to toggle source
# File lib/ui.rb, line 24
def display_map(args)
  map = args[:map]
  new_line
  draw_frame({ text: map })
  new_line
end
display_name(args) click to toggle source
# File lib/ui.rb, line 209
def display_name(args)
  player = args[:player]
  print 'You are ' + player.name.light_white +
          '. Have you forgotten your own name?'
  new_line
end
display_version() click to toggle source
# File lib/ui.rb, line 163
def display_version
  puts ' Version ' + MISSIONMISSIONGAME_VERSION.light_white
  new_line
end
draw_frame(args) click to toggle source

Draw text surrounded in a nice frame

# File lib/ui.rb, line 143
def draw_frame(args)
  # Figure out width automatically
  text = args[:text]
  width = get_max_size_from_array(text)
  draw_top_frame(width)
  text.each do |t|
    t_size = get_real_size(t)
    draw_vert_frame_begin
    if t.kind_of?(Array)
      t.each { |s| print s }
    else
      print t
    end
    (width - (t_size + 4)).times { print ' ' }
    draw_vert_frame_end
    new_line
  end
  draw_bottom_frame(width)
end
enemy_greet(args) click to toggle source
# File lib/ui.rb, line 224
def enemy_greet(args)
  enemy = args[:enemy]
  print enemy.name.light_white + ' attacks!'
  new_line
end
enemy_info(args) click to toggle source
# File lib/ui.rb, line 79
def enemy_info(args)
  player = args[:player]
  enemy = player.current_enemy
  print enemy.name.light_red + ' has ' + enemy.str.to_s.light_white +
          ' strength and ' + enemy.lives.to_s.light_white + ' lives.'
  new_line
end
get_cmd() click to toggle source
# File lib/ui.rb, line 198
def get_cmd
  print 'Type '.white + 'help'.light_white + " for possible commands.\n"
  print "\u2712 ".red + 'Your command? '.light_white
  return gets.chomp.downcase
end
help() click to toggle source
# File lib/ui.rb, line 31
def help
  new_line
  print 'Valid Commands'.light_green
  new_line(2)
  print UI_ARROW.light_yellow + ' ' + 'right, r, '.light_white +
          ++' - Move right/east'
  new_line
  print UI_ARROW.light_yellow + ' ' + 'backward, b, '.light_white +
          ++' - Move backward/south'
  new_line
  print UI_ARROW.light_yellow + ' ' + 'left, l, '.light_white +
          ++' - Move left/west'
  new_line
  print UI_ARROW.light_yellow + ' ' + 'forward, f, '.light_white +
          ++' - Move forward/north'
  new_line
  print UI_ARROW.light_yellow + ' ' + 'map, m'.light_white + ' - Display map'
  new_line
  print UI_ARROW.light_yellow + ' ' + 'where'.light_white +
          ' - Describe current surroundings'
  new_line
  print UI_ARROW.light_yellow + ' ' + 'name'.light_white +
                  ' - Reminds player their name'
  new_line
  print UI_ARROW.light_yellow + ' ' + 'a, attack'.light_white +
          ' - Attack (only in combat)'
  new_line
  print UI_ARROW.light_yellow + ' ' + 'enemy'.light_white +
          ' - Display information about your enemy'
  new_line
  print UI_ARROW.light_yellow + ' ' +
          'points, score, status, info'.light_white +
          ' - Display points (score)'
  new_line
  print UI_ARROW.light_yellow + ' ' + 'clear, cls'.light_white +
          ' - Clears the screen'
  new_line
  print UI_ARROW.light_yellow + ' ' + 'q, quit, exit'.light_white +
          ' - Quits the MISSIONGAME'
  new_line
end
new_line(times = 1) click to toggle source

Prints a new line. Optinally can print multiple lines.

# File lib/ui.rb, line 138
def new_line(times = 1)
  times.times { print "\n" }
end
not_found() click to toggle source
# File lib/ui.rb, line 168
def not_found
  print 'Command not understood. Use the ' + 'help or h'.red +
          ' to see available commands.'.light_white
  new_line
end
not_in_combat() click to toggle source
# File lib/ui.rb, line 187
def not_in_combat
  puts 'No vampire has attacked you just yet.'
end
out_of_bounds() click to toggle source
# File lib/ui.rb, line 204
def out_of_bounds
  print 'x'.red + ' Requested move out of bounds.'
  new_line
end
player_dead(args) click to toggle source
# File lib/ui.rb, line 216
def player_dead(args)
  story = args[:story]
  new_line
  text = story.player_dead
  draw_frame(text: text)
  new_line
end
player_info(args) click to toggle source
# File lib/ui.rb, line 87
def player_info(args)
  player = args[:player]
  print 'You have ' + player.lives.to_s.light_white + ' lives and have ' +
          player.points.to_s.light_white + ' points.'
  new_line
end
points(args) click to toggle source
# File lib/ui.rb, line 73
def points(args)
  player = args[:player]
  print 'You currently have ' + player.points.to_s.light_white + ' points.'
  new_line
end
quit() click to toggle source
# File lib/ui.rb, line 191
def quit
  new_line
  print 'You abandoned your journey to getting answers to all of your many unaswered questions.'
          .red
  new_line(2)
end
show_location(args) click to toggle source
# File lib/ui.rb, line 174
def show_location(args)
  player = args[:player]
  print 'You are currently on row ' + player.y.to_s.light_white +
          ', column ' + player.x.to_s.light_white
  new_line
  print 'Use the ' + 'map'.light_white + ' command to see the map.'
  new_line
end
welcome() click to toggle source

Display welcome

# File lib/ui.rb, line 118
def welcome
  text = Array.new
  text <<
    'This is a text adventure game inspired by the movie series '.white +
      'The Originals'.light_green
  text <<
    'Written by Webster Avosa as a '.white + UI_EMAIL.light_white +
      'Livestorm Back-End Hiring Test'.light_green
  text <<
    'Copyright ' + UI_COPYRIGHT +
      ' Webster Avosa, All Rights Reserved.'.white
  text << 'Licensed under MIT.'.white
  text <<
    'Contact me '.white + UI_EMAIL.light_white +
      ' websterb17@gmail.com'.white
  draw_frame({ text: text })
  new_line
end

Private Instance Methods

draw_bottom_frame(width) click to toggle source
# File lib/ui.rb, line 247
def draw_bottom_frame(width)
  print UI_FRAME_LOWER_LEFT.yellow
  (width - 2).times { print UI_FRAME_HORIZONTAL.yellow }
  print UI_FRAME_LOWER_RIGHT.yellow
  new_line
end
draw_top_frame(width) click to toggle source
# File lib/ui.rb, line 240
def draw_top_frame(width)
  print UI_FRAME_UPPER_LEFT.yellow
  (width - 2).times { print UI_FRAME_HORIZONTAL.yellow }
  print UI_FRAME_UPPER_RIGHT.yellow
  new_line
end
draw_vert_frame_begin() click to toggle source
# File lib/ui.rb, line 232
def draw_vert_frame_begin
  print UI_FRAME_VERTICAL.yellow + ' '
end
draw_vert_frame_end() click to toggle source
# File lib/ui.rb, line 236
def draw_vert_frame_end
  print ' ' + UI_FRAME_VERTICAL.yellow
end
get_max_size_from_array(array) click to toggle source

Returns size of longest string in array

# File lib/ui.rb, line 260
def get_max_size_from_array(array)
  max = 0
  array.each do |s|
    s_size = get_real_size(s)
    max = s_size if s_size >= max
  end
  max + 4
end
get_real_size(text) click to toggle source

Returns actual length of text accounting for UTF-8 and ANSI

# File lib/ui.rb, line 255
def get_real_size(text)
  text.kind_of?(Array) ? text.size : text.uncolorize.size
end