Renamed zone > map
This commit is contained in:
54
src/models/mapCharacter.ts
Normal file
54
src/models/mapCharacter.ts
Normal file
@ -0,0 +1,54 @@
|
||||
import { Server } from 'socket.io'
|
||||
|
||||
import { TSocket } from '#application/types'
|
||||
import { Character } from '#entities/character'
|
||||
import SocketManager from '#managers/socketManager'
|
||||
import MapManager from '#managers/mapManager'
|
||||
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).update()
|
||||
}
|
||||
|
||||
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
|
Reference in New Issue
Block a user