class TheGame::Game

Constants

I

Attributes

created_at[RW]
items[RW]
level[RW]
name[RW]
saved_at[RW]

Public Class Methods

new() click to toggle source
# File lib/the_game/game.rb, line 10
def initialize
  TheGame.initialize!
  @created_at = Time.now
  @saved_at   = Time.now
  @name       = "anonymous"
  @level      = 1
  @items      = [I.new("Gold"), I.new("Sword"), I.new("Orb of POWAH")]
end

Public Instance Methods

load(savefile = TheGame::SAVEFILE_PATH) click to toggle source
# File lib/the_game/game.rb, line 28
def load(savefile = TheGame::SAVEFILE_PATH)
  yaml = YAML.load(read_savefile(savefile))
  @created_at = yaml['created_at']
  @saved_at   = yaml['saved_at']
  @name       = yaml['name']
  @level      = yaml['level']
end
prompt() click to toggle source

Names are green, locations are magenta, levels are blue.

# File lib/the_game/game.rb, line 55
def prompt
  "\e[1;32m#{@name} \e[35m/ \e[34m#{@level} $\e[0m "
end
read_savefile(savefile = TheGame::SAVEFILE_PATH) click to toggle source
# File lib/the_game/game.rb, line 41
def read_savefile(savefile = TheGame::SAVEFILE_PATH)
  Zlib::GzipReader.open(savefile) { |gz| gz.read }
end
save(savefile = TheGame::SAVEFILE_PATH) click to toggle source
# File lib/the_game/game.rb, line 36
def save(savefile = TheGame::SAVEFILE_PATH)
  @saved_at = Time.now
  Zlib::GzipWriter.open(savefile, Zlib::BEST_COMPRESSION) { |gz| gz << yaml_dump }
end
set(key, value) click to toggle source
# File lib/the_game/game.rb, line 45
def set(key, value)
              case key.downcase.to_sym
              when :name then @name = value
              when :level then @level = value
              else raise ArgumentError, "Cannot set #{key}!" end
end
start!() click to toggle source
# File lib/the_game/game.rb, line 59
def start!
  puts "~ the_game.rb v#{TheGame::Version::STRING} ~\nuse `help` for available commands"

  trap(:INT) { puts; exit }
  
  loop do
    input = ::Readline.readline(prompt, true)
    output = begin
      TheGame::Command.send(self, input)
    rescue => e
      e.pp
    end
    puts output unless input.blank? or output.blank?
  end
end
yaml_dump() click to toggle source
# File lib/the_game/game.rb, line 19
def yaml_dump
  YAML.dump({
    'created_at' => @created_at,
    'saved_at'   => @saved_at,
    'name'       => @name,
    'level'      => @level
  })
end