From 464a82775a0059ad913a6b0ca2dc3abaa66d59d2 Mon Sep 17 00:00:00 2001 From: Florian Hanke Date: Tue, 11 May 2010 22:47:30 +0200 Subject: [PATCH] + init hooks, default rotation --- gem/lib/core/initializer_hooks.rb | 22 +++++++++++++++++----- gem/lib/units/sprite.rb | 15 +++++++-------- gem/lib/units/thing.rb | 3 +++ 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/gem/lib/core/initializer_hooks.rb b/gem/lib/core/initializer_hooks.rb index c0e8806..41d32ad 100644 --- a/gem/lib/core/initializer_hooks.rb +++ b/gem/lib/core/initializer_hooks.rb @@ -8,15 +8,27 @@ module InitializerHooks self.hooks = {} # Runs the hooks for this instance for - # each class and superclass up to Sprite. + # each class and superclass from Sprite on down. # def after_initialize - self.class.ancestors.each do |klass| - run_hooks_for klass - break if klass == Sprite - end + relevant_ancestors.each { |klass| run_hooks_for klass } + end + + # Relevant ancestors for self. + # + def relevant_ancestors + ancestors = self.class.ancestors + sprite? ? ancestors[0..ancestors.index(Sprite)] : ancestors[0..0] end + # Is this class a sprite? + # + def sprite? + is_a? Sprite + end + + # Get and run the hooks for the class. + # def run_hooks_for klass hooks = InitializerHooks.hooks[klass] hooks && hooks.each do |hook| diff --git a/gem/lib/units/sprite.rb b/gem/lib/units/sprite.rb index 943de49..ce3e323 100644 --- a/gem/lib/units/sprite.rb +++ b/gem/lib/units/sprite.rb @@ -42,7 +42,12 @@ def layer # Default rotation is upwards. # def rotation - @rotation || -Rotation::Half + @rotation || -Rotation::Quarter + end + # + # + def rotation= rotation + @rotation = rotation % (2*Math::PI) end class << self @@ -60,7 +65,7 @@ def layer layer def rotation amount = nil, &block block ||= lambda { amount } InitializerHooks.append self do - self.rotation = block[] + self.rotation = block.call end end def random_rotation @@ -154,12 +159,6 @@ def move # def moved; end - # - # - def rotation= rotation - @rotation = rotation % (2*Math::PI) - end - # Movement rules # # Note: Use these in method move. diff --git a/gem/lib/units/thing.rb b/gem/lib/units/thing.rb index d5b526a..8629d47 100644 --- a/gem/lib/units/thing.rb +++ b/gem/lib/units/thing.rb @@ -20,6 +20,7 @@ def layer end class << self + @@form_shape_class_mapping = { :circle => CP::Shape::Circle, # :circle, radius :poly => CP::Shape::Poly, # :poly, CP::Vec2.new(-22, -18), CP::Vec2.new(-22, -10), etc. @@ -38,6 +39,8 @@ def shape form, *args params << CP::Vec2.new(0.0, 0.0) unless CP::Vec2 === args.first @shape = shape_class.new *params + @shape.body.a = -Rotation::Quarter # default rotation + @shape end end def mass amount