nitro-renderer/packages/room/src/RoomObjectManager.ts

131 lines
2.9 KiB
TypeScript
Raw Normal View History

2024-03-20 02:53:17 +01:00
import { IAdvancedMap, IRoomObjectController, IRoomObjectManager } from '@nitrots/api';
import { AdvancedMap } from '@nitrots/utils';
2022-10-30 07:08:37 +01:00
import { RoomObject } from './object';
2021-03-17 03:02:09 +01:00
export class RoomObjectManager implements IRoomObjectManager
{
2022-11-03 06:39:10 +01:00
private _objects: IAdvancedMap<number, IRoomObjectController>;
2024-03-20 02:53:17 +01:00
private _objectsPerType: IAdvancedMap<string, IAdvancedMap<number, IRoomObjectController>>;
2021-03-17 03:02:09 +01:00
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-11-08 22:50:16 +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-11-08 22:50:16 +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-11-08 22:50:16 +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-11-08 22:50:16 +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-11-08 22:50:16 +01:00
if(object)
2021-03-17 03:02:09 +01:00
{
const typeMap = this.getTypeMap(object.type);
2022-11-08 22:50:16 +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-11-08 22:50:16 +01:00
while(i < this._objects.length)
2021-03-17 03:02:09 +01:00
{
const object = this._objects.getWithIndex(i);
2022-11-08 22:50:16 +01:00
if(object) object.dispose();
2021-03-17 03:02:09 +01:00
i++;
}
this._objects.reset();
i = 0;
2022-11-08 22:50:16 +01:00
while(i < this._objectsPerType.length)
2021-03-17 03:02:09 +01:00
{
const typeMap = this._objectsPerType.getWithIndex(i);
2022-11-08 22:50:16 +01:00
if(typeMap) typeMap.dispose();
2021-03-17 03:02:09 +01:00
i++;
}
this._objectsPerType.reset();
}
2022-11-03 06:39:10 +01:00
private getTypeMap(k: string, _arg_2: boolean = true): IAdvancedMap<number, IRoomObjectController>
2021-03-17 03:02:09 +01:00
{
let existing = this._objectsPerType.getValue(k);
2022-11-08 22:50:16 +01:00
if(!existing && _arg_2)
2021-03-17 03:02:09 +01:00
{
existing = new AdvancedMap();
this._objectsPerType.add(k, existing);
}
return existing;
}
2022-11-03 06:39:10 +01:00
public get objects(): IAdvancedMap<number, IRoomObjectController>
2021-03-17 03:02:09 +01:00
{
return this._objects;
}
public get totalObjects(): number
{
return this._objects.length;
}
2022-10-30 05:02:19 +01:00
}