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.03 kB
JavaScript
import { ClientInternalEventName } from "../../../net/common/events/types";
import { CCMPPed } from "./CCMPPed";
export class CCMPPedsManager {
constructor(_events) {
this._events = _events;
this._peds = new Map();
this._pedsByRemoteId = new Map();
this._iterator = {
all: () => this._filter(() => true),
dimension: (value) => this._filter((ped) => ped.dimension === value),
range2D: (center, range) => this._filter((ped) => {
const position = ped.position;
const squaredDistance = (position.x - center.x) ** 2 + (position.y - center.y) ** 2;
return squaredDistance <= range * range;
}),
range3D: (center, range) => this._filter((ped) => ped.position.isInRange(center, range)),
};
this._registerStreamEvents();
this.syncWithMpPool();
}
create(options) {
const { model, position, rotation, dimension } = options;
const ccmpPed = ccmp.peds.create(model, position, rotation.z, { dimension });
if (!ccmpPed) {
throw new Error(`CCMPPedsManager.create: ccmp.peds.create failed for model "${model}"`);
}
return this._register(ccmpPed);
}
syncWithMpPool() {
this._pruneDestroyed();
for (const ccmpPed of ccmp.peds.all) {
this._register(ccmpPed);
}
}
registerById(id) {
const existingPed = this.findByID(id);
if (existingPed) {
return existingPed;
}
const ccmpPed = ccmp.peds.getById(id);
if (!ccmpPed) {
throw new Error(`CCMPPedsManager.registerById(${id}): ped not found.`);
}
return this._register(ccmpPed);
}
unregisterById(id) {
return this.deleteById(id);
}
findByID(id) {
var _a;
const ped = (_a = this._peds.get(id)) !== null && _a !== void 0 ? _a : null;
if (ped && !ped.isExists) {
this._unregister(ped);
}
else if (ped) {
return ped;
}
const ccmpPed = ccmp.peds.getById(id);
if (!ccmpPed) {
return null;
}
return this._register(ccmpPed);
}
getByID(id) {
const ped = this.findByID(id);
if (!ped) {
throw new Error(`CCMPPedsManager.getByID(${id}): ped not found.`);
}
return ped;
}
findByRemoteID(remoteId) {
var _a;
const ped = (_a = this._pedsByRemoteId.get(remoteId)) !== null && _a !== void 0 ? _a : null;
if (ped && !ped.isExists) {
this._unregister(ped);
}
else if (ped) {
return ped;
}
const ccmpPed = ccmp.peds.getByRemoteId(remoteId);
if (!ccmpPed) {
return null;
}
return this._register(ccmpPed);
}
getByRemoteID(remoteId) {
const ped = this.findByRemoteID(remoteId);
if (!ped) {
throw new Error(`CCMPPedsManager.getByRemoteID(${remoteId}): ped not found.`);
}
return ped;
}
deleteById(id) {
const ped = this.getByID(id);
ped.destroy();
return ped;
}
get iterator() {
return this._iterator;
}
*_filter(predicate) {
for (const ped of this._peds.values()) {
if (!ped.isExists) {
this._unregister(ped);
continue;
}
if (predicate(ped)) {
yield ped;
}
}
}
_register(ccmpPed) {
const existingPed = this._findRegistered(ccmpPed);
if (existingPed && existingPed.isExists) {
return existingPed;
}
if (existingPed) {
this._unregister(existingPed);
}
const ped = new CCMPPed(ccmpPed, (destroyedPed) => {
this._unregister(destroyedPed);
});
this._peds.set(ped.id, ped);
if (ped.remoteId !== null) {
this._pedsByRemoteId.set(ped.remoteId, ped);
}
return ped;
}
_unregister(ped) {
this._peds.delete(ped.id);
if (ped.remoteId !== null) {
this._pedsByRemoteId.delete(ped.remoteId);
}
}
_findRegistered(ccmpPed) {
var _a, _b, _c;
return ((_c = (_b = (ccmpPed.remoteId === null ? null : ((_a = this._pedsByRemoteId.get(ccmpPed.remoteId)) !== null && _a !== void 0 ? _a : null))) !== null && _b !== void 0 ? _b : this._peds.get(ccmpPed.id)) !== null && _c !== void 0 ? _c : null);
}
_registerStreamEvents() {
ccmp.on("pedCreated", (ccmpPed) => {
if (ccmpPed) {
const ped = this._register(ccmpPed);
this._events.emitInternal(ClientInternalEventName.EntityCreated, ped);
}
});
ccmp.on("pedDestroyed", (ccmpPed) => {
var _a;
if (ccmpPed) {
const ped = (_a = this._findRegistered(ccmpPed)) !== null && _a !== void 0 ? _a : this._register(ccmpPed);
this._events.emitInternal(ClientInternalEventName.EntityDestroyed, ped);
this._unregister(ped);
}
});
ccmp.on("pedStreamIn", (ccmpPed) => {
if (ccmpPed) {
const ped = this._register(ccmpPed);
this._events.emitInternal(ClientInternalEventName.EntityStreamIn, ped);
}
});
ccmp.on("pedStreamOut", (ccmpPed) => {
var _a;
if (ccmpPed) {
const ped = (_a = this._findRegistered(ccmpPed)) !== null && _a !== void 0 ? _a : this._register(ccmpPed);
if (ped) {
this._events.emitInternal(ClientInternalEventName.EntityStreamOut, ped);
}
}
});
}
_pruneDestroyed() {
for (const ped of this._peds.values()) {
if (!ped.isExists) {
this._unregister(ped);
}
}
}
}