Class: Shatter::GameLoader
- Includes:
- Singleton
- Defined in:
- lib/game_loader.rb
Overview
This class is loads the view, controller, and model. It then loads and starts the game based on these.
Instance Attribute Summary collapse
-
#current_state ⇒ Object
Returns the value of attribute current_state.
-
#environment ⇒ Object
writeonly
Sets the attribute environment.
-
#input_manager ⇒ Object
readonly
Returns the value of attribute input_manager.
-
#key_converter ⇒ Object
readonly
Returns the value of attribute key_converter.
-
#key_manager ⇒ Object
readonly
Returns the value of attribute key_manager.
-
#media_paths ⇒ Object
readonly
Returns the value of attribute media_paths.
-
#render_window ⇒ Object
Returns the value of attribute render_window.
Instance Method Summary collapse
-
#create_state(state_name) ⇒ Object
Create a state from the given name :sample creates a new SampleState object.
-
#each_frame ⇒ Object
Main game loop.
-
#load_all_sources(shattered_root) ⇒ Object
This will recourse into the app/** directories and load all ruby files inside those directories.
- #load_environment ⇒ Object
-
#load_resources(paths) ⇒ Object
Given [‘app/media’, ‘media’], searches those directories, recursively, for OGRE media.
-
#quit! ⇒ Object
Leave the game.
-
#run ⇒ Object
start the game.
-
#setup_input ⇒ Object
load the input manager, the key manager, etc TODO: Investigate any use cases for non-buffered input.
-
#setup_keyboard ⇒ Object
Sets up our keyboard object in buffered mode.
-
#setup_ogre ⇒ Object
TODO: allow for the user to specify whether the window dialogue pops up further TODO: allow the user to skin the render window dialogue.
-
#setup_window ⇒ Object
Creates our window and our window event listener.
-
#shutdown! ⇒ Object
Shutdown Ogre / OIS / Navi.
-
#start_game ⇒ Object
Every time this exits, a game dies.
Instance Attribute Details
#current_state ⇒ Object
Returns the value of attribute current_state.
14 15 16 |
# File 'lib/game_loader.rb', line 14 def current_state @current_state end |
#environment=(value) ⇒ Object (writeonly)
Sets the attribute environment
12 13 14 |
# File 'lib/game_loader.rb', line 12 def environment=(value) @environment = value end |
#input_manager ⇒ Object (readonly)
Returns the value of attribute input_manager.
13 14 15 |
# File 'lib/game_loader.rb', line 13 def input_manager @input_manager end |
#key_converter ⇒ Object (readonly)
Returns the value of attribute key_converter.
13 14 15 |
# File 'lib/game_loader.rb', line 13 def key_converter @key_converter end |
#key_manager ⇒ Object (readonly)
Returns the value of attribute key_manager.
13 14 15 |
# File 'lib/game_loader.rb', line 13 def key_manager @key_manager end |
#media_paths ⇒ Object (readonly)
Returns the value of attribute media_paths.
13 14 15 |
# File 'lib/game_loader.rb', line 13 def media_paths @media_paths end |
#render_window ⇒ Object
Returns the value of attribute render_window.
14 15 16 |
# File 'lib/game_loader.rb', line 14 def render_window @render_window end |
Instance Method Details
#create_state(state_name) ⇒ Object
Create a state from the given name :sample creates a new SampleState object
37 38 39 40 41 42 43 44 45 |
# File 'lib/game_loader.rb', line 37 def create_state(state_name) state_class = eval("#{state_name.to_s.camelize}State") raise Error, "#{state_class} is not a class" unless state_class.is_a? Class state = state_class.new unless state.is_a? ShatteredState::Base raise Error, "#{state_class} is an invalid State object (not of ShatteredState::Base)" end return state end |
#each_frame ⇒ Object
Main game loop
91 92 93 94 95 96 97 98 99 |
# File 'lib/game_loader.rb', line 91 def each_frame timer = Ogre::Timer.new while !@environment[:quit] && Ogre::Root::instance.render_one_frame return false if @render_window.closed? seconds = timer.get_microseconds / 1000000.0 timer.reset yield seconds end end |
#load_all_sources(shattered_root) ⇒ Object
This will recourse into the app/** directories and load all ruby files inside those directories.
18 19 20 21 22 |
# File 'lib/game_loader.rb', line 18 def load_all_sources(shattered_root) File.find_by_extension(shattered_root+"/app", "rb").each do |file| require(file) end end |
#load_environment ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/game_loader.rb', line 47 def load_environment #Setup Ogre and the scene for the state raise Error, "Ogre failed to initialize" if !setup_ogre setup_input load_resources(@environment[:media]) end |
#load_resources(paths) ⇒ Object
Given [‘app/media’, ‘media’], searches those directories, recursively, for OGRE media
56 57 58 59 60 61 |
# File 'lib/game_loader.rb', line 56 def load_resources(paths) @media_paths = paths # setup our resource paths specified in environment.rb Resources.instance.add_resource_paths(*paths) Resources.instance.setup end |
#quit! ⇒ Object
Leave the game
115 116 117 |
# File 'lib/game_loader.rb', line 115 def quit! @environment[:quit]=true end |
#run ⇒ Object
start the game
25 26 27 28 29 30 31 32 33 |
# File 'lib/game_loader.rb', line 25 def run load_all_sources(SHATTERED_ROOT) load_environment # Create our initial state, as defined by the environment. create_state(@environment[:start_state]) start_game end |
#setup_input ⇒ Object
load the input manager, the key manager, etc TODO: Investigate any use cases for non-buffered input
121 122 123 124 125 |
# File 'lib/game_loader.rb', line 121 def setup_input setup_window @input_manager = OIS::InputManager.create_input_system(@environment[:window]) setup_keyboard end |
#setup_keyboard ⇒ Object
Sets up our keyboard object in buffered mode.
135 136 137 138 139 |
# File 'lib/game_loader.rb', line 135 def setup_keyboard @keyboard = @input_manager.create_input_object(OIS::OISKeyboard, true) @key_manager = OIS::KeyManager.new(@keyboard) @key_converter = ShatteredPack::KeyConverter.new(@key_manager) end |
#setup_ogre ⇒ Object
TODO: allow for the user to specify whether the window dialogue pops up further TODO: allow the user to skin the render window dialogue
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/game_loader.rb', line 66 def setup_ogre # TODO: create platform loader for other platforms plugins = SHATTERED_ROOT + "/config/ogre_plugins.windows.cfg" config_save=SHATTERED_ROOT + "/config/ogrerb_defaults.save" log=SHATTERED_ROOT + "/log/ogrerb.log" @root = ShatteredOgre.create_root(plugins, config_save, log) return false if !@root.show_config_dialog # TODO: allow application name to be set. @render_window = @root.initialise(true) return true end |
#setup_window ⇒ Object
Creates our window and our window event listener.
128 129 130 131 132 |
# File 'lib/game_loader.rb', line 128 def setup_window windowHnd = render_window.get_custom_attribute_unsigned_long("WINDOW") @environment[:window]["WINDOW"] = windowHnd.to_s #@window_listener = WindowEventsListener.new(@render_window) end |
#shutdown! ⇒ Object
Shutdown Ogre / OIS / Navi
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/game_loader.rb', line 102 def shutdown! #@input_manager.destroy_input_object(@mouse) @input_manager.destroy_input_object(@keyboard) #@input_manager.destroy_input_object(@joy_stick) OIS::InputManager.destroy_input_system(@input_manager) #NEED TO UNREGISTER THE WINDOW LISTENER HERE!!!!!!!!!!!! @input_manager = nil end |
#start_game ⇒ Object
Every time this exits, a game dies.
80 81 82 83 84 85 86 87 88 |
# File 'lib/game_loader.rb', line 80 def start_game each_frame do |time_elapsed| Ogre::WindowEventUtilities:: current_state.update_timers(time_elapsed) @key_manager.flush @keyboard.capture end shutdown! end |