]>
git.r.bdr.sh - rbdr/sumo/blob - lib/systems/render_points.js
1 import { System
} from '@serpentity/serpentity';
2 import { Graphics
} from 'pixi.js';
4 import Config
from '../config';
5 import PointsNode
from '../nodes/points';
8 kNoPixiError: 'No pixi application passed to render system. Make sure you set the `application` key in the config object when initializing.',
9 redBar: new Graphics(),
10 blueBar: new Graphics()
14 * Renders points assuming there exist a "red" and a "blue" score
16 * @extends {external:Serpentity.System}
17 * @class RenderPointsSystem
18 * @param {object} config a configuration object to extend.
20 export default class RenderPointsSystem
extends System
{
22 constructor(config
= {}) {
27 * The node collection of points keepers
29 * @property {external:Serpentity.NodeCollection} pointsKeepers
31 * @memberof RenderPointsSystem
33 this.pointsKeepers
= null;
36 * The pixi engine we will use to render
38 * @property {external:PixiJs.Application} application
40 * @memberof RenderPointsSystem
42 this.application
= config
.application
;
44 if (!this.application
) {
45 throw new Error(internals
.kNoPixiError
);
50 * Initializes system when added. Requests renderable nodes and
51 * attaches to event listeners to add / remove them to pixi stage
54 * @memberof RenderPointsSystem
56 * @param {external:Serpentity.Engine} engine the serpentity engine to
57 * which we are getting added
61 this.pointsKeepers
= engine
.getNodes(PointsNode
);
62 this.application
.stage
.addChild(internals
.blueBar
);
63 this.application
.stage
.addChild(internals
.redBar
);
67 * Clears system resources when removed.
71 * @memberof RenderPointsSystem
75 this.pointsKeepers
= null;
76 this.application
.stage
.removeChild(internals
.blueBar
);
77 this.application
.stage
.removeChild(internals
.redBar
);
81 * Runs on every update of the loop. Updates the bars
85 * @param {Number} currentFrameDuration the duration of the current
87 * @memberof RenderPointsSystem
89 update(currentFrameDuration
) {
91 for (const pointsKeeper
of this.pointsKeepers
) {
93 const redPoints
= pointsKeeper
.points
.red
;
94 const bluePoints
= pointsKeeper
.points
.blue
;
96 const redBarHeight
= redPoints
* Config
.verticalResolution
/ Config
.maxPoints
;
97 const blueBarHeight
= bluePoints
* Config
.verticalResolution
/ Config
.maxPoints
;
99 internals
.redBar
.removeChildren();
100 const redBar
= new Graphics();
101 redBar
.lineStyle(20, 0xeaacac, 0.75)
102 .moveTo(Config
.horizontalResolution
, 0)
103 .lineTo(Config
.horizontalResolution
, redBarHeight
);
104 internals
.redBar
.addChild(redBar
);
106 internals
.blueBar
.removeChildren();
107 const blueBar
= new Graphics();
108 blueBar
.lineStyle(20, 0x87c5ea, 0.75)
110 .lineTo(0, blueBarHeight
);
111 internals
.blueBar
.addChild(blueBar
);