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 (readonly)

Returns the value of attribute render_window.



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

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



89
90
91
92
93
94
95
96
# File 'lib/game_loader.rb', line 89

def each_frame
 	timer = Ogre::Timer.new
   while !@environment[:quit] && Ogre::Root::instance.render_one_frame
   	seconds = timer.get_microseconds / 1000000.0
    yield seconds
     timer.reset
	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



99
100
101
# File 'lib/game_loader.rb', line 99

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: Consider how to make input non-exclusive, as well as mouse, etc. TODO: Investigate any use cases for non-buffered input



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/game_loader.rb', line 106

def setup_input
	windowHnd = render_window.get_custom_attribute_unsigned_long("WINDOW")
	@input_manager = OIS::InputManager.create_input_system(
{  "WINDOW" => "#{windowHnd}",
	 "w32_mouse" => ["DISCL_FOREGROUND", "DISCL_NONEXCLUSIVE"],
	 "w32_keyboard" => ["DISCL_FOREGROUND", "DISCL_NONEXCLUSIVE"]
})
	@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

#start_gameObject

Every time this exits, a game dies.



80
81
82
83
84
85
86
# File 'lib/game_loader.rb', line 80

def start_game
  each_frame do |time_elapsed|
    current_state.update_timers(time_elapsed)
    @keyboard.capture
    #@@environment[:input].flush
  end
end