diff --git a/src/events/zone/characterMove.ts b/src/events/zone/characterMove.ts index 406514d..a8ec3dc 100644 --- a/src/events/zone/characterMove.ts +++ b/src/events/zone/characterMove.ts @@ -21,10 +21,9 @@ export default class CharacterMove extends BaseEvent { return } - // If already moving, cancel current movement and wait for it to fully stop + // If already moving, ignore new movement request if (zoneCharacter.isMoving) { - zoneCharacter.isMoving = false - await new Promise((resolve) => setTimeout(resolve, 100)) + return } const path = await this.characterService.calculatePath(zoneCharacter.character, positionX, positionY) @@ -42,40 +41,44 @@ export default class CharacterMove extends BaseEvent { private async moveAlongPath(zoneCharacter: ZoneCharacter, path: Array<{ x: number; y: number }>): Promise { const { character } = zoneCharacter - for (let i = 0; i < path.length - 1; i++) { - if (!zoneCharacter.isMoving || zoneCharacter.currentPath !== path) { - return + try { + for (let i = 0; i < path.length - 1; i++) { + if (!zoneCharacter.isMoving || zoneCharacter.currentPath !== path) { + return + } + + const [start, end] = [path[i], path[i + 1]] + character.rotation = CharacterService.calculateRotation(start.x, start.y, end.x, end.y) + + const zoneEventTile = await zoneEventTileRepository.getEventTileByZoneIdAndPosition( + character.zone.id, + Math.floor(end.x), + Math.floor(end.y) + ) + + if (zoneEventTile?.type === 'BLOCK') break + if (zoneEventTile?.type === 'TELEPORT' && zoneEventTile.teleport) { + await this.handleZoneEventTile(zoneEventTile as ZoneEventTileWithTeleport) + break + } + + character.positionX = end.x + character.positionY = end.y + + this.io.in(character.zone.id).emit('zone:character:move', { + characterId: character.id, + positionX: character.positionX, + positionY: character.positionY, + rotation: character.rotation, + isMoving: true + }) + + await this.characterService.applyMovementDelay() } - - const [start, end] = [path[i], path[i + 1]] - character.rotation = CharacterService.calculateRotation(start.x, start.y, end.x, end.y) - - const zoneEventTile = await zoneEventTileRepository.getEventTileByZoneIdAndPosition(character.zone.id, Math.floor(end.x), Math.floor(end.y)) - - if (zoneEventTile?.type === 'BLOCK') break - if (zoneEventTile?.type === 'TELEPORT' && zoneEventTile.teleport) { - await this.handleZoneEventTile(zoneEventTile as ZoneEventTileWithTeleport) - break + } finally { + if (zoneCharacter.isMoving && zoneCharacter.currentPath === path) { + this.finalizeMovement(zoneCharacter) } - - // Update position first - character.positionX = end.x - character.positionY = end.y - - // Then emit with the same properties - this.io.in(character.zone.id).emit('zone:character:move', { - characterId: character.id, - positionX: character.positionX, - positionY: character.positionY, - rotation: character.rotation, - isMoving: true - }) - - await this.characterService.applyMovementDelay() - } - - if (zoneCharacter.isMoving && zoneCharacter.currentPath === path) { - this.finalizeMovement(zoneCharacter) } }