Foundation.CollisionUtility

Simplified overlap and collision checking functions.


Implementation Details

Reference paths

  • '/foundation/_digest.ts'
  • '/foundation/core/utilities/_digest.ts'
  • '/foundation/core/utilities/CollisionUtility.ts'

Extends

This type does not extend a base type.

Implements

This type does not implement any interfaces.


// Create a new type which extends the sprite type (minimal)
class MySpriteType extends Foundation.Sprite {

  // Override update to check for collisions
  public update(): boolean {
    if (super.update()) {

      // Handle all collision on the same layer
      Foundation.CollisionUtility.handleCollisions(this);

      return true;
    } else {
      return false;
    }
  }

  // Override the collision handler
  public handleCollision(s: Foundation.Sprite): void {
    console.log("I am colliding!");
  }
}

Static Methods

handleCollisions(obj: Sprite, handleFirtOnly: boolean = false, layerList?: OrderedUpdateDictionary<Layer>): void

  • Check the passed sprite against all other sprites on their layer for collider overlaps. If an overlap is found, the passed object's handleCollision() method will be called and a reference to the object which is colliding will be passed in.
  • By default, all collisions on the layer will trigger a handle. To only handle the first encountered collision, pass true for handleFirstOnly.
  • By Default, only objects on the passed object's layer will be checked. To check objects on multiple layers, pass an OrderedUpdateDictionary of Layer objects and all will be checked.
  • Checks between sticky and non-sticky layers will not be performed.

handleOverlaps(obj: Tile|Sprite, handleFirtOnly: boolean = false, layerList?: OrderedUpdateDictionary<Layer>): void

  • Check the passed tile or sprite against all other objects on their layer for bounds overlaps. If an overlap is found, a reference to the passed object's handleOverlap() method will be called and object which is colliding will be passed in.
  • By default, all overlaps on the layer will trigger a handle. To only handle the first encountered overlap, pass true for handleFirstOnly.
  • By Default, only objects on the passed object's layer will be checked. To check objects on multiple layers, pass an OrderedUpdateDictionary of Layer objects and all will be checked.
  • Checks between sticky and non-sticky layers will not be performed.