]>
git.r.bdr.sh - rbdr/sumo/blob - lib/systems/detect_winner.js
610effb2a5daa474403b0ba8783ad07a759f1047
1 import { System
} from '@serpentity/serpentity';
3 import WinnerNode
from '../nodes/winner';
4 import PointsNode
from '../nodes/points';
6 import Config
from '../config';
9 * Handles finding the winner
11 * @extends {external:Serpentity.System}
12 * @class DetectWinnerSystem
13 * @param {object} config a configuration object to extend.
15 export default class DetectWinnerSystem
extends System
{
17 constructor(config
= {}) {
22 * The collection of points keepers
24 * @property {external:Serpentity.NodeCollection} pointsKeepers
26 * @memberof DetectWinnerSystem
28 this.pointsKeepers
= null;
31 * The collection of winner keepers
33 * @property {external:Serpentity.NodeCollection} winnerKeepers
35 * @memberof DetectWinnerSystem
37 this.winnerKeepers
= null;
41 * Initializes system when added. Requests points and winner keepers
44 * @memberof DetectWinnerSystem
46 * @param {external:Serpentity.Engine} engine the serpentity engine to
47 * which we are getting added
51 this.pointsKeepers
= engine
.getNodes(PointsNode
);
52 this.winnerKeepers
= engine
.getNodes(WinnerNode
);
56 * Clears system resources when removed.
60 * @memberof DetectWinnerSystem
64 this.pointsKeepers
= null;
65 this.winnerKeepers
= null;
69 * Runs on every update of the loop. Checks if someone won
73 * @param {Number} currentFrameDuration the duration of the current
75 * @memberof DetectWinnerSystem
77 update(currentFrameDuration
) {
81 for (const pointsKeeper
of this.pointsKeepers
) {
82 for (const pointsCandidate
of Object
.keys(pointsKeeper
.points
)) {
83 if (pointsKeeper
.points
[pointsCandidate
] >= Config
.maxPoints
) {
84 winner
= pointsCandidate
;
94 for (const winnerKeeper
of this.winnerKeepers
) {
96 // Only one winner, another system should reset
97 if (!winnerKeeper
.winner
.winner
) {
98 winnerKeeper
.winner
.winner
= winner
;