]> git.r.bdr.sh - rbdr/sumo/blame_incremental - lib/systems/render_winner.js
Merge branch 'feature/rbdr-gitlab-pipeline' into 'develop'
[rbdr/sumo] / lib / systems / render_winner.js
... / ...
CommitLineData
1import { System } from '@serpentity/serpentity';
2import { Text } from 'pixi.js';
3
4import Config from '../config';
5import WinnerNode from '../nodes/winner';
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};
10
11/**
12 * Renders a winner if there is one
13 *
14 * @extends {external:Serpentity.System}
15 * @class RenderWinnerSystem
16 * @param {object} config a configuration object to extend.
17 */
18export default class RenderWinnerSystem extends System {
19
20 constructor(config = {}) {
21
22 super();
23
24 /**
25 * The keepers of the winner
26 *
27 * @property {external:Serpentity.NodeCollection} pointsKeepers
28 * @instance
29 * @memberof RenderWinnerSystem
30 */
31 this.winnerKeepers = null;
32
33 /**
34 * The pixi engine we will use to render
35 *
36 * @property {external:PixiJs.Application} application
37 * @instance
38 * @memberof RenderWinnerSystem
39 */
40 this.application = config.application;
41
42 if (!this.application) {
43 throw new Error(internals.kNoPixiError);
44 }
45 }
46
47 /**
48 * Initializes system when added. Requests winner keepers
49 *
50 * @function added
51 * @memberof RenderWinnerSystem
52 * @instance
53 * @param {external:Serpentity.Engine} engine the serpentity engine to
54 * which we are getting added
55 */
56 added(engine) {
57
58 this.winnerKeepers = engine.getNodes(WinnerNode);
59 }
60
61 /**
62 * Clears system resources when removed.
63 *
64 * @function removed
65 * @instance
66 * @memberof RenderWinnerSystem
67 */
68 removed() {
69
70 this.pointsKeepers = null;
71 if (internals.winnerText) {
72 this.application.stage.removeChild(internals.winnerText);
73 internals.winnerText = null;
74 }
75 }
76
77 /**
78 * Runs on every update of the loop. Updates the bars
79 *
80 * @function update
81 * @instance
82 * @param {Number} currentFrameDuration the duration of the current
83 * frame
84 * @memberof RenderWinnerSystem
85 */
86 update(currentFrameDuration) {
87
88 // Right now this is final, once a winner is rendered you would need
89 // to restart the whole system.
90 if (internals.winnerText) {
91 return;
92 }
93
94 for (const winnerKeeper of this.winnerKeepers) {
95 const winner = winnerKeeper.winner.winner;
96 if (winner) {
97 const message = `${winner} has won`;
98 internals.winnerText = new Text(message, {
99 fontFamily: 'Arial',
100 fontSize: 96,
101 fontWeight: 'bold',
102 fill: 0xffffff,
103 align: 'center'
104 });
105 internals.winnerText.position.x = Config.horizontalResolution / 2;
106 internals.winnerText.position.y = Config.verticalResolution / 2;
107 internals.winnerText.anchor.set(0.5);
108 this.application.stage.addChild(internals.winnerText);
109 return;
110 }
111 }
112 }
113};
114