forked from noxious/server
#174: Refactor character manager into zoneManager for better DX, major refactor of time and weather system (data is stored in DB now instead of JSON file), npm update, npm format, many other improvements
This commit is contained in:
@ -1,9 +1,10 @@
|
||||
import { Zone } from '@prisma/client'
|
||||
import { Character, Zone } from '@prisma/client'
|
||||
import zoneRepository from '../repositories/zoneRepository'
|
||||
import ZoneCharacter from './zoneCharacter'
|
||||
|
||||
class LoadedZone {
|
||||
private readonly zone: Zone
|
||||
// private readonly npcs: ZoneNPC[] = []
|
||||
private characters: ZoneCharacter[] = []
|
||||
|
||||
constructor(zone: Zone) {
|
||||
this.zone = zone
|
||||
@ -13,6 +14,27 @@ class LoadedZone {
|
||||
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))
|
||||
|
||||
@ -27,20 +49,6 @@ class LoadedZone {
|
||||
|
||||
return grid
|
||||
}
|
||||
|
||||
/**
|
||||
* @TODO: Implement this
|
||||
* @param position
|
||||
*/
|
||||
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
|
||||
|
25
src/models/zoneCharacter.ts
Normal file
25
src/models/zoneCharacter.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import { Character } from '@prisma/client'
|
||||
import prisma from '../utilities/prisma'
|
||||
|
||||
class ZoneCharacter {
|
||||
public readonly character: Character
|
||||
public isMoving: boolean = false
|
||||
|
||||
constructor(character: Character) {
|
||||
this.character = character
|
||||
}
|
||||
|
||||
public async savePosition() {
|
||||
await prisma.character.update({
|
||||
where: { id: this.character.id },
|
||||
data: {
|
||||
positionX: this.character.positionX,
|
||||
positionY: this.character.positionY,
|
||||
rotation: this.character.rotation,
|
||||
zoneId: this.character.zoneId
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export default ZoneCharacter
|
Reference in New Issue
Block a user