Pathfinding spam fix, persistence character location without saving every step
This commit is contained in:
parent
293b2be919
commit
592496861a
@ -6,6 +6,9 @@ import prisma from '../../utilities/prisma'
|
|||||||
import { ZoneEventTile, ZoneEventTileTeleport } from '@prisma/client'
|
import { ZoneEventTile, ZoneEventTileTeleport } from '@prisma/client'
|
||||||
import Rotation from '../../utilities/character/rotation'
|
import Rotation from '../../utilities/character/rotation'
|
||||||
import logger from '../../utilities/logger'
|
import logger from '../../utilities/logger'
|
||||||
|
import CharacterManager from '../../managers/characterManager'
|
||||||
|
import zoneManager from '../../managers/zoneManager'
|
||||||
|
import ZoneManager from '../../managers/zoneManager'
|
||||||
|
|
||||||
type ZoneEventTileWithTeleport = ZoneEventTile & {
|
type ZoneEventTileWithTeleport = ZoneEventTile & {
|
||||||
teleport: ZoneEventTileTeleport
|
teleport: ZoneEventTileTeleport
|
||||||
@ -29,22 +32,32 @@ export default class CharacterMove {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async handleCharacterMove({ positionX, positionY }: { positionX: number; positionY: number }): Promise<void> {
|
private async handleCharacterMove({ positionX, positionY }: { positionX: number; positionY: number }): Promise<void> {
|
||||||
const { character } = this.socket
|
let character = CharacterManager.getCharacterFromSocket(this.socket);
|
||||||
if (!character) {
|
if (!character) {
|
||||||
logger.error('character:move error', 'Character not found')
|
logger.error('character:move error', 'Character not found')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(!character) {
|
||||||
|
logger.error('character:move error', 'character has not been initialized?');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const path = await this.characterMoveService.calculatePath(character, positionX, positionY)
|
const path = await this.characterMoveService.calculatePath(character, positionX, positionY)
|
||||||
if (!path) {
|
if (!path) {
|
||||||
this.io.in(character.zoneId.toString()).emit('character:moveError', 'No valid path found')
|
this.io.in(character.zoneId.toString()).emit('character:moveError', 'No valid path found')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(!character.isMoving && character.resetMovement) {
|
||||||
|
character.resetMovement = false;
|
||||||
|
}
|
||||||
if (character.isMoving && !character.resetMovement) {
|
if (character.isMoving && !character.resetMovement) {
|
||||||
character.resetMovement = true
|
character.resetMovement = true
|
||||||
this.nextPath[character.id] = path
|
this.nextPath[character.id] = path
|
||||||
} else {
|
}
|
||||||
|
if(!character.isMoving && !character.resetMovement) {
|
||||||
|
character.isMoving = true;
|
||||||
await this.moveAlongPath(character, path)
|
await this.moveAlongPath(character, path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -58,16 +71,9 @@ export default class CharacterMove {
|
|||||||
// break
|
// break
|
||||||
// }
|
// }
|
||||||
|
|
||||||
if (character.isMoving && character.resetMovement) {
|
if (character.resetMovement) {
|
||||||
character.isMoving = false
|
|
||||||
character.resetMovement = false
|
|
||||||
const nextPath = this.nextPath[character.id]
|
|
||||||
this.moveAlongPath(character, nextPath)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!character.isMoving) {
|
break
|
||||||
character.isMoving = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
character.rotation = Rotation.calculate(start.x, start.y, end.x, end.y)
|
character.rotation = Rotation.calculate(start.x, start.y, end.x, end.y)
|
||||||
@ -98,13 +104,18 @@ export default class CharacterMove {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.characterMoveService.updatePosition(character, end)
|
this.characterMoveService.updatePosition(character, end)
|
||||||
this.io.in(character.zoneId.toString()).emit('character:move', character)
|
this.io.in(character.zoneId.toString()).emit('character:move', character)
|
||||||
|
|
||||||
await this.characterMoveService.applyMovementDelay()
|
await this.characterMoveService.applyMovementDelay()
|
||||||
}
|
}
|
||||||
|
|
||||||
this.finalizeMovement(character)
|
if(character.resetMovement) {
|
||||||
|
character.resetMovement = false;
|
||||||
|
await this.moveAlongPath(character, this.nextPath[character.id]);
|
||||||
|
} else {
|
||||||
|
this.finalizeMovement(character)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async handleZoneEventTile(zoneEventTile: ZoneEventTileWithTeleport): Promise<void> {
|
private async handleZoneEventTile(zoneEventTile: ZoneEventTileWithTeleport): Promise<void> {
|
||||||
@ -122,7 +133,7 @@ export default class CharacterMove {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private finalizeMovement(character: ExtendedCharacter): void {
|
private finalizeMovement(character: ExtendedCharacter): void {
|
||||||
character.isMoving = false
|
character.isMoving = false;
|
||||||
this.io.in(character.zoneId.toString()).emit('character:move', character)
|
this.io.in(character.zoneId.toString()).emit('character:move', character)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import { ExtendedCharacter } from '../utilities/types'
|
import { ExtendedCharacter, TSocket } from '../utilities/types'
|
||||||
import { Zone } from '@prisma/client'
|
import { Zone } from '@prisma/client'
|
||||||
|
import prisma from '../utilities/prisma'
|
||||||
|
|
||||||
class CharacterManager {
|
class CharacterManager {
|
||||||
private characters!: ExtendedCharacter[];
|
private characters!: ExtendedCharacter[];
|
||||||
@ -12,10 +13,27 @@ class CharacterManager {
|
|||||||
this.characters = [...this.characters, character]
|
this.characters = [...this.characters, character]
|
||||||
}
|
}
|
||||||
|
|
||||||
public removeCharacter(character: ExtendedCharacter) {
|
public async removeCharacter(character: ExtendedCharacter) {
|
||||||
|
await prisma.character.update({
|
||||||
|
where: { id: character.id },
|
||||||
|
data: {
|
||||||
|
positionX: character.positionX,
|
||||||
|
positionY: character.positionY,
|
||||||
|
rotation: character.rotation,
|
||||||
|
zoneId: character.zoneId
|
||||||
|
}
|
||||||
|
})
|
||||||
this.characters = this.characters.filter(x => x.id !== character.id);
|
this.characters = this.characters.filter(x => x.id !== character.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public getCharacter(characterId: number) {
|
||||||
|
return this.characters.find((x) => x.id === characterId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public getCharacterFromSocket(socket: TSocket) {
|
||||||
|
return this.characters.find((x) => x.id === socket?.character?.id);
|
||||||
|
}
|
||||||
|
|
||||||
public getCharactersInZone(zone: Zone) {
|
public getCharactersInZone(zone: Zone) {
|
||||||
return this.characters.filter(x => x.zoneId === zone.id);
|
return this.characters.filter(x => x.zoneId === zone.id);
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ import Rotation from '../../utilities/character/rotation'
|
|||||||
import logger from '../../utilities/logger'
|
import logger from '../../utilities/logger'
|
||||||
|
|
||||||
export class CharacterMoveService {
|
export class CharacterMoveService {
|
||||||
public async updatePosition(character: ExtendedCharacter, position: { x: number; y: number }, newZoneId?: number): Promise<void> {
|
public updatePosition(character: ExtendedCharacter, position: { x: number; y: number }, newZoneId?: number) {
|
||||||
Object.assign(character, {
|
Object.assign(character, {
|
||||||
positionX: position.x,
|
positionX: position.x,
|
||||||
positionY: position.y,
|
positionY: position.y,
|
||||||
@ -14,15 +14,15 @@ export class CharacterMoveService {
|
|||||||
zoneId: newZoneId || character.zoneId
|
zoneId: newZoneId || character.zoneId
|
||||||
})
|
})
|
||||||
|
|
||||||
await prisma.character.update({
|
// await prisma.character.update({
|
||||||
where: { id: character.id },
|
// where: { id: character.id },
|
||||||
data: {
|
// data: {
|
||||||
positionX: position.x,
|
// positionX: position.x,
|
||||||
positionY: position.y,
|
// positionY: position.y,
|
||||||
rotation: character.rotation,
|
// rotation: character.rotation,
|
||||||
zoneId: newZoneId
|
// zoneId: newZoneId
|
||||||
}
|
// }
|
||||||
})
|
// })
|
||||||
}
|
}
|
||||||
|
|
||||||
public async calculatePath(character: ExtendedCharacter, targetX: number, targetY: number): Promise<Array<{ x: number; y: number }> | null> {
|
public async calculatePath(character: ExtendedCharacter, targetX: number, targetY: number): Promise<Array<{ x: number; y: number }> | null> {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user