hook up entity-scripting-interface to actions

This commit is contained in:
Seth Alves 2015-06-03 11:19:45 -07:00
parent 57c3a14b6a
commit c8ab22c517
10 changed files with 143 additions and 62 deletions

View file

@ -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

View file

@ -11,13 +11,14 @@
#include <VariantMapToScriptValue.h>
#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<QUuid> EntityScriptingInterface::findEntities(const glm::vec3& center, f
QVector<EntityItemPointer> entities;
_entityTree->findEntities(center, radius, entities);
_entityTree->unlock();
foreach (EntityItemPointer entity, entities) {
result << entity->getEntityItemID();
}
@ -236,7 +238,7 @@ QVector<QUuid> EntityScriptingInterface::findEntitiesInBox(const glm::vec3& corn
QVector<EntityItemPointer> 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<void(PolyVoxEntityItem&)> 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;
}

View file

@ -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<QUuid> 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<QUuid> 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<void(PolyVoxEntityItem&)> 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<void(PolyVoxEntityItem&)> 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

View file

@ -56,10 +56,14 @@ public:
friend class EntityTree;
virtual void addAction(EntityActionInterface* action) {}
virtual void removeAction(const QUuid actionID) {}
virtual void removeActions(QList<QUuid> 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<QUuid> 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<EntityActionInterface*> _actionsToAdd;
QList<QUuid> _actionsToRemove;
};
#endif // hifi_EntitySimulation_h

View file

@ -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) {

View file

@ -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:

View file

@ -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";
}

View file

@ -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 <QUuid>
#include <EntityItem.h>
#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

View file

@ -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<QUuid> 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();
}

View file

@ -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<QUuid> 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<EntityActionInterface*> _actionsToAdd;
QList<QUuid> _actionsToRemove;
};
#endif // hifi_PhysicalEntitySimulation_h