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) • 7.36 kB
JavaScript
import { ClientInternalEventName } from "../../../net/common/events/types";
import { CCMPColshape } from "./CCMPColshape";
export class CCMPColshapesManager {
constructor(_events) {
this._events = _events;
this._colshapes = new Map();
this._colshapesByRemoteId = new Map();
this._iterator = {
all: () => this._filter(() => true),
dimension: (value) => this._filter((colshape) => colshape.dimension === value),
range2D: (center, range) => this._filter((colshape) => {
const position = colshape.position;
const squaredDistance = (position.x - center.x) ** 2 + (position.y - center.y) ** 2;
return squaredDistance <= range * range;
}),
range3D: (center, range) => this._filter((colshape) => colshape.position.isInRange(center, range)),
};
this._registerLifecycleEvents();
this.syncWithMpPool();
}
createCircle(options) {
const ccmpColshape = ccmp.colshapes.createCircle(options.position, options.range, {
dimension: options.dimension,
});
return this._register(ccmpColshape);
}
createCuboid(options) {
const ccmpColshape = ccmp.colshapes.createCuboid(options.position, { x: options.width, y: options.depth, z: options.height }, { dimension: options.dimension });
return this._register(ccmpColshape);
}
createCylinder(options) {
const ccmpColshape = ccmp.colshapes.createCylinder(options.position, options.range, options.height, {
dimension: options.dimension,
});
return this._register(ccmpColshape);
}
createRectangle(options) {
void options;
throw new Error("CCMPColshapesManager.createRectangle: not supported by CCMP");
}
createSphere(options) {
const ccmpColshape = ccmp.colshapes.createSphere(options.position, options.range, {
dimension: options.dimension,
});
return this._register(ccmpColshape);
}
syncWithMpPool() {
this._pruneDestroyed();
for (const ccmpColshape of ccmp.colshapes.all) {
this._register(ccmpColshape);
}
}
registerById(id) {
const existingColshape = this.findByID(id);
if (existingColshape) {
return existingColshape;
}
const ccmpColshape = ccmp.colshapes.getById(id);
if (!ccmpColshape) {
throw new Error(`CCMPColshapesManager.registerById(${id}): colshape not found.`);
}
return this._register(ccmpColshape);
}
unregisterById(id) {
return this.deleteById(id);
}
findByID(id) {
var _a;
const colshape = (_a = this._colshapes.get(id)) !== null && _a !== void 0 ? _a : null;
if (colshape && !colshape.isExists) {
this._unregister(colshape);
}
else if (colshape) {
return colshape;
}
const ccmpColshape = ccmp.colshapes.getById(id);
if (!ccmpColshape) {
return null;
}
return this._register(ccmpColshape);
}
getByID(id) {
const colshape = this.findByID(id);
if (!colshape) {
throw new Error(`CCMPColshapesManager.getByID(${id}): colshape not found.`);
}
return colshape;
}
findByRemoteID(remoteId) {
var _a;
const colshape = (_a = this._colshapesByRemoteId.get(remoteId)) !== null && _a !== void 0 ? _a : null;
if (colshape && !colshape.isExists) {
this._unregister(colshape);
}
else if (colshape) {
return colshape;
}
const ccmpColshape = ccmp.colshapes.getByRemoteId(remoteId);
if (!ccmpColshape) {
return null;
}
return this._register(ccmpColshape);
}
getByRemoteID(remoteId) {
const colshape = this.findByRemoteID(remoteId);
if (!colshape) {
throw new Error(`CCMPColshapesManager.getByRemoteID(${remoteId}): colshape not found.`);
}
return colshape;
}
deleteById(id) {
const colshape = this.getByID(id);
colshape.destroy();
return colshape;
}
get iterator() {
return this._iterator;
}
_register(ccmpColshape) {
const existingColshape = this._findRegistered(ccmpColshape);
if (existingColshape && existingColshape.isExists) {
return existingColshape;
}
if (existingColshape) {
this._unregister(existingColshape);
}
const colshape = new CCMPColshape(ccmpColshape, (destroyedColshape) => {
this._unregister(destroyedColshape);
});
this._colshapes.set(colshape.id, colshape);
if (colshape.remoteId !== null) {
this._colshapesByRemoteId.set(colshape.remoteId, colshape);
}
return colshape;
}
_unregister(colshape) {
this._colshapes.delete(colshape.id);
if (colshape.remoteId !== null) {
this._colshapesByRemoteId.delete(colshape.remoteId);
}
}
_findRegistered(ccmpColshape) {
var _a, _b, _c;
return ((_c = (_b = (ccmpColshape.remoteId === null ? null : ((_a = this._colshapesByRemoteId.get(ccmpColshape.remoteId)) !== null && _a !== void 0 ? _a : null))) !== null && _b !== void 0 ? _b : this._colshapes.get(ccmpColshape.id)) !== null && _c !== void 0 ? _c : null);
}
_registerLifecycleEvents() {
ccmp.on("colshapeCreated", (ccmpColshape) => {
if (!ccmpColshape)
return;
const colshape = this._register(ccmpColshape);
this._events.emitInternal(ClientInternalEventName.EntityCreated, colshape);
});
ccmp.on("colshapeDestroyed", (ccmpColshape) => {
var _a;
if (!ccmpColshape)
return;
const colshape = (_a = this._findRegistered(ccmpColshape)) !== null && _a !== void 0 ? _a : this._register(ccmpColshape);
this._events.emitInternal(ClientInternalEventName.EntityDestroyed, colshape);
this._unregister(colshape);
});
ccmp.on("colshapeStreamIn", (ccmpColshape) => {
if (!ccmpColshape)
return;
const colshape = this._register(ccmpColshape);
this._events.emitInternal(ClientInternalEventName.EntityStreamIn, colshape);
});
ccmp.on("colshapeStreamOut", (ccmpColshape) => {
var _a;
if (!ccmpColshape)
return;
const colshape = (_a = this._findRegistered(ccmpColshape)) !== null && _a !== void 0 ? _a : this._register(ccmpColshape);
this._events.emitInternal(ClientInternalEventName.EntityStreamOut, colshape);
});
}
*_filter(predicate) {
for (const colshape of this._colshapes.values()) {
if (!colshape.isExists) {
this._unregister(colshape);
continue;
}
if (predicate(colshape)) {
yield colshape;
}
}
}
_pruneDestroyed() {
for (const colshape of this._colshapes.values()) {
if (!colshape.isExists) {
this._unregister(colshape);
}
}
}
}