2022-10-30 05:02:19 +01:00
|
|
|
import { AdvancedMap } from '../core';
|
2021-03-17 03:02:09 +01:00
|
|
|
import { IRoomObjectManager } from './IRoomObjectManager';
|
|
|
|
import { IRoomObjectController } from './object/IRoomObjectController';
|
|
|
|
import { RoomObject } from './object/RoomObject';
|
|
|
|
|
|
|
|
export class RoomObjectManager implements IRoomObjectManager
|
|
|
|
{
|
|
|
|
private _objects: AdvancedMap<number, IRoomObjectController>;
|
|
|
|
private _objectsPerType: AdvancedMap<string, AdvancedMap<number, IRoomObjectController>>;
|
|
|
|
|
|
|
|
constructor()
|
|
|
|
{
|
2022-03-03 01:21:30 +01:00
|
|
|
this._objects = new AdvancedMap();
|
|
|
|
this._objectsPerType = new AdvancedMap();
|
2021-03-17 03:02:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public dispose(): void
|
|
|
|
{
|
|
|
|
this.removeAllObjects();
|
|
|
|
}
|
|
|
|
|
|
|
|
public getObject(id: number): IRoomObjectController
|
|
|
|
{
|
|
|
|
const object = this._objects.getValue(id);
|
|
|
|
|
2022-10-30 05:02:19 +01:00
|
|
|
if (!object) return null;
|
2021-03-17 03:02:09 +01:00
|
|
|
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
public getObjectByIndex(index: number): IRoomObjectController
|
|
|
|
{
|
|
|
|
const object = this._objects.getWithIndex(index);
|
|
|
|
|
2022-10-30 05:02:19 +01:00
|
|
|
if (!object) return null;
|
2021-03-17 03:02:09 +01:00
|
|
|
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
public createObject(id: number, stateCount: number, type: string): IRoomObjectController
|
|
|
|
{
|
|
|
|
const object = new RoomObject(id, stateCount, type);
|
|
|
|
|
|
|
|
return this.addObject(id, type, object);
|
|
|
|
}
|
|
|
|
|
|
|
|
private addObject(id: number, type: string, object: IRoomObjectController): IRoomObjectController
|
|
|
|
{
|
2022-10-30 05:02:19 +01:00
|
|
|
if (this._objects.getValue(id))
|
2021-03-17 03:02:09 +01:00
|
|
|
{
|
|
|
|
object.dispose();
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._objects.add(id, object);
|
|
|
|
|
|
|
|
const typeMap = this.getTypeMap(type);
|
|
|
|
|
2022-10-30 05:02:19 +01:00
|
|
|
if (typeMap) typeMap.add(id, object);
|
2021-03-17 03:02:09 +01:00
|
|
|
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
public removeObject(id: number): void
|
|
|
|
{
|
|
|
|
const object = this._objects.remove(id);
|
|
|
|
|
2022-10-30 05:02:19 +01:00
|
|
|
if (object)
|
2021-03-17 03:02:09 +01:00
|
|
|
{
|
|
|
|
const typeMap = this.getTypeMap(object.type);
|
|
|
|
|
2022-10-30 05:02:19 +01:00
|
|
|
if (typeMap) typeMap.remove(object.id);
|
2021-03-17 03:02:09 +01:00
|
|
|
|
|
|
|
object.dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public removeAllObjects(): void
|
|
|
|
{
|
|
|
|
let i = 0;
|
|
|
|
|
2022-10-30 05:02:19 +01:00
|
|
|
while (i < this._objects.length)
|
2021-03-17 03:02:09 +01:00
|
|
|
{
|
|
|
|
const object = this._objects.getWithIndex(i);
|
|
|
|
|
2022-10-30 05:02:19 +01:00
|
|
|
if (object) object.dispose();
|
2021-03-17 03:02:09 +01:00
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._objects.reset();
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
|
2022-10-30 05:02:19 +01:00
|
|
|
while (i < this._objectsPerType.length)
|
2021-03-17 03:02:09 +01:00
|
|
|
{
|
|
|
|
const typeMap = this._objectsPerType.getWithIndex(i);
|
|
|
|
|
2022-10-30 05:02:19 +01:00
|
|
|
if (typeMap) typeMap.dispose();
|
2021-03-17 03:02:09 +01:00
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._objectsPerType.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
private getTypeMap(k: string, _arg_2: boolean = true): AdvancedMap<number, IRoomObjectController>
|
|
|
|
{
|
|
|
|
let existing = this._objectsPerType.getValue(k);
|
|
|
|
|
2022-10-30 05:02:19 +01:00
|
|
|
if (!existing && _arg_2)
|
2021-03-17 03:02:09 +01:00
|
|
|
{
|
|
|
|
existing = new AdvancedMap();
|
|
|
|
|
|
|
|
this._objectsPerType.add(k, existing);
|
|
|
|
}
|
|
|
|
|
|
|
|
return existing;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get objects(): AdvancedMap<number, IRoomObjectController>
|
|
|
|
{
|
|
|
|
return this._objects;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get totalObjects(): number
|
|
|
|
{
|
|
|
|
return this._objects.length;
|
|
|
|
}
|
2022-10-30 05:02:19 +01:00
|
|
|
}
|