aboutsummaryrefslogtreecommitdiff
path: root/lib/systems/render_winner.js
blob: 53f3602865cef0db744d4bd8227a6232fff3a7a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { System } from '@serpentity/serpentity';
import { Text } from 'pixi.js';

import Config from '../config';
import WinnerNode from '../nodes/winner';

const internals = {
  kNoPixiError: 'No pixi application passed to render system. Make sure you set the `application` key in the config object when initializing.'
};

/**
 * Renders a winner if there is one
 *
 * @extends {external:Serpentity.System}
 * @class RenderWinnerSystem
 * @param {object} config a configuration object to extend.
 */
export default class RenderWinnerSystem extends System {

  constructor(config = {}) {

    super();

    /**
     * The keepers of the winner
     *
     * @property {external:Serpentity.NodeCollection} pointsKeepers
     * @instance
     * @memberof RenderWinnerSystem
     */
    this.winnerKeepers = null;

    /**
     * The pixi engine we will use to render
     *
     * @property {external:PixiJs.Application} application
     * @instance
     * @memberof RenderWinnerSystem
     */
    this.application = config.application;

    if (!this.application) {
      throw new Error(internals.kNoPixiError);
    }
  }

  /**
   * Initializes system when added. Requests winner keepers
   *
   * @function added
   * @memberof RenderWinnerSystem
   * @instance
   * @param {external:Serpentity.Engine} engine the serpentity engine to
   * which we are getting added
   */
  added(engine) {

    this.winnerKeepers = engine.getNodes(WinnerNode);
  }

  /**
   * Clears system resources when removed.
   *
   * @function removed
   * @instance
   * @memberof RenderWinnerSystem
   */
  removed() {

    this.pointsKeepers = null;
    if (internals.winnerText) {
      this.application.stage.removeChild(internals.winnerText);
      internals.winnerText = null;
    }
  }

  /**
   * Runs on every update of the loop. Updates the bars
   *
   * @function update
   * @instance
   * @param {Number} currentFrameDuration the duration of the current
   * frame
   * @memberof RenderWinnerSystem
   */
  update(currentFrameDuration) {

    // Right now this is final, once a winner is rendered you would need
    // to restart the whole system.
    if (internals.winnerText) {
      return;
    }

    for (const winnerKeeper of this.winnerKeepers) {
      const winner = winnerKeeper.winner.winner;
      if (winner) {
        const message = `${winner} has won`;
        internals.winnerText = new Text(message, {
          fontFamily: 'Arial',
          fontSize: 96,
          fontWeight: 'bold',
          fill: 0xffffff,
          align: 'center'
        });
        internals.winnerText.position.x = Config.horizontalResolution / 2;
        internals.winnerText.position.y = Config.verticalResolution / 2;
        internals.winnerText.anchor.set(0.5);
        this.application.stage.addChild(internals.winnerText);
        return;
      }
    }
  }
};