forked from noxious/server
55 lines
1.7 KiB
TypeScript
55 lines
1.7 KiB
TypeScript
import { Server } from 'socket.io'
|
|
|
|
import { TSocket } from '#application/types'
|
|
import { Character } from '#entities/character'
|
|
import MapManager from '#managers/mapManager'
|
|
import SocketManager from '#managers/socketManager'
|
|
import TeleportService from '#services/teleportService'
|
|
|
|
class MapCharacter {
|
|
public readonly character: Character
|
|
public isMoving: boolean = false
|
|
public currentPath: Array<{ x: number; y: number }> | null = null
|
|
|
|
constructor(character: Character) {
|
|
this.character = 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: number, targetX: number, targetY: number): Promise<void> {
|
|
await TeleportService.teleportCharacter(this.character.id, {
|
|
targetMapId: mapId,
|
|
targetX,
|
|
targetY
|
|
})
|
|
}
|
|
|
|
public async disconnect(socket: TSocket, io: Server): Promise<void> {
|
|
try {
|
|
// Stop any movement and save final position
|
|
this.isMoving = false
|
|
this.currentPath = null
|
|
await this.savePosition()
|
|
|
|
// Leave map and remove from manager
|
|
if (this.character.map) {
|
|
socket.leave(this.character.map.id)
|
|
MapManager.removeCharacter(this.character.id)
|
|
|
|
// Notify map players
|
|
io.in(this.character.map.id).emit('map:character:leave', this.character.id)
|
|
}
|
|
|
|
// Notify all players
|
|
io.emit('character:disconnect', this.character.id)
|
|
} catch (error) {
|
|
console.error(`Error disconnecting character ${this.character.id}:`, error)
|
|
}
|
|
}
|
|
}
|
|
|
|
export default MapCharacter
|