shattered_controller 0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ require File.dirname(__FILE__)+'/runner'
2
+ require File.dirname(__FILE__)+'/state'
data/lib/base.rb ADDED
@@ -0,0 +1,16 @@
1
+ include ShatteredSupport
2
+
3
+ $:.unshift(File.dirname(__FILE__)) unless
4
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
5
+
6
+ require 'keyboard_input/keyboard_input'
7
+
8
+ module ShatteredController
9
+ class Base < ShatteredSupport::Base
10
+ end
11
+ end
12
+
13
+ ShatteredController::Base.class_eval do
14
+ include ShatteredController::KeyboardInput
15
+ end
16
+
@@ -0,0 +1,43 @@
1
+ module ShatteredController
2
+ # This is a wrapper around Ogre's input listener.
3
+ # It allows us to query the status of each device every frame.
4
+ class Input
5
+
6
+ def initialize( ogre_input )
7
+ @ogre_input = ogre_input
8
+ end
9
+ def key_event?( type, key_symbol )
10
+ return @ogre_input.send(:"isKey#{type.to_s.capitalize}", self.class.convert(key_symbol))
11
+ end
12
+ def flush
13
+ @ogre_input.flush
14
+ end
15
+
16
+ def self.back_conversions
17
+ @@back_conversions ||= conversions.invert
18
+ end
19
+ def self.conversions
20
+ begin
21
+ return @@conversions
22
+ rescue NameError
23
+ end
24
+ @@conversions = {}
25
+ ShatteredOgre.constants.each do |constant|
26
+ kc_evaled = eval("ShatteredOgre::#{constant}")
27
+ @@conversions[kc_evaled] = symbolize(constant[3..-1].downcase) if constant.to_s =~ /^KC_/
28
+ end
29
+ return @@conversions
30
+ end
31
+ def self.convert( kc_character )
32
+ return self.conversions[kc_character] if kc_character.is_a? Fixnum
33
+ return self.back_conversions[kc_character]
34
+ end
35
+ private
36
+ def self.symbolize( name )
37
+ exceptions = { 'pgdown' => :page_down,
38
+ 'pgup' => :page_up }
39
+ return name.to_sym if exceptions[name] == nil
40
+ return exceptions[name]
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,77 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ require 'input'
5
+
6
+ module ShatteredController
7
+ module KeyboardInput
8
+ def self.append_features(base)
9
+ super
10
+ base.extend(ClassMethods)
11
+ base.send(:include, InstanceMethods)
12
+ end
13
+ module ClassMethods
14
+ # Key pressing can take any of the following forms:
15
+ # key :released => :up, :action => :clicked_up
16
+ # key :pressed => :left, :action => :left_pressed
17
+ # key :held => :down, :action => :move_down_fluidly
18
+ # where each of the actions correspond to an actor function:
19
+ #
20
+ # class ExampleView < ShatteredView::Base
21
+ # def clicked_up
22
+ # #called when up is released
23
+ # end
24
+ # def left_pressed
25
+ # #called when left is released
26
+ # end
27
+ # def move_down_fluidly(time_elapsed)
28
+ # #called when down is held
29
+ # end
30
+ # end
31
+ #
32
+ # Only key :pressed recieves the time_elapsed variable (as it is updated per frame)
33
+ def key(options = {})
34
+ before_init_call(:key, options)
35
+ end
36
+ end
37
+
38
+ module InstanceMethods
39
+ def key_types
40
+ [:pressed, :released, :held]
41
+ end
42
+ # Given an input of:
43
+ # key :held=>:up, :action => :key_action
44
+ # This will return [:held, :up, :key_action]
45
+ # This is not used. Todo: use in new_key
46
+ def reaction_from(options)
47
+ # Define the method to send the actions when the keys are pressed/clicked/released.
48
+ key_types.each do |reaction|
49
+ return [reaction, options[reaction],options[:action]] if !options[reaction].nil?
50
+ end
51
+ raise ArgumentError, "Reaction to key input must be pressed, released, or held. #{options.inspect}" if options[:reaction].nil?
52
+ end
53
+ # Todo: when we refactor before_init_set to not use ='s optionally, we need to allow non *args
54
+ def key(actions={})
55
+ key_actions << reaction_from(actions)
56
+ end
57
+ # Update_key will send each of the events to the actor object for whatever key options
58
+ # are defined.
59
+ def key_action(reaction, action, time_elapsed)
60
+ if reaction == :held
61
+ actor.send(action, time_elapsed)
62
+ else
63
+ actor.send(action)
64
+ end
65
+ end
66
+ def key_actions
67
+ @key_actions ||= []
68
+ end
69
+ # update_input takes the input object, and sends the actor every key event this frame.
70
+ def update_input(time_elapsed, input)
71
+ key_actions.each do |reaction, key, action|
72
+ key_action( reaction, action, time_elapsed ) if input.key_event?(reaction,key)
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,9 @@
1
+
2
+ module ShatteredController
3
+ class MockCamera
4
+ attr_reader :position
5
+ def position=(x,y,z)
6
+ @position=[x,y,z]
7
+ end
8
+ end
9
+ end
data/lib/runner.rb ADDED
@@ -0,0 +1,34 @@
1
+
2
+ module ShatteredController
3
+ #The class Runner is present in model, view, and controller, and signifies the
4
+ #main entry point into the program. This is called by script/runner.rb
5
+ class Runner
6
+ def initialize( options = {} )
7
+ @@environment ||= options
8
+ @@environment[:input] = ShatteredController::Input.new(@@environment[:input])
9
+ end
10
+ #Every time this exits, a game dies.
11
+ def start_game
12
+ each_frame do |time_elapsed|
13
+ @@environment[:state].update_input(time_elapsed, @@environment[:input])
14
+ @@environment[:input].flush
15
+ @@environment[:state].update_actors time_elapsed
16
+ end
17
+ end
18
+ def each_frame
19
+ yield @@environment[:renderer].timeSinceLastFrame while @@environment[:renderer].nextFrame
20
+ end
21
+ def self.environment
22
+ begin
23
+ return @@environment
24
+ rescue NameError
25
+ return mock_environment
26
+ end
27
+ end
28
+ def self.mock_environment
29
+ retv = {}
30
+ retv[:camera] = MockCamera.new
31
+ return retv
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,13 @@
1
+ begin
2
+ # require File.dirname(__FILE__)+"/../../shattered_support/lib/shattered_support"
3
+ require 'shattered_support'
4
+ rescue MissingSourceFile
5
+ require 'rubygems'
6
+ require 'active_support'
7
+ require 'shattered_support'
8
+ end
9
+
10
+ require File.dirname(__FILE__)+'/runner'
11
+ require File.dirname(__FILE__)+'/base'
12
+ require File.dirname(__FILE__)+'/state'
13
+ require File.dirname(__FILE__)+'/mock_camera'
@@ -0,0 +1,4 @@
1
+ ["shattered_support","shattered_ogre"].each do |dependancy|
2
+ $: << File.dirname(__FILE__) + "/../../#{dependancy}/lib"
3
+ end
4
+ require 'shattered_controller'
data/lib/state.rb ADDED
@@ -0,0 +1,63 @@
1
+ include ShatteredSupport
2
+
3
+ module ShatteredController
4
+ def self.append_features(base)
5
+ super
6
+ base.extend(ClassMethods)
7
+ end
8
+ module ClassMethods
9
+ # In order to set the initial position of the camera, and any other attributes you would like,
10
+ # use the following format.
11
+ # class CameraTestState < ShatteredController::Base
12
+ # camera :position => [10,0,0], :orientation => [0.5, 0.5, 0.5, 0.5]
13
+ # end
14
+ #
15
+ # If you pass a symbol as a value, the symbol will be evaluated.
16
+ #
17
+ # This allows for things such as:
18
+ #
19
+ # class CameraTestState < ShatteredController::Base
20
+ # camera :position => :camera_position
21
+ # def camera_position
22
+ # return [rand*10,0,0]
23
+ # end
24
+ # end
25
+ #
26
+ # The valid options are any public functions within camera that ends in =
27
+ # (such as position=, looking_at=) See ShatteredView::Camera for more.
28
+ def camera(options = {})
29
+ before_init_call( 'camera', options )
30
+ end
31
+
32
+ # This blurs the lines between view and controller.
33
+ # Valid types are [:box]
34
+ def sky(type, options = {})
35
+ before_init_set( "self", { :"sky" => [type, options[:material]] })
36
+ end
37
+ end
38
+ class State < ShatteredSupport::Base
39
+ def pre_initialize
40
+ ShatteredSupport::Configuration.environment[:state] = self
41
+ super
42
+ end
43
+ def camera(*args)
44
+ if args.length == 0
45
+ return @camera ||= Runner.environment[:camera]
46
+ end
47
+ call_object_function_for_each_key(:camera, args[0])
48
+ end
49
+
50
+ def update_input(time_elapsed, input)
51
+ actors.each { |actor| actor.update_input(time_elapsed, input) }
52
+ end
53
+
54
+ def unload!
55
+ actors.each { |actor| actor.unload! }
56
+ end
57
+
58
+ def sky=(type, material)
59
+ Runner.environment[:scene].send("setSky#{type.to_s.capitalize}",material)
60
+ end
61
+ end
62
+ end
63
+
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.10
3
+ specification_version: 1
4
+ name: shattered_controller
5
+ version: !ruby/object:Gem::Version
6
+ version: "0.3"
7
+ date: 2006-04-23
8
+ summary: "Shattered Controller: Controls the game states and objects in Shattered."
9
+ require_paths:
10
+ - lib
11
+ email:
12
+ homepage: http://www.hastilymade.com
13
+ rubyforge_project:
14
+ description: Shattered Controller is used to create game states and objects within those gamestates. Shattered Controller is an integral part of any Shattered game.
15
+ autorequire: shattered_controller
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: "true"
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ -
22
+ - ">"
23
+ - !ruby/object:Gem::Version
24
+ version: 0.0.0
25
+ version:
26
+ platform: ruby
27
+ authors: []
28
+ files:
29
+ - lib/auto_require.rb
30
+ - lib/base.rb
31
+ - lib/mock_camera.rb
32
+ - lib/runner.rb
33
+ - lib/shattered_controller.rb
34
+ - lib/shattered_controller_tester.rb
35
+ - lib/state.rb
36
+ - lib/keyboard_input/input.rb
37
+ - lib/keyboard_input/keyboard_input.rb
38
+ test_files: []
39
+ rdoc_options: []
40
+ extra_rdoc_files: []
41
+ executables: []
42
+ extensions: []
43
+ requirements:
44
+ - Shattered Controller is reliant on Shattered View and Ogre.
45
+ dependencies: []