Added models to store extra data in RAM

This commit is contained in:
2024-09-09 18:31:12 +02:00
parent 32b390bb20
commit 636aa6cc55
11 changed files with 116 additions and 190 deletions

View File

@ -0,0 +1,60 @@
import { Character, Tile, Zone } from '@prisma/client'
import ZoneCharacter from './zoneCharacter'
import zoneRepository from '../../repositories/zoneRepository'
import { ExtendedCharacter } from '../../utilities/types'
import ZoneManager from '../../managers/zoneManager'
class LoadedZone {
private readonly zone: Zone
private characters: ZoneCharacter[] = []
// private readonly npcs: ZoneNPC[] = []
private readonly grid: number[][] = []
constructor(zone: Zone) {
this.zone = zone
}
public getZone(): Zone {
return this.zone
}
public getCharacters(): ZoneCharacter[] {
return this.characters
}
public addCharacter(character: Character): void {
const zoneCharacter = new ZoneCharacter(character)
this.characters.push(zoneCharacter)
}
public removeCharacter(character: Character): void {
this.characters = this.characters.filter((zoneCharacter) => zoneCharacter.getCharacter().id !== character.id)
}
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

View File

@ -0,0 +1,14 @@
import { Character } from '@prisma/client'
export default class ZoneCharacter {
private readonly character: Character
private isMoving: boolean = false
constructor(character: Character) {
this.character = character
}
public getCharacter(): Character {
return this.character
}
}