import { System } from '@serpentity/serpentity'; import { Text } from 'pixi.js'; import Tinycolor from 'tinycolor2'; 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) { const color = Tinycolor(internals.winnerText.tint.toString(16)); internals.winnerText.tint = parseInt(color.spin(2).toString().substring(1,7), 16); 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 * 8, fontWeight: 'bold', fill: 0xffffff, align: 'center' }); internals.winnerText.tint = 0xffbbff; internals.winnerText.scale.x = 0.125; internals.winnerText.scale.y = 0.125; 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; } } } }