forked from noxious/server
44 lines
1.6 KiB
TypeScript
44 lines
1.6 KiB
TypeScript
import { ExtendedCharacter } from '../../utilities/types'
|
|
import { AStar } from '../../utilities/character/aStar'
|
|
import ZoneManager from '../../managers/zoneManager'
|
|
import Rotation from '../../utilities/character/rotation'
|
|
import { gameLogger } from '../../utilities/logger'
|
|
|
|
export class CharacterMoveService {
|
|
public updatePosition(character: ExtendedCharacter, position: { x: number; y: number }, newZoneId?: number) {
|
|
Object.assign(character, {
|
|
positionX: position.x,
|
|
positionY: position.y,
|
|
rotation: Rotation.calculate(character.positionX, character.positionY, position.x, position.y),
|
|
zoneId: newZoneId || character.zoneId
|
|
})
|
|
|
|
// await prisma.character.update({
|
|
// where: { id: character.id },
|
|
// data: {
|
|
// positionX: position.x,
|
|
// positionY: position.y,
|
|
// rotation: character.rotation,
|
|
// zoneId: newZoneId
|
|
// }
|
|
// })
|
|
}
|
|
|
|
public async calculatePath(character: ExtendedCharacter, targetX: number, targetY: number): Promise<Array<{ x: number; y: number }> | null> {
|
|
const grid = await ZoneManager.getZoneById(character.zoneId)?.getGrid()
|
|
if (!grid?.length) {
|
|
gameLogger.error('character:move error', 'Grid not found or empty')
|
|
return null
|
|
}
|
|
|
|
const start = { x: Math.floor(character.positionX), y: Math.floor(character.positionY) }
|
|
const end = { x: Math.floor(targetX), y: Math.floor(targetY) }
|
|
|
|
return AStar.findPath(start, end, grid)
|
|
}
|
|
|
|
public async applyMovementDelay(): Promise<void> {
|
|
await new Promise((resolve) => setTimeout(resolve, 250)) // 250ms delay between steps
|
|
}
|
|
}
|