UNPKG

gibbon.js

Version:

Actor/Component system for use with pixi.js.

112 lines 3.34 kB
import { Component } from "../core/component"; import { quickSplice } from "../utils/array-utils"; import { Point } from 'pixi.js'; import { EngineEvent } from "../events/engine-events"; export class Transform extends Component { _position = new Point(); _rotation = 0; name; get position() { return this._position; } set position(v) { this._position.set(v.x, v.y); } get x() { return this._position.x; } set x(x) { this._position.x = x; } get y() { return this._position.y; } set y(y) { this._position.y = y; } get rotation() { return this._rotation; } set rotation(v) { while (v > Math.PI) v -= 2 * Math.PI; while (v < -Math.PI) v += 2 * Math.PI; this._rotation = v; } get size() { return this._size; } get width() { return this._size.x; } set width(v) { this._size.x = v; } get height() { return this._size.y; } set height(v) { this._size.y = v; } _size = { x: 0, y: 0 }; _parent; get parent() { return this._parent; } _children = []; get children() { return this._children.values(); } [Symbol.iterator]() { return this._children.values(); } constructor(pos) { super(); if (pos) { this._position.set(pos.x, pos.y); } } init() { } /** * Find all children with component type. * @param {*} cls * @param results - Optional array to place results in. */ findInChildren(cls, results = []) { for (let i = this._children.length - 1; i >= 0; i--) { const comp = this._children[i].get(cls); if (comp) { results.push(comp); } } return results; } /** * Find components recursively in all children. * This is an expensive operation. * @param cls */ findRecursive(cls, results = []) { for (let i = this._children.length - 1; i >= 0; i--) { const child = this._children[i]; const comp = child.get(cls); if (comp) { results.push(comp); } child.findRecursive(cls, results); } return results; } /** * * @param {number} x * @param {number} y */ translate(x, y) { this._position.x += x; this._position.y += y; } addChild(t) { if (t === this) { console.warn(`Attempt to set self as parent failed.`); } else if (t._parent != this) { const oldParent = t._parent; t._parent = this; oldParent?.removeChild(t); this._children.push(t); this.actor.emit(EngineEvent.ChildAdded, t); } } /** * * @param t - Child transform to remove. * The child will be added to the root actor's children. * A transform cannot be removed from root. * To do this, add it to another transform's children instead. * @returns */ removeChild(t) { const ind = this._children.indexOf(t); if (ind >= 0) { quickSplice(this._children, ind); } this.actor?.emit(EngineEvent.ChildRemoved, t); if (t._parent == this) { t._parent = undefined; } } } //# sourceMappingURL=transform.js.map