Class SoundEmitterPolygon

Classdesc

SoundEmitterPolygon class, used for emitting sounds from a polygon. This treats the polygon as a sort of speaker

Example

 const soundEmitter = new ANVIL.SoundEmitterPolygon({
points: [[0,0],[100,0],[100,100],[0,100]],
backgroundColor: "red",
}, {
listener: playerObject,
source: "path/to/sound.mp3",
loop: true,
volume: 1,
maxDistance: 1000,
minDistance: 0,
fallOffFunction: (distance) => {
var falloffstart = this.maxDistance - this.minDistance;
var dist = distance - this.minDistance;
var vol = 1 - (dist / falloffstart);
if (vol < 0) vol = 0;
return vol;
}
}
});

Hierarchy (view full)

Constructors

Properties

_state: {
    [key: string]: any;
}

used for state() and returnState(), builds states that are returnable. Stacking two states is destructive.

Type declaration

  • [key: string]: any
blockedBy: GameObject[]

List of objects that block this object

blocks: GameObject[]

List of objects that this object blocks

body: any

reference to the physics body (matter.js). Empty if physics is not enabled

bounds: number[]

how the object is bounded in the scene (set with scene.setBoundaries())

boundsActive: boolean

are the bounds active on this object?

convex: boolean

true if the polygon is convex, false otherwise

coordinates: Vec2

coordinates of the polygon, or the top left most point of the polygon

gameObjectOptions: GameObjectOptions

Game object options of the game object (for serialization and recreation)

hitbox: Vec2

hitbox of the polygon, if the polygon is a square

id: string

unique ID for each object

isLocalPlayer: boolean

True if the object is the local player, false otherwise

listener: GameObject

The GameObject that "listens" to the sound. Sound volume will be determined by distance to this game object

meta: any

Meta data of the object

physicsEnabled: boolean

boolean if physics is enabled on the object

physicsOptions: PhysicsOptions

options for the physics engine

pinRef: "center" | "coordinates"

Reference point to pin the object to (only applies if the object is pinned)

pinned: null | GameObject

does nothing!

points: Vec2[]

points of the polygon

sound: Sound

The Sound instance that the SoundEmitterPolygon plays

square: boolean

true if the polygon is a square, false otherwise

type: string

type of the object, either "gameObject", "sprite", or "polygon"

Methods

  • Applies a force to the object (only works if physics enabled)

    Parameters

    • vector: Vec2

      The force vector to apply to the object

    Returns void

  • Checks for a collision with another object

    Parameters

    • object: GameObject

      The object to check for a collision with

    Returns boolean | Boolean

    Boolean, true of the object is colliding with the other object, false otherwise

  • Checks for a collision with a polygon

    Parameters

    • polygon: Vec2[]

      The polygon (lower case, not type Polygon) to check for a collision with

    Returns boolean | Boolean

    Boolean, true of the object is colliding with the other object, false otherwise

  • Draws the polygon onto the provided drawing context. This is handled automatically with scene and scene managers

    Parameters

    Returns void

  • Draws the object's label on top of the object The label is the objects meta label (eg: object.meta.label = "...")

    Parameters

    Returns void

  • Calculates the height of the polygon.

    Returns number

    The height of the polygon.

  • Calculates the width of the polygon.

    Returns number

    The width of the polygon.

  • Moves the polygon

    Parameters

    • vector: Vec2

      The vector to move the polygon by

    Returns Boolean

    Boolean, true if the move was successful, false if it was not (if it was out of bounds, it will not move)

  • Moves an object that has physics enabled by a vector (no forces pr boundaries involved)

    Parameters

    • vector: Vec2

      The vector to move the object by

    Returns true | Boolean

    Whether the object was moved or not (if it was out of bounds, it will not move)

  • Moves the polygon to a point as apposed to moving it by a vector

    Parameters

    • point: Vec2

      The point in space to move the polygon to

    Returns boolean

    True

  • Modifies pin to a game object

    Parameters

    • object: GameObject

      The game object to pin to

    • to: "center" | "coordinates"

      The reference point to pin to (either "center" or "coordinates")

    Returns void

  • Returns a list of points that make up the polygon, with an offset applied.

    Parameters

    • offset: Vec2

      The offset to apply to the polygon

    Returns Vec2[]

    The vertices of the polygon, with the offset applied

  • Returns to the old state (before state() was called). Used for clicking

    Returns void

  • Sets the object's bounds (where it can move)

    Parameters

    • bounds: number[]

      The bounds to set the object to

    Returns void

  • Sets the hitbox of the polygon. Useful if the polygon is concave and you need more reliable collision detection.

    Parameters

    • width: number

      The width of the hitbox

    • height: number

      The height of the hitbox

    Returns void

    Example

    const polygon = new Polygon({
    points: [[0, 0], [0, 100], [100, 100], [100, 0]],
    backgroundColor: "red"
    });
    polygon.setHitBox(polygon.getWidth(), polygon.getHeight());
  • Changes an attribute of the object (non destructive). To return to the original object, use returnState. Used for clicking

    Parameters

    • attr: string

      The attribute to change

    • value: any

      The new value of that attribute

    Returns void

  • Updates the sound based on the listener's position, then calls the update method of the Polygon

    Returns void

Generated using TypeDoc