import { LitElement, html } from "lit"; import { customElement, state } from "lit/decorators.js"; import { EventBus } from "../../../core/EventBus"; import { GameView } from "../../../core/game/GameView"; import { ClientID } from "../../../core/Schemas"; import { AttackRatioEvent } from "../../InputHandler"; import { SendSetTargetTroopRatioEvent } from "../../Transport"; 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 targetTroopRatio = 0.95; @state() private currentTroopRatio = 0.95; @state() private _population: number; @state() private _maxPopulation: number; @state() private popRate: number; @state() private _troops: number; @state() private _workers: number; @state() private _isVisible = false; @state() private _manpower: number = 0; @state() private _gold: number; @state() private _goldPerSecond: number; private _lastPopulationIncreaseRate: number; private _popRateIsIncreasing: boolean = true; private init_: boolean = false; init() { this.attackRatio = Number( localStorage.getItem("settings.attackRatio") ?? "0.2", ); this.targetTroopRatio = Number( localStorage.getItem("settings.troopRatio") ?? "0.95", ); this.init_ = true; this.uiState.attackRatio = this.attackRatio; this.currentTroopRatio = this.targetTroopRatio; 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.init_) { this.eventBus.emit( new SendSetTargetTroopRatioEvent(this.targetTroopRatio), ); this.init_ = false; } if (!this._isVisible && !this.game.inSpawnPhase()) { this.setVisibile(true); } const player = this.game.myPlayer(); if (player == null || !player.isAlive()) { this.setVisibile(false); return; } const popIncreaseRate = player.population() - this._population; if (this.game.ticks() % 5 == 0) { this._popRateIsIncreasing = popIncreaseRate >= this._lastPopulationIncreaseRate; this._lastPopulationIncreaseRate = popIncreaseRate; } this._population = player.population(); this._maxPopulation = this.game.config().maxPopulation(player); this._gold = player.gold(); this._troops = player.troops(); this._workers = player.workers(); this.popRate = this.game.config().populationIncreaseRate(player) * 10; this._goldPerSecond = this.game.config().goldAdditionRate(player) * 10; this.currentTroopRatio = player.troops() / player.population(); this.requestUpdate(); } 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(); } targetTroops(): number { return this._manpower * this.targetTroopRatio; } onTroopChange(newRatio: number) { this.eventBus.emit(new SendSetTargetTroopRatioEvent(newRatio)); } delta(): number { const d = this._population - this.targetTroops(); return d; } render() { return html`
e.preventDefault()} >
{ this.targetTroopRatio = parseInt((e.target as HTMLInputElement).value) / 100; this.onTroopChange(this.targetTroopRatio); }} class="absolute left-0 right-0 top-2 m-0 h-4 cursor-pointer targetTroopRatio" />
{ 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 } }