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