]> git.r.bdr.sh - rbdr/sumo/blame_incremental - lib/systems/render_points.js
Update dependencies
[rbdr/sumo] / lib / systems / render_points.js
... / ...
CommitLineData
1import { System } from '@serpentity/serpentity';
2import { Graphics } from 'pixi.js';
3
4import Config from '../config';
5import PointsNode from '../nodes/points';
6
7const internals = {
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()
11};
12
13/**
14 * Renders points assuming there exist a "red" and a "blue" score
15 *
16 * @extends {external:Serpentity.System}
17 * @class RenderPointsSystem
18 * @param {object} config a configuration object to extend.
19 */
20export default class RenderPointsSystem extends System {
21
22 constructor(config = {}) {
23
24 super();
25
26 /**
27 * The node collection of points keepers
28 *
29 * @property {external:Serpentity.NodeCollection} pointsKeepers
30 * @instance
31 * @memberof RenderPointsSystem
32 */
33 this.pointsKeepers = null;
34
35 /**
36 * The pixi engine we will use to render
37 *
38 * @property {external:PixiJs.Application} application
39 * @instance
40 * @memberof RenderPointsSystem
41 */
42 this.application = config.application;
43
44 if (!this.application) {
45 throw new Error(internals.kNoPixiError);
46 }
47 }
48
49 /**
50 * Initializes system when added. Requests renderable nodes and
51 * attaches to event listeners to add / remove them to pixi stage
52 *
53 * @function added
54 * @memberof RenderPointsSystem
55 * @instance
56 * @param {external:Serpentity.Engine} engine the serpentity engine to
57 * which we are getting added
58 */
59 added(engine) {
60
61 this.pointsKeepers = engine.getNodes(PointsNode);
62 this.application.stage.addChild(internals.blueBar);
63 this.application.stage.addChild(internals.redBar);
64 }
65
66 /**
67 * Clears system resources when removed.
68 *
69 * @function removed
70 * @instance
71 * @memberof RenderPointsSystem
72 */
73 removed() {
74
75 this.pointsKeepers = null;
76 this.application.stage.removeChild(internals.blueBar);
77 this.application.stage.removeChild(internals.redBar);
78 }
79
80 /**
81 * Runs on every update of the loop. Updates the bars
82 *
83 * @function update
84 * @instance
85 * @param {Number} currentFrameDuration the duration of the current
86 * frame
87 * @memberof RenderPointsSystem
88 */
89 update(currentFrameDuration) {
90
91 for (const pointsKeeper of this.pointsKeepers) {
92
93 const redPoints = pointsKeeper.points.red;
94 const bluePoints = pointsKeeper.points.blue;
95
96 const redBarHeight = redPoints * Config.verticalResolution / Config.maxPoints;
97 const blueBarHeight = bluePoints * Config.verticalResolution / Config.maxPoints;
98
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);
105
106 internals.blueBar.removeChildren();
107 const blueBar = new Graphics();
108 blueBar.lineStyle(20, 0x87c5ea, 0.75)
109 .moveTo(0, 0)
110 .lineTo(0, blueBarHeight);
111 internals.blueBar.addChild(blueBar);
112 }
113 }
114}