Skip to content

Commit

Permalink
+ init hooks, default rotation
Browse files Browse the repository at this point in the history
  • Loading branch information
floere committed May 11, 2010
1 parent 2bd533d commit 464a827
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
22 changes: 17 additions & 5 deletions gem/lib/core/initializer_hooks.rb
Expand Up @@ -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|
Expand Down
15 changes: 7 additions & 8 deletions gem/lib/units/sprite.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions gem/lib/units/thing.rb
Expand Up @@ -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.
Expand All @@ -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
Expand Down

0 comments on commit 464a827

Please sign in to comment.