import { BaseEvent } from '#application/base/baseEvent' import { MapEventTileWithTeleport } from '#application/types' import MapManager from '#managers/mapManager' import MapCharacter from '#models/mapCharacter' import MapEventTileRepository from '#repositories/mapEventTileRepository' import CharacterService from '#services/characterMoveService' import TeleportService from '#services/characterTeleportService' export default class CharacterMove extends BaseEvent { private readonly characterService = CharacterService public listen(): void { this.socket.on('map:character:move', this.handleEvent.bind(this)) } private async handleEvent({ positionX, positionY }: { positionX: number; positionY: number }): Promise { const mapCharacter = MapManager.getCharacterById(this.socket.characterId!) if (!mapCharacter?.getCharacter()) { this.logger.error('map:character:move error: Character not found or not initialized') return } // If already moving, cancel current movement and wait for it to fully stop if (mapCharacter.isMoving) { mapCharacter.isMoving = false await new Promise((resolve) => setTimeout(resolve, 100)) } const path = await this.characterService.calculatePath(mapCharacter.character, positionX, positionY) if (!path) { this.io.in(mapCharacter.character.map.id).emit('map:character:moveError', 'No valid path found') return } // Start new movement mapCharacter.isMoving = true mapCharacter.currentPath = path // Add this property to MapCharacter class await this.moveAlongPath(mapCharacter, path) } private async moveAlongPath(mapCharacter: MapCharacter, path: Array<{ positionX: number; positionY: number }>): Promise { const character = mapCharacter.getCharacter() for (let i = 0; i < path.length - 1; i++) { if (!mapCharacter.isMoving || mapCharacter.currentPath !== path) { return } const [start, end] = [path[i], path[i + 1]] character.setRotation(CharacterService.calculateRotation(start.positionX, start.positionY, end.positionX, end.positionY)) const mapEventTileRepository = new MapEventTileRepository() const mapEventTile = await mapEventTileRepository.getEventTileByMapIdAndPosition(character.getMap().getId(), Math.floor(end.positionX), Math.floor(end.positionY)) if (mapEventTile?.type === 'BLOCK') break if (mapEventTile?.type === 'TELEPORT' && mapEventTile.teleport) { await this.handleTeleportMapEventTile(mapEventTile as MapEventTileWithTeleport) return } // Update position first character.setPositionX(end.positionX).setPositionY(end.positionY) // Then emit with the same properties this.io.in(character.map.id).emit('map:character:move', { characterId: character.id, positionX: character.getPositionX(), positionY: character.getPositionY(), rotation: character.getRotation(), isMoving: true }) await this.characterService.applyMovementDelay() } if (mapCharacter.isMoving && mapCharacter.currentPath === path) { this.finalizeMovement(mapCharacter) } } private async handleTeleportMapEventTile(mapEventTile: MapEventTileWithTeleport): Promise { if (mapEventTile.getTeleport()) { await TeleportService.teleportCharacter(this.socket.characterId!, { targetMapId: mapEventTile.getTeleport()!.getToMap().getId(), targetX: mapEventTile.getTeleport()!.getToPositionX(), targetY: mapEventTile.getTeleport()!.getToPositionY(), rotation: mapEventTile.getTeleport()!.getToRotation() }) } } private finalizeMovement(mapCharacter: MapCharacter): void { mapCharacter.isMoving = false this.io.in(mapCharacter.character.map.id).emit('map:character:move', { characterId: mapCharacter.character.id, positionX: mapCharacter.character.positionX, positionY: mapCharacter.character.positionY, rotation: mapCharacter.character.rotation, isMoving: false }) } }