1
0
forked from noxious/server

#263 - Updated character movement to ts (WIP)

This commit is contained in:
Colin Kallemein 2025-01-02 23:36:56 +01:00
parent 887da447e0
commit ce80eb223c

View File

@ -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,6 +41,7 @@ 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
try {
for (let i = 0; i < path.length - 1; i++) { for (let i = 0; i < path.length - 1; i++) {
if (!zoneCharacter.isMoving || zoneCharacter.currentPath !== path) { if (!zoneCharacter.isMoving || zoneCharacter.currentPath !== path) {
return return
@ -50,7 +50,11 @@ export default class CharacterMove extends BaseEvent {
const [start, end] = [path[i], path[i + 1]] const [start, end] = [path[i], path[i + 1]]
character.rotation = CharacterService.calculateRotation(start.x, start.y, end.x, end.y) 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)) const zoneEventTile = await zoneEventTileRepository.getEventTileByZoneIdAndPosition(
character.zone.id,
Math.floor(end.x),
Math.floor(end.y)
)
if (zoneEventTile?.type === 'BLOCK') break if (zoneEventTile?.type === 'BLOCK') break
if (zoneEventTile?.type === 'TELEPORT' && zoneEventTile.teleport) { if (zoneEventTile?.type === 'TELEPORT' && zoneEventTile.teleport) {
@ -58,11 +62,9 @@ export default class CharacterMove extends BaseEvent {
break break
} }
// Update position first
character.positionX = end.x character.positionX = end.x
character.positionY = end.y character.positionY = end.y
// Then emit with the same properties
this.io.in(character.zone.id).emit('zone:character:move', { this.io.in(character.zone.id).emit('zone:character:move', {
characterId: character.id, characterId: character.id,
positionX: character.positionX, positionX: character.positionX,
@ -73,11 +75,12 @@ export default class CharacterMove extends BaseEvent {
await this.characterService.applyMovementDelay() await this.characterService.applyMovementDelay()
} }
} finally {
if (zoneCharacter.isMoving && zoneCharacter.currentPath === path) { if (zoneCharacter.isMoving && zoneCharacter.currentPath === path) {
this.finalizeMovement(zoneCharacter) this.finalizeMovement(zoneCharacter)
} }
} }
}
private async handleZoneEventTile(zoneEventTile: ZoneEventTileWithTeleport): Promise<void> { private async handleZoneEventTile(zoneEventTile: ZoneEventTileWithTeleport): Promise<void> {
const zoneCharacter = ZoneManager.getCharacterById(this.socket.characterId!) const zoneCharacter = ZoneManager.getCharacterById(this.socket.characterId!)