Skip to main content

Class: Object2D

object2d.Object2D

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.

Hierarchy

Constructors

constructor

โ€ข new Object2D()

Defined in

src/object2d.ts:34

Properties

listeners

โ€ข Protected listeners: Object = {}

Index signature

โ–ช [index: string]: Listener[]

Defined in

src/object2d.ts:32


position

โ€ข Private position: vec2

Defined in

src/object2d.ts:29


rotation

โ€ข Private rotation: number = 0

Defined in

src/object2d.ts:30


setVec

โ€ข Private setVec: vec2

Defined in

src/object2d.ts:49

Methods

addEventListener

โ–ธ addEventListener(event, listener): boolean

Executes a function when an event is fired.

Parameters

NameTypeDescription
eventstringThe event to listen for
listenerListenerThe function to execute when the event fired

Returns

boolean

Wether or not the listener was added

Defined in

src/object2d.ts:209


fireEvent

โ–ธ fireEvent(event, ...e): void

Executes all listeners attached to a given event.

Parameters

NameTypeDescription
eventstringThe event to fire
...eanyThe data to pass to each event listener

Returns

void

Defined in

src/object2d.ts:185


getEventListeners

โ–ธ getEventListeners(event): Listener[]

Gets the listeners attached to an event.

Parameters

NameType
eventstring

Returns

Listener[]

An array of listeners attached to the given event

Defined in

src/object2d.ts:198


getEvents

โ–ธ getEvents(): string[]

Gets the events that can be listened to on the object.

Returns

string[]

The events the object supports

Defined in

src/object2d.ts:175


getPosition

โ–ธ getPosition(): vec2

Gets the object's position.

Returns

vec2

The object's position as a vec2

Defined in

src/object2d.ts:82


getRotation

โ–ธ getRotation(): number

Gets the object's rotation as a number in radians relative to y+.

Returns

number

The object's rotation

Defined in

src/object2d.ts:139


moveRight

โ–ธ moveRight(dist): void

Moves the object's position right (x+) relative to it's rotation.

Parameters

NameTypeDescription
distnumberThe distance to move right, can be + or -

Returns

void

Defined in

src/object2d.ts:100


moveUp

โ–ธ moveUp(dist): void

Moves the object's position up (y+) relative to it's rotation.

Parameters

NameTypeDescription
distnumberThe distance to move up, can be + or -

Returns

void

Defined in

src/object2d.ts:114


removeEventListener

โ–ธ removeEventListener(event, listener): boolean

Removes a listener from an event.

Parameters

NameTypeDescription
eventstringThe event the listener is attached to
listenerListenerThe listener to remove

Returns

boolean

Wether or not the listener was removed

Defined in

src/object2d.ts:223


rotate

โ–ธ rotate(angle): void

Increments the object's rotation by an angle in radians.

Parameters

NameTypeDescription
anglenumberThe angle to rotate by in radians

Returns

void

Defined in

src/object2d.ts:148


setPosition

โ–ธ setPosition(pos): void

Sets the object's position.

Parameters

NameTypeDescription
posvec2The object's new position

Returns

void

Defined in

src/object2d.ts:43


setPositionX

โ–ธ setPositionX(x): void

Sets the x component of the object's position.

Parameters

NameTypeDescription
xnumberThe object's new x coordinate

Returns

void

Defined in

src/object2d.ts:56


setPositionY

โ–ธ setPositionY(y): void

Sets the y component of the object's position.

Parameters

NameTypeDescription
ynumberThe object's new y coordinate

Returns

void

Defined in

src/object2d.ts:69


setRotation

โ–ธ setRotation(angle): void

Sets the object's rotation.

Parameters

NameTypeDescription
anglenumberThe object's new rotation

Returns

void

Defined in

src/object2d.ts:128


setupEvents

โ–ธ Protected setupEvents(): void

Sets all the events that can be used in listeners.

Listeners can be added for an event using:

   this.listeners.eventName = [];

example Setup listeners for "position" and "rotate" events. this.listeners.position = []; this.listeners.rotate = [];

Returns

void

Defined in

src/object2d.ts:165


translate

โ–ธ translate(v): void

Translates the object by the provided vector.

Parameters

NameTypeDescription
vvec2The vector to translate by

Returns

void

Defined in

src/object2d.ts:91