Walk improvements

This commit is contained in:
2025-02-06 13:47:02 +01:00
parent ba96ae7dd4
commit 765a0468bc
2 changed files with 86 additions and 37 deletions

View File

@ -8,6 +8,7 @@ export type Node = Position & { parent?: Node; g: number; h: number; f: number }
class CharacterMoveService extends BaseService {
private readonly MOVEMENT_DELAY_MS = 260
private readonly MAX_PATH_LENGTH = 20 // Limit maximum path length
private readonly DIRECTIONS = [
{ x: 0, y: -1 }, // Up
@ -45,7 +46,21 @@ class CharacterMoveService extends BaseService {
return null
}
return this.findPath(start, end, grid)
// Add maximum distance check
const directDistance = Math.sqrt(Math.pow(targetX - character.positionX, 2) + Math.pow(targetY - character.positionY, 2))
if (directDistance > this.MAX_PATH_LENGTH) {
return null
}
const path = this.findPath(start, end, grid)
// Validate path length
if (path.length > this.MAX_PATH_LENGTH) {
return path.slice(0, this.MAX_PATH_LENGTH)
}
return path
}
public calculateRotation(X1: number, Y1: number, X2: number, Y2: number): number {