import { LitElement } from "lit"; import { customElement } from "lit/decorators.js"; import { EventBus } from "../../../core/EventBus"; import { PlayerActions, TerraNullius } from "../../../core/game/Game"; import { TileRef } from "../../../core/game/GameMap"; import { GameView, PlayerView } from "../../../core/game/GameView"; import { TransformHandler } from "../TransformHandler"; import { UIState } from "../UIState"; import { BuildMenu } from "./BuildMenu"; import { ChatIntegration } from "./ChatIntegration"; import { EmojiTable } from "./EmojiTable"; import { Layer } from "./Layer"; import { PlayerActionHandler } from "./PlayerActionHandler"; import { PlayerPanel } from "./PlayerPanel"; import { RadialMenu, RadialMenuConfig } from "./RadialMenu"; import { centerButtonElement, COLORS, MenuElementParams, rootMenuItems, } from "./RadialMenuElements"; import swordIcon from "../../../../resources/images/SwordIconWhite.svg"; import { ContextMenuEvent } from "../../InputHandler"; @customElement("main-radial-menu") export class MainRadialMenu extends LitElement implements Layer { private radialMenu: RadialMenu; private playerActionHandler: PlayerActionHandler; private chatIntegration: ChatIntegration; private clickedTile: TileRef | null = null; private selectedPlayer: PlayerView | TerraNullius | null = null; constructor( private eventBus: EventBus, private game: GameView, private transformHandler: TransformHandler, private emojiTable: EmojiTable, private buildMenu: BuildMenu, private uiState: UIState, private playerPanel: PlayerPanel, ) { super(); const menuConfig: RadialMenuConfig = { centerButtonIcon: swordIcon, tooltipStyle: ` .radial-tooltip .cost { margin-top: 4px; color: ${COLORS.tooltip.cost}; } .radial-tooltip .count { color: ${COLORS.tooltip.count}; } `, }; this.radialMenu = new RadialMenu(menuConfig); this.playerActionHandler = new PlayerActionHandler( this.eventBus, this.uiState, ); this.chatIntegration = new ChatIntegration(this.game, this.eventBus); this.radialMenu.setRootMenuItems(rootMenuItems, centerButtonElement); } init() { this.radialMenu.init(); this.eventBus.on(ContextMenuEvent, (event) => { const worldCoords = this.transformHandler.screenToWorldCoordinates( event.x, event.y, ); if (!this.game.isValidCoord(worldCoords.x, worldCoords.y)) { return; } if (this.game.myPlayer() === null) { return; } this.clickedTile = this.game.ref(worldCoords.x, worldCoords.y); this.selectedPlayer = this.game.owner(this.clickedTile); this.game .myPlayer()! .actions(this.clickedTile) .then((actions) => { this.handlePlayerActions( this.game.myPlayer()!, actions, this.clickedTile!, event.x, event.y, ); }); }); } private async handlePlayerActions( myPlayer: PlayerView, actions: PlayerActions, tile: TileRef, screenX: number, screenY: number, ) { this.buildMenu.playerActions = actions; const tileOwner = this.game.owner(tile); const recipient = tileOwner.isPlayer() ? (tileOwner as PlayerView) : null; if (myPlayer && recipient) { this.chatIntegration.setupChatModal(myPlayer, recipient); } const params: MenuElementParams = { myPlayer, selected: recipient, tile, playerActions: actions, game: this.game, buildMenu: this.buildMenu, emojiTable: this.emojiTable, playerActionHandler: this.playerActionHandler, playerPanel: this.playerPanel, chatIntegration: this.chatIntegration, closeMenu: () => this.closeMenu(), }; this.radialMenu.setRootMenuItems(rootMenuItems, centerButtonElement); this.radialMenu.setParams(params); this.radialMenu.showRadialMenu(screenX, screenY); } async tick() { if (!this.radialMenu.isMenuVisible() || this.clickedTile === null) return; if (this.selectedPlayer === null) return; const currentPlayer = this.game.owner(this.clickedTile); if (currentPlayer.id() !== this.selectedPlayer.id()) { this.closeMenu(); return; } } renderLayer(context: CanvasRenderingContext2D) { this.radialMenu.renderLayer(context); } shouldTransform(): boolean { return this.radialMenu.shouldTransform(); } redraw() { // No redraw implementation needed } closeMenu() { if (this.radialMenu.isMenuVisible()) { this.radialMenu.hideRadialMenu(); } if (this.buildMenu.isVisible) { this.buildMenu.hideMenu(); } if (this.emojiTable.isVisible) { this.emojiTable.hideTable(); } if (this.playerPanel.isVisible) { this.playerPanel.hide(); } } }