Class: Gemthief::Executive

Inherits:
Object
  • Object
show all
Includes:
Callbacks, WindowManagement
Defined in:
lib/gemthief/executive.rb,
lib/gemthief/executive/callbacks.rb,
lib/gemthief/executive/window_management.rb

Defined Under Namespace

Modules: Callbacks, WindowManagement

Constant Summary

Constants included from Callbacks

Callbacks::CALLBACKS

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from WindowManagement

#init_curses!, #refresh_curses!, #root_window

Class Attribute Details

.called_fromObject

Returns the value of attribute called_from.



39
40
41
# File 'lib/gemthief/executive.rb', line 39

def called_from
  @called_from
end

.initial_scene_nameObject

Returns the value of attribute initial_scene_name.



41
42
43
# File 'lib/gemthief/executive.rb', line 41

def initial_scene_name
  @initial_scene_name
end

.initial_scene_setupObject

Returns the value of attribute initial_scene_setup.



42
43
44
# File 'lib/gemthief/executive.rb', line 42

def initial_scene_setup
  @initial_scene_setup
end

Instance Attribute Details

#active_sceneObject

Returns the value of attribute active_scene.



10
11
12
# File 'lib/gemthief/executive.rb', line 10

def active_scene
  @active_scene
end

Class Method Details

.find_root(from) ⇒ Object



59
60
61
# 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) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# 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) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 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) ⇒ Object



75
76
77
78
79
80
81
82
# 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

Instance Method Details

#configObject



12
13
14
# File 'lib/gemthief/executive.rb', line 12

def config
  @config ||= Gemthief::Config.new(self.class.root_path(self.class.called_from))
end

#initial_sceneObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# 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!Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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