const PIXI = 'undefined' !== typeof window ? require('pixi.js') : undefined; import {Renderable} from './renderable'; export class Container extends Renderable { constructor() { super(); this._children = []; this._isDirty = false; this.internal = PIXI ? new PIXI.Container() : undefined; } addChild(child) { child.parent = this; this.isDirty = true; this._children.push(child); this.internal.addChild(child.internal); } get children() { return this._children; } destroy() { this.children.forEach((child) => { child.destroy(); }); super.destroy(); } get isDirty() { return this._isDirty; } set isDirty(isDirty) { if (this.parent) { this.parent.isDirty = isDirty; } this._isDirty = isDirty; } removeChild(child) { const index = this._children.indexOf(child); if (-1 === index) { return; } this._children.splice(index, 1); this.internal.removeChild(child.internal); } removeAllChildren() { for (const child of this._children) { this.removeChild(child); } } tick() { if (!this.isDirty) { return; } let needsSort = false; for (const child of this._children) { if (0 !== child.zIndex) { needsSort = true; } if (child instanceof Container) { child.tick(); } } if (!needsSort) { return; } this._children.sort((l, r) => { return l.zIndex - r.zIndex; }); this.internal.children = this._children.map((child) => { return child.internal; }); } }