forked from noxious/server
use camelcase file names from now on...
This commit is contained in:
125
src/managers/zoneManager.ts
Normal file
125
src/managers/zoneManager.ts
Normal file
@ -0,0 +1,125 @@
|
||||
import { Character, Zone } from '@prisma/client'
|
||||
import ZoneRepository from '../repositories/zoneRepository'
|
||||
import ZoneService from '../services/zoneService'
|
||||
import zoneRepository from '../repositories/zoneRepository'
|
||||
import logger from '../utilities/logger'
|
||||
|
||||
type TLoadedZone = {
|
||||
zone: Zone
|
||||
characters: Character[]
|
||||
grid: number[][]
|
||||
}
|
||||
|
||||
class ZoneManager {
|
||||
private loadedZones: TLoadedZone[] = []
|
||||
|
||||
// Method to initialize zone manager
|
||||
public async boot() {
|
||||
if (!(await ZoneRepository.getById(1))) {
|
||||
const zoneService = new ZoneService()
|
||||
await zoneService.createDemoZone()
|
||||
}
|
||||
|
||||
const zones = await ZoneRepository.getAll()
|
||||
|
||||
for (const zone of zones) {
|
||||
await this.loadZone(zone)
|
||||
}
|
||||
|
||||
logger.info('Zone manager loaded')
|
||||
}
|
||||
|
||||
// Method to handle individual zone loading
|
||||
public async loadZone(zone: Zone) {
|
||||
const grid = await this.getGrid(zone.id) // Create the grid for the zone
|
||||
this.loadedZones.push({
|
||||
zone,
|
||||
characters: [],
|
||||
grid
|
||||
})
|
||||
logger.info(`Zone ID ${zone.id} loaded`)
|
||||
}
|
||||
|
||||
// Method to handle individual zone unloading
|
||||
public unloadZone(zoneId: number) {
|
||||
this.loadedZones = this.loadedZones.filter((loadedZone) => {
|
||||
return loadedZone.zone.id !== zoneId
|
||||
})
|
||||
logger.info(`Zone ID ${zoneId} unloaded`)
|
||||
}
|
||||
|
||||
// Getter for loaded zones
|
||||
public getLoadedZones(): TLoadedZone[] {
|
||||
return this.loadedZones
|
||||
}
|
||||
|
||||
// Check if position is walkable
|
||||
private isPositionWalkable(zoneId: number, x: number, y: number): boolean {
|
||||
const loadedZone = this.loadedZones.find((lz) => lz.zone.id === zoneId)
|
||||
return loadedZone ? loadedZone.grid[y][x] === 0 : false
|
||||
}
|
||||
|
||||
public addCharacterToZone(zoneId: number, character: Character) {
|
||||
const loadedZone = this.loadedZones.find((loadedZone) => {
|
||||
return loadedZone.zone.id === zoneId
|
||||
})
|
||||
if (loadedZone && this.isPositionWalkable(zoneId, character.position_x, character.position_y)) {
|
||||
loadedZone.characters.push(character)
|
||||
}
|
||||
}
|
||||
|
||||
public removeCharacterFromZone(zoneId: number, character: Character) {
|
||||
const loadedZone = this.loadedZones.find((loadedZone) => {
|
||||
return loadedZone.zone.id === zoneId
|
||||
})
|
||||
if (loadedZone) {
|
||||
loadedZone.characters = loadedZone.characters.filter((loadedCharacter) => {
|
||||
return loadedCharacter.id !== character.id
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
public updateCharacterInZone(zoneId: number, character: Character) {
|
||||
const loadedZone = this.loadedZones.find((loadedZone) => {
|
||||
return loadedZone.zone.id === zoneId
|
||||
})
|
||||
if (loadedZone) {
|
||||
const characterIndex = loadedZone.characters.findIndex((loadedCharacter) => {
|
||||
return loadedCharacter.id === character.id
|
||||
})
|
||||
if (characterIndex !== -1) {
|
||||
loadedZone.characters[characterIndex] = character
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public getCharactersInZone(zoneId: number): Character[] {
|
||||
const loadedZone = this.loadedZones.find((loadedZone) => {
|
||||
return loadedZone.zone.id === zoneId
|
||||
})
|
||||
return loadedZone ? loadedZone.characters : []
|
||||
}
|
||||
|
||||
public async getGrid(zoneId: number): Promise<number[][]> {
|
||||
const zone = this.loadedZones.find((z) => z.zone.id === zoneId)
|
||||
if (zone) return zone.grid
|
||||
|
||||
const loadedZone = await ZoneRepository.getById(zoneId)
|
||||
if (!loadedZone) return []
|
||||
|
||||
let grid: number[][] = Array.from({ length: loadedZone.height }, () => Array.from({ length: loadedZone.width }, () => 0))
|
||||
|
||||
const eventTiles = await zoneRepository.getEventTiles(zoneId)
|
||||
|
||||
// Set the grid values based on the event tiles, these are strings
|
||||
eventTiles.forEach((eventTile) => {
|
||||
if (eventTile.type === 'BLOCK') {
|
||||
grid[eventTile.position_y][eventTile.position_x] = 1
|
||||
}
|
||||
})
|
||||
|
||||
return grid
|
||||
}
|
||||
}
|
||||
|
||||
export default new ZoneManager()
|
Reference in New Issue
Block a user