mirror of
https://github.com/overte-org/overte.git
synced 2025-04-26 19:16:21 +02:00
91 lines
2.7 KiB
JavaScript
91 lines
2.7 KiB
JavaScript
"use strict";
|
|
|
|
// cloneEntity.js
|
|
//
|
|
// Distributed under the Apache License, Version 2.0.
|
|
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
|
|
|
|
Script.include("/~/system/controllers/controllerDispatcherUtils.js");
|
|
|
|
|
|
// Object assign polyfill
|
|
if (typeof Object.assign != 'function') {
|
|
Object.assign = function(target, varArgs) {
|
|
if (target === null) {
|
|
throw new TypeError('Cannot convert undefined or null to object');
|
|
}
|
|
var to = Object(target);
|
|
for (var index = 1; index < arguments.length; index++) {
|
|
var nextSource = arguments[index];
|
|
if (nextSource !== null) {
|
|
for (var nextKey in nextSource) {
|
|
if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
|
|
to[nextKey] = nextSource[nextKey];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return to;
|
|
};
|
|
}
|
|
|
|
entityIsCloneable = function(props) {
|
|
if (props) {
|
|
var grabbableData = getGrabbableData(props);
|
|
return grabbableData.cloneable;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
cloneEntity = function(cloneableProps, worldEntityProps) {
|
|
if (!cloneableProps) {
|
|
return null;
|
|
}
|
|
|
|
// we need all the properties, for this
|
|
cloneableProps = Entities.getEntityProperties(cloneableProps.id);
|
|
|
|
var count = 0;
|
|
worldEntityProps.forEach(function(itemWE) {
|
|
if (itemWE.name.indexOf('-clone-' + cloneableProps.id) !== -1) {
|
|
count++;
|
|
}
|
|
});
|
|
|
|
var grabInfo = getGrabbableData(cloneableProps);
|
|
|
|
var limit = grabInfo.cloneLimit ? grabInfo.cloneLimit : 0;
|
|
if (count >= limit && limit !== 0) {
|
|
return null;
|
|
}
|
|
|
|
cloneableProps.name = cloneableProps.name + '-clone-' + cloneableProps.id;
|
|
var lifetime = grabInfo.cloneLifetime ? grabInfo.cloneLifetime : 300;
|
|
var dynamic = grabInfo.cloneDynamic ? grabInfo.cloneDynamic : false;
|
|
var cUserData = Object.assign({}, cloneableProps.userData);
|
|
var cProperties = Object.assign({}, cloneableProps);
|
|
|
|
try {
|
|
delete cUserData.grabbableKey.cloneLifetime;
|
|
delete cUserData.grabbableKey.cloneable;
|
|
delete cUserData.grabbableKey.cloneDynamic;
|
|
delete cUserData.grabbableKey.cloneLimit;
|
|
delete cProperties.id;
|
|
} catch(e) {
|
|
}
|
|
|
|
cProperties.dynamic = dynamic;
|
|
cProperties.locked = false;
|
|
if (!cUserData.grabbableKey) {
|
|
cUserData.grabbableKey = {};
|
|
}
|
|
cUserData.grabbableKey.triggerable = true;
|
|
cUserData.grabbableKey.grabbable = true;
|
|
cProperties.lifetime = lifetime;
|
|
cProperties.userData = JSON.stringify(cUserData);
|
|
|
|
var cloneID = Entities.addEntity(cProperties);
|
|
return cloneID;
|
|
}
|