UNPKG

rock-mod

Version:

Rock-Mod is a powerful framework designed for creating and managing mods for Grand Theft Auto (GTA) games.

178 lines (177 loc) 6.43 kB
import { ClientInternalEventName } from "../../../net/common/events/types"; import { CCMPObject } from "./CCMPObject"; export class CCMPObjectsManager { constructor(_events) { this._events = _events; this._objects = new Map(); this._objectsByRemoteId = new Map(); this._iterator = { all: () => this._filter(() => true), dimension: (value) => this._filter((object) => object.dimension === value), range2D: (center, range) => this._filter((object) => { const position = object.position; const squaredDistance = (position.x - center.x) ** 2 + (position.y - center.y) ** 2; return squaredDistance <= range * range; }), range3D: (center, range) => this._filter((object) => object.position.isInRange(center, range)), }; this._registerLifecycleEvents(); this.syncWithMpPool(); } create(options) { const ccmpObject = ccmp.objects.create(options.model, options.position, options.rotation, { dimension: options.dimension, alpha: options.alpha, }); if (!ccmpObject) { throw new Error(`CCMPObjectsManager.create: ccmp.objects.create failed for model "${options.model}"`); } return this._register(ccmpObject); } syncWithMpPool() { this._pruneDestroyed(); for (const ccmpObject of ccmp.objects.all) { this._register(ccmpObject); } } registerById(id) { const existingObject = this.findByID(id); if (existingObject) { return existingObject; } const ccmpObject = ccmp.objects.getById(id); if (!ccmpObject) { throw new Error(`CCMPObjectsManager.registerById(${id}): object not found.`); } return this._register(ccmpObject); } unregisterById(id) { return this.deleteById(id); } findByID(id) { var _a; const object = (_a = this._objects.get(id)) !== null && _a !== void 0 ? _a : null; if (object && !object.isExists) { this._unregister(object); } else if (object) { return object; } const ccmpObject = ccmp.objects.getById(id); if (!ccmpObject) { return null; } return this._register(ccmpObject); } getByID(id) { const object = this.findByID(id); if (!object) { throw new Error(`CCMPObjectsManager.getByID(${id}): object not found.`); } return object; } findByRemoteID(remoteId) { var _a; const object = (_a = this._objectsByRemoteId.get(remoteId)) !== null && _a !== void 0 ? _a : null; if (object && !object.isExists) { this._unregister(object); } else if (object) { return object; } const ccmpObject = ccmp.objects.getByRemoteId(remoteId); if (!ccmpObject) { return null; } return this._register(ccmpObject); } getByRemoteID(remoteId) { const object = this.findByRemoteID(remoteId); if (!object) { throw new Error(`CCMPObjectsManager.getByRemoteID(${remoteId}): object not found.`); } return object; } deleteById(id) { const object = this.getByID(id); object.destroy(); return object; } get iterator() { return this._iterator; } _register(ccmpObject) { const existingObject = this._findRegistered(ccmpObject); if (existingObject && existingObject.isExists) { return existingObject; } if (existingObject) { this._unregister(existingObject); } const object = new CCMPObject(ccmpObject, (destroyedObject) => { this._unregister(destroyedObject); }); this._objects.set(object.id, object); if (object.remoteId !== null) { this._objectsByRemoteId.set(object.remoteId, object); } return object; } _unregister(object) { this._objects.delete(object.id); if (object.remoteId !== null) { this._objectsByRemoteId.delete(object.remoteId); } } _findRegistered(ccmpObject) { var _a, _b, _c; return ((_c = (_b = (ccmpObject.remoteId === null ? null : ((_a = this._objectsByRemoteId.get(ccmpObject.remoteId)) !== null && _a !== void 0 ? _a : null))) !== null && _b !== void 0 ? _b : this._objects.get(ccmpObject.id)) !== null && _c !== void 0 ? _c : null); } _registerLifecycleEvents() { ccmp.on("objectCreated", (ccmpObject) => { if (!ccmpObject) return; const object = this._register(ccmpObject); this._events.emitInternal(ClientInternalEventName.EntityCreated, object); }); ccmp.on("objectDestroyed", (ccmpObject) => { var _a; if (!ccmpObject) return; const object = (_a = this._findRegistered(ccmpObject)) !== null && _a !== void 0 ? _a : this._register(ccmpObject); this._events.emitInternal(ClientInternalEventName.EntityDestroyed, object); this._unregister(object); }); ccmp.on("objectStreamIn", (ccmpObject) => { if (!ccmpObject) return; const object = this._register(ccmpObject); this._events.emitInternal(ClientInternalEventName.EntityStreamIn, object); }); ccmp.on("objectStreamOut", (ccmpObject) => { var _a; if (!ccmpObject) return; const object = (_a = this._findRegistered(ccmpObject)) !== null && _a !== void 0 ? _a : this._register(ccmpObject); this._events.emitInternal(ClientInternalEventName.EntityStreamOut, object); }); } *_filter(predicate) { for (const object of this._objects.values()) { if (!object.isExists) { this._unregister(object); continue; } if (predicate(object)) { yield object; } } } _pruneDestroyed() { for (const object of this._objects.values()) { if (!object.isExists) { this._unregister(object); } } } }