Sha256: 0afbef2b1af2c905c40395791a61993977b444a2d7e8a3c717abb195640de6f0

Contents?: true

Size: 1.82 KB

Versions: 9

Compression:

Stored size: 1.82 KB

Contents

# A thing that is a pod can have attachments.
#
#
#
#
module Pod
  
  manual <<-MANUAL
    Defines:
      attach <class>, x_pos, y_pos
    
    Example:
      class Battleship
        attach Cannon, 10, 20
  MANUAL
  
  def self.included target_class
    target_class.extend IncludeMethods
    target_class.holds_attachments
  end
  
  # TODO is_a Rack
  #
  # TODO Maybe
  #      is_a Rack.
  #           with(Cannon, 10, 10).
  #           with(Cannon, 20, 10)
  #
  module IncludeMethods
    
    def holds_attachments
      include InstanceMethods
      alias_method_chain :move, :attachments
      
      class_inheritable_accessor :prototype_attachments
      extend ClassMethods
      hook = lambda do
        self.class.prototype_attachments.each do |type, x, y|
          attach type.new(self.window), x, y
        end
      end
      InitializerHooks.append self, &hook
    end
    
  end
  
  module ClassMethods
    
    # Example:
    # class MotherShip
    #   is_a Pod
    #   attach Cannon, 10, 10
    #   attach Cannon, 20, 10
    #   attach Cannon, 30, 10
    #
    def attach type, x, y
      self.prototype_attachments ||= []
      self.prototype_attachments << [type, x, y]
    end
    
  end
  
  module InstanceMethods
    
    attr_accessor :attachments
    
    #
    #
    def attach attachment, x, y
      self.attachments ||= []
      attachment.extend Attachable # This is where Ruby shines.
      window.register attachment
      # attachment.rotation = self.rotation
      attachment.relative_position = CP::Vec2.new x, y
      self.attachments << attachment
    end
    
    #
    #
    def move_attachments
      self.attachments.each do |attachment|
        attachment.move_relative self
      end
    end
    
    #
    #
    def move_with_attachments
      move_attachments
      move_without_attachments
    end
    
  end
  
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
gosu_extensions-0.1.8 lib/traits/pod.rb
gosu_extensions-0.1.7 lib/traits/pod.rb
gosu_extensions-0.1.6 lib/traits/pod.rb
gosu_extensions-0.1.5 lib/traits/pod.rb
gosu_extensions-0.1.4 lib/traits/pod.rb
gosu_extensions-0.1.3 lib/traits/pod.rb
gosu_extensions-0.1.2 lib/traits/pod.rb
gosu_extensions-0.1.1 lib/traits/pod.rb
gosu_extensions-0.1.0 lib/traits/pod.rb