diff --git a/src/client/graphics/layers/NukeTrajectoryPreviewLayer.ts b/src/client/graphics/layers/NukeTrajectoryPreviewLayer.ts index b51fe2c98..f7f9dbe64 100644 --- a/src/client/graphics/layers/NukeTrajectoryPreviewLayer.ts +++ b/src/client/graphics/layers/NukeTrajectoryPreviewLayer.ts @@ -104,36 +104,7 @@ export class NukeTrajectoryPreviewLayer implements Layer { return; } - let targetTile: TileRef | null = null; - - // If ghost is locked, use the locked tile; otherwise use mouse position - if (this.uiState.lockedGhostTile) { - targetTile = this.uiState.lockedGhostTile; - } else { - // Convert mouse position to world coordinates - const rect = this.transformHandler.boundingRect(); - if (!rect) { - this.trajectoryPoints = []; - this.cachedSpawnTile = null; - return; - } - - const localX = this.mousePos.x - rect.left; - const localY = this.mousePos.y - rect.top; - const worldCoords = this.transformHandler.screenToWorldCoordinates( - localX, - localY, - ); - - if (!this.game.isValidCoord(worldCoords.x, worldCoords.y)) { - this.trajectoryPoints = []; - this.lastTargetTile = null; - this.cachedSpawnTile = null; - return; - } - - targetTile = this.game.ref(worldCoords.x, worldCoords.y); - } + const targetTile = this.getTargetTile(); if (targetTile === null) { this.trajectoryPoints = []; @@ -204,33 +175,7 @@ export class NukeTrajectoryPreviewLayer implements Layer { return; } - let targetTile: TileRef | null = null; - - // If ghost is locked, use the locked tile; otherwise use mouse position - if (this.uiState.lockedGhostTile) { - targetTile = this.uiState.lockedGhostTile; - } else { - // Convert mouse position to world coordinates - const rect = this.transformHandler.boundingRect(); - if (!rect) { - this.trajectoryPoints = []; - return; - } - - const localX = this.mousePos.x - rect.left; - const localY = this.mousePos.y - rect.top; - const worldCoords = this.transformHandler.screenToWorldCoordinates( - localX, - localY, - ); - - if (!this.game.isValidCoord(worldCoords.x, worldCoords.y)) { - this.trajectoryPoints = []; - return; - } - - targetTile = this.game.ref(worldCoords.x, worldCoords.y); - } + const targetTile = this.getTargetTile(); if (targetTile === null) { this.trajectoryPoints = []; @@ -319,6 +264,31 @@ export class NukeTrajectoryPreviewLayer implements Layer { } } + // Shared helper to resolve the current target tile, preferring a locked ghost tile when present + private getTargetTile(): TileRef | null { + if (this.uiState.lockedGhostTile) { + return this.uiState.lockedGhostTile; + } + + const rect = this.transformHandler.boundingRect(); + if (!rect) { + return null; + } + + const localX = this.mousePos.x - rect.left; + const localY = this.mousePos.y - rect.top; + const worldCoords = this.transformHandler.screenToWorldCoordinates( + localX, + localY, + ); + + if (!this.game.isValidCoord(worldCoords.x, worldCoords.y)) { + return null; + } + + return this.game.ref(worldCoords.x, worldCoords.y); + } + /** * Draw trajectory preview line on the canvas */