diff --git a/examples/example/ui/MyEnergyBar.js b/examples/example/ui/MyEnergyBar.js index ac7ef0d39c..c24f998e1a 100644 --- a/examples/example/ui/MyEnergyBar.js +++ b/examples/example/ui/MyEnergyBar.js @@ -56,11 +56,7 @@ function energyChanged(newValue) { function debitAvatarEnergy(value) { MyAvatar.energy = MyAvatar.energy - value; } -function calculateCost(mass, oldVelocity, newVelocity) { - return mass * (newVelocity - oldVelocity); -} - -Entities.addCostFunction(calculateCost); +Entities.costMultiplier = 0.002; Entities.debitEnergySource.connect(debitAvatarEnergy); MyAvatar.energyChanged.connect(energyChanged); Script.update.connect(update); diff --git a/libraries/entities/src/EntityScriptingInterface.cpp b/libraries/entities/src/EntityScriptingInterface.cpp index 82e843c94d..3b02c42699 100644 --- a/libraries/entities/src/EntityScriptingInterface.cpp +++ b/libraries/entities/src/EntityScriptingInterface.cpp @@ -126,7 +126,7 @@ QUuid EntityScriptingInterface::addEntity(const EntityItemProperties& properties auto density = propertiesWithSimID.getDensity(); auto newVelocity = propertiesWithSimID.getVelocity().length(); double cost = calculateCost(density*volume, 0, newVelocity); - cost *= ENTITY_MANIPULATION_MULTIPLIER; //try this as a constant for now + cost *= costMultiplier; if(cost > _currentAvatarEnergy) { return QUuid(); @@ -231,7 +231,7 @@ QUuid EntityScriptingInterface::editEntity(QUuid id, const EntityItemProperties& auto density = properties.getDensity(); auto newVelocity = properties.getVelocity().length(); double cost = calculateCost(density*volume, 0, newVelocity); - cost *= ENTITY_MANIPULATION_MULTIPLIER; + cost *= costMultiplier; if(cost > _currentAvatarEnergy) { return QUuid(); @@ -351,7 +351,7 @@ void EntityScriptingInterface::deleteEntity(QUuid id) { auto density = entity->getDensity(); auto velocity = entity->getVelocity().length(); double cost = calculateCost(density*volume, velocity, 0); - cost *= ENTITY_MANIPULATION_MULTIPLIER; + cost *= costMultiplier; if(cost > _currentAvatarEnergy) { return; @@ -1037,11 +1037,7 @@ QStringList EntityScriptingInterface::getJointNames(const QUuid& entityID) { return result; } -void EntityScriptingInterface::addCostFunction(QScriptValue costFunction) { - _costFunction = &costFunction; -} - -double EntityScriptingInterface::calculateCost(float mass, float oldVelocity,float newVelocity) { +float EntityScriptingInterface::calculateCost(float mass, float oldVelocity,float newVelocity) { return std::abs(mass * (newVelocity - oldVelocity)); } @@ -1049,3 +1045,11 @@ void EntityScriptingInterface::setCurrentAvatarEnergy(float energy) { // qCDebug(entities) << "NEW AVATAR ENERGY IN ENTITY SCRIPTING INTERFACE: " << energy; _currentAvatarEnergy = energy; } + +float EntityScriptingInterface::getCostMultiplier() { + return costMultiplier; +} + +void EntityScriptingInterface::setCostMultiplier(float value) { + costMultiplier = value; +} diff --git a/libraries/entities/src/EntityScriptingInterface.h b/libraries/entities/src/EntityScriptingInterface.h index ff98ab4eb7..3bb86fba53 100644 --- a/libraries/entities/src/EntityScriptingInterface.h +++ b/libraries/entities/src/EntityScriptingInterface.h @@ -61,6 +61,7 @@ class EntityScriptingInterface : public OctreeScriptingInterface, public Depende Q_OBJECT Q_PROPERTY(float currentAvatarEnergy READ getCurrentAvatarEnergy WRITE setCurrentAvatarEnergy) + Q_PROPERTY(float costMultiplier READ getCostMultiplier WRITE setCostMultiplier) public: EntityScriptingInterface(); @@ -71,8 +72,7 @@ public: void setEntityTree(EntityTreePointer modelTree); EntityTreePointer getEntityTree() { return _entityTree; } void setEntitiesScriptEngine(EntitiesScriptEngineProvider* engine) { _entitiesScriptEngine = engine; } - double calculateCost(float mass, float oldVelocity, float newVelocity); - Q_INVOKABLE void addCostFunction(QScriptValue costFunction); + float calculateCost(float mass, float oldVelocity, float newVelocity); public slots: // returns true if the DomainServer will allow this Node/Avatar to make changes @@ -194,7 +194,7 @@ signals: void deletingEntity(const EntityItemID& entityID); void addingEntity(const EntityItemID& entityID); void clearingEntities(); - void debitEnergySource(double value); + void debitEnergySource(float value); private: bool actionWorker(const QUuid& entityID, std::function actor); @@ -212,11 +212,13 @@ private: EntityTreePointer _entityTree; EntitiesScriptEngineProvider* _entitiesScriptEngine = nullptr; - QScriptValue* _costFunction = nullptr; - float _currentAvatarEnergy; + float _currentAvatarEnergy = { FLT_MAX }; float getCurrentAvatarEnergy() { return _currentAvatarEnergy; } void setCurrentAvatarEnergy(float energy); - float ENTITY_MANIPULATION_MULTIPLIER = { 0.01f }; + + float costMultiplier = { 0.01f }; + float getCostMultiplier(); + void setCostMultiplier(float value); }; #endif // hifi_EntityScriptingInterface_h