improved zone manager
This commit is contained in:
@ -1,19 +1,23 @@
|
||||
import { Zone } from "@prisma/client";
|
||||
import { LoadedZoneEntity } from "./entities/LoadedZoneEntity";
|
||||
import {Character, Zone} from "@prisma/client";
|
||||
import ZoneRepository from "./repositories/zone";
|
||||
|
||||
interface ILoadedZone {
|
||||
zone: Zone;
|
||||
characters: Character[];
|
||||
}
|
||||
|
||||
class ZoneManager {
|
||||
private static _instance: ZoneManager;
|
||||
private _loadedZones: LoadedZoneEntity[] = [];
|
||||
private static instance: ZoneManager;
|
||||
private loadedZones: ILoadedZone[] = [];
|
||||
|
||||
private constructor() {}
|
||||
|
||||
// Singleton instance getter
|
||||
public static getInstance(): ZoneManager {
|
||||
if (!ZoneManager._instance) {
|
||||
ZoneManager._instance = new ZoneManager();
|
||||
if (!ZoneManager.instance) {
|
||||
ZoneManager.instance = new ZoneManager();
|
||||
}
|
||||
return ZoneManager._instance;
|
||||
return ZoneManager.instance;
|
||||
}
|
||||
|
||||
// Method to initialize zone loading
|
||||
@ -25,28 +29,31 @@ class ZoneManager {
|
||||
this.loadZone(zone);
|
||||
}
|
||||
|
||||
console.log('[✅] ZoneManager loaded');
|
||||
console.log('[✅] Zone manager loaded');
|
||||
}
|
||||
|
||||
// Method to handle individual zone loading
|
||||
public loadZone(zone: Zone) {
|
||||
const loadedZone = new LoadedZoneEntity(zone);
|
||||
this._loadedZones.push(loadedZone);
|
||||
this.loadedZones.push({
|
||||
zone,
|
||||
characters: []
|
||||
});
|
||||
console.log(`[✅] Zone ID ${zone.id} loaded`);
|
||||
}
|
||||
|
||||
// Method to handle individual zone unloading
|
||||
public unloadZone(zoneId: number) {
|
||||
const index = this._loadedZones.findIndex(loadedZone => loadedZone.getZone().id === zoneId);
|
||||
if (index > -1) {
|
||||
this._loadedZones.splice(index, 1);
|
||||
console.log(`[❌] Zone ID ${zoneId} unloaded`);
|
||||
}
|
||||
this.loadedZones = this.loadedZones.filter((loadedZone) => {
|
||||
return loadedZone.zone.id !== zoneId;
|
||||
});
|
||||
console.log(`[❌] Zone ID ${zoneId} unloaded`);
|
||||
}
|
||||
|
||||
// Getter for loaded zones
|
||||
public getLoadedZones(): LoadedZoneEntity[] {
|
||||
return this._loadedZones;
|
||||
public getLoadedZones(): Zone[] {
|
||||
return this.loadedZones.map((loadedZone) => {
|
||||
return loadedZone.zone;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,37 +0,0 @@
|
||||
import { PlayerEntity } from "./PlayerEntity";
|
||||
import {Zone} from "@prisma/client";
|
||||
|
||||
export class LoadedZoneEntity
|
||||
{
|
||||
zone: Zone;
|
||||
players: Map<string, PlayerEntity>;
|
||||
|
||||
constructor(zone: Zone) {
|
||||
this.zone = zone;
|
||||
this.players = new Map();
|
||||
}
|
||||
|
||||
getZone() {
|
||||
return this.zone;
|
||||
}
|
||||
|
||||
addPlayer(player: PlayerEntity) {
|
||||
this.players.set(player.id, player);
|
||||
}
|
||||
|
||||
removePlayer(player: PlayerEntity) {
|
||||
this.players.delete(player.id);
|
||||
}
|
||||
|
||||
getPlayer(playerId: string) {
|
||||
return this.players.get(playerId);
|
||||
}
|
||||
|
||||
getPlayers() {
|
||||
return Array.from(this.players.values());
|
||||
}
|
||||
|
||||
getPlayersCount() {
|
||||
return this.players.size;
|
||||
}
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
export class PlayerEntity
|
||||
{
|
||||
id: string;
|
||||
name: string;
|
||||
position: { x: number, y: number };
|
||||
|
||||
constructor(id: string, name: string, position: { x: number, y: number }) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
move(x: number, y: number) {
|
||||
this.position.x = x;
|
||||
this.position.y = y;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user