Sha256: 6836c282e3cf19b1315b4ad7ea8f1e95ab42661b695d9a8a82552de944442b3f

Contents?: true

Size: 1.52 KB

Versions: 3

Compression:

Stored size: 1.52 KB

Contents

class Collision
  
  # Default collision functions
  #
  None   = nil       # do not collide
  Simple = lambda {} # just collide
  # Kill   = lambda { kill! }
  # Damage = lambda { damage! }
  
  #
  #
  attr_reader :window, :this, :that, :definition
  
  #
  #
  def initialize window, this, that = this, &definition
    @window     = window # TODO Remove.
    @this       = this
    @that       = that
    @definition = definition && package(definition)
  end
  
  # TODO Extend the definition to incorporate this
  #      method. Or at least #complex, #simple.
  #
  def package definition
    if definition.arity == 2
      complex_package definition
    else
      simple_package definition
    end
  end
  
  #
  #
  def simple_package definition
    lambda do |this_shape, _|
      # TODO break if found.
      #
      window.moveables.each do |movable|
        if movable.shape == this_shape
          movable.instance_eval &definition
        end
      end
    end
  end
  
  #
  #
  def complex_package definition
    lambda do |this_shape, that_shape|
      this_that = Array.new(2)
      # TODO break if found
      #
      window.moveables.each do |movable|
        if movable.shape == this_shape
          this_that[0] = movable
        end
        if movable.shape == that_shape
          this_that[1] = movable
        end
      end
      definition.call *this_that
    end
  end
  
  # Install this collision on the given environment.
  #
  def install_on environment
    environment.add_collision_func this, that, &definition
  end
  
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gosu_extensions-0.2.5 lib/core/collision.rb
gosu_extensions-0.2.4 lib/core/collision.rb
gosu_extensions-0.2.3 lib/core/collision.rb