@duydang2311/ragemp-utils-client
Version:
A collection of utilities for RAGE Multiplayer JavaScript module.
313 lines (309 loc) • 8.1 kB
JavaScript
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
RageMpLocalMarker: () => RageMpLocalMarker,
RageMpStreamedMetaStore: () => RageMpStreamedMetaStore,
renderPromise: () => renderPromise
});
module.exports = __toCommonJS(index_exports);
// src/local-marker.ts
var RageMpLocalMarker = class {
#type;
#position;
#destination;
#direction;
#rotation;
#scale;
#radius;
#color;
#bobUpAndDown;
#faceCamera;
#rotate;
#textureDict;
#textureName;
#drawOnEnts;
constructor(type, position, destination, rotation, scale, radius, color, bobUpAndDown = false, faceCamera = false, rotate = false, textureDict = null, textureName = null, drawOnEnts = false) {
this.#type = type;
this.#position = position;
this.#destination = destination;
this.#rotation = rotation;
this.#scale = scale;
this.#radius = radius;
this.#color = color;
this.#bobUpAndDown = bobUpAndDown;
this.#faceCamera = faceCamera;
this.#rotate = rotate;
this.#textureDict = textureDict;
this.#textureName = textureName;
this.#drawOnEnts = drawOnEnts;
this.#direction = this.#getDirectionVector();
}
get type() {
return this.#type;
}
get position() {
return this.#position;
}
get destination() {
return this.#destination;
}
get rotation() {
return this.#rotation;
}
get scale() {
return this.#scale;
}
get radius() {
return this.#radius;
}
get color() {
return this.#color;
}
get bobUpAndDown() {
return this.#bobUpAndDown;
}
get faceCamera() {
return this.#faceCamera;
}
get rotate() {
return this.#rotate;
}
get textureDict() {
return this.#textureDict;
}
get textureName() {
return this.#textureName;
}
get drawOnEnts() {
return this.#drawOnEnts;
}
set type(value) {
this.#type = value;
}
set position(value) {
this.#position = value;
}
set destination(value) {
this.#destination = value;
this.#direction = this.#getDirectionVector();
}
set rotation(value) {
this.#rotation = value;
}
set scale(value) {
this.#scale = value;
}
set radius(value) {
this.#radius = value;
}
set color(value) {
this.#color = value;
}
set bobUpAndDown(value) {
this.#bobUpAndDown = value;
}
set faceCamera(value) {
this.#faceCamera = value;
}
set rotate(value) {
this.#rotate = value;
}
set textureDict(value) {
this.#textureDict = value;
}
set textureName(value) {
this.#textureName = value;
}
set drawOnEnts(value) {
this.#drawOnEnts = value;
}
render() {
mp.game.graphics.drawMarker(
this.#type,
this.#position.x,
this.#position.y,
this.#position.z,
this.#direction.x,
this.#direction.y,
this.#direction.z,
this.#rotation.x,
this.#rotation.y,
this.#rotation.z,
this.#scale.x,
this.#scale.y,
this.#scale.z,
...this.#color,
this.#bobUpAndDown,
this.#faceCamera,
2,
this.#rotate,
this.#textureDict,
this.#textureName,
this.#drawOnEnts
);
}
#getDirectionVector() {
return new mp.Vector3(
this.#destination.x - this.#position.x,
this.#destination.y - this.#position.y,
this.#destination.z - this.#position.z
);
}
};
// src/streamed-meta.ts
var RageMpStreamedMetaStore = class _RageMpStreamedMetaStore {
static #poolByType = /* @__PURE__ */ new Map([
// ['blip', mp.blips],
["checkpoint", mp.checkpoints],
["colshape", mp.colshapes],
// ['dummy', mp.dummies],
["marker", mp.markers],
["object", mp.objects],
["pickup", mp.pickups],
["player", mp.players],
["vehicle", mp.vehicles],
["ped", mp.peds],
["textlabel", mp.labels]
]);
#changeHandlersByName = /* @__PURE__ */ new Map();
#entityTypes;
#changeEventName;
#streamedInEventName;
#streamedOutEventName;
#debug;
constructor(options) {
this.#entityTypes = new Set(options?.entityTypes ?? []);
this.#changeEventName = options?.changeEventName ?? "streamedMeta.change";
this.#streamedInEventName = options?.streamedInEventName ?? "streamedMeta.streamedIn";
this.#streamedOutEventName = options?.streamedOutEventName ?? "streamedMeta.streamedOut";
this.#debug = options?.debug ?? false;
}
init() {
mp.events.add("entityStreamIn", (entity) => {
if (this.#debug) {
mp.console.logInfo(
`[StreamedMetaStore] entityStreamIn, ${entity.type}, ${entity.remoteId}`
);
}
if (!this.#entityTypes.has(entity.type)) {
return;
}
mp.events.callRemote(
this.#streamedInEventName,
entity.type,
entity.remoteId
);
});
mp.events.add("entityStreamOut", (entity) => {
if (this.#debug) {
mp.console.logInfo(
`[StreamedMetaStore] entityStreamOut, ${entity.type}, ${entity.remoteId}`
);
}
if (!this.#entityTypes.has(entity.type)) {
return;
}
mp.events.callRemote(
this.#streamedOutEventName,
entity.type,
entity.remoteId
);
});
mp.events.add(
this.#changeEventName,
(type, remoteId, name, current, previous) => {
if (this.#debug) {
mp.console.logInfo(
`[StreamedMetaStore] ${this.#changeEventName}, ${type}, ${remoteId}, ${name}, ${current}, ${previous}`
);
}
const pool = _RageMpStreamedMetaStore.#poolByType.get(type);
if (!pool) {
return;
}
const entity = pool.atRemoteId(remoteId);
if (!entity || !pool.exists(entity)) {
return;
}
const handlers = this.#changeHandlersByName.get(name);
if (handlers) {
for (const handler of handlers) {
handler(entity, current, previous);
}
}
}
);
}
on(eventName, name, fn) {
switch (eventName) {
case "change":
let handlers = this.#changeHandlersByName.get(name);
if (!handlers) {
handlers = /* @__PURE__ */ new Set([
fn
]);
this.#changeHandlersByName.set(
name,
/* @__PURE__ */ new Set([
fn
])
);
} else {
handlers.add(fn);
}
return () => {
handlers.delete(fn);
if (handlers.size === 0) {
this.#changeHandlersByName.delete(name);
}
};
default:
throw new Error(`Unsupported event: ${eventName}`);
}
}
};
// src/utils.ts
var renderPromise = (fn) => {
let scopedResolve = void 0;
let scopedReject = void 0;
const promise = new Promise((resolve, reject) => {
scopedResolve = (value) => {
resolve(value);
mp.events.remove("render", wrapped);
};
scopedReject = (reason) => {
reject(reason);
mp.events.remove("render", wrapped);
};
});
const wrapped = () => {
fn(scopedResolve, scopedReject);
};
mp.events.add("render", wrapped);
return promise;
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
RageMpLocalMarker,
RageMpStreamedMetaStore,
renderPromise
});
//# sourceMappingURL=index.cjs.map