UNPKG

rock-mod

Version:

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

194 lines (193 loc) 6.92 kB
import { ClientInternalEventName } from "../../../net/common/events/types"; import { CCMPBlip } from "./CCMPBlip"; export class CCMPBlipsManager { constructor(_events) { this._events = _events; this._blips = new Map(); this._blipsByRemoteId = new Map(); this._iterator = { all: () => this._filter(() => true), dimension: (value) => this._filter((blip) => blip.dimension === value), range2D: (center, range) => this._filter((blip) => { const position = blip.position; const squaredDistance = (position.x - center.x) ** 2 + (position.y - center.y) ** 2; return squaredDistance <= range * range; }), range3D: (center, range) => this._filter((blip) => blip.position.isInRange(center, range)), }; this._registerLifecycleEvents(); this.syncWithMpPool(); } create(options) { const createOptions = { dimension: options.dimension, }; if (options.alpha !== undefined) createOptions.alpha = options.alpha; if (options.color !== undefined) createOptions.color = options.color; if (options.drawDistance !== undefined) createOptions.drawDistance = options.drawDistance; if (options.global !== undefined) createOptions.global = options.global; if (options.name !== undefined) createOptions.name = options.name; if (options.rotation !== undefined) createOptions.rotation = options.rotation; if (options.scale !== undefined) createOptions.scale = options.scale; if (options.shortRange !== undefined) createOptions.shortRange = options.shortRange; const ccmpBlip = ccmp.blips.create(options.sprite, options.position, createOptions); if (!ccmpBlip) { throw new Error(`CCMPBlipsManager.create: ccmp.blips.create failed for sprite "${options.sprite}"`); } return this._register(ccmpBlip); } syncWithMpPool() { this._pruneDestroyed(); for (const ccmpBlip of ccmp.blips.all) { this._register(ccmpBlip); } } registerById(id) { const existingBlip = this.findByID(id); if (existingBlip) { return existingBlip; } const ccmpBlip = ccmp.blips.getById(id); if (!ccmpBlip) { throw new Error(`CCMPBlipsManager.registerById(${id}): blip not found.`); } return this._register(ccmpBlip); } unregisterById(id) { return this.deleteById(id); } findByID(id) { var _a; const blip = (_a = this._blips.get(id)) !== null && _a !== void 0 ? _a : null; if (blip && !blip.isExists) { this._unregister(blip); } else if (blip) { return blip; } const ccmpBlip = ccmp.blips.getById(id); if (!ccmpBlip) { return null; } return this._register(ccmpBlip); } getByID(id) { const blip = this.findByID(id); if (!blip) { throw new Error(`CCMPBlipsManager.getByID(${id}): blip not found.`); } return blip; } findByRemoteID(remoteId) { var _a; const blip = (_a = this._blipsByRemoteId.get(remoteId)) !== null && _a !== void 0 ? _a : null; if (blip && !blip.isExists) { this._unregister(blip); } else if (blip) { return blip; } const ccmpBlip = ccmp.blips.getByRemoteId(remoteId); if (!ccmpBlip) { return null; } return this._register(ccmpBlip); } getByRemoteID(remoteId) { const blip = this.findByRemoteID(remoteId); if (!blip) { throw new Error(`CCMPBlipsManager.getByRemoteID(${remoteId}): blip not found.`); } return blip; } deleteById(id) { const blip = this.getByID(id); blip.destroy(); return blip; } get iterator() { return this._iterator; } _register(ccmpBlip) { const existingBlip = this._findRegistered(ccmpBlip); if (existingBlip && existingBlip.isExists) { return existingBlip; } if (existingBlip) { this._unregister(existingBlip); } const blip = new CCMPBlip(ccmpBlip, (destroyedBlip) => { this._unregister(destroyedBlip); }); this._blips.set(blip.id, blip); if (blip.remoteId !== null) { this._blipsByRemoteId.set(blip.remoteId, blip); } return blip; } _unregister(blip) { this._blips.delete(blip.id); if (blip.remoteId !== null) { this._blipsByRemoteId.delete(blip.remoteId); } } _findRegistered(ccmpBlip) { var _a, _b, _c; return ((_c = (_b = (ccmpBlip.remoteId === null ? null : ((_a = this._blipsByRemoteId.get(ccmpBlip.remoteId)) !== null && _a !== void 0 ? _a : null))) !== null && _b !== void 0 ? _b : this._blips.get(ccmpBlip.id)) !== null && _c !== void 0 ? _c : null); } _registerLifecycleEvents() { ccmp.on("blipCreated", (ccmpBlip) => { if (!ccmpBlip) return; const blip = this._register(ccmpBlip); this._events.emitInternal(ClientInternalEventName.EntityCreated, blip); }); ccmp.on("blipDestroyed", (ccmpBlip) => { var _a; if (!ccmpBlip) return; const blip = (_a = this._findRegistered(ccmpBlip)) !== null && _a !== void 0 ? _a : this._register(ccmpBlip); this._events.emitInternal(ClientInternalEventName.EntityDestroyed, blip); this._unregister(blip); }); ccmp.on("blipStreamIn", (ccmpBlip) => { if (!ccmpBlip) return; const blip = this._register(ccmpBlip); this._events.emitInternal(ClientInternalEventName.EntityStreamIn, blip); }); ccmp.on("blipStreamOut", (ccmpBlip) => { var _a; if (!ccmpBlip) return; const blip = (_a = this._findRegistered(ccmpBlip)) !== null && _a !== void 0 ? _a : this._register(ccmpBlip); this._events.emitInternal(ClientInternalEventName.EntityStreamOut, blip); }); } *_filter(predicate) { for (const blip of this._blips.values()) { if (!blip.isExists) { this._unregister(blip); continue; } if (predicate(blip)) { yield blip; } } } _pruneDestroyed() { for (const blip of this._blips.values()) { if (!blip.isExists) { this._unregister(blip); } } } }