UNPKG

rock-mod

Version:

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

190 lines (189 loc) 7.83 kB
/// <reference types="@classic-mp/types/client" /> import { RockMod } from "../../../RockMod"; import { ClientInternalEventName } from "../../../net/common/events/types"; import { CCMPVehicle } from "./CCMPVehicle"; export class CCMPVehiclesManager { constructor() { this._vehicles = new Map(); this._vehiclesByRemoteId = new Map(); this._iterator = { all: () => this._filter(() => true), dimension: (value) => this._filter((vehicle) => vehicle.dimension === value), range2D: (center, range) => this._filter((vehicle) => { const position = vehicle.position; const squaredDistance = (position.x - center.x) ** 2 + (position.y - center.y) ** 2; return squaredDistance <= range * range; }), range3D: (center, range) => this._filter((vehicle) => vehicle.position.isInRange(center, range)), }; this._registerLifecycleEvents(); this.syncWithMpPool(); } create(options) { var _a; const createOptions = { dimension: options.dimension, engine: options.engine, locked: options.locked, }; if (options.numberPlate !== undefined) createOptions.numberPlate = options.numberPlate; if (options.numberPlateType !== undefined) createOptions.numberPlateType = options.numberPlateType; const ccmpVehicle = (_a = this._getNativeVehiclesApi()) === null || _a === void 0 ? void 0 : _a.create(options.model, options.position, options.rotation, createOptions); if (!ccmpVehicle) { throw new Error(`CCMPVehiclesManager.create: ccmp.vehicles.create failed for model "${options.model}"`); } return this._register(ccmpVehicle); } getDisplayNameFromVehicleModel(modelHash) { var _a, _b; return (_b = (_a = this._getNativeVehiclesApi()) === null || _a === void 0 ? void 0 : _a.getDisplayNameFromModel(modelHash)) !== null && _b !== void 0 ? _b : ""; } syncWithMpPool() { this._pruneDestroyed(); const ccmpVehicles = this._getNativeVehiclesApi(); if (!ccmpVehicles) return; for (const ccmpVehicle of ccmpVehicles.all) { this._register(ccmpVehicle); } } registerById(id) { var _a, _b; const existingVehicle = this.findByID(id); if (existingVehicle) return existingVehicle; const ccmpVehicle = (_b = (_a = this._getNativeVehiclesApi()) === null || _a === void 0 ? void 0 : _a.getById(id)) !== null && _b !== void 0 ? _b : null; if (!ccmpVehicle) { throw new Error(`CCMPVehiclesManager.registerById(${id}): vehicle not found.`); } return this._register(ccmpVehicle); } unregisterById(id) { return this.deleteById(id); } findByID(id) { var _a, _b, _c; const vehicle = (_a = this._vehicles.get(id)) !== null && _a !== void 0 ? _a : null; if (vehicle && vehicle.isExists) return vehicle; if (vehicle) this._unregister(vehicle); const ccmpVehicle = (_c = (_b = this._getNativeVehiclesApi()) === null || _b === void 0 ? void 0 : _b.getById(id)) !== null && _c !== void 0 ? _c : null; if (!ccmpVehicle) return null; return this._register(ccmpVehicle); } getByID(id) { const vehicle = this.findByID(id); if (!vehicle) { throw new Error(`CCMPVehiclesManager.getByID(${id}): vehicle not found.`); } return vehicle; } findByRemoteID(remoteId) { var _a, _b, _c; const vehicle = (_a = this._vehiclesByRemoteId.get(remoteId)) !== null && _a !== void 0 ? _a : null; if (vehicle && vehicle.isExists) return vehicle; if (vehicle) this._unregister(vehicle); const ccmpVehicle = (_c = (_b = this._getNativeVehiclesApi()) === null || _b === void 0 ? void 0 : _b.getByRemoteId(remoteId)) !== null && _c !== void 0 ? _c : null; if (!ccmpVehicle) return null; return this._register(ccmpVehicle); } getByRemoteID(remoteId) { const vehicle = this.findByRemoteID(remoteId); if (!vehicle) { throw new Error(`CCMPVehiclesManager.getByRemoteID(${remoteId}): vehicle not found.`); } return vehicle; } deleteById(id) { const vehicle = this.getByID(id); vehicle.destroy(); return vehicle; } get iterator() { return this._iterator; } _register(ccmpVehicle) { const existingVehicle = this._findRegistered(ccmpVehicle); if (existingVehicle && existingVehicle.isExists) return existingVehicle; if (existingVehicle) this._unregister(existingVehicle); const vehicle = new CCMPVehicle(ccmpVehicle, (destroyedVehicle) => { this._unregister(destroyedVehicle); }); this._vehicles.set(vehicle.id, vehicle); if (vehicle.remoteId !== null) { this._vehiclesByRemoteId.set(vehicle.remoteId, vehicle); } return vehicle; } _unregister(vehicle) { this._vehicles.delete(vehicle.id); if (vehicle.remoteId !== null) { this._vehiclesByRemoteId.delete(vehicle.remoteId); } } _findRegistered(ccmpVehicle) { var _a, _b, _c; return ((_c = (_b = (ccmpVehicle.remoteId === null ? null : ((_a = this._vehiclesByRemoteId.get(ccmpVehicle.remoteId)) !== null && _a !== void 0 ? _a : null))) !== null && _b !== void 0 ? _b : this._vehicles.get(ccmpVehicle.id)) !== null && _c !== void 0 ? _c : null); } _registerLifecycleEvents() { const eventsApi = ccmp; eventsApi.on("vehicleCreated", (ccmpVehicle) => { if (!ccmpVehicle) return; const vehicle = this._register(ccmpVehicle); RockMod.instance.net.events.emitInternal(ClientInternalEventName.EntityCreated, vehicle); }); eventsApi.on("vehicleDestroyed", (ccmpVehicle) => { var _a; if (!ccmpVehicle) return; const vehicle = (_a = this._findRegistered(ccmpVehicle)) !== null && _a !== void 0 ? _a : this._register(ccmpVehicle); RockMod.instance.net.events.emitInternal(ClientInternalEventName.EntityDestroyed, vehicle); this._unregister(vehicle); }); eventsApi.on("vehicleStreamIn", (ccmpVehicle) => { if (!ccmpVehicle) return; const vehicle = this._register(ccmpVehicle); RockMod.instance.net.events.emitInternal(ClientInternalEventName.EntityStreamIn, vehicle); }); eventsApi.on("vehicleStreamOut", (ccmpVehicle) => { var _a; if (!ccmpVehicle) return; const vehicle = (_a = this._findRegistered(ccmpVehicle)) !== null && _a !== void 0 ? _a : this._register(ccmpVehicle); RockMod.instance.net.events.emitInternal(ClientInternalEventName.EntityStreamOut, vehicle); }); } *_filter(predicate) { for (const vehicle of this._vehicles.values()) { if (!vehicle.isExists) { this._unregister(vehicle); continue; } if (predicate(vehicle)) { yield vehicle; } } } _pruneDestroyed() { for (const vehicle of this._vehicles.values()) { if (!vehicle.isExists) { this._unregister(vehicle); } } } _getNativeVehiclesApi() { var _a; return (_a = ccmp.vehicles) !== null && _a !== void 0 ? _a : null; } }