import { SocketEvent } from '@/application/enums' import type { TSocket, UUID } from '@/application/types' import { Character } from '@/entities/character' import MapManager from '@/managers/mapManager' import TeleportService from '@/services/characterTeleportService' import { Server } from 'socket.io' class MapCharacter { public readonly character: Character public isMoving: boolean = false public currentPath: Array<{ positionX: number; positionY: number }> | null = null constructor(character: Character) { this.character = character } public getCharacter(): Character { return this.character } public async savePosition() { await this.character.setPositionX(this.character.positionX).setPositionY(this.character.positionY).setRotation(this.character.rotation).setMap(this.character.map).save() } public async teleport(mapId: UUID, targetX: number, targetY: number): Promise { await TeleportService.teleportCharacter(this.character.id, { targetMapId: mapId, targetX, targetY }) } public async disconnect(socket: TSocket, io: Server): Promise { try { // Stop any movement and save final position this.isMoving = false this.currentPath = null await this.savePosition() // Leave map and remove from manager socket.leave(this.character.map.id) const map = MapManager.getMapById(this.character.map.id) if (map) { await map.removeCharacter(this.character.id) } // Notify map players io.in(this.character.map.id).emit(SocketEvent.MAP_CHARACTER_LEAVE, this.character.id) // Notify all players io.emit(SocketEvent.CHARACTER_DISCONNECT, this.character.id) } catch (error) { console.error(`Error disconnecting character ${this.character.id}:`, error) } } } export default MapCharacter