shattered_support 0.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/actor.rb ADDED
@@ -0,0 +1,40 @@
1
+ module ShatteredSupport
2
+ # An actor is a delegator to a model view control game object.
3
+ class Actor
4
+ attr_accessor :view, :model, :controller
5
+ def initialize( name, options = {} )
6
+ @actor_name = name
7
+ @view = options[:view]
8
+ @model = options[:model]
9
+ @controller = options[:controller]
10
+
11
+ [@view,@model,@controller].each do |component|
12
+ component.actor = self if not component.nil?
13
+ end
14
+
15
+ if @view.nil? and @model.nil? and @controller.nil? and not options[:testing]
16
+ raise NameError, "No model view or controller found for actor #{name}"
17
+ end
18
+ end
19
+ # Whenever an function is called on the actor, it will call that function on it's
20
+ # respective model, view, and control objects.
21
+ def method_missing(name, *args)
22
+ retv=nil
23
+ called=false
24
+ [@controller, @view, @model].each do |callee|
25
+ if(callee.method_defined? name)
26
+ retv = callee.send(name,*args)
27
+ called = true
28
+ end
29
+ end
30
+ raise NoMethodError, "Could not find method #{name} for actor #{@actor_name}" if !called
31
+ return retv
32
+ end
33
+ # The actor should not normally know of the functions used in it's mvc components.
34
+ # is this incorrect behaviour?
35
+ def update_input(time_elapsed, input)
36
+ return if @controller.nil?
37
+ @controller.update_input(time_elapsed, input)
38
+ end
39
+ end
40
+ end
data/lib/base.rb ADDED
@@ -0,0 +1,198 @@
1
+ module ShatteredSupport
2
+
3
+ BEFORE_INIT_CALL_VALUES = :before_init_call_values
4
+ BEFORE_INIT_SET_VALUES = :before_init_set_values
5
+
6
+
7
+ def self.append_features(base)
8
+ super
9
+ base.extend(ClassMethods)
10
+ end
11
+ module ClassMethods
12
+ # An actor is the MVC encapsulated into one object.
13
+ # With actors, you can send requests to each of the model, view and control
14
+ # objects, and even retrieve results from the MVC objects.
15
+ #
16
+ # Here is how it works: You specify a actor in a game state with some starting
17
+ # attributes:
18
+ #
19
+ # actor :napolean, :mood => 'snobbish'
20
+ #
21
+ # The options are the same as the camera options, except they propogate through
22
+ # the MVC object(as all commands do).
23
+ #
24
+ # When obtaining a result, the result will be the a response in the following order
25
+ # - Model
26
+ # - View
27
+ # - Controller
28
+ #
29
+ # They are sorted so that you get the most useful response first.
30
+ def actor( name, options = {} )
31
+ before_init_call( :actor, name, options )
32
+ end
33
+
34
+ def before_init_set(variable, options={})
35
+ self.write_inheritable_array(BEFORE_INIT_SET_VALUES, [[ variable, options ]] )
36
+ end
37
+
38
+ def before_init_call(function, *arguments)
39
+ self.write_inheritable_array(BEFORE_INIT_CALL_VALUES, [[function, [*arguments]]])
40
+ end
41
+ end
42
+ class Base
43
+ attr_writer :actor
44
+ def self.new(*options)
45
+ new_base = allocate
46
+ new_base.pre_initialize
47
+ new_base.send(:initialize, *options)
48
+ return new_base
49
+ end
50
+ def state
51
+ Configuration.environment[:state]
52
+ end
53
+ def pre_initialize
54
+ pre_initialize_set
55
+ pre_initialize_call
56
+ end
57
+
58
+ def each_init_value(name)
59
+ startup_attributes = self.class.read_inheritable_attribute( name ) || []
60
+ startup_attributes.each do |actor, options|
61
+ next if options.nil?
62
+ yield actor, options
63
+ end
64
+ end
65
+
66
+ def pre_initialize_set
67
+ each_init_value(BEFORE_INIT_SET_VALUES) do |variable, options|
68
+ call_object_function_for_each_key( variable, options )
69
+ end
70
+ # self.class.write_inheritable_attribute(BEFORE_INIT_SET_VALUES, nil)
71
+ end
72
+
73
+ def pre_initialize_call
74
+ each_init_value(BEFORE_INIT_CALL_VALUES) do |function, args|
75
+ call_object_function( :self, function, args )
76
+ end
77
+ # self.class.write_inheritable_attribute(BEFORE_INIT_CALL_VALUES, nil)
78
+ end
79
+
80
+ def actors
81
+ return @actors || []
82
+ end
83
+
84
+ # There are two uses for the instance class actor function:
85
+ # The first is to access the actor that the base object is a part of.
86
+ # The second is to create a new actor with the same arguments
87
+ # ShatteredSupport::Base.actor
88
+ def actor(*args)
89
+ return @actor if args.length == 0
90
+
91
+ name, options = args
92
+ actor_options={}
93
+ ['model', 'view', 'controller'].each do |type|
94
+ begin
95
+ require "#{SHATTERED_ROOT}/app/#{type.pluralize}/#{name}_#{type}"
96
+ class_name = "#{name.to_s.camelize}#{type.camelize}"
97
+ actor_options[type.to_sym] = eval(class_name).new
98
+ rescue MissingSourceFile
99
+ end
100
+ end
101
+ actor_options[:testing] = options.delete(:testing)
102
+ actor = Actor.new(name,actor_options)
103
+
104
+ attr_reader(name.to_sym, actor)
105
+ call_object_function_for_each_key( name.to_sym, options )
106
+
107
+ options[:testing] = actor_options[:testing] if !actor_options[:testing].nil?
108
+
109
+ @actors ||= []
110
+ @actors << actor
111
+ end
112
+
113
+ # Overwrite this function to recieve an update event every frame, with the
114
+ # seconds elapsed since last update.
115
+ def update_event(time_elapsed)
116
+ actors.each do |actor|
117
+ actor.update_actors(time_elapsed)
118
+ actor.update(time_elapsed)
119
+ end
120
+ end
121
+
122
+ # remove_from_scene? should just be defined in model.
123
+ # Refactor?
124
+ def remove_from_scene?
125
+ return false
126
+ end
127
+
128
+ def update_actors(time_elapsed)
129
+ update_event time_elapsed
130
+ remove_dead_actors
131
+ end
132
+
133
+ def update(time_elapsed)
134
+ end
135
+
136
+ def remove_dead_actors
137
+ actors.each do |actor|
138
+ remove_from_scene actor if actor.remove_from_scene?
139
+ end
140
+ end
141
+
142
+ def remove_from_scene(actor)
143
+ actors.delete actor
144
+ actor.unload!
145
+ end
146
+
147
+ protected
148
+ def call_object_function_for_each_key( actor, options )
149
+ call_object_function(actor, :position=, options.delete(:position)) if options.has_key? :position
150
+ options.each_pair do |action, params|
151
+ call_object_function actor, "#{action}=", params
152
+ end
153
+ end
154
+ def call_object_function(actor, action, params)
155
+ begin
156
+ if params.is_a? Symbol #|| (params.length == 1 && params[0].is_a?(Symbol)) this will allow substitution in before_init_call. This may not be intended behavior.
157
+ params = eval(params.to_s)
158
+ end
159
+ rescue NameError
160
+ puts "It is not advisable to pass #{params.inspect} to #{action.inspect} for #{actor.inspect}."
161
+ puts " It will try to be evaluated. Use a string instead."
162
+ end
163
+ begin
164
+ eval(actor.to_s).send( action.to_sym, *params )
165
+ rescue NoMethodError => bang
166
+ raise NoMethodError, "Error upon method #{action} for #{actor} of type #{eval(actor.to_s).class}: #{bang.message}"
167
+ end
168
+ end
169
+ def unload!
170
+ end
171
+
172
+ public
173
+
174
+ # attr helpers. This defines instance level attr_* functions.
175
+ def attr_reader(*args)
176
+ name, value = args
177
+ attr_define(:reader, name, value)
178
+ end
179
+
180
+ def attr_writer(*args)
181
+ name, value = args
182
+ attr_define(:reader, name, value)
183
+ end
184
+
185
+ def attr_accessor(*args)
186
+ name, value = args
187
+ attr_define(:accessor, name, value)
188
+ end
189
+
190
+ protected
191
+
192
+ def attr_define(accessor_level, name, value)
193
+ self.class.send(:"attr_#{accessor_level}", :"#{name}")
194
+ instance_variable_set(:"@#{name}", value) if !value.nil?
195
+ end
196
+
197
+ end
198
+ end
data/lib/runner.rb ADDED
@@ -0,0 +1,14 @@
1
+ module ShatteredSupport
2
+ class Configuration
3
+ def self.environment
4
+ begin
5
+ return @@environment
6
+ rescue NameError
7
+ return {}
8
+ end
9
+ end
10
+ def self.environment=(environment)
11
+ @@environment=environment
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+
4
+ class Object
5
+ def method_defined?(method)
6
+ return self.class.method_defined?(method)
7
+ end
8
+ end
9
+
10
+ require File.dirname(__FILE__)+"/base"
11
+ require File.dirname(__FILE__)+"/actor"
12
+ require File.dirname(__FILE__)+"/runner"
metadata ADDED
@@ -0,0 +1,40 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.10
3
+ specification_version: 1
4
+ name: shattered_support
5
+ version: !ruby/object:Gem::Version
6
+ version: "0.3"
7
+ date: 2006-04-23
8
+ summary: "Shattered Support: Allows a common derivation point for shattered MVC."
9
+ require_paths:
10
+ - lib
11
+ email:
12
+ homepage: http://www.hastilymade.com
13
+ rubyforge_project:
14
+ description: Shattered Support is the base for Shattered MVC.
15
+ autorequire: shattered_support
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/actor.rb
30
+ - lib/base.rb
31
+ - lib/runner.rb
32
+ - lib/shattered_support.rb
33
+ test_files: []
34
+ rdoc_options: []
35
+ extra_rdoc_files: []
36
+ executables: []
37
+ extensions: []
38
+ requirements:
39
+ - Shattered Support is a base for the rest of shattered.
40
+ dependencies: []