class Gemthief::Executive

Attributes

called_from[RW]
initial_scene_name[RW]
initial_scene_setup[RW]
active_scene[RW]

Public Class Methods

find_root(from) click to toggle source
# File lib/gemthief/executive.rb, line 59
def find_root(from)
  find_root_with_flag "lib", from
end
find_root_with_flag(flag, root_path, default=nil) click to toggle source
# File lib/gemthief/executive.rb, line 63
def find_root_with_flag(flag, root_path, default=nil)
  while root_path && File.directory?(root_path) && !File.exist?("#{root_path}/#{flag}")
    parent = File.dirname(root_path)
    root_path = parent != root_path && parent
  end

  root = File.exist?("#{root_path}/#{flag}") ? root_path : default
  raise "Could not find root path for #{self}" unless root

  Pathname.new File.realpath root
end
inherited(base) click to toggle source
Calls superclass method
# File lib/gemthief/executive.rb, line 44
def inherited(base)
  super
  Gemthief.game_class = base

  base.called_from = begin
    call_stack = if Kernel.respond_to?(:caller_locations)
      caller_locations.map { |l| l.absolute_path || l.path }
    else
      caller.map { |p| p.sub(/:\d+.*/, '') }
    end

    File.dirname(callstack.detect { |p| p !~ %r[lib/gemthief] })
  end
end
initial_scene(scene, &block) click to toggle source
# File lib/gemthief/executive.rb, line 75
def initial_scene(scene, &block)
  @initial_scene_name = case scene
  when Class  then scene.name.underscore
  when String then scene
  when Symbol then scene
  end
  @initial_scene_setup = block
end

Public Instance Methods

config() click to toggle source
# File lib/gemthief/executive.rb, line 12
def config
  @config ||= Gemthief::Config.new(self.class.root_path(self.class.called_from))
end
initial_scene() click to toggle source
# File lib/gemthief/executive.rb, line 85
def initial_scene
  @scene_instance ||= begin
    scene_name = if self.class.initial_scene_name.nil?
      "Initial"
    else
      self.class.initial_scene_name
    end
    scene_class = scene_name.to_s.camelize + "Scene"
    scene_instance = scene_class.constantize.new
    if self.class.initial_scene_setup.respond_to? :call
      self.class.initial_scene_setup.call(scene_instance)
    end
    scene_instance
  end
end
start!() click to toggle source
# File lib/gemthief/executive.rb, line 16
def start!
  run_callbacks :start do
    init_curses!
    self.active_scene = initial_scene
  end

  catch(:game_end) do
    loop do
      active_scene.render!
      refresh_curses!
      active_scene.handle_input stdscr.getch
    end
  end
rescue SystemExit
  # no-op
rescue => ex
  # TODO: Move logging into the framework
  # log! ex
ensure
  run_callbacks :finish
end