UNPKG

blaze-2d

Version:

A fast and simple WebGL 2 2D game engine written in TypeScript

142 lines (141 loc) 4.03 kB
import { vec2 } from "gl-matrix"; /** * A generic interface to represent any 3D object's neighbours in a grid. */ export interface Neighbours<T> { [index: string]: T; left?: T; right?: T; bottom?: T; top?: T; topLeft?: T; topRight?: T; bottomLeft?: T; bottomRight?: T; } export declare type Listener = (e?: any, obj?: Object2D) => void; /** * Represents an object in 2D space with a position and rotation. * * The position should be in world space and represent the centre of the object, if the object is given a width and/or height. * * Also contains an events system which can be used to execute arbitrary functions on certain events. */ export default class Object2D { private position; private rotation; protected listeners: { [index: string]: Listener[]; }; constructor(); /** * Sets the object's position. * * @param pos The object's new position */ setPosition(pos: vec2): void; private setVec; /** * Sets the x component of the object's position. * * @param x The object's new x coordinate */ setPositionX(x: number): void; /** * Sets the y component of the object's position. * * @param y The object's new y coordinate */ setPositionY(y: number): void; /** * Gets the object's position. * * @returns The object's position as a vec2 */ getPosition(): vec2; /** * Translates the object by the provided vector. * * @param v The vector to translate by */ translate(v: vec2): void; /** * Moves the object's position right (x+) relative to it's rotation. * * @param dist The distance to move right, can be + or - */ moveRight(dist: number): void; /** * Moves the object's position up (y+) relative to it's rotation. * * @param dist The distance to move up, can be + or - */ moveUp(dist: number): void; /** * Sets the object's rotation. * * @param angle The object's new rotation */ setRotation(angle: number): void; /** * Gets the object's rotation as a number in radians relative to y+. * * @returns The object's rotation */ getRotation(): number; /** * Increments the object's rotation by an angle in radians. * * @param angle The angle to rotate by in radians */ rotate(angle: number): void; /** * Sets all the events that can be used in `listeners`. * * Listeners can be added for an event using: * * ```js * this.listeners.eventName = []; * ``` * * @example <caption>Setup listeners for "position" and "rotate" events.</caption> * this.listeners.position = []; * this.listeners.rotate = []; */ protected setupEvents(): void; /** * Gets the events that can be listened to on the object. * * @returns The events the object supports */ getEvents(): string[]; /** * Executes all listeners attached to a given event. * * @param event The event to fire * @param e The data to pass to each event listener */ fireEvent(event: string, e: any): void; /** * Gets the listeners attached to an event. * * @returns An array of listeners attached to the given event */ getEventListeners(event: string): Listener[]; /** * Executes a function when an event is fired. * * @param event The event to listen for * @param listener The function to execute when the event fired * @returns Wether or not the listener was added */ addEventListener(event: string, listener: Listener): boolean; /** * Removes a listener from an event. * * @param event The event the listener is attached to * @param listener The listener to remove * @returns Wether or not the listener was removed */ removeEventListener(event: string, listener: Listener): boolean; }