Added models to store extra data in RAM
This commit is contained in:
60
src/models/zone/loadedZone.ts
Normal file
60
src/models/zone/loadedZone.ts
Normal 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
|
14
src/models/zone/zoneCharacter.ts
Normal file
14
src/models/zone/zoneCharacter.ts
Normal 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
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user