Renamed zone > map
This commit is contained in:
104
src/events/map/characterMove.ts
Normal file
104
src/events/map/characterMove.ts
Normal file
@ -0,0 +1,104 @@
|
||||
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/characterService'
|
||||
import MapEventTileService from '#services/mapEventTileService'
|
||||
|
||||
export default class CharacterMove extends BaseEvent {
|
||||
private readonly characterService = CharacterService
|
||||
private readonly mapEventTileService = MapEventTileService
|
||||
|
||||
public listen(): void {
|
||||
this.socket.on('map:character:move', this.handleEvent.bind(this))
|
||||
}
|
||||
|
||||
private async handleEvent({ positionX, positionY }: { positionX: number; positionY: number }): Promise<void> {
|
||||
const mapCharacter = MapManager.getCharacterById(this.socket.characterId!)
|
||||
if (!mapCharacter?.character) {
|
||||
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<{ x: number; y: number }>): Promise<void> {
|
||||
const { character } = mapCharacter
|
||||
|
||||
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.rotation = CharacterService.calculateRotation(start.x, start.y, end.x, end.y)
|
||||
|
||||
const mapEventTile = await mapEventTileRepository.getEventTileByMapIdAndPosition(character.map.id, Math.floor(end.x), Math.floor(end.y))
|
||||
|
||||
if (mapEventTile?.type === 'BLOCK') break
|
||||
if (mapEventTile?.type === 'TELEPORT' && mapEventTile.teleport) {
|
||||
await this.handleMapEventTile(mapEventTile as MapEventTileWithTeleport)
|
||||
break
|
||||
}
|
||||
|
||||
// Update position first
|
||||
character.positionX = end.x
|
||||
character.positionY = end.y
|
||||
|
||||
// Then emit with the same properties
|
||||
this.io.in(character.map.id).emit('map:character:move', {
|
||||
characterId: character.id,
|
||||
positionX: character.positionX,
|
||||
positionY: character.positionY,
|
||||
rotation: character.rotation,
|
||||
isMoving: true
|
||||
})
|
||||
|
||||
await this.characterService.applyMovementDelay()
|
||||
}
|
||||
|
||||
if (mapCharacter.isMoving && mapCharacter.currentPath === path) {
|
||||
this.finalizeMovement(mapCharacter)
|
||||
}
|
||||
}
|
||||
|
||||
private async handleMapEventTile(mapEventTile: MapEventTileWithTeleport): Promise<void> {
|
||||
const mapCharacter = MapManager.getCharacterById(this.socket.characterId!)
|
||||
if (!mapCharacter) {
|
||||
this.logger.error('map:character:move error: Character not found')
|
||||
return
|
||||
}
|
||||
|
||||
if (mapEventTile.teleport) {
|
||||
await this.mapEventTileService.handleTeleport(this.io, this.socket, mapCharacter.character, mapEventTile.teleport)
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
})
|
||||
}
|
||||
}
|
17
src/events/map/weather.ts
Normal file
17
src/events/map/weather.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import { BaseEvent } from '#application/base/baseEvent'
|
||||
import WeatherManager from '#managers/weatherManager'
|
||||
|
||||
export default class Weather extends BaseEvent {
|
||||
public listen(): void {
|
||||
this.socket.on('weather', this.handleEvent.bind(this))
|
||||
}
|
||||
|
||||
private async handleEvent(): Promise<void> {
|
||||
try {
|
||||
const weather = WeatherManager.getWeatherState()
|
||||
this.socket.emit('weather', weather)
|
||||
} catch (error: any) {
|
||||
this.logger.error('weather error: ' + error.message)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user