1
0
forked from noxious/server

Many many more improvements

This commit is contained in:
2024-12-28 19:21:15 +01:00
parent bd3bf6f580
commit 918f5141fc
27 changed files with 178 additions and 191 deletions

View File

@ -1,5 +1,5 @@
import { BaseService } from '#application/base/baseService'
import config from '#application/config'
import { gameLogger } from '#application/logger'
import { Character } from '#entities/character'
import { Zone } from '#entities/zone'
import ZoneManager from '#managers/zoneManager'
@ -9,40 +9,25 @@ import ZoneRepository from '#repositories/zoneRepository'
type Position = { x: number; y: number }
export type Node = Position & { parent?: Node; g: number; h: number; f: number }
export class CharacterService {
class CharacterService extends BaseService {
private readonly MOVEMENT_DELAY_MS = 250
private readonly DIRECTIONS = [
{ x: 0, y: -1 }, // Up
{ x: 0, y: 1 }, // Down
{ x: -1, y: 0 }, // Left
{ x: 1, y: 0 }, // Right
{ x: -1, y: -1 },
{ x: -1, y: 1 },
{ x: 1, y: -1 },
{ x: 1, y: 1 }
{ x: -1, y: -1 }, // Up left
{ x: -1, y: 1 }, // Up right
{ x: 1, y: -1 }, // Down left
{ x: 1, y: 1 } // Down right
]
public async updateCharacterPosition(id: number, positionX: number, positionY: number, rotation: number, zoneId: number) {
const character = await CharacterRepository.getById(id)
if (!character) return null
character
.setPositionX(positionX)
.setPositionY(positionY)
.setRotation(rotation)
.setZone((await ZoneRepository.getById(zoneId)) as Zone)
await character.save()
return character
}
public async calculatePath(character: Character, targetX: number, targetY: number): Promise<Position[] | null> {
const zone = ZoneManager.getZoneById(character.zone!.id)
const grid = await zone?.getGrid()
if (!grid?.length) {
gameLogger.error('character:move error', 'Grid not found or empty')
this.logger.error('character:move error: Grid not found or empty')
return null
}
@ -144,3 +129,5 @@ export class CharacterService {
return path
}
}
export default new CharacterService()