mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-22 04:43:49 +00:00
Extracted shared target resolution logic into getTargetTile()
This commit is contained in:
@@ -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
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user