import { Zone } from '@prisma/client' import zoneRepository from '../repositories/zoneRepository' class LoadedZone { private readonly zone: Zone // private readonly npcs: ZoneNPC[] = [] constructor(zone: Zone) { this.zone = zone } public getZone(): Zone { return this.zone } public async getGrid(): Promise { 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 } /** * @TODO: Implement this * @param position */ public async isPositionWalkable(position: { x: number; y: number }): Promise { 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