class LOTS::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 86
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 + " "
    return gets.chomp
  end
end
cannot_travel_combat() click to toggle source
# File lib/ui.rb, line 168
def cannot_travel_combat
  puts "You are in combat and cannot travel!"
end
clear() click to toggle source

Clear the screen

# File lib/ui.rb, line 27
def clear
  print "\e[H\e[2J"
end
display_map(args) click to toggle source
# File lib/ui.rb, line 31
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 193
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 150
def display_version
  puts "This is " + "Legend of the Sourcerer".light_red + " Version " + LOTS_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 126
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 do |s|
        print s
      end
    else
      print t
    end
    (width - (t_size + 4)).times do
      print " " 
    end
    draw_vert_frame_end
    new_line
  end
  draw_bottom_frame(width)
end
enemy_greet(args) click to toggle source
# File lib/ui.rb, line 207
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 72
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.health.to_s.light_white + " health."
  new_line
end
get_cmd() click to toggle source
# File lib/ui.rb, line 182
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 38
def help
  new_line
  print "Valid Commands".light_green
  new_line(2)
  print UI_ARROW.light_yellow + " " + "east, e, right, ".light_white + "or " + "r".light_white + " - Move east (right)"
  new_line
  print UI_ARROW.light_yellow + " " + "south, s, down, ".light_white + "or " + "d".light_white + " - Move south (down)"
  new_line
  print UI_ARROW.light_yellow + " " + "west, w, left, ".light_white + "or " + "l".light_white + " - Move west (left)"
  new_line
  print UI_ARROW.light_yellow + " " + "north, n, up, ".light_white + "or " + "u".light_white + " - Move north (up)"
  new_line
  print UI_ARROW.light_yellow + " " + "map".light_white + " - Display map"
  new_line
  print UI_ARROW.light_yellow + " " + "where".light_white + " - Describe current surroundings"
  new_line
  print UI_ARROW.light_yellow + " " + "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 + " " + "lines, score, status, info".light_white + " - Display lines of code (score)"
  new_line
  print UI_ARROW.light_yellow + " " + "clear, cls".light_white + " - Clears the screen"
  new_line
  print UI_ARROW.light_yellow + " " + "quit, exit".light_white + " - Quits the game"
  new_line
end
lines(args) click to toggle source
# File lib/ui.rb, line 66
def lines(args)
  player = args[:player]
  print "You currently have " + player.lines.to_s.light_white + " lines of code."
  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 119
def new_line(times = 1)
  times.times do
    print "\n"
  end
end
not_found() click to toggle source
# File lib/ui.rb, line 155
def not_found
  print "Command not understood. Please try again.".red
  new_line
end
not_in_combat() click to toggle source
# File lib/ui.rb, line 172
def not_in_combat
  puts "You are not in combat."
end
out_of_bounds() click to toggle source
# File lib/ui.rb, line 188
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 199
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 79
def player_info(args)
  player = args[:player]
  print "You have " + player.health.to_s.light_white + " health and have " + player.lines.to_s.light_white + " lines of code."
  new_line
end
quit() click to toggle source
# File lib/ui.rb, line 176
def quit
  new_line
  print "You abandoned your journey.".red
  new_line(2)
end
show_location(args) click to toggle source
# File lib/ui.rb, line 160
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 108
def welcome
  text = Array.new
  text << "Legend of the Sourcerer".light_green
  text << "Written by Robert W. Oliver II ".white + UI_EMAIL.light_white + " robert@cidergrove.com".white
  text << "Copyright " + UI_COPYRIGHT + " Sourcerer, All Rights Reserved.".white
  text << "Licensed under GPLv3.".white
  draw_frame({:text => text})
  new_line
end

Private Instance Methods

draw_bottom_frame(width) click to toggle source
# File lib/ui.rb, line 232
def draw_bottom_frame(width)
  print UI_FRAME_LOWER_LEFT.yellow
  (width - 2).times do
    print UI_FRAME_HORIZONTAL.yellow
  end
  print UI_FRAME_LOWER_RIGHT.yellow
  new_line
end
draw_top_frame(width) click to toggle source
# File lib/ui.rb, line 223
def draw_top_frame(width)
  print UI_FRAME_UPPER_LEFT.yellow
  (width - 2).times do
    print UI_FRAME_HORIZONTAL.yellow
  end
  print UI_FRAME_UPPER_RIGHT.yellow
  new_line
end
draw_vert_frame_begin() click to toggle source
# File lib/ui.rb, line 215
def draw_vert_frame_begin
  print UI_FRAME_VERTICAL.yellow + " "
end
draw_vert_frame_end() click to toggle source
# File lib/ui.rb, line 219
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 251
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 242
def get_real_size(text)
  if text.kind_of?(Array)
    text.size
  else
    text.uncolorize.size
  end
end