shattered_support 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/base.rb CHANGED
@@ -2,10 +2,11 @@
2
2
  $:.unshift(File.dirname(__FILE__)) unless
3
3
  $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
4
4
 
5
+ require 'active_support'
5
6
  require 'timer/timer'
6
7
  require 'vector'
7
8
 
8
- module ShatteredSupport
9
+ module ShatteredSupport #:nodoc:
9
10
 
10
11
  BEFORE_INIT_CALL_VALUES = :before_init_call_values
11
12
  BEFORE_INIT_SET_VALUES = :before_init_set_values
@@ -16,30 +17,60 @@ module ShatteredSupport
16
17
  base.extend(ClassMethods)
17
18
  end
18
19
  module ClassMethods
20
+ # In the pre_initialization phase (see #before_init_call), send an object a bunch of sets.
21
+ #
22
+ # This is used in actor, camera, mesh, and virtually everywhere to support the format of:
23
+ # mesh "ruby", :position => v(1,0,0)
24
+ #
25
+ # becomes
26
+ # before_init_set( :ruby, {:position => v(1,0,0) } )
27
+ # becomes
28
+ # ruby.position=v(1,0,0)
29
+ # when the obect is initialized.
19
30
  def before_init_set(variable, options={})
20
31
  self.write_inheritable_array(BEFORE_INIT_SET_VALUES, [[ variable, options ]] )
21
32
  end
22
33
 
34
+ # All shattered objects have a pre_initialization phase. Use this to specify that
35
+ # a function should be called when the object is created.
36
+ #
37
+ # _Example_:
38
+ # class BulletModel < ShatteredModel::Base
39
+ # before_init_call(:calculate_trajectory, v(0,0,0), v(0,0,1))
40
+ # def calculate_trajectory
41
+ # #called before initialize is
42
+ # end
43
+ # end
44
+ #
23
45
  def before_init_call(function, *arguments)
24
46
  self.write_inheritable_array(BEFORE_INIT_CALL_VALUES, [[function, [*arguments]]])
25
47
  end
26
48
  end
27
49
  class Base
28
- def self.new(*options)
50
+
51
+ # This is overwritten to allow for a pre_initialize before initialize
52
+ def self.new(*options) #:nodoc
29
53
  new_base = allocate
30
54
  new_base.pre_initialize
31
55
  new_base.send(:initialize, *options)
32
56
  return new_base
33
57
  end
34
- def state
58
+
59
+ # Retrieve the current state
60
+ def state
35
61
  Configuration.environment[:state]
36
62
  end
37
- def pre_initialize
63
+
64
+ # This function is called after an object is allocated, but before it's initialized.
65
+ #
66
+ # See ShatteredSupport::ClassMethods#before_init_call for usage.
67
+ def pre_initialize #:nodoc:
38
68
  pre_initialize_set
39
69
  pre_initialize_call
40
70
  end
41
71
 
42
- def each_init_value(name)
72
+ # Used in pre_initialize
73
+ def each_init_value(name) #:nodoc:
43
74
  startup_attributes = self.class.read_inheritable_attribute( name ) || []
44
75
  startup_attributes.each do |actor, options|
45
76
  next if options.nil?
@@ -47,24 +78,23 @@ module ShatteredSupport
47
78
  end
48
79
  end
49
80
 
50
- def pre_initialize_set
81
+ # Used in pre_initialize for before_init_set
82
+ def pre_initialize_set #:nodoc:
51
83
  each_init_value(BEFORE_INIT_SET_VALUES) do |variable, options|
52
84
  call_object_function_for_each_key( variable, options )
53
85
  end
54
- # self.class.write_inheritable_attribute(BEFORE_INIT_SET_VALUES, nil)
55
86
  end
56
87
 
57
- def pre_initialize_call
88
+ # Used in pre_initialize for before_init_call
89
+ def pre_initialize_call #:nodoc:
58
90
  each_init_value(BEFORE_INIT_CALL_VALUES) do |function, args|
59
91
  call_object_function( :self, function, args )
60
92
  end
61
- # self.class.write_inheritable_attribute(BEFORE_INIT_CALL_VALUES, nil)
62
93
  end
63
94
 
64
95
 
65
- # Overwrite this function to recieve an update event every frame, with the
66
- # seconds elapsed since last update.
67
- def update_event(time_elapsed)
96
+ # TODO - is this called anymore?
97
+ def update_event(time_elapsed) #:nodoc:
68
98
  actors.each do |actor|
69
99
  actor.update_actors(time_elapsed)
70
100
  actor.update(time_elapsed)
@@ -72,25 +102,25 @@ module ShatteredSupport
72
102
  end
73
103
 
74
104
  # remove_from_scene? should just be defined in model.
75
- # Refactor?
76
- def remove_from_scene?
105
+ # Refactor? TODO
106
+ def remove_from_scene? #:nodoc:
77
107
  return false
78
108
  end
79
109
 
80
- def update_actors(time_elapsed)
110
+ # TODO outdated?
111
+ def update_actors(time_elapsed) #:nodoc:
81
112
  update_event time_elapsed
82
113
  remove_dead_actors
83
114
  end
84
115
 
85
- def update(time_elapsed)
86
- end
87
-
88
- def remove_dead_actors
116
+ # TODO outdated?
117
+ def remove_dead_actors #:nodoc:
89
118
  actors.each do |actor|
90
119
  remove_from_scene actor if actor.remove_from_scene?
91
120
  end
92
121
  end
93
-
122
+
123
+ # TODO outdated?
94
124
  def remove_from_scene(actor)
95
125
  actors.delete actor
96
126
  actor.unload!
@@ -103,6 +133,7 @@ module ShatteredSupport
103
133
  call_object_function actor, "#{action}=", params
104
134
  end
105
135
  end
136
+
106
137
  def call_object_function(actor, action, params)
107
138
  begin
108
139
  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.
@@ -121,40 +152,62 @@ module ShatteredSupport
121
152
  raise bang
122
153
  end
123
154
  end
124
- def unload!
125
- end
126
155
 
127
156
  public
128
157
 
129
- # attr helpers. This defines instance level attr_* functions.
158
+ # attr helpers. These are instance level attr_* functions.
130
159
  def attr_reader(*args)
131
160
  name, value = args
132
161
  attr_define(:reader, name, value)
133
162
  end
134
-
163
+
164
+ # attr helpers. These are instance level attr_* functions.
165
+ # attr_writer accepts a value as the second argument
135
166
  def attr_writer(*args)
136
167
  name, value = args
137
168
  attr_define(:writer, name, value)
138
169
  end
139
170
 
171
+ # attr helpers. These are instance level attr_* functions.
172
+ # attr_accessor accepts a value as the second argument
140
173
  def attr_accessor(*args)
141
174
  name, value = args
142
175
  attr_define(:accessor, name, value)
143
176
  end
144
177
 
178
+ # Define a block to execute when an object is unloaded.
179
+ def when_unloaded(&block)
180
+ @unloaded_events ||= []
181
+ @unloaded_events << block
182
+ end
183
+
145
184
  protected
146
185
 
147
186
  def attr_define(accessor_level, name, value)
148
- self.class.send(:"attr_#{accessor_level}", :"#{name}")
149
- instance_variable_set(:"@#{name}", value) if !value.nil?
187
+ self.class.send("attr_#{accessor_level}".to_sym, "#{name}".to_sym)
188
+ instance_variable_set("@#{name}".to_sym, value) if !value.nil?
189
+ end
190
+
191
+ private
192
+
193
+ def disabled?
194
+ @unloaded_events == nil
195
+ end
196
+
197
+ def unload!
198
+ return if disabled?
199
+ @unloaded_events.each do |event|
200
+ event.call
201
+ end
202
+ @unloaded_events = nil
150
203
  end
151
204
 
152
205
  end
153
206
 
154
- class Error < StandardError
207
+ class Error < StandardError # :nodoc:
155
208
  end
156
209
 
157
- class RetossError < StandardError
210
+ class RetossError < StandardError # :nodoc:
158
211
  attr_accessor :message
159
212
  def initialize(error, message)
160
213
  self.message = message
data/lib/runner.rb CHANGED
@@ -1,11 +1,8 @@
1
1
  module ShatteredSupport
2
- class Configuration
2
+ class Configuration #:nodoc:
3
3
  def self.environment
4
- begin
5
- return @@environment
6
- rescue NameError
7
- return {}
8
- end
4
+ @@environment={} unless defined? @@environment
5
+ return @@environment
9
6
  end
10
7
  def self.environment=(environment)
11
8
  @@environment=environment
@@ -1,17 +1,19 @@
1
1
  require 'rubygems'
2
- require 'active_support'
3
2
 
4
3
  class Object
5
4
  def method_defined?(method)
6
5
  return self.class.method_defined?(method)
7
6
  end
8
7
 
8
+ # This is a shorthand to define vector object coordinates.
9
+ #
10
+ # Creates a vector object at coordinates x,y,z
9
11
  def v(x, y, z)
10
12
  Vector.new(x, y, z)
11
13
  end
12
14
  end
13
15
 
14
- class File
16
+ class File #:nodoc:
15
17
  # File.each_in_path will recursively look for all files, starting at the given path.
16
18
  # It will yield with each result.
17
19
  def self.each_in_path(path)
@@ -46,7 +48,7 @@ class File
46
48
  end
47
49
  end
48
50
 
49
- class Dir
51
+ class Dir #:nodoc:
50
52
  # Dir.each_in_path will recursively yield all non-hidden directories from the given path.
51
53
  def self.each_in_path(path, &block)
52
54
  return unless File.directory? path
@@ -1,5 +1,5 @@
1
1
  module ShatteredSupport
2
- module Timer
2
+ module Timer #:nodoc:all
3
3
 
4
4
  class Timer
5
5
  attr_reader :events_remaining
data/lib/timer/timer.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require File.dirname(__FILE__)+'/timed_event'
2
2
 
3
3
  module ShatteredSupport
4
- module Timer
4
+ module Timer #:nodoc:
5
5
  def self.append_features(base)
6
6
  super
7
7
  base.extend(ClassMethods)
data/lib/vector.rb CHANGED
@@ -1,5 +1,5 @@
1
1
 
2
- class Array
2
+ class Array #:nodoc:
3
3
  # Vector extensions for array. Allows for coersion of a 3 element Array into a vector.
4
4
 
5
5
  # Randomize the order of an array
@@ -26,26 +26,33 @@ class Array
26
26
  end
27
27
  end
28
28
 
29
- module ShatteredSupport
29
+ module ShatteredSupport #:nodoc:
30
+ # Vector is a three dimensional array, that allows for various operations on itself.
31
+ # To create a vector, use the shorthand:
32
+ # v(0,0,0) # Creates a zero vector
30
33
  class Vector
31
34
 
32
35
  attr_reader :x, :y, :z
33
36
 
34
- # Create a new Vector object. It requires exactly 3 arguments: x, y and z.
37
+ # Create a new Vector object. It requires exactly 3 arguments: x, y and z. Any value that
38
+ # can be converted to a float is acceptable.
39
+ #
40
+ # *NOTE:* The recomended way to create Vector objects is to use the <tt>v(1,2,3)</tt> shorthand.
35
41
  def initialize(x_val, y_val, z_val)
36
- @x, @y, @z = x_val.to_f, y_val.to_f, z_val.to_f
42
+ @x, @y, @z = [x_val, y_val, z_val].collect(&:to_f)
37
43
  end
38
44
 
39
- # Iterate through x, y and z with a black. The passed value is the value of the component.
45
+ # Iterate through x, y and z with a block. The passed value is the value of each component of
46
+ # the vector.
40
47
  def each(&block)
41
- self.to_a.each do |item|
42
- yield item
48
+ self.to_a.each do |component|
49
+ yield component
43
50
  end
44
51
  end
45
52
 
46
53
  # Returns this Vector as an Ogre vector object: Vector3. Should only be used internally by
47
54
  # Shattered to communicate with the Ogre engine.
48
- def to_v3
55
+ def to_v3 # :nodoc:
49
56
  ShatteredOgre::Vector3.new @x, @y, @z
50
57
  end
51
58
 
@@ -54,14 +61,14 @@ module ShatteredSupport
54
61
  self
55
62
  end
56
63
 
57
- # Returns an array with x, y and z dumped into its elements
58
- #
59
- # Vector.new(1, 2, 3).to_a #=> [1, 2, 3]
64
+ # Returns an array with x, y and z dumped into its elements.
65
+ # v(1, 2, 3).to_a #=> [1.0, 2.0, 3.0]
60
66
  def to_a
61
67
  [@x, @y, @z]
62
68
  end
63
69
 
64
70
  # Add 2 Vectors together.
71
+ # v(1,1,1) + v(1,2,3) #=> v(2,3,4)
65
72
  def +(*args)
66
73
  vector = convert_args_to_vector(args)
67
74
  Vector.new(
@@ -71,7 +78,8 @@ module ShatteredSupport
71
78
  )
72
79
  end
73
80
 
74
- # Subtract one Vector form another.
81
+ # Subtract one Vector from another.
82
+ # v(1,2,3) - v(1,1,1) #=> v(0,1,2)
75
83
  def -(args)
76
84
  vector = convert_args_to_vector(args)
77
85
  Vector.new(
@@ -82,6 +90,7 @@ module ShatteredSupport
82
90
  end
83
91
 
84
92
  # Multiply all components of a vector by a scalar amount.
93
+ # v(1,2,3) * 3 #=> v(3,6,9)
85
94
  def *(value)
86
95
  result = []
87
96
  each do |i|
@@ -91,6 +100,7 @@ module ShatteredSupport
91
100
  end
92
101
 
93
102
  # Divide all components of a vector by a scalar amount
103
+ # v(5,10,15) / 5 #=> v(1,2,3)
94
104
  def /(value)
95
105
  result = Array.new
96
106
  self.each do |i|
@@ -100,18 +110,25 @@ module ShatteredSupport
100
110
  end
101
111
 
102
112
  # Returns this Vector but normalized to a length of 1.
113
+ # v(9, 0, 0).normalize #=> v(1,0,0)
103
114
  def normalize
104
115
  self * (1 / length)
105
116
  end
106
117
  alias_method :normalise, :normalize
107
118
 
108
- # Same as #normalize but is destructive.
119
+ # Same as #normalize but modifies the receiver in place.
109
120
  def normalize!
110
121
  @x, @y, @z = normalize.to_a
111
122
  end
112
123
  alias_method :normalise!, :normalize!
113
124
 
114
- # Return the value specified by bracket notation.
125
+ # Return the value specified by bracket notation. Integers, Symbols or Strings
126
+ # are accepted as keys.
127
+ #
128
+ # vector = v(1,2,3)
129
+ # vector[:x] #=> 1
130
+ # vector['y'] #=> 2
131
+ # vector[2] #=> 3
115
132
  def [](index)
116
133
  case
117
134
  when index == 0 || index == :x || index == 'x'
@@ -123,7 +140,8 @@ module ShatteredSupport
123
140
  end
124
141
  end
125
142
 
126
- # Set the value specified by bracket notation.
143
+ # Set the value specified by bracket notation. Accepts the same keys as the #[]
144
+ # method.
127
145
  def []=(index, value)
128
146
  case
129
147
  when index == 0 || index == :x || index == 'x'
@@ -135,28 +153,44 @@ module ShatteredSupport
135
153
  end
136
154
  end
137
155
 
156
+ # Set the value of the X component.
138
157
  def x=(value)
139
158
  @x = value.to_f
140
159
  end
141
160
 
161
+ # Set the value of the Y component.
142
162
  def y=(value)
143
163
  @y = value.to_f
144
164
  end
145
165
 
166
+ # Set the value of the Z component.
146
167
  def z=(value)
147
168
  @z = value.to_f
148
169
  end
149
170
 
150
171
 
151
172
  # Returns the length of this vector.
173
+ # v(0,0,7).length #=> 7
152
174
  def length
153
175
  Math.sqrt(x**2 + y**2 + z**2)
154
176
  end
155
177
 
178
+ # Converts the vector into an easily identifiable form. Mostly used for debugging
179
+ # and console output.
180
+ # v(1,2,3).to_s #=> "#<Vector [1.0, 2.0, 3.0]>"
156
181
  def to_s
157
182
  "#<Vector [#@x, #@y, #@z]>"
158
183
  end
159
184
 
185
+ # Equality test. This method will return true if all components of both vectors are
186
+ # indentical.
187
+ def ==(vector)
188
+ vector.kind_of?(Vector) &&
189
+ x == vector.x &&
190
+ y == vector.y &&
191
+ z == vector.z
192
+ end
193
+
160
194
  private
161
195
 
162
196
  def convert_args_to_vector(*args)
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.10
3
3
  specification_version: 1
4
4
  name: shattered_support
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.3.1
7
- date: 2006-05-16
6
+ version: 0.3.2
7
+ date: 2006-06-04
8
8
  summary: "Shattered Support: Allows a common derivation point for shattered MVC."
9
9
  require_paths:
10
10
  - lib