Character move bug fix

If already walking and then select an invalid position, isMoving was kept true. This is fixed.
This commit is contained in:
Dennis Postma 2025-02-15 21:35:16 +01:00
parent 47be8597bf
commit 086c7cd6d6

View File

@ -6,6 +6,7 @@ import MapCharacter from '@/models/mapCharacter'
import MapEventTileRepository from '@/repositories/mapEventTileRepository'
import CharacterService from '@/services/characterMoveService'
import TeleportService from '@/services/characterTeleportService'
import type {Character} from "@/entities/character";
export default class CharacterMove extends BaseEvent {
private readonly characterService = CharacterService
@ -41,8 +42,6 @@ export default class CharacterMove extends BaseEvent {
const movementValidation = this.characterService.validateMovementDistance(currentX, currentY, this.lastKnownPosition, this.STEP_DELAY, mapCharacter.isMoving)
if (!movementValidation.isValid) {
this.logger.warn(`Suspicious movement detected: ${this.socket.characterId}`)
// Force position reset
character.setPositionX(this.lastKnownPosition!.x).setPositionY(this.lastKnownPosition!.y)
this.broadcastMovement(character, false)
return
@ -71,10 +70,9 @@ export default class CharacterMove extends BaseEvent {
this.lastKnownPosition = { x: currentX, y: currentY }
// Calculate path to target position
const path = await this.characterService.calculatePath(mapCharacter.character, Math.floor(positionX), Math.floor(positionY))
const path = await this.characterService.calculatePath(character, Math.floor(positionX), Math.floor(positionY))
if (!path?.length) {
this.io.in(mapCharacter.character.map.id).emit(SocketEvent.MAP_CHARACTER_MOVEERROR, 'No valid path found')
return
}
@ -105,6 +103,7 @@ export default class CharacterMove extends BaseEvent {
try {
for (let i = 0; i < path.length - 1; i++) {
if (!mapCharacter.isMoving || mapCharacter.currentPath !== path) {
this.broadcastMovement(character, false)
return
}
@ -118,8 +117,7 @@ export default class CharacterMove extends BaseEvent {
nextTile = path[i + 1]
if (!currentTile || !nextTile || !this.isValidStep(currentTile, nextTile)) {
this.logger.error('Invalid movement step detected')
break
return
}
// Update character rotation and position in a single operation
@ -139,7 +137,9 @@ export default class CharacterMove extends BaseEvent {
}
// Broadcast movement
this.broadcastMovement(character, true)
if (mapCharacter.isMoving && mapCharacter.currentPath === path) {
this.broadcastMovement(character, true)
}
// Apply movement delay between steps
if (i < path.length - 2) {
@ -181,7 +181,7 @@ export default class CharacterMove extends BaseEvent {
}
}
private broadcastMovement(character: any, isMoving: boolean): void {
private broadcastMovement(character: Character, isMoving: boolean): void {
this.io.in(character.map.id).emit(SocketEvent.MAP_CHARACTER_MOVE, {
characterId: character.id,
positionX: character.getPositionX(),