1
0
forked from noxious/server
noxious_server/src/models/loadedZone.ts
2024-12-27 02:50:11 +01:00

57 lines
1.5 KiB
TypeScript

import ZoneCharacter from './zoneCharacter'
import { Character } from '#entities/character'
import { Zone } from '#entities/zone'
import zoneEventTileRepository from '#repositories/zoneEventTileRepository'
class LoadedZone {
private readonly zone: Zone
private characters: ZoneCharacter[] = []
constructor(zone: Zone) {
this.zone = zone
}
public getZone(): Zone {
return this.zone
}
public addCharacter(character: Character) {
const zoneCharacter = new ZoneCharacter(character)
this.characters.push(zoneCharacter)
}
public async removeCharacter(id: number) {
const zoneCharacter = this.getCharacterById(id)
if (zoneCharacter) {
await zoneCharacter.savePosition()
this.characters = this.characters.filter((c) => c.character.id !== id)
}
}
public getCharacterById(id: number): ZoneCharacter | undefined {
return this.characters.find((c) => c.character.id === id)
}
public getCharactersInZone(): ZoneCharacter[] {
return this.characters
}
public async getGrid(): Promise<number[][]> {
let grid: number[][] = Array.from({ length: this.zone.height }, () => Array.from({ length: this.zone.width }, () => 0))
const eventTiles = await zoneEventTileRepository.getAll(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
}
}
export default LoadedZone