diff --git a/libraries/entities/src/EntityActionInterface.h b/libraries/entities/src/EntityActionInterface.h index 4250c938c8..d8bb19821c 100644 --- a/libraries/entities/src/EntityActionInterface.h +++ b/libraries/entities/src/EntityActionInterface.h @@ -12,14 +12,20 @@ #ifndef hifi_EntityActionInterface_h #define hifi_EntityActionInterface_h +enum EntityActionType { + ACTION_TYPE_NONE, + ACTION_TYPE_PULL_TO_POINT +}; + + class EntityActionInterface { public: - virtual ~EntityActionInterface(); + virtual ~EntityActionInterface() {}; virtual const QUuid& getID() const = 0; virtual const EntityItemPointer& getOwnerEntity() const = 0; virtual void setOwnerEntity(const EntityItemPointer ownerEntity) = 0; - virtual QByteArray serialize() = 0; - static EntityActionInterface* deserialize(EntityItemPointer ownerEntity, QByteArray data); + // virtual QByteArray serialize() = 0; + // static EntityActionInterface* deserialize(EntityItemPointer ownerEntity, QByteArray data); }; #endif // hifi_EntityActionInterface_h diff --git a/libraries/entities/src/EntityScriptingInterface.cpp b/libraries/entities/src/EntityScriptingInterface.cpp index 6866505d58..739665194e 100644 --- a/libraries/entities/src/EntityScriptingInterface.cpp +++ b/libraries/entities/src/EntityScriptingInterface.cpp @@ -11,13 +11,14 @@ #include -#include "EntityScriptingInterface.h" #include "EntityTree.h" #include "LightEntityItem.h" #include "ModelEntityItem.h" #include "ZoneEntityItem.h" #include "EntitiesLogging.h" +#include "EntitySimulation.h" +#include "EntityScriptingInterface.h" EntityScriptingInterface::EntityScriptingInterface() : _entityTree(NULL) @@ -83,7 +84,8 @@ QUuid EntityScriptingInterface::addEntity(const EntityItemProperties& properties entity->setLastBroadcast(usecTimestampNow()); // This Node is creating a new object. If it's in motion, set this Node as the simulator. bidForSimulationOwnership(propertiesWithSimID); - entity->setSimulatorID(propertiesWithSimID.getSimulatorID()); // and make note of it now, so we can act on it right away. + // and make note of it now, so we can act on it right away. + entity->setSimulatorID(propertiesWithSimID.getSimulatorID()); } else { qCDebug(entities) << "script failed to add new Entity to local Octree"; success = false; @@ -105,7 +107,7 @@ EntityItemProperties EntityScriptingInterface::getEntityProperties(QUuid identit _entityTree->lockForRead(); EntityItemPointer entity = _entityTree->findEntityByEntityItemID(EntityItemID(identity)); - + if (entity) { results = entity->getProperties(); @@ -124,7 +126,7 @@ EntityItemProperties EntityScriptingInterface::getEntityProperties(QUuid identit } _entityTree->unlock(); } - + return results; } @@ -220,7 +222,7 @@ QVector EntityScriptingInterface::findEntities(const glm::vec3& center, f QVector entities; _entityTree->findEntities(center, radius, entities); _entityTree->unlock(); - + foreach (EntityItemPointer entity, entities) { result << entity->getEntityItemID(); } @@ -236,7 +238,7 @@ QVector EntityScriptingInterface::findEntitiesInBox(const glm::vec3& corn QVector entities; _entityTree->findEntities(box, entities); _entityTree->unlock(); - + foreach (EntityItemPointer entity, entities) { result << entity->getEntityItemID(); } @@ -393,7 +395,6 @@ void RayToEntityIntersectionResultFromScriptValue(const QScriptValue& object, Ra } } - bool EntityScriptingInterface::setVoxels(QUuid entityID, std::function actor) { if (!_entityTree) { @@ -431,23 +432,48 @@ bool EntityScriptingInterface::setVoxels(QUuid entityID, return true; } - bool EntityScriptingInterface::setVoxelSphere(QUuid entityID, const glm::vec3& center, float radius, int value) { return setVoxels(entityID, [center, radius, value](PolyVoxEntityItem& polyVoxEntity) { polyVoxEntity.setSphere(center, radius, value); }); } - bool EntityScriptingInterface::setVoxel(QUuid entityID, const glm::vec3& position, int value) { return setVoxels(entityID, [position, value](PolyVoxEntityItem& polyVoxEntity) { polyVoxEntity.setVoxelInVolume(position, value); }); } - bool EntityScriptingInterface::setAllVoxels(QUuid entityID, int value) { return setVoxels(entityID, [value](PolyVoxEntityItem& polyVoxEntity) { polyVoxEntity.setAll(value); }); } + +QUuid EntityScriptingInterface::addActionPullToPoint(QUuid entityID, const glm::vec3& target) { + if (!_entityTree) { + return QUuid(); + } + + _entityTree->lockForWrite(); + + EntitySimulation* simulation = _entityTree->getSimulation(); + QUuid actionID = QUuid::createUuid(); + EntityItemPointer entity = _entityTree->findEntityByEntityItemID(entityID); + if (!entity) { + qDebug() << "addAction -- unknown entity" << entityID; + _entityTree->unlock(); + return QUuid(); + } + + QVariantMap arguments; + QVariantList targetList; + targetList << QVariant(target[0]) << QVariant(target[1]) << QVariant(target[2]); + arguments["target"] = targetList; + EntityActionInterface* action = simulation->actionFactory(ACTION_TYPE_PULL_TO_POINT, actionID, entity, arguments); + entity->addAction(action); + + _entityTree->unlock(); + + return actionID; +} diff --git a/libraries/entities/src/EntityScriptingInterface.h b/libraries/entities/src/EntityScriptingInterface.h index 7761effe2f..df42cb3375 100644 --- a/libraries/entities/src/EntityScriptingInterface.h +++ b/libraries/entities/src/EntityScriptingInterface.h @@ -54,14 +54,14 @@ class EntityScriptingInterface : public OctreeScriptingInterface, public Depende Q_OBJECT public: EntityScriptingInterface(); - + EntityEditPacketSender* getEntityPacketSender() const { return (EntityEditPacketSender*)getPacketSender(); } virtual NodeType_t getServerNodeType() const { return NodeType::EntityServer; } virtual OctreeEditPacketSender* createPacketSender() { return new EntityEditPacketSender(); } void setEntityTree(EntityTree* modelTree); EntityTree* getEntityTree(EntityTree*) { return _entityTree; } - + public slots: // returns true if the DomainServer will allow this Node/Avatar to make changes @@ -88,11 +88,11 @@ public slots: /// will return a EntityItemID.isKnownID = false if no models are in the radius /// this function will not find any models in script engine contexts which don't have access to models Q_INVOKABLE QUuid findClosestEntity(const glm::vec3& center, float radius) const; - + /// finds models within the search sphere specified by the center point and radius /// this function will not find any models in script engine contexts which don't have access to models Q_INVOKABLE QVector findEntities(const glm::vec3& center, float radius) const; - + /// finds models within the search sphere specified by the center point and radius /// this function will not find any models in script engine contexts which don't have access to models Q_INVOKABLE QVector findEntitiesInBox(const glm::vec3& corner, const glm::vec3& dimensions) const; @@ -118,13 +118,14 @@ public slots: Q_INVOKABLE void setSendPhysicsUpdates(bool value); Q_INVOKABLE bool getSendPhysicsUpdates() const; - bool setVoxels(QUuid entityID, std::function actor); Q_INVOKABLE bool setVoxelSphere(QUuid entityID, const glm::vec3& center, float radius, int value); Q_INVOKABLE bool setVoxel(QUuid entityID, const glm::vec3& position, int value); Q_INVOKABLE bool setAllVoxels(QUuid entityID, int value); Q_INVOKABLE void dumpTree() const; + Q_INVOKABLE QUuid addActionPullToPoint(QUuid entityID, const glm::vec3& target); + signals: void entityCollisionWithEntity(const EntityItemID& idA, const EntityItemID& idB, const Collision& collision); void collisionWithEntity(const EntityItemID& idA, const EntityItemID& idB, const Collision& collision); @@ -152,6 +153,7 @@ signals: void clearingEntities(); private: + bool setVoxels(QUuid entityID, std::function actor); void queueEntityMessage(PacketType packetType, EntityItemID entityID, const EntityItemProperties& properties); /// actually does the work of finding the ray intersection, can be called in locking mode or tryLock mode diff --git a/libraries/entities/src/EntitySimulation.h b/libraries/entities/src/EntitySimulation.h index baf1736d2e..178d5e1358 100644 --- a/libraries/entities/src/EntitySimulation.h +++ b/libraries/entities/src/EntitySimulation.h @@ -56,10 +56,14 @@ public: friend class EntityTree; - virtual void addAction(EntityActionInterface* action) {} - virtual void removeAction(const QUuid actionID) {} - virtual void removeActions(QList actionIDsToRemove) {} - virtual void applyActionChanges() {} + virtual EntityActionInterface* actionFactory(EntityActionType type, + QUuid id, + EntityItemPointer ownerEntity, + QVariantMap arguments) { return nullptr; } + virtual void addAction(EntityActionInterface* action) { _actionsToAdd += action; } + virtual void removeAction(const QUuid actionID) { _actionsToRemove += actionID; } + virtual void removeActions(QList actionIDsToRemove) { _actionsToRemove += actionIDsToRemove; } + virtual void applyActionChanges() { _actionsToAdd.clear(); _actionsToRemove.clear(); } protected: // these only called by the EntityTree? /// \param entity pointer to EntityItem to be added @@ -119,6 +123,10 @@ protected: private: void moveSimpleKinematics(); + + protected: + QList _actionsToAdd; + QList _actionsToRemove; }; #endif // hifi_EntitySimulation_h diff --git a/libraries/physics/src/ObjectAction.cpp b/libraries/physics/src/ObjectAction.cpp index 78524c1f14..cb27f36248 100644 --- a/libraries/physics/src/ObjectAction.cpp +++ b/libraries/physics/src/ObjectAction.cpp @@ -17,19 +17,11 @@ ObjectAction::ObjectAction(QUuid id, EntityItemPointer ownerEntity) : _ownerEntity(ownerEntity) { } -ObjectAction::ObjectAction(EntityItemPointer ownerEntity) : - ObjectAction(QUuid::createUuid(), ownerEntity) { -} - ObjectAction::~ObjectAction() { } void ObjectAction::updateAction(btCollisionWorld* collisionWorld, btScalar deltaTimeStep) { - qDebug() << "updateAction called"; -} - -EntityActionInterface* ObjectAction::deserialize(EntityItemPointer ownerEntity, QByteArray data) { - return new ObjectAction(ownerEntity); + qDebug() << "ObjectAction::updateAction called"; } void ObjectAction::debugDraw(btIDebugDraw* debugDrawer) { diff --git a/libraries/physics/src/ObjectAction.h b/libraries/physics/src/ObjectAction.h index d2504a09c2..8ee4ed4faf 100644 --- a/libraries/physics/src/ObjectAction.h +++ b/libraries/physics/src/ObjectAction.h @@ -22,7 +22,6 @@ class ObjectAction : public btActionInterface, public EntityActionInterface { public: ObjectAction(QUuid id, EntityItemPointer ownerEntity); - ObjectAction(EntityItemPointer ownerEntity); virtual ~ObjectAction(); const QUuid& getID() const { return _id; } @@ -31,9 +30,6 @@ public: virtual void updateAction(btCollisionWorld* collisionWorld, btScalar deltaTimeStep); - virtual QByteArray serialize() { return QByteArray(); } - static EntityActionInterface* deserialize(EntityItemPointer ownerEntity, QByteArray data); - virtual void debugDraw(btIDebugDraw* debugDrawer); private: diff --git a/libraries/physics/src/ObjectActionPullToPoint.cpp b/libraries/physics/src/ObjectActionPullToPoint.cpp new file mode 100644 index 0000000000..eebf5c3608 --- /dev/null +++ b/libraries/physics/src/ObjectActionPullToPoint.cpp @@ -0,0 +1,24 @@ +// +// ObjectActionPullToPoint.cpp +// libraries/physics/src +// +// Created by Seth Alves 2015-6-2 +// Copyright 2015 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include "ObjectActionPullToPoint.h" + +ObjectActionPullToPoint::ObjectActionPullToPoint(QUuid id, EntityItemPointer ownerEntity, glm::vec3 target) : + ObjectAction(id, ownerEntity), + _target(target) { +} + +ObjectActionPullToPoint::~ObjectActionPullToPoint() { +} + +void ObjectActionPullToPoint::updateAction(btCollisionWorld* collisionWorld, btScalar deltaTimeStep) { + qDebug() << "ObjectActionPullToPoint::updateAction called"; +} diff --git a/libraries/physics/src/ObjectActionPullToPoint.h b/libraries/physics/src/ObjectActionPullToPoint.h new file mode 100644 index 0000000000..a090a2f134 --- /dev/null +++ b/libraries/physics/src/ObjectActionPullToPoint.h @@ -0,0 +1,31 @@ +// +// ObjectActionPullToPoint.h +// libraries/physics/src +// +// Created by Seth Alves 2015-6-3 +// Copyright 2015 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_ObjectActionPullToPoint_h +#define hifi_ObjectActionPullToPoint_h + +#include + +#include +#include "ObjectAction.h" + +class ObjectActionPullToPoint : public ObjectAction { +public: + ObjectActionPullToPoint(QUuid id, EntityItemPointer ownerEntity, glm::vec3 target); + virtual ~ObjectActionPullToPoint(); + + void updateAction(btCollisionWorld* collisionWorld, btScalar deltaTimeStep); + +private: + glm::vec3 _target; +}; + +#endif // hifi_ObjectActionPullToPoint_h diff --git a/libraries/physics/src/PhysicalEntitySimulation.cpp b/libraries/physics/src/PhysicalEntitySimulation.cpp index 35762cbf03..a75f897c24 100644 --- a/libraries/physics/src/PhysicalEntitySimulation.cpp +++ b/libraries/physics/src/PhysicalEntitySimulation.cpp @@ -9,10 +9,12 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -#include "PhysicalEntitySimulation.h" #include "PhysicsHelpers.h" #include "PhysicsLogging.h" #include "ShapeManager.h" +#include "ObjectActionPullToPoint.h" + +#include "PhysicalEntitySimulation.h" PhysicalEntitySimulation::PhysicalEntitySimulation() { } @@ -232,25 +234,22 @@ void PhysicalEntitySimulation::handleCollisionEvents(CollisionEvents& collisionE } } -void PhysicalEntitySimulation::addAction(EntityActionInterface* action) { - // if (_physicsEngine) { - // _physicsEngine->addAction(action); - // } - _actionsToAdd += action; -} +EntityActionInterface* PhysicalEntitySimulation::actionFactory(EntityActionType type, + QUuid id, + EntityItemPointer ownerEntity, + QVariantMap arguments) { + switch (type) { + case ACTION_TYPE_NONE: + assert(false); + return nullptr; + case ACTION_TYPE_PULL_TO_POINT: + QVariantList target = arguments["target"].toList(); + glm::vec3 glmTarget(target[0].toFloat(), target[1].toFloat(), target[2].toFloat()); + return (EntityActionInterface*) new ObjectActionPullToPoint(id, ownerEntity, glmTarget); + } -void PhysicalEntitySimulation::removeAction(const QUuid actionID) { - // if (_physicsEngine) { - // _physicsEngine->removeAction(actionID); - // } - _actionsToRemove += actionID; -} - -void PhysicalEntitySimulation::removeActions(QList actionIDsToRemove) { - // if (_physicsEngine) { - // _physicsEngine->removeActions(actionIDsToRemove); - // } - _actionsToRemove += actionIDsToRemove; + assert(false); + return nullptr; } void PhysicalEntitySimulation::applyActionChanges() { @@ -258,10 +257,9 @@ void PhysicalEntitySimulation::applyActionChanges() { foreach (EntityActionInterface* actionToAdd, _actionsToAdd) { _physicsEngine->addAction(actionToAdd); } - _actionsToAdd.clear(); foreach (QUuid actionToRemove, _actionsToRemove) { _physicsEngine->removeAction(actionToRemove); } - _actionsToRemove.clear(); } + EntitySimulation::applyActionChanges(); } diff --git a/libraries/physics/src/PhysicalEntitySimulation.h b/libraries/physics/src/PhysicalEntitySimulation.h index e57cd4326b..6195271ce9 100644 --- a/libraries/physics/src/PhysicalEntitySimulation.h +++ b/libraries/physics/src/PhysicalEntitySimulation.h @@ -32,9 +32,10 @@ public: void init(EntityTree* tree, PhysicsEngine* engine, EntityEditPacketSender* packetSender); - virtual void addAction(EntityActionInterface* action); - virtual void removeAction(const QUuid actionID); - virtual void removeActions(QList actionIDsToRemove); + virtual EntityActionInterface* actionFactory(EntityActionType type, + QUuid id, + EntityItemPointer ownerEntity, + QVariantMap arguments); virtual void applyActionChanges(); protected: // only called by EntitySimulation @@ -69,9 +70,6 @@ private: EntityEditPacketSender* _entityPacketSender = nullptr; uint32_t _lastStepSendPackets = 0; - - QList _actionsToAdd; - QList _actionsToRemove; }; #endif // hifi_PhysicalEntitySimulation_h