shattered_controller 0.3 → 0.3.1

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,91 @@
1
+ module ShatteredController
2
+ module Actor
3
+ def self.append_features(base)
4
+ super
5
+ base.extend(ClassMethods)
6
+ base.send(:include, InstanceMethods)
7
+ end
8
+ module ClassMethods
9
+
10
+ # An actor is the MVC encapsulated into one object.
11
+ # With actors, you can send requests to each of the model, view and control
12
+ # objects, and even retrieve results from the MVC objects.
13
+ #
14
+ # Here is how it works: You specify a actor in a game state with some starting
15
+ # attributes:
16
+ #
17
+ # actor :napolean, :mood => 'snobbish'
18
+ #
19
+ # The options are the same as the camera options, except they propogate through
20
+ # the MVC object(as all commands do).
21
+ #
22
+ # When obtaining a result, the result will be the a response in the following order
23
+ # - Model
24
+ # - View
25
+ # - Controller
26
+ #
27
+ # They are sorted so that you get the most useful response first.
28
+ def actor( name, options = {} )
29
+ before_init_call( :actor, name, options )
30
+ end
31
+
32
+ end
33
+ module InstanceMethods
34
+
35
+ def actors
36
+ return @actors || []
37
+ end
38
+
39
+ # Used by Base#actor
40
+ def actor(name=nil, options={})
41
+ raise Error, "actor needs a name" if name.nil?
42
+ actor = Actor.new(name,load_actor_classes(name))
43
+
44
+ attr_reader(name.to_sym, actor)
45
+ call_object_function_for_each_key( name.to_sym, options )
46
+
47
+ @actors ||= []
48
+ @actors << actor
49
+ end
50
+
51
+ private
52
+
53
+ def load_actor_classes(name)
54
+ actor_options={}
55
+ ['model', 'view', 'controller'].each do |type|
56
+ evaled_class = nil
57
+ begin
58
+ evaled_class = eval("#{name.to_s.camelize}#{type.camelize}")
59
+ rescue NameError
60
+ next
61
+ end
62
+ actor_options[type.to_sym] = evaled_class.new
63
+ end
64
+ raise Error, "No Model, View, or Control defined for Actor #{name}." if actor_options == {}
65
+ return actor_options
66
+ end
67
+
68
+ end
69
+
70
+ # An actor is a delegator to a controller game object.
71
+ class Actor
72
+ attr_reader :controller
73
+ def initialize( name, options = {} )
74
+ @actor_name = name
75
+ @controller = (options[:controller] || ShatteredController::Base.new)
76
+ @controller.model = options[:model]
77
+ @controller.view = options[:view]
78
+ @controller.view.model = options[:model] unless @controller.view.nil?
79
+
80
+ if options[:view].nil? && options[:model].nil? && @controller.nil? && !options[:testing]
81
+ raise NameError, "No model view or controller found for actor #{name}"
82
+ end
83
+ end
84
+
85
+ # Delegate to controller
86
+ def method_missing(name, *args, &block)
87
+ @controller.send(name, *args, &block)
88
+ end
89
+ end
90
+ end
91
+ end
data/lib/base.rb CHANGED
@@ -4,13 +4,41 @@ $:.unshift(File.dirname(__FILE__)) unless
4
4
  $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
5
5
 
6
6
  require 'keyboard_input/keyboard_input'
7
+ require 'actor/actor'
7
8
 
8
9
  module ShatteredController
10
+
9
11
  class Base < ShatteredSupport::Base
12
+ attr_accessor :model, :view
13
+
14
+ # Controller delegates all unknown methods to the view and model.
15
+ def method_missing(method_name, *args, &block)
16
+ raise_no_method_error = true
17
+ result = nil
18
+ [view, model].each do |delegate|
19
+ if delegate.method_defined?(method_name)
20
+ result = delegate.send(method_name, *args, &block)
21
+ raise_no_method_error = false
22
+ end
23
+ end
24
+ raise NoMethodError, "Model, View, and Controller have no method defined named '#{method_name}'" if raise_no_method_error
25
+ return result
26
+ end
27
+
28
+
29
+ def update_timer(time_elapsed)
30
+ timer.update(time_elapsed)
31
+ model.timer.update(time_elapsed)
32
+ view.timer.update(time_elapsed)
10
33
  unless view.nil?
34
+ end
11
35
  end
36
+
37
+ class Error < StandardError
38
+ end
12
39
  end
13
40
 
14
41
  ShatteredController::Base.class_eval do
15
42
  include ShatteredController::KeyboardInput
43
+ include ShatteredController::Actor
16
44
  end
17
45
 
@@ -1,7 +1,7 @@
1
1
  module ShatteredController
2
2
  # This is a wrapper around Ogre's input listener.
3
3
  # It allows us to query the status of each device every frame.
4
- class Input
4
+ class KeyConverter
5
5
 
6
6
  def initialize( ogre_input )
7
7
  @ogre_input = ogre_input
@@ -1,7 +1,7 @@
1
1
  $:.unshift(File.dirname(__FILE__)) unless
2
2
  $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
3
 
4
- require 'input'
4
+ require 'key_converter'
5
5
 
6
6
  module ShatteredController
7
7
  module KeyboardInput
@@ -36,6 +36,14 @@ module ShatteredController
36
36
  end
37
37
 
38
38
  module InstanceMethods
39
+ def pre_initialize
40
+ # update our keyboard input
41
+ timer.every(:frame) do |time_elapsed|
42
+ update_input(time_elapsed, ShatteredController::Runner.environment[:input])
43
+ end
44
+ super
45
+ end
46
+
39
47
  def key_types
40
48
  [:pressed, :released, :held]
41
49
  end
@@ -46,11 +54,11 @@ module ShatteredController
46
54
  def reaction_from(options)
47
55
  # Define the method to send the actions when the keys are pressed/clicked/released.
48
56
  key_types.each do |reaction|
49
- return [reaction, options[reaction],options[:action]] if !options[reaction].nil?
57
+ return [reaction, options[reaction].to_sym, options[:action]] if !options[reaction].nil?
50
58
  end
51
- raise ArgumentError, "Reaction to key input must be pressed, released, or held. #{options.inspect}" if options[:reaction].nil?
59
+ raise Error, "Reaction to key input must be pressed, released, or held. #{options.inspect}" if options[:reaction].nil?
52
60
  end
53
- # Todo: when we refactor before_init_set to not use ='s optionally, we need to allow non *args
61
+ # See KeyboardInput::key
54
62
  def key(actions={})
55
63
  key_actions << reaction_from(actions)
56
64
  end
@@ -58,9 +66,9 @@ module ShatteredController
58
66
  # are defined.
59
67
  def key_action(reaction, action, time_elapsed)
60
68
  if reaction == :held
61
- actor.send(action, time_elapsed)
69
+ send(action, time_elapsed)
62
70
  else
63
- actor.send(action)
71
+ send(action)
64
72
  end
65
73
  end
66
74
  def key_actions
@@ -72,6 +80,7 @@ module ShatteredController
72
80
  key_action( reaction, action, time_elapsed ) if input.key_event?(reaction,key)
73
81
  end
74
82
  end
83
+
75
84
  end
76
85
  end
77
86
  end
data/lib/runner.rb CHANGED
@@ -5,14 +5,13 @@ module ShatteredController
5
5
  class Runner
6
6
  def initialize( options = {} )
7
7
  @@environment ||= options
8
- @@environment[:input] = ShatteredController::Input.new(@@environment[:input])
8
+ @@environment[:input] = ShatteredController::KeyConverter.new(@@environment[:input])
9
9
  end
10
10
  #Every time this exits, a game dies.
11
11
  def start_game
12
12
  each_frame do |time_elapsed|
13
- @@environment[:state].update_input(time_elapsed, @@environment[:input])
13
+ @@environment[:state].update_timers(time_elapsed)
14
14
  @@environment[:input].flush
15
- @@environment[:state].update_actors time_elapsed
16
15
  end
17
16
  end
18
17
  def each_frame
@@ -1,5 +1,4 @@
1
1
  begin
2
- # require File.dirname(__FILE__)+"/../../shattered_support/lib/shattered_support"
3
2
  require 'shattered_support'
4
3
  rescue MissingSourceFile
5
4
  require 'rubygems'
data/lib/state.rb CHANGED
@@ -35,7 +35,8 @@ module ShatteredController
35
35
  before_init_set( "self", { :"sky" => [type, options[:material]] })
36
36
  end
37
37
  end
38
- class State < ShatteredSupport::Base
38
+ class State < ShatteredController::Base
39
+
39
40
  def pre_initialize
40
41
  ShatteredSupport::Configuration.environment[:state] = self
41
42
  super
@@ -47,8 +48,11 @@ module ShatteredController
47
48
  call_object_function_for_each_key(:camera, args[0])
48
49
  end
49
50
 
50
- def update_input(time_elapsed, input)
51
- actors.each { |actor| actor.update_input(time_elapsed, input) }
51
+ # This is called every frame by the main game loop. All update events are now
52
+ # run through Timers.
53
+ def update_timers(time_elapsed)
54
+ update_timer(time_elapsed)
55
+ actors.each { |actor| actor.update_timer(time_elapsed) }
52
56
  end
53
57
 
54
58
  def unload!
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.10
3
3
  specification_version: 1
4
4
  name: shattered_controller
5
5
  version: !ruby/object:Gem::Version
6
- version: "0.3"
7
- date: 2006-04-23
6
+ version: 0.3.1
7
+ date: 2006-05-16
8
8
  summary: "Shattered Controller: Controls the game states and objects in Shattered."
9
9
  require_paths:
10
10
  - lib
@@ -26,14 +26,14 @@ required_ruby_version: !ruby/object:Gem::Version::Requirement
26
26
  platform: ruby
27
27
  authors: []
28
28
  files:
29
- - lib/auto_require.rb
30
29
  - lib/base.rb
31
30
  - lib/mock_camera.rb
32
31
  - lib/runner.rb
33
32
  - lib/shattered_controller.rb
34
33
  - lib/shattered_controller_tester.rb
35
34
  - lib/state.rb
36
- - lib/keyboard_input/input.rb
35
+ - lib/actor/actor.rb
36
+ - lib/keyboard_input/key_converter.rb
37
37
  - lib/keyboard_input/keyboard_input.rb
38
38
  test_files: []
39
39
  rdoc_options: []
data/lib/auto_require.rb DELETED
@@ -1,2 +0,0 @@
1
- require File.dirname(__FILE__)+'/runner'
2
- require File.dirname(__FILE__)+'/state'