darker-engine
Version:
ecs lightweight library
38 lines (37 loc) • 1.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.UIDKey = exports.uid = void 0;
const uid = (getEntityList) => {
const lastSafeIdMap = {
[UIDKey.ENTITY]: 0,
[UIDKey.SYSTEM]: 0, //<< meh
};
const lastIdMap = {
[UIDKey.ENTITY]: 0,
[UIDKey.SYSTEM]: 0,
};
const idCheckMapFunc = {
[UIDKey.ENTITY]: (currentId) => currentId !== undefined && getEntityList()[currentId] !== undefined,
[UIDKey.SYSTEM]: () => false,
};
const getUID = (key, safe = false) => {
const increment = safe ? 10000 : 0;
const _getUID = () => (safe ? lastSafeIdMap[key] : lastIdMap[key]) + increment;
do {
if (safe)
lastSafeIdMap[key]++;
else
lastIdMap[key]++;
} while (idCheckMapFunc[key](_getUID()));
return _getUID();
};
return {
getUID,
};
};
exports.uid = uid;
var UIDKey;
(function (UIDKey) {
UIDKey[UIDKey["ENTITY"] = 0] = "ENTITY";
UIDKey[UIDKey["SYSTEM"] = 1] = "SYSTEM";
})(UIDKey = exports.UIDKey || (exports.UIDKey = {}));