import { html, LitElement } from "lit"; import { customElement } from "lit/decorators.js"; import goldCoinIcon from "../../../../resources/images/GoldCoinIcon.svg"; import populationIcon from "../../../../resources/images/PopulationIconSolidWhite.svg"; import troopIcon from "../../../../resources/images/TroopIconWhite.svg"; import workerIcon from "../../../../resources/images/WorkerIconWhite.svg"; import { EventBus } from "../../../core/EventBus"; import { GameUpdateType } from "../../../core/game/GameUpdates"; import { GameView } from "../../../core/game/GameView"; import { UserSettings } from "../../../core/game/UserSettings"; import { renderNumber, renderTroops } from "../../Utils"; import { Layer } from "./Layer"; @customElement("game-top-bar") export class GameTopBar extends LitElement implements Layer { public game: GameView; public eventBus: EventBus; private _userSettings: UserSettings = new UserSettings(); private _population = 0; private _troops = 0; private _workers = 0; private _lastPopulationIncreaseRate = 0; private _popRateIsIncreasing = false; private hasWinner = false; createRenderRoot() { return this; } init() { this.requestUpdate(); } tick() { this.updatePopulationIncrease(); const player = this.game?.myPlayer(); if (!player) return; this._troops = player.troops(); this._workers = player.workers(); const updates = this.game.updatesSinceLastTick(); if (updates) { this.hasWinner = this.hasWinner || updates[GameUpdateType.Win].length > 0; } this.requestUpdate(); } private updatePopulationIncrease() { const player = this.game?.myPlayer(); if (player === null) return; const popIncreaseRate = player.population() - this._population; if (this.game.ticks() % 5 === 0) { this._popRateIsIncreasing = popIncreaseRate >= this._lastPopulationIncreaseRate; this._lastPopulationIncreaseRate = popIncreaseRate; } } render() { const myPlayer = this.game?.myPlayer(); if (!this.game || !myPlayer || this.game.inSpawnPhase()) { return null; } const isAlt = this.game.config().isReplay(); if (isAlt) { return html`
`; } const popRate = myPlayer ? this.game.config().populationIncreaseRate(myPlayer) * 10 : 0; const maxPop = myPlayer ? this.game.config().maxPopulation(myPlayer) : 0; const goldPerSecond = myPlayer ? this.game.config().goldAdditionRate(myPlayer) * 10n : 0n; return html`
${myPlayer?.isAlive() && !this.game.inSpawnPhase() ? html`
gold +${renderNumber(goldPerSecond)}
${renderNumber(myPlayer.gold())}
population +${renderTroops(popRate)}
${renderTroops(myPlayer.population())} / ${renderTroops(maxPop)}
troops ${renderTroops(this._troops)}
gold ${renderTroops(this._workers)}
` : html`
`}
`; } }