]>
git.r.bdr.sh - rbdr/sumo/blob - lib/systems/render.js
8d3d4468ed8b3c4104ae89ccbab13a114ef8a83d
1 import { System
} from '@serpentity/serpentity';
3 import RenderableNode
from '../nodes/renderable';
6 kNoPixiError: 'No pixi application passed to render system. Make sure you set the `application` key in the config object when initializing.'
10 * Renders renderable objects to console
12 * @extends {external:Serpentity.System}
14 * @param {object} config a configuration object to extend.
16 export default class RenderSystem
extends System
{
18 constructor(config
= {}) {
23 * The node collection of renderable entities
25 * @property {external:Serpentity.NodeCollection} renderables
27 * @memberof RenderSystem
29 this.renderables
= null;
32 * The pixi engine we will use to render
34 * @property {external:PixiJs.Application} renderables
36 * @memberof RenderSystem
38 this._application
= config
.application
;
40 if (!this._application
) {
41 throw new Error(internals
.kNoPixiError
);
46 * Initializes system when added. Requests renderable nodes and
47 * attaches to event listeners to add / remove them to pixi stage
50 * @memberof RenderSystem
52 * @param {external:Serpentity.Engine} engine the serpentity engine to
53 * which we are getting added
57 this.renderables
= engine
.getNodes(RenderableNode
);
58 this.renderables
.on('nodeAdded', (event
) => {
60 this._application
.stage
.addChild(event
.node
.container
.container
);
62 this.renderables
.on('nodeRemoved', (event
) => {
64 this._application
.stage
.removeChild(event
.node
.container
.container
);
69 * Clears system resources when removed.
73 * @memberof RenderSystem
77 this.renderables
.removeAllListeners('nodeAdded');
78 this.renderables
.removeAllListeners('nodeRemoved');
79 this.renderables
= null;
83 * Runs on every update of the loop. Prints the location of every
88 * @param {Number} currentFrameDuration the duration of the current
90 * @memberof RenderSystem
92 update(currentFrameDuration
) {
94 for (const renderable
of this.renderables
) {
95 renderable
.container
.container
.position
.x
= renderable
.position
.x
;
96 renderable
.container
.container
.position
.y
= renderable
.position
.y
;