#174: Refactor character manager into zoneManager for better DX, major refactor of time and weather system (data is stored in DB now instead of JSON file), npm update, npm format, many other improvements
This commit is contained in:
@ -1,43 +1,57 @@
|
||||
import { ExtendedCharacter } from '../../utilities/types'
|
||||
import { AStar } from '../../utilities/character/aStar'
|
||||
import ZoneManager from '../../managers/zoneManager'
|
||||
import Rotation from '../../utilities/character/rotation'
|
||||
import { gameLogger } from '../../utilities/logger'
|
||||
import { Character } from '@prisma/client'
|
||||
|
||||
interface Position {
|
||||
x: number
|
||||
y: number
|
||||
}
|
||||
|
||||
export class CharacterMoveService {
|
||||
public updatePosition(character: ExtendedCharacter, position: { x: number; y: number }, newZoneId?: number) {
|
||||
private static readonly MOVEMENT_DELAY_MS = 250
|
||||
|
||||
public updatePosition(character: Character, position: Position, newZoneId?: number): void {
|
||||
if (!this.isValidPosition(position)) {
|
||||
gameLogger.error(`Invalid position coordinates: ${position.x}, ${position.y}`)
|
||||
}
|
||||
|
||||
Object.assign(character, {
|
||||
positionX: position.x,
|
||||
positionY: position.y,
|
||||
rotation: Rotation.calculate(character.positionX, character.positionY, position.x, position.y),
|
||||
zoneId: newZoneId || character.zoneId
|
||||
zoneId: newZoneId ?? character.zoneId
|
||||
})
|
||||
|
||||
// await prisma.character.update({
|
||||
// where: { id: character.id },
|
||||
// data: {
|
||||
// positionX: position.x,
|
||||
// positionY: position.y,
|
||||
// rotation: character.rotation,
|
||||
// zoneId: newZoneId
|
||||
// }
|
||||
// })
|
||||
}
|
||||
|
||||
public async calculatePath(character: ExtendedCharacter, targetX: number, targetY: number): Promise<Array<{ x: number; y: number }> | null> {
|
||||
const grid = await ZoneManager.getZoneById(character.zoneId)?.getGrid()
|
||||
public async calculatePath(character: Character, targetX: number, targetY: number): Promise<Position[] | null> {
|
||||
const zone = ZoneManager.getZoneById(character.zoneId)
|
||||
const grid = await zone?.getGrid()
|
||||
|
||||
if (!grid?.length) {
|
||||
gameLogger.error('character:move error', 'Grid not found or empty')
|
||||
return null
|
||||
}
|
||||
|
||||
const start = { x: Math.floor(character.positionX), y: Math.floor(character.positionY) }
|
||||
const end = { x: Math.floor(targetX), y: Math.floor(targetY) }
|
||||
const start: Position = {
|
||||
x: Math.floor(character.positionX),
|
||||
y: Math.floor(character.positionY)
|
||||
}
|
||||
|
||||
const end: Position = {
|
||||
x: Math.floor(targetX),
|
||||
y: Math.floor(targetY)
|
||||
}
|
||||
|
||||
return AStar.findPath(start, end, grid)
|
||||
}
|
||||
|
||||
public async applyMovementDelay(): Promise<void> {
|
||||
await new Promise((resolve) => setTimeout(resolve, 250)) // 250ms delay between steps
|
||||
await new Promise((resolve) => setTimeout(resolve, CharacterMoveService.MOVEMENT_DELAY_MS))
|
||||
}
|
||||
|
||||
private isValidPosition(position: Position): boolean {
|
||||
return Number.isFinite(position.x) && Number.isFinite(position.y) && position.x >= 0 && position.y >= 0
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user