Class: Shatter::GameLoader

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

Instance Method Summary collapse

Instance Attribute Details

#current_stateObject

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

Parameters:

  • value

    the value to set the attribute environment to.



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

def environment=(value)
  @environment = value
end

#input_managerObject (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_converterObject (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_managerObject (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_pathsObject (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_windowObject

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

Raises:

  • (Error)


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_frameObject

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_environmentObject

Raises:

  • (Error)


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

#runObject

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_inputObject

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_keyboardObject

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_ogreObject

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_windowObject

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_gameObject

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::message_pump
    current_state.update_timers(time_elapsed)
	  @key_manager.flush
  @keyboard.capture
  end
  shutdown!  
end