forked from noxious/server
Path walk improvements
This commit is contained in:
parent
fee4277b4f
commit
bf7f585270
@ -4,6 +4,7 @@ import prisma from '../utilities/prisma'
|
|||||||
class ZoneCharacter {
|
class ZoneCharacter {
|
||||||
public readonly character: Character
|
public readonly character: Character
|
||||||
public isMoving: boolean = false
|
public isMoving: boolean = false
|
||||||
|
public currentPath: Array<{ x: number; y: number }> | null = null;
|
||||||
|
|
||||||
constructor(character: Character) {
|
constructor(character: Character) {
|
||||||
this.character = character
|
this.character = character
|
||||||
|
@ -39,7 +39,7 @@ export default class CharacterConnectEvent {
|
|||||||
this.socket.characterId = character.id
|
this.socket.characterId = character.id
|
||||||
this.socket.emit('character:connect', character)
|
this.socket.emit('character:connect', character)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
this.handleError('Failed to connect character', error)
|
this.handleError('Failed to connect character', error) // @TODO : Make global error handler
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,7 +11,6 @@ import ZoneCharacter from '../../models/zoneCharacter'
|
|||||||
export default class CharacterMove {
|
export default class CharacterMove {
|
||||||
private readonly characterMoveService = new CharacterMoveService()
|
private readonly characterMoveService = new CharacterMoveService()
|
||||||
private readonly zoneEventTileService = new ZoneEventTileService()
|
private readonly zoneEventTileService = new ZoneEventTileService()
|
||||||
private nextPath = new Map<number, { x: number; y: number }[]>()
|
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly io: Server,
|
private readonly io: Server,
|
||||||
@ -29,24 +28,33 @@ export default class CharacterMove {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If already moving, cancel current movement and wait for it to fully stop
|
||||||
|
if (zoneCharacter.isMoving) {
|
||||||
|
zoneCharacter.isMoving = false
|
||||||
|
await new Promise(resolve => setTimeout(resolve, 100))
|
||||||
|
}
|
||||||
|
|
||||||
const path = await this.characterMoveService.calculatePath(zoneCharacter.character, positionX, positionY)
|
const path = await this.characterMoveService.calculatePath(zoneCharacter.character, positionX, positionY)
|
||||||
if (!path) {
|
if (!path) {
|
||||||
this.io.in(zoneCharacter.character.zoneId.toString()).emit('character:moveError', 'No valid path found')
|
this.io.in(zoneCharacter.character.zoneId.toString()).emit('character:moveError', 'No valid path found')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!zoneCharacter.isMoving) {
|
// Start new movement
|
||||||
zoneCharacter.isMoving = true
|
zoneCharacter.isMoving = true
|
||||||
await this.moveAlongPath(zoneCharacter, path)
|
zoneCharacter.currentPath = path // Add this property to ZoneCharacter class
|
||||||
} else {
|
await this.moveAlongPath(zoneCharacter, path)
|
||||||
this.nextPath.set(zoneCharacter.character.id, path)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async moveAlongPath(zoneCharacter: ZoneCharacter, path: Array<{ x: number; y: number }>): Promise<void> {
|
private async moveAlongPath(zoneCharacter: ZoneCharacter, path: Array<{ x: number; y: number }>): Promise<void> {
|
||||||
const { character } = zoneCharacter
|
const { character } = zoneCharacter
|
||||||
|
|
||||||
for (let i = 0; i < path.length - 1; i++) {
|
for (let i = 0; i < path.length - 1; i++) {
|
||||||
|
// Exit if movement was cancelled or interrupted
|
||||||
|
if (!zoneCharacter.isMoving || zoneCharacter.currentPath !== path) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const [start, end] = [path[i], path[i + 1]]
|
const [start, end] = [path[i], path[i + 1]]
|
||||||
character.rotation = Rotation.calculate(start.x, start.y, end.x, end.y)
|
character.rotation = Rotation.calculate(start.x, start.y, end.x, end.y)
|
||||||
|
|
||||||
@ -70,11 +78,8 @@ export default class CharacterMove {
|
|||||||
await this.characterMoveService.applyMovementDelay()
|
await this.characterMoveService.applyMovementDelay()
|
||||||
}
|
}
|
||||||
|
|
||||||
const nextPath = this.nextPath.get(character.id)
|
// Only finalize if this path wasn't interrupted
|
||||||
if (nextPath) {
|
if (zoneCharacter.isMoving && zoneCharacter.currentPath === path) {
|
||||||
this.nextPath.delete(character.id)
|
|
||||||
await this.moveAlongPath(zoneCharacter, nextPath)
|
|
||||||
} else {
|
|
||||||
this.finalizeMovement(zoneCharacter)
|
this.finalizeMovement(zoneCharacter)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user