Showing the changes to libraries/entities/src/EntityScriptingInterface.cpp (1 of 6 files). Show all
Unified · Split
| @@ -8,7 +8,6 @@ · 7 unmodified lines | ||
| 8 | 8 | // Distributed under the Apache License, Version 2.0. |
| 9 | 9 | // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html |
| 10 | 10 | // |
| 11 | − | |
| 12 | 11 | #include "EntityScriptingInterface.h" |
| 13 | 12 | |
| 14 | 13 | #include "EntityItemID.h" |
| @@ -122,6 +121,21 @@ QUuid EntityScriptingInterface::addEntity(const EntityItemProperties& properties · 107 unmodified lines | ||
| 122 | 121 | EntityItemProperties propertiesWithSimID = convertLocationFromScriptSemantics(properties); |
| 123 | 122 | propertiesWithSimID.setDimensionsInitialized(properties.dimensionsChanged()); |
| 124 | 123 | |
| 124 | + auto dimensions = propertiesWithSimID.getDimensions(); | |
| 125 | + float volume = dimensions.x*dimensions.y*dimensions.z; | |
| 126 | + auto density = propertiesWithSimID.getDensity(); | |
| 127 | + auto newVelocity = propertiesWithSimID.getVelocity().length(); | |
| 128 | + double cost = calculateCost(density*volume, 0, newVelocity); | |
| 129 | + cost *= ENTITY_MANIPULATION_MULTIPLIER; //try this as a constant for now | |
| 130 | + | |
| 131 | + if(cost > _currentAvatarEnergy) { | |
| 132 | + return QUuid(); | |
| 133 | + } else { | |
| 134 | + //debit the avatar energy and continue | |
| 135 | + emit debitEnergySource(cost); | |
| 136 | + } | |
| 137 | + | |
| 138 | + | |
| 125 | 139 | EntityItemID id = EntityItemID(QUuid::createUuid()); |
| 126 | 140 | |
| 127 | 141 | // If we have a local entity tree set, then also update it. |
| @@ -211,6 +225,21 @@ EntityItemProperties EntityScriptingInterface::getEntityProperties(QUuid identit · 83 unmodified lines | ||
| 211 | 225 | |
| 212 | 226 | QUuid EntityScriptingInterface::editEntity(QUuid id, const EntityItemProperties& scriptSideProperties) { |
| 213 | 227 | EntityItemProperties properties = scriptSideProperties; |
| 228 | + | |
| 229 | + auto dimensions = properties.getDimensions(); | |
| 230 | + float volume = dimensions.x*dimensions.y*dimensions.z; | |
| 231 | + auto density = properties.getDensity(); | |
| 232 | + auto newVelocity = properties.getVelocity().length(); | |
| 233 | + double cost = calculateCost(density*volume, 0, newVelocity); | |
| 234 | + cost *= ENTITY_MANIPULATION_MULTIPLIER; | |
| 235 | + | |
| 236 | + if(cost > _currentAvatarEnergy) { | |
| 237 | + return QUuid(); | |
| 238 | + } else { | |
| 239 | + //debit the avatar energy and continue | |
| 240 | + emit debitEnergySource(cost); | |
| 241 | + } | |
| 242 | + | |
| 214 | 243 | EntityItemID entityID(id); |
| 215 | 244 | if (!_entityTree) { |
| 216 | 245 | queueEntityMessage(PacketType::EntityEdit, entityID, properties); |
| @@ -316,6 +345,21 @@ void EntityScriptingInterface::deleteEntity(QUuid id) { · 99 unmodified lines | ||
| 316 | 345 | _entityTree->withWriteLock([&] { |
| 317 | 346 | EntityItemPointer entity = _entityTree->findEntityByEntityItemID(entityID); |
| 318 | 347 | if (entity) { |
| 348 | + | |
| 349 | + auto dimensions = entity->getDimensions(); | |
| 350 | + float volume = dimensions.x*dimensions.y*dimensions.z; | |
| 351 | + auto density = entity->getDensity(); | |
| 352 | + auto velocity = entity->getVelocity().length(); | |
| 353 | + double cost = calculateCost(density*volume, velocity, 0); | |
| 354 | + cost *= ENTITY_MANIPULATION_MULTIPLIER; | |
| 355 | + | |
| 356 | + if(cost > _currentAvatarEnergy) { | |
| 357 | + return; | |
| 358 | + } else { | |
| 359 | + //debit the avatar energy and continue | |
| 360 | + emit debitEnergySource(cost); | |
| 361 | + } | |
| 362 | + | |
| 319 | 363 | if (entity->getLocked()) { |
| 320 | 364 | shouldDelete = false; |
| 321 | 365 | } else { |
| @@ -992,3 +1036,16 @@ QStringList EntityScriptingInterface::getJointNames(const QUuid& entityID) { · 670 unmodified lines | ||
| 992 | 1036 | Q_RETURN_ARG(QStringList, result), Q_ARG(QUuid, entityID)); |
| 993 | 1037 | return result; |
| 994 | 1038 | } |
| 1039 | + | |
| 1040 | +void EntityScriptingInterface::addCostFunction(QScriptValue costFunction) { | |
| 1041 | + _costFunction = &costFunction; | |
| 1042 | +} | |
| 1043 | + | |
| 1044 | +double EntityScriptingInterface::calculateCost(float mass, float oldVelocity,float newVelocity) { | |
| 1045 | + return std::abs(mass * (newVelocity - oldVelocity)); | |
| 1046 | +} | |
| 1047 | + | |
| 1048 | +void EntityScriptingInterface::setCurrentAvatarEnergy(float energy) { | |
| 1049 | + // qCDebug(entities) << "NEW AVATAR ENERGY IN ENTITY SCRIPTING INTERFACE: " << energy; | |
| 1050 | + _currentAvatarEnergy = energy; | |
| 1051 | +} | |