History

Working energy usage for entity manipulation. - Working example in examples/example/ui/MyEnergyBar.js

Babiuch, Ryan Nicholas committed 10 years ago

Commit 47f7d551 parent 8efe15c6 files · +202 −42Browse files

Showing the changes to libraries/entities/src/EntityScriptingInterface.cpp (1 of 6 files). Show all

Unified · Split

libraries/entities/src/EntityScriptingInterface.cpp+58−1
@@ -8,7 +8,6 @@ · 7 unmodified lines
88 // Distributed under the Apache License, Version 2.0.
99 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
1010 //
11−
1211 #include "EntityScriptingInterface.h"
1312
1413 #include "EntityItemID.h"
@@ -122,6 +121,21 @@ QUuid EntityScriptingInterface::addEntity(const EntityItemProperties& properties · 107 unmodified lines
122121 EntityItemProperties propertiesWithSimID = convertLocationFromScriptSemantics(properties);
123122 propertiesWithSimID.setDimensionsInitialized(properties.dimensionsChanged());
124123
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+
125139 EntityItemID id = EntityItemID(QUuid::createUuid());
126140
127141 // If we have a local entity tree set, then also update it.
@@ -211,6 +225,21 @@ EntityItemProperties EntityScriptingInterface::getEntityProperties(QUuid identit · 83 unmodified lines
211225
212226 QUuid EntityScriptingInterface::editEntity(QUuid id, const EntityItemProperties& scriptSideProperties) {
213227 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+
214243 EntityItemID entityID(id);
215244 if (!_entityTree) {
216245 queueEntityMessage(PacketType::EntityEdit, entityID, properties);
@@ -316,6 +345,21 @@ void EntityScriptingInterface::deleteEntity(QUuid id) { · 99 unmodified lines
316345 _entityTree->withWriteLock([&] {
317346 EntityItemPointer entity = _entityTree->findEntityByEntityItemID(entityID);
318347 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+
319363 if (entity->getLocked()) {
320364 shouldDelete = false;
321365 } else {
@@ -992,3 +1036,16 @@ QStringList EntityScriptingInterface::getJointNames(const QUuid& entityID) { · 670 unmodified lines
9921036 Q_RETURN_ARG(QStringList, result), Q_ARG(QUuid, entityID));
9931037 return result;
9941038 }
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+}