Class: Metro::SetupHandlers::ReloadGameOnGameFileChanges

Inherits:
Object
  • Object
show all
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

Instance Method Details

#asset_filepathsObject



27
28
29
# File 'lib/setup_handlers/reload_game_on_game_file_changes.rb', line 27

def asset_filepaths
  [ 'assets' ]
end

#on_changeProc

Returns the body of code to execute when a file change event has been received.

Returns:

  • (Proc)

    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

Parameters:



35
36
37
# File 'lib/setup_handlers/reload_game_on_game_file_changes.rb', line 35

def setup(options)
  start_watcher if Game.debug? and not options.packaged?
end

#source_filepathsObject

See Also:



19
20
21
# File 'lib/setup_handlers/reload_game_on_game_file_changes.rb', line 19

def source_filepaths
  [ 'lib', 'scenes', 'models' ]
end

#start_watcherObject



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_filepathsObject



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_filepathsObject



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