Improved service names, added attack anim. sprite to init.ts, added attackService, added attack event
This commit is contained in:
137
src/services/characterMoveService.ts
Normal file
137
src/services/characterMoveService.ts
Normal file
@ -0,0 +1,137 @@
|
||||
import { BaseService } from '#application/base/baseService'
|
||||
import config from '#application/config'
|
||||
import { Character } from '#entities/character'
|
||||
import MapManager from '#managers/mapManager'
|
||||
|
||||
type Position = { positionX: number; positionY: number }
|
||||
export type Node = Position & { parent?: Node; g: number; h: number; f: number }
|
||||
|
||||
class CharacterMoveService extends BaseService {
|
||||
private readonly MOVEMENT_DELAY_MS = 260
|
||||
|
||||
private readonly DIRECTIONS = [
|
||||
{ x: 0, y: -1 }, // Up
|
||||
{ x: 0, y: 1 }, // Down
|
||||
{ x: -1, y: 0 }, // Left
|
||||
{ x: 1, y: 0 }, // Right
|
||||
{ x: -1, y: -1 }, // Up left
|
||||
{ x: -1, y: 1 }, // Up right
|
||||
{ x: 1, y: -1 }, // Down left
|
||||
{ x: 1, y: 1 } // Down right
|
||||
]
|
||||
|
||||
public async calculatePath(character: Character, targetX: number, targetY: number): Promise<Position[] | null> {
|
||||
const map = MapManager.getMapById(character.map.id)
|
||||
const grid = await map?.getGrid()
|
||||
|
||||
if (!grid?.length) {
|
||||
this.logger.error('map:character:move error: Grid not found or empty')
|
||||
return null
|
||||
}
|
||||
|
||||
const start: Position = {
|
||||
positionX: Math.floor(character.positionX),
|
||||
positionY: Math.floor(character.positionY)
|
||||
}
|
||||
|
||||
const end: Position = {
|
||||
positionX: Math.floor(targetX),
|
||||
positionY: Math.floor(targetY)
|
||||
}
|
||||
|
||||
return this.findPath(start, end, grid)
|
||||
}
|
||||
|
||||
public calculateRotation(X1: number, Y1: number, X2: number, Y2: number): number {
|
||||
if (config.ALLOW_DIAGONAL_MOVEMENT) {
|
||||
// Check diagonal movements
|
||||
if (X1 > X2 && Y1 > Y2) {
|
||||
return 7
|
||||
} else if (X1 < X2 && Y1 < Y2) {
|
||||
return 3
|
||||
} else if (X1 > X2 && Y1 < Y2) {
|
||||
return 5
|
||||
} else if (X1 < X2 && Y1 > Y2) {
|
||||
return 1
|
||||
}
|
||||
}
|
||||
|
||||
// Non-diagonal movements
|
||||
if (X1 > X2) {
|
||||
return 6
|
||||
} else if (X1 < X2) {
|
||||
return 2
|
||||
} else if (Y1 < Y2) {
|
||||
return 4
|
||||
} else if (Y1 > Y2) {
|
||||
return 0
|
||||
}
|
||||
|
||||
return 0 // Default case
|
||||
}
|
||||
|
||||
public async applyMovementDelay(): Promise<void> {
|
||||
await new Promise((resolve) => setTimeout(resolve, this.MOVEMENT_DELAY_MS))
|
||||
}
|
||||
|
||||
private findPath(start: Position, end: Position, grid: number[][]): Node[] {
|
||||
const openList: Node[] = [{ ...start, g: 0, h: 0, f: 0 }]
|
||||
const closedSet = new Set<string>()
|
||||
const getKey = (p: Position) => `${p.positionX},${p.positionY}`
|
||||
|
||||
while (openList.length > 0) {
|
||||
const current = openList.reduce((min, node) => (node.f < min.f ? node : min))
|
||||
if (current.positionX === end.positionX && current.positionY === end.positionY) return this.reconstructPath(current)
|
||||
|
||||
openList.splice(openList.indexOf(current), 1)
|
||||
closedSet.add(getKey(current))
|
||||
|
||||
const neighbors = this.DIRECTIONS.slice(0, config.ALLOW_DIAGONAL_MOVEMENT ? 8 : 4)
|
||||
.map((dir) => ({ positionX: current.positionX + dir.x, positionY: current.positionY + dir.y }))
|
||||
.filter((pos) => this.isValidPosition(pos, grid, end))
|
||||
|
||||
for (const neighbor of neighbors) {
|
||||
if (closedSet.has(getKey(neighbor))) continue
|
||||
|
||||
const g = current.g + this.getDistance(current, neighbor)
|
||||
const existing = openList.find((node) => node.positionX === neighbor.positionX && node.positionY === neighbor.positionY)
|
||||
|
||||
if (!existing || g < existing.g) {
|
||||
const h = this.getDistance(neighbor, end)
|
||||
const node: Node = { ...neighbor, g, h, f: g + h, parent: current }
|
||||
if (!existing) openList.push(node)
|
||||
else Object.assign(existing, node)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return [] // No path found
|
||||
}
|
||||
|
||||
private isValidPosition(pos: Position, grid: number[][], end: Position): boolean {
|
||||
return (
|
||||
pos.positionX >= 0 &&
|
||||
pos.positionY >= 0 &&
|
||||
pos.positionX < grid[0].length &&
|
||||
pos.positionY < grid.length &&
|
||||
(grid[pos.positionY][pos.positionX] === 0 || (pos.positionX === end.positionX && pos.positionY === end.positionY))
|
||||
)
|
||||
}
|
||||
|
||||
private getDistance(a: Position, b: Position): number {
|
||||
const dx = Math.abs(a.positionX - b.positionX),
|
||||
dy = Math.abs(a.positionY - b.positionY)
|
||||
// Manhattan distance for straight paths, then Euclidean for diagonals
|
||||
return dx + dy + (Math.sqrt(2) - 2) * Math.min(dx, dy)
|
||||
}
|
||||
|
||||
private reconstructPath(endNode: Node): Node[] {
|
||||
const path: Node[] = []
|
||||
for (let current: Node | undefined = endNode; current; current = current.parent) {
|
||||
path.unshift(current)
|
||||
}
|
||||
return path
|
||||
}
|
||||
}
|
||||
|
||||
export default new CharacterMoveService()
|
Reference in New Issue
Block a user