Moved model into parent folder, npm update, small auth improvement

This commit is contained in:
2024-09-14 17:29:26 +02:00
parent b439421516
commit 48155b8d66
4 changed files with 27 additions and 90 deletions

49
src/models/loadedZone.ts Normal file
View File

@ -0,0 +1,49 @@
import { Zone } from '@prisma/client'
import zoneRepository from '../repositories/zoneRepository'
import characterManager from '../managers/characterManager'
import { ExtendedCharacter } from '../utilities/types'
class LoadedZone {
private readonly zone: Zone
// private readonly npcs: ZoneNPC[] = []
private readonly grid: number[][] = []
constructor(zone: Zone) {
this.zone = zone
}
public getZone(): Zone {
return this.zone
}
public getCharacters(): ExtendedCharacter[] {
return characterManager.getCharactersInZone(this.zone);
}
public async getGrid(): Promise<number[][]> {
let grid: number[][] = Array.from({ length: this.zone.height }, () => Array.from({ length: this.zone.width }, () => 0))
const eventTiles = await zoneRepository.getEventTiles(this.zone.id)
// Set the grid values based on the event tiles, these are strings
eventTiles.forEach((eventTile) => {
if (eventTile.type === 'BLOCK') {
grid[eventTile.positionY][eventTile.positionX] = 1
}
})
return grid
}
public async isPositionWalkable(position: { x: number; y: number }): Promise<boolean> {
const grid = await this.getGrid()
if (!grid?.length) return false
const gridX = Math.floor(position.x)
const gridY = Math.floor(position.y)
return grid[gridY]?.[gridX] === 1 || grid[gridY]?.[Math.ceil(position.x)] === 1 || grid[Math.ceil(position.y)]?.[gridX] === 1 || grid[Math.ceil(position.y)]?.[Math.ceil(position.x)] === 1
}
}
export default LoadedZone