From 3100e0533cb89a185ea021dfb83c4f364750180f Mon Sep 17 00:00:00 2001 From: Rubén Beltrán del Río Date: Tue, 29 May 2018 02:34:38 -0500 Subject: Add Naive Game Rules (#11) * Add components for tracking points * Add methods to create new entities * Adds nodes for points * Add system to detect points * Adds system to detect a winner * Add system to render points and winner * Add points required to win to config * Add it all to the engine * Add mention of points to changelog --- lib/systems/detect_winner.js | 104 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 lib/systems/detect_winner.js (limited to 'lib/systems/detect_winner.js') diff --git a/lib/systems/detect_winner.js b/lib/systems/detect_winner.js new file mode 100644 index 0000000..e7fbd76 --- /dev/null +++ b/lib/systems/detect_winner.js @@ -0,0 +1,104 @@ +import { System } from '@serpentity/serpentity'; + +import WinnerNode from '../nodes/winner'; +import PointsNode from '../nodes/points'; + +import Config from '../config'; + +/** + * Handles finding the winner + * + * @extends {external:Serpentity.System} + * @class DetectWinnerSystem + * @param {object} config a configuration object to extend. + */ +export default class DetectWinnerSystem extends System { + + constructor(config = {}) { + + super(); + + /** + * The collection of points keepers + * + * @property {external:Serpentity.NodeCollection} pointsKeepers + * @instance + * @memberof DetectWinnerSystem + */ + this.pointsKeepers = null; + + /** + * The collection of winner keepers + * + * @property {external:Serpentity.NodeCollection} winnerKeepers + * @instance + * @memberof DetectWinnerSystem + */ + this.winnerKeepers = null; + } + + /** + * Initializes system when added. Requests points and winner keepers + * + * @function added + * @memberof DetectWinnerSystem + * @instance + * @param {external:Serpentity.Engine} engine the serpentity engine to + * which we are getting added + */ + added(engine) { + + this.pointsKeepers = engine.getNodes(PointsNode); + this.winnerKeepers = engine.getNodes(WinnerNode); + } + + /** + * Clears system resources when removed. + * + * @function removed + * @instance + * @memberof DetectWinnerSystem + */ + removed() { + + this.pointsKeepers = null; + this.winnerKeepers = null; + } + + /** + * Runs on every update of the loop. Checks if someone won + * + * @function update + * @instance + * @param {Number} currentFrameDuration the duration of the current + * frame + * @memberof DetectWinnerSystem + */ + update(currentFrameDuration) { + + let winner = null; + + for (const pointsKeeper of this.pointsKeepers) { + for (const pointsCandidate of Object.keys(pointsKeeper.points)) { + if (pointsKeeper.points[pointsCandidate] >= Config.maxPoints) { + winner = pointsCandidate; + break; + } + } + } + + if (!winner) { + return; + } + + for (const winnerKeeper of this.winnerKeepers) { + + // Only one winner, another system should reset + if (!winnerKeeper.winner.winner) { + winnerKeeper.winner.winner = winner; + } + + } + } +}; + -- cgit