Sha256: 09187e32eecd35be4a1f243fc5a33636620fd482ee727f48d2a651816c2ca093

Contents?: true

Size: 1.5 KB

Versions: 9

Compression:

Stored size: 1.5 KB

Contents

class Collision
  
  # Default collision functions
  #
  None   = nil       # do not collide
  Simple = lambda {} # just collide
  # Kill   = lambda { kill! }
  # Damage = lambda { damage! }
  
  #
  #
  attr_reader :things, :this, :that, :definition
  
  # Things can collide.
  #
  def initialize things, this, that = this, &definition
    @things     = things
    @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, _|
      things.each do |thing|
        if thing.shape == this_shape
          thing.instance_eval &definition
          break
        end
      end
    end
  end
  
  #
  #
  def complex_package definition
    lambda do |this_shape, that_shape|
      this_that = Array.new 2
      things.each do |thing|
        if thing.shape == this_shape
          this_that[0] = thing
          break if this_that.all?
        end
        if thing.shape == that_shape
          this_that[1] = thing
          break if this_that.all?
        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

9 entries across 9 versions & 1 rubygems

Version Path
gosu_extensions-0.3.8 lib/core/collision.rb
gosu_extensions-0.3.7 lib/core/collision.rb
gosu_extensions-0.3.6 lib/core/collision.rb
gosu_extensions-0.3.5 lib/core/collision.rb
gosu_extensions-0.3.4 lib/core/collision.rb
gosu_extensions-0.3.3 lib/core/collision.rb
gosu_extensions-0.3.2 lib/core/collision.rb
gosu_extensions-0.3.1 lib/core/collision.rb
gosu_extensions-0.3.0 lib/core/collision.rb