forked from noxious/server
59 lines
1.5 KiB
TypeScript
59 lines
1.5 KiB
TypeScript
import MapCharacter from './mapCharacter'
|
|
|
|
import { UUID } from '#application/types'
|
|
import { Character } from '#entities/character'
|
|
import { Map } from '#entities/map'
|
|
import mapEventTileRepository from '#repositories/mapEventTileRepository'
|
|
|
|
class LoadedMap {
|
|
private readonly map: Map
|
|
private characters: MapCharacter[] = []
|
|
|
|
constructor(map: Map) {
|
|
this.map = map
|
|
}
|
|
|
|
public getMap(): Map {
|
|
return this.map
|
|
}
|
|
|
|
public addCharacter(character: Character) {
|
|
const mapCharacter = new MapCharacter(character)
|
|
this.characters.push(mapCharacter)
|
|
}
|
|
|
|
public async removeCharacter(id: UUID) {
|
|
const mapCharacter = this.getCharacterById(id)
|
|
if (mapCharacter) {
|
|
await mapCharacter.savePosition()
|
|
this.characters = this.characters.filter((c) => c.character.id !== id)
|
|
}
|
|
}
|
|
|
|
public getCharacterById(id: UUID): MapCharacter | undefined {
|
|
return this.characters.find((c) => c.character.id === id)
|
|
}
|
|
|
|
public getCharactersInMap(): MapCharacter[] {
|
|
console.log(this.characters)
|
|
return this.characters
|
|
}
|
|
|
|
public async getGrid(): Promise<number[][]> {
|
|
let grid: number[][] = Array.from({ length: this.map.height }, () => Array.from({ length: this.map.width }, () => 0))
|
|
|
|
const eventTiles = await mapEventTileRepository.getAll(this.map.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 LoadedMap
|