darker-engine
Version:
ecs lightweight library
34 lines (33 loc) • 991 B
JavaScript
export 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,
};
};
export var UIDKey;
(function (UIDKey) {
UIDKey[UIDKey["ENTITY"] = 0] = "ENTITY";
UIDKey[UIDKey["SYSTEM"] = 1] = "SYSTEM";
})(UIDKey || (UIDKey = {}));