Class: Metro::SetupHandlers::ReloadGameOnGameFileChanges
- Inherits:
-
Object
- Object
- Metro::SetupHandlers::ReloadGameOnGameFileChanges
- Defined in:
- lib/setup_handlers/reload_game_on_game_file_changes.rb
Overview
When the game is launched in debug mode the game directories will be monitored for changes. When a change occurs within one of the game source files the game and scene will be reloaded.
Instance Method Summary collapse
- #asset_filepaths ⇒ Object
-
#on_change ⇒ Proc
The body of code to execute when a file change event has been received.
-
#reload_game_because_files_changed(changed) ⇒ Object
Perform a game reload if the game source is valid.
- #setup(options) ⇒ Object
- #source_filepaths ⇒ Object
- #start_watcher ⇒ Object
- #view_filepaths ⇒ Object
-
#watch_filepaths(filepaths) ⇒ Object
Defines the listener that will watch the filepaths.
- #watched_filepaths ⇒ Object
Instance Method Details
#asset_filepaths ⇒ Object
27 28 29 |
# File 'lib/setup_handlers/reload_game_on_game_file_changes.rb', line 27 def asset_filepaths [ 'assets' ] end |
#on_change ⇒ Proc
Returns the body of code to execute when a file change event has been received.
57 58 59 |
# File 'lib/setup_handlers/reload_game_on_game_file_changes.rb', line 57 def on_change Proc.new { |modified,added,removed| reload_game_because_files_changed(modified + added + removed) } end |
#reload_game_because_files_changed(changed) ⇒ Object
Perform a game reload if the game source is valid.
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/setup_handlers/reload_game_on_game_file_changes.rb', line 64 def reload_game_because_files_changed(changed) log.debug "Metro has detected #{changed.count} game source #{changed.count != 1 ? 'files have' : 'file has'} changed. RELOADING GAME CODE!" if Metro.game_has_valid_code? Game.current_scene.after(1.tick) do Metro.reload! scene_is_being_edited = scene_name == "metro/edit_transition" transition_to(scene_name) unless scene_is_being_edited end end end |
#setup(options) ⇒ Object
35 36 37 |
# File 'lib/setup_handlers/reload_game_on_game_file_changes.rb', line 35 def setup() start_watcher if Game.debug? and not .packaged? end |
#source_filepaths ⇒ Object
19 20 21 |
# File 'lib/setup_handlers/reload_game_on_game_file_changes.rb', line 19 def source_filepaths [ 'lib', 'scenes', 'models' ] end |
#start_watcher ⇒ Object
39 40 41 42 |
# File 'lib/setup_handlers/reload_game_on_game_file_changes.rb', line 39 def start_watcher Thread.abort_on_exception = true Thread.new { watch_filepaths(watched_filepaths) } end |
#view_filepaths ⇒ Object
23 24 25 |
# File 'lib/setup_handlers/reload_game_on_game_file_changes.rb', line 23 def view_filepaths [ 'views' ] end |
#watch_filepaths(filepaths) ⇒ Object
Defines the listener that will watch the filepaths
47 48 49 50 51 |
# File 'lib/setup_handlers/reload_game_on_game_file_changes.rb', line 47 def watch_filepaths(filepaths) listener = Listen.to(*filepaths) listener.change(&on_change) listener.start end |
#watched_filepaths ⇒ Object
11 12 13 |
# File 'lib/setup_handlers/reload_game_on_game_file_changes.rb', line 11 def watched_filepaths source_filepaths + view_filepaths + asset_filepaths end |