import { LitElement, html } from "lit"; import { customElement, state } from "lit/decorators.js"; import { translateText } from "../../../client/Utils"; import { EventBus } from "../../../core/EventBus"; import { Gold } from "../../../core/game/Game"; import { GameView } from "../../../core/game/GameView"; import { ClientID } from "../../../core/Schemas"; import { AttackRatioEvent } from "../../InputHandler"; import { renderNumber, renderTroops } from "../../Utils"; import { UIState } from "../UIState"; import { Layer } from "./Layer"; @customElement("control-panel") export class ControlPanel extends LitElement implements Layer { public game: GameView; public clientID: ClientID; public eventBus: EventBus; public uiState: UIState; @state() private attackRatio: number = 0.2; @state() private _maxTroops: number; @state() private troopRate: number; @state() private _troops: number; @state() private _isVisible = false; @state() private _gold: Gold; private _troopRateIsIncreasing: boolean = true; private _lastTroopIncreaseRate: number; init() { this.attackRatio = Number( localStorage.getItem("settings.attackRatio") ?? "0.2", ); this.uiState.attackRatio = this.attackRatio; this.eventBus.on(AttackRatioEvent, (event) => { let newAttackRatio = (parseInt( (document.getElementById("attack-ratio") as HTMLInputElement).value, ) + event.attackRatio) / 100; if (newAttackRatio < 0.01) { newAttackRatio = 0.01; } if (newAttackRatio > 1) { newAttackRatio = 1; } if (newAttackRatio === 0.11 && this.attackRatio === 0.01) { // If we're changing the ratio from 1%, then set it to 10% instead of 11% to keep a consistency newAttackRatio = 0.1; } this.attackRatio = newAttackRatio; this.onAttackRatioChange(this.attackRatio); }); } tick() { if (!this._isVisible && !this.game.inSpawnPhase()) { this.setVisibile(true); } const player = this.game.myPlayer(); if (player === null || !player.isAlive()) { this.setVisibile(false); return; } if (this.game.ticks() % 5 === 0) { this.updateTroopIncrease(); } this._maxTroops = this.game.config().maxTroops(player); this._gold = player.gold(); this._troops = player.troops(); this.troopRate = this.game.config().troopIncreaseRate(player) * 10; this.requestUpdate(); } private updateTroopIncrease() { const player = this.game?.myPlayer(); if (player === null) return; const troopIncreaseRate = this.game.config().troopIncreaseRate(player); this._troopRateIsIncreasing = troopIncreaseRate >= this._lastTroopIncreaseRate; this._lastTroopIncreaseRate = troopIncreaseRate; } onAttackRatioChange(newRatio: number) { this.uiState.attackRatio = newRatio; } renderLayer(context: CanvasRenderingContext2D) { // Render any necessary canvas elements } shouldTransform(): boolean { return false; } setVisibile(visible: boolean) { this._isVisible = visible; this.requestUpdate(); } render() { return html`
e.preventDefault()} >
${translateText("control_panel.troops")}: ${renderTroops(this._troops)} / ${renderTroops(this._maxTroops)} (+${renderTroops(this.troopRate)})
${translateText("control_panel.gold")}: ${renderNumber(this._gold)}
{ this.attackRatio = parseInt((e.target as HTMLInputElement).value) / 100; this.onAttackRatioChange(this.attackRatio); }} class="absolute left-0 right-0 top-2 m-0 h-4 cursor-pointer attackRatio" />
`; } createRenderRoot() { return this; // Disable shadow DOM to allow Tailwind styles } }