module Goby

Collection of classes, modules, and functions that make up the Goby framework.

Public Instance Methods

load_game(filename) click to toggle source

Reads and check the save file and parses into the player object

@param [String] filename the file containing the save data. @return [Player] the player corresponding to the save data.

# File lib/goby/util.rb, line 106
def load_game(filename)
  begin
    player = YAML.load_file(filename)
    return player
  rescue
    return nil
  end
end
player_input(lowercase: true, prompt: '', doublespace: true) click to toggle source

Simple player input script.

@param [Boolean] lowercase mark true if response should be returned lowercase. @param [String] prompt the prompt for the user to input information. @param [Boolean] doublespace mark false if extra space should not be printed after input.

# File lib/goby/util.rb, line 55
def player_input(lowercase: true, prompt: '', doublespace: true)

  # When using Readline, rspec actually prompts the user for input, freezing the tests.
  print prompt
  input = (ENV['TEST'] == 'rspec') ? gets.chomp : Readline.readline(" \b", false)
  puts "\n" if doublespace

  if ((input.size > 1) and (input != Readline::HISTORY.to_a[-1]))
    Readline::HISTORY.push(input)
  end

  return lowercase ? input.downcase : input
end
run_driver(player) click to toggle source

Runs the main game loop.

@param [Player] player the player of the game.

# File lib/goby/driver.rb, line 11
def run_driver(player)
  while (run_turn(player)); end
  stop_music
end
save_game(player, filename) click to toggle source

Serializes the player object into a YAML file and saves it

@param [Player] player the player object to be saved. @param [String] filename the name under which to save the file.

# File lib/goby/util.rb, line 88
def save_game(player, filename)

  # Set 'moved' to true so we see minimap on game load.
  player.moved = true
  player_data = YAML::dump(player)
  player.moved = false

  File.open(filename, "w") do |file|
    file.puts player_data
  end
  print "Successfully saved the game!\n\n"
  return
end
type(message) click to toggle source

Prints text as if it were being typed.

@param [String] message the message to type out.

# File lib/goby/util.rb, line 72
def type(message)

  # Amount of time to sleep between printing character.
  time = ENV['TEST'] ? 0 : 0.015

  # Sleep between printing of each char.
  message.split("").each do |i|
    sleep(time) if time.nonzero?
    print i
  end
end

Private Instance Methods

clear_and_minimap(player) click to toggle source

Clear the terminal and display the minimap.

@param [Player] player the player of the game.

# File lib/goby/driver.rb, line 21
def clear_and_minimap(player)
  system("clear") unless ENV["TEST"]
  describe_tile(player)
end
run_turn(player) click to toggle source

Runs a single command from the player on the world map.

@param [Player] player the player of the game. @return [Bool] true iff the player does not want to quit.

# File lib/goby/driver.rb, line 30
def run_turn(player)

  # Play music and re-display the minimap (when appropriate).
  music = player.location.map.music
  play_music(music) if music
  if player.moved
    clear_and_minimap(player)
    player.moved = false
  end

  # Receive input and run the command.
  input = player_input prompt: '> '
  interpret_command(input, player)

  return !input.eql?("quit")
end