forked from noxious/server
#263 - Updated character movement to ts (WIP)
This commit is contained in:
parent
887da447e0
commit
ce80eb223c
@ -21,10 +21,9 @@ export default class CharacterMove extends BaseEvent {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// If already moving, cancel current movement and wait for it to fully stop
|
// If already moving, ignore new movement request
|
||||||
if (zoneCharacter.isMoving) {
|
if (zoneCharacter.isMoving) {
|
||||||
zoneCharacter.isMoving = false
|
return
|
||||||
await new Promise((resolve) => setTimeout(resolve, 100))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const path = await this.characterService.calculatePath(zoneCharacter.character, positionX, positionY)
|
const path = await this.characterService.calculatePath(zoneCharacter.character, positionX, positionY)
|
||||||
@ -42,40 +41,44 @@ export default class CharacterMove extends BaseEvent {
|
|||||||
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++) {
|
try {
|
||||||
if (!zoneCharacter.isMoving || zoneCharacter.currentPath !== path) {
|
for (let i = 0; i < path.length - 1; i++) {
|
||||||
return
|
if (!zoneCharacter.isMoving || zoneCharacter.currentPath !== path) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const [start, end] = [path[i], path[i + 1]]
|
||||||
|
character.rotation = CharacterService.calculateRotation(start.x, start.y, end.x, end.y)
|
||||||
|
|
||||||
|
const zoneEventTile = await zoneEventTileRepository.getEventTileByZoneIdAndPosition(
|
||||||
|
character.zone.id,
|
||||||
|
Math.floor(end.x),
|
||||||
|
Math.floor(end.y)
|
||||||
|
)
|
||||||
|
|
||||||
|
if (zoneEventTile?.type === 'BLOCK') break
|
||||||
|
if (zoneEventTile?.type === 'TELEPORT' && zoneEventTile.teleport) {
|
||||||
|
await this.handleZoneEventTile(zoneEventTile as ZoneEventTileWithTeleport)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
character.positionX = end.x
|
||||||
|
character.positionY = end.y
|
||||||
|
|
||||||
|
this.io.in(character.zone.id).emit('zone:character:move', {
|
||||||
|
characterId: character.id,
|
||||||
|
positionX: character.positionX,
|
||||||
|
positionY: character.positionY,
|
||||||
|
rotation: character.rotation,
|
||||||
|
isMoving: true
|
||||||
|
})
|
||||||
|
|
||||||
|
await this.characterService.applyMovementDelay()
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
const [start, end] = [path[i], path[i + 1]]
|
if (zoneCharacter.isMoving && zoneCharacter.currentPath === path) {
|
||||||
character.rotation = CharacterService.calculateRotation(start.x, start.y, end.x, end.y)
|
this.finalizeMovement(zoneCharacter)
|
||||||
|
|
||||||
const zoneEventTile = await zoneEventTileRepository.getEventTileByZoneIdAndPosition(character.zone.id, Math.floor(end.x), Math.floor(end.y))
|
|
||||||
|
|
||||||
if (zoneEventTile?.type === 'BLOCK') break
|
|
||||||
if (zoneEventTile?.type === 'TELEPORT' && zoneEventTile.teleport) {
|
|
||||||
await this.handleZoneEventTile(zoneEventTile as ZoneEventTileWithTeleport)
|
|
||||||
break
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update position first
|
|
||||||
character.positionX = end.x
|
|
||||||
character.positionY = end.y
|
|
||||||
|
|
||||||
// Then emit with the same properties
|
|
||||||
this.io.in(character.zone.id).emit('zone:character:move', {
|
|
||||||
characterId: character.id,
|
|
||||||
positionX: character.positionX,
|
|
||||||
positionY: character.positionY,
|
|
||||||
rotation: character.rotation,
|
|
||||||
isMoving: true
|
|
||||||
})
|
|
||||||
|
|
||||||
await this.characterService.applyMovementDelay()
|
|
||||||
}
|
|
||||||
|
|
||||||
if (zoneCharacter.isMoving && zoneCharacter.currentPath === path) {
|
|
||||||
this.finalizeMovement(zoneCharacter)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user