forked from noxious/server
Minor improvements
This commit is contained in:
parent
daeb232d3b
commit
cbd6e2c307
@ -4,13 +4,9 @@ import type { MapEventTileWithTeleport } from '@/application/types'
|
||||
import type { Character } from '@/entities/character'
|
||||
import MapManager from '@/managers/mapManager'
|
||||
import MapCharacter from '@/models/mapCharacter'
|
||||
import MapEventTileRepository from '@/repositories/mapEventTileRepository'
|
||||
import CharacterService from '@/services/characterMoveService'
|
||||
import characterMoveService from '@/services/characterMoveService'
|
||||
import TeleportService from '@/services/characterTeleportService'
|
||||
import CharacterMoveService from '@/services/characterMoveService'
|
||||
|
||||
export default class CharacterMove extends BaseEvent {
|
||||
private readonly characterService = CharacterService
|
||||
private readonly STEP_DELAY = 100
|
||||
private readonly THROTTLE_DELAY = 230
|
||||
private readonly MAX_REQUEST_DISTANCE = 30 // Maximum allowed distance for movement requests
|
||||
@ -25,7 +21,6 @@ export default class CharacterMove extends BaseEvent {
|
||||
try {
|
||||
const mapCharacter = MapManager.getCharacterById(this.socket.characterId!)
|
||||
if (!mapCharacter?.getCharacter()) {
|
||||
this.logger.error('map:character:move error: Character not found or not initialized')
|
||||
return
|
||||
}
|
||||
|
||||
@ -40,7 +35,7 @@ export default class CharacterMove extends BaseEvent {
|
||||
}
|
||||
|
||||
// Validate current position against last known position
|
||||
const movementValidation = this.characterService.validateMovementDistance(currentX, currentY, this.lastKnownPosition, this.STEP_DELAY, mapCharacter.isMoving)
|
||||
const movementValidation = CharacterMoveService.validateMovementDistance(currentX, currentY, this.lastKnownPosition, this.STEP_DELAY, mapCharacter.isMoving)
|
||||
|
||||
if (!movementValidation.isValid) {
|
||||
character.setPositionX(this.lastKnownPosition!.x).setPositionY(this.lastKnownPosition!.y)
|
||||
@ -49,7 +44,7 @@ export default class CharacterMove extends BaseEvent {
|
||||
}
|
||||
|
||||
// Validate requested position distance
|
||||
const requestValidation = this.characterService.validateRequestDistance(currentX, currentY, positionX, positionY, this.MAX_REQUEST_DISTANCE)
|
||||
const requestValidation = CharacterMoveService.validateRequestDistance(currentX, currentY, positionX, positionY, this.MAX_REQUEST_DISTANCE)
|
||||
|
||||
if (!requestValidation.isValid) {
|
||||
this.logger.warn(`Invalid movement distance detected: ${this.socket.characterId}`)
|
||||
@ -65,13 +60,13 @@ export default class CharacterMove extends BaseEvent {
|
||||
}
|
||||
|
||||
// Cancel any ongoing movement
|
||||
this.cancelCurrentMovement(mapCharacter)
|
||||
CharacterMoveService.cancelCurrentMovement(mapCharacter)
|
||||
|
||||
// Update last known position
|
||||
this.lastKnownPosition = { x: currentX, y: currentY }
|
||||
|
||||
// Calculate path to target position
|
||||
const path = await this.characterService.calculatePath(character, Math.floor(positionX), Math.floor(positionY))
|
||||
const path = await CharacterMoveService.calculatePath(character, Math.floor(positionX), Math.floor(positionY))
|
||||
|
||||
if (!path?.length) {
|
||||
return
|
||||
@ -86,12 +81,6 @@ export default class CharacterMove extends BaseEvent {
|
||||
}
|
||||
}
|
||||
|
||||
private cancelCurrentMovement(mapCharacter: MapCharacter): void {
|
||||
if (!mapCharacter.isMoving) return
|
||||
mapCharacter.isMoving = false
|
||||
mapCharacter.currentPath = null
|
||||
}
|
||||
|
||||
private async moveAlongPath(mapCharacter: MapCharacter): Promise<void> {
|
||||
const character = mapCharacter.getCharacter()
|
||||
const path = mapCharacter.currentPath
|
||||
@ -117,22 +106,22 @@ export default class CharacterMove extends BaseEvent {
|
||||
currentTile = path[i]
|
||||
nextTile = path[i + 1]
|
||||
|
||||
if (!currentTile || !nextTile || !characterMoveService.isValidStep(currentTile, nextTile)) {
|
||||
if (!currentTile || !nextTile || !CharacterMoveService.isValidStep(currentTile, nextTile)) {
|
||||
return
|
||||
}
|
||||
|
||||
// Update character rotation and position in a single operation
|
||||
character
|
||||
.setRotation(CharacterService.calculateRotation(currentTile.positionX, currentTile.positionY, nextTile.positionX, nextTile.positionY))
|
||||
.setRotation(CharacterMoveService.calculateRotation(currentTile.positionX, currentTile.positionY, nextTile.positionX, nextTile.positionY))
|
||||
.setPositionX(nextTile.positionX)
|
||||
.setPositionY(nextTile.positionY)
|
||||
|
||||
// Check for map events at the next tile
|
||||
const mapEventTile = await characterMoveService.checkMapEvents(character.getMap().getId(), nextTile)
|
||||
const mapEventTile = await CharacterMoveService.checkMapEvents(character.getMap().getId(), nextTile)
|
||||
if (mapEventTile) {
|
||||
if (mapEventTile.type === 'BLOCK') break
|
||||
if (mapEventTile.type === 'TELEPORT' && mapEventTile.teleport) {
|
||||
await characterMoveService.handleTeleportMapEventTile(character.id, mapEventTile as MapEventTileWithTeleport)
|
||||
await CharacterMoveService.handleTeleportMapEventTile(character.id, mapEventTile as MapEventTileWithTeleport)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import { Character } from '@/entities/character'
|
||||
import MapManager from '@/managers/mapManager'
|
||||
import MapEventTileRepository from '@/repositories/mapEventTileRepository'
|
||||
import TeleportService from '@/services/characterTeleportService'
|
||||
import MapCharacter from "@/models/mapCharacter";
|
||||
|
||||
type Position = { positionX: number; positionY: number }
|
||||
export type Node = Position & { parent?: Node; g: number; h: number; f: number }
|
||||
@ -276,6 +277,12 @@ class CharacterMoveService extends BaseService {
|
||||
return path
|
||||
}
|
||||
|
||||
public cancelCurrentMovement(mapCharacter: MapCharacter): void {
|
||||
if (!mapCharacter.isMoving) return
|
||||
mapCharacter.isMoving = false
|
||||
mapCharacter.currentPath = null
|
||||
}
|
||||
|
||||
public validateMovementDistance(
|
||||
currentX: number,
|
||||
currentY: number,
|
||||
|
Loading…
x
Reference in New Issue
Block a user