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 | // Distributed under the Apache License, Version 2.0. | 8 | // Distributed under the Apache License, Version 2.0. |
| 9 | // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html | 9 | // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html |
| 10 | // | 10 | // |
| 11 | |||
| 12 | #include "EntityScriptingInterface.h" | 11 | #include "EntityScriptingInterface.h" |
| 13 | 12 | ||
| 14 | #include "EntityItemID.h" | 13 | #include "EntityItemID.h" |
| @@ -122,6 +121,21 @@ QUuid EntityScriptingInterface::addEntity(const EntityItemProperties& properties · 107 unmodified lines | |||
| 122 | EntityItemProperties propertiesWithSimID = convertLocationFromScriptSemantics(properties); | 121 | EntityItemProperties propertiesWithSimID = convertLocationFromScriptSemantics(properties); |
| 123 | propertiesWithSimID.setDimensionsInitialized(properties.dimensionsChanged()); | 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 | EntityItemID id = EntityItemID(QUuid::createUuid()); | 139 | EntityItemID id = EntityItemID(QUuid::createUuid()); |
| 126 | 140 | ||
| 127 | // If we have a local entity tree set, then also update it. | 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 | QUuid EntityScriptingInterface::editEntity(QUuid id, const EntityItemProperties& scriptSideProperties) { | 226 | QUuid EntityScriptingInterface::editEntity(QUuid id, const EntityItemProperties& scriptSideProperties) { |
| 213 | EntityItemProperties properties = scriptSideProperties; | 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 | EntityItemID entityID(id); | 243 | EntityItemID entityID(id); |
| 215 | if (!_entityTree) { | 244 | if (!_entityTree) { |
| 216 | queueEntityMessage(PacketType::EntityEdit, entityID, properties); | 245 | queueEntityMessage(PacketType::EntityEdit, entityID, properties); |
| @@ -316,6 +345,21 @@ void EntityScriptingInterface::deleteEntity(QUuid id) { · 99 unmodified lines | |||
| 316 | _entityTree->withWriteLock([&] { | 345 | _entityTree->withWriteLock([&] { |
| 317 | EntityItemPointer entity = _entityTree->findEntityByEntityItemID(entityID); | 346 | EntityItemPointer entity = _entityTree->findEntityByEntityItemID(entityID); |
| 318 | if (entity) { | 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 | if (entity->getLocked()) { | 363 | if (entity->getLocked()) { |
| 320 | shouldDelete = false; | 364 | shouldDelete = false; |
| 321 | } else { | 365 | } else { |
| @@ -992,3 +1036,16 @@ QStringList EntityScriptingInterface::getJointNames(const QUuid& entityID) { · 670 unmodified lines | |||
| 992 | Q_RETURN_ARG(QStringList, result), Q_ARG(QUuid, entityID)); | 1036 | Q_RETURN_ARG(QStringList, result), Q_ARG(QUuid, entityID)); |
| 993 | return result; | 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 | } | ||