Class: Metro::SetupHandlers::LoadGameFiles

Inherits:
Object
  • Object
show all
Defined in:
lib/setup_handlers/load_game_files.rb

Overview

LoadGameFiles will load all the game files within the current working directory that are in the pre-defined Metro game folders.

LoadGameFiles uses ActiveSupport::Dependencies::WatchStack to spy on all the constants that are added when the game runs. This grants the class the ability to provide dynamic reloading of the classes as they change.

Instance Method Summary collapse

Instance Method Details

#execute_watcher!Object

Note:

an exception is raised if the watcher is not prepared every time this is called.

The watcher will now mark all the constants that it has watched being loaded as unloadable. Doing so exhausts the list of constants found so the watcher will be empty.



93
94
95
# File 'lib/setup_handlers/load_game_files.rb', line 93

def execute_watcher!
  watcher.new_constants.each { |constant| unloadable constant }
end

#launch_game_in_dry_run_modeObject

Launch the game in a sub-process in dry-run mode. Starting the game in dry-run mode here makes it so it will not launch a window and simply check to see if the code is valid and working correctly.



46
47
48
# File 'lib/setup_handlers/load_game_files.rb', line 46

def launch_game_in_dry_run_mode
  GameExecution.execute Game.execution_parameters + [ "--dry-run" ]
end

#load_game_filesObject



68
69
70
71
72
73
# File 'lib/setup_handlers/load_game_files.rb', line 68

def load_game_files
  $LOAD_PATH.unshift(Dir.pwd) unless $LOAD_PATH.include?(Dir.pwd)
  load_paths 'lib'
  load_path 'scenes', prioritize: 'game_scene.rb'
  load_path 'models', prioritize: 'game_model.rb'
end

#load_game_files!Object

Drop any already defined events. Drop all the existing classes, reload the game files, and prepare the new game files for unloading the next time around that reload has been called.



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/setup_handlers/load_game_files.rb', line 29

def load_game_files!
  EventDictionary.reset!
  Image.images.clear
  Animation.images.clear
  Song.songs.clear
  Font.fonts.clear

  prepare_watcher!
  load_game_files
  execute_watcher!
end

#load_path(path, options = {}) ⇒ Object



79
80
81
82
83
# File 'lib/setup_handlers/load_game_files.rb', line 79

def load_path(path,options = {})
  files = Dir["#{path}/**/*.rb"]
  files.sort! {|file| File.basename(file) == options[:prioritize] ? -1 : 1 }
  files.each {|file| require_or_load file }
end

#load_paths(*paths) ⇒ Object



75
76
77
# File 'lib/setup_handlers/load_game_files.rb', line 75

def load_paths(*paths)
  paths.flatten.compact.each {|path| load_path path }
end

#prepare_watcher!Object

The watcher should watch the Object Namespace for any changes. Any constants that are added will be tracked from this point forward.



54
55
56
57
# File 'lib/setup_handlers/load_game_files.rb', line 54

def prepare_watcher!
  ActiveSupport::Dependencies.clear
  watcher.watch_namespaces([ Object ])
end

#setup(options) ⇒ Object

Parameters:



20
21
22
# File 'lib/setup_handlers/load_game_files.rb', line 20

def setup(options)
  load_game_files!
end

#watcherObject

The watcher will keep track of all the constants that were added to the Object Namespace after the start of the execution of the game. This will allow for only those objects to be reloaded.



64
65
66
# File 'lib/setup_handlers/load_game_files.rb', line 64

def watcher
  @watcher ||= ActiveSupport::Dependencies::WatchStack.new
end