From a0771f8ee511c3f2ae0cb2d8a8ba03d1d34e32e6 Mon Sep 17 00:00:00 2001 From: Seth Alves Date: Fri, 6 May 2016 11:10:22 -0700 Subject: [PATCH] use smart pointers for entity simulations --- assignment-client/src/AssignmentAction.cpp | 2 +- assignment-client/src/AssignmentAction.h | 2 +- .../src/entities/EntityServer.cpp | 2 +- assignment-client/src/entities/EntityServer.h | 4 +++- interface/src/Application.cpp | 23 ++++++++++--------- interface/src/Application.h | 2 +- .../src/RenderablePolyVoxEntityItem.cpp | 4 ++-- .../entities/src/EntityActionInterface.h | 4 +++- libraries/entities/src/EntityItem.cpp | 18 +++++++-------- libraries/entities/src/EntityItem.h | 14 +++++------ .../entities/src/EntityScriptingInterface.cpp | 14 +++++------ .../entities/src/EntityScriptingInterface.h | 4 ++-- libraries/entities/src/EntitySimulation.cpp | 2 +- libraries/entities/src/EntitySimulation.h | 11 ++++++--- libraries/entities/src/EntityTree.cpp | 2 +- libraries/entities/src/EntityTree.h | 6 ++--- .../entities/src/EntityTreeHeadlessViewer.cpp | 2 +- .../entities/src/EntityTreeHeadlessViewer.h | 2 +- .../entities/src/SimpleEntitySimulation.h | 4 ++++ libraries/physics/src/ObjectAction.cpp | 2 +- libraries/physics/src/ObjectAction.h | 2 +- .../physics/src/PhysicalEntitySimulation.cpp | 2 +- .../physics/src/PhysicalEntitySimulation.h | 4 +++- 23 files changed, 74 insertions(+), 58 deletions(-) diff --git a/assignment-client/src/AssignmentAction.cpp b/assignment-client/src/AssignmentAction.cpp index 388f4123f6..8d296cd6ab 100644 --- a/assignment-client/src/AssignmentAction.cpp +++ b/assignment-client/src/AssignmentAction.cpp @@ -23,7 +23,7 @@ AssignmentAction::AssignmentAction(EntityActionType type, const QUuid& id, Entit AssignmentAction::~AssignmentAction() { } -void AssignmentAction::removeFromSimulation(EntitySimulation* simulation) const { +void AssignmentAction::removeFromSimulation(EntitySimulationPointer simulation) const { withReadLock([&]{ simulation->removeAction(_id); simulation->applyActionChanges(); diff --git a/assignment-client/src/AssignmentAction.h b/assignment-client/src/AssignmentAction.h index 6b901c9766..6b55b280bb 100644 --- a/assignment-client/src/AssignmentAction.h +++ b/assignment-client/src/AssignmentAction.h @@ -24,7 +24,7 @@ public: AssignmentAction(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity); virtual ~AssignmentAction(); - virtual void removeFromSimulation(EntitySimulation* simulation) const; + virtual void removeFromSimulation(EntitySimulationPointer simulation) const; virtual EntityItemWeakPointer getOwnerEntity() const { return _ownerEntity; } virtual void setOwnerEntity(const EntityItemPointer ownerEntity) { _ownerEntity = ownerEntity; } virtual bool updateArguments(QVariantMap arguments); diff --git a/assignment-client/src/entities/EntityServer.cpp b/assignment-client/src/entities/EntityServer.cpp index 867412f6c0..0555f95c65 100644 --- a/assignment-client/src/entities/EntityServer.cpp +++ b/assignment-client/src/entities/EntityServer.cpp @@ -56,7 +56,7 @@ OctreePointer EntityServer::createTree() { tree->createRootElement(); tree->addNewlyCreatedHook(this); if (!_entitySimulation) { - SimpleEntitySimulation* simpleSimulation = new SimpleEntitySimulation(); + SimpleEntitySimulationPointer simpleSimulation { new SimpleEntitySimulation() }; simpleSimulation->setEntityTree(tree); tree->setSimulation(simpleSimulation); _entitySimulation = simpleSimulation; diff --git a/assignment-client/src/entities/EntityServer.h b/assignment-client/src/entities/EntityServer.h index ec58cda6cd..1685f08e01 100644 --- a/assignment-client/src/entities/EntityServer.h +++ b/assignment-client/src/entities/EntityServer.h @@ -28,6 +28,8 @@ struct ViewerSendingStats { }; class SimpleEntitySimulation; +using SimpleEntitySimulationPointer = std::shared_ptr; + class EntityServer : public OctreeServer, public NewlyCreatedEntityHook { Q_OBJECT @@ -69,7 +71,7 @@ private slots: void handleEntityPacket(QSharedPointer message, SharedNodePointer senderNode); private: - SimpleEntitySimulation* _entitySimulation; + SimpleEntitySimulationPointer _entitySimulation; QTimer* _pruneDeletedEntitiesTimer = nullptr; QReadWriteLock _viewerSendingStatsLock; diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index 4a829b3191..0f14aa299c 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -509,6 +509,7 @@ Application::Application(int& argc, char** argv, QElapsedTimer& startupTimer) : _sessionRunTimer(startupTimer), _previousSessionCrashed(setupEssentials(argc, argv)), _undoStackScriptingInterface(&_undoStack), + _entitySimulation(new PhysicalEntitySimulation()), _physicsEngine(new PhysicsEngine(Vectors::ZERO)), _entityClipboardRenderer(false, this, this), _entityClipboard(new EntityTree()), @@ -2995,13 +2996,13 @@ void Application::init() { _physicsEngine->init(); EntityTreePointer tree = getEntities()->getTree(); - _entitySimulation.init(tree, _physicsEngine, &_entityEditSender); - tree->setSimulation(&_entitySimulation); + _entitySimulation->init(tree, _physicsEngine, &_entityEditSender); + tree->setSimulation(_entitySimulation); auto entityScriptingInterface = DependencyManager::get(); // connect the _entityCollisionSystem to our EntityTreeRenderer since that's what handles running entity scripts - connect(&_entitySimulation, &EntitySimulation::entityCollisionWithEntity, + connect(_entitySimulation.get(), &EntitySimulation::entityCollisionWithEntity, getEntities(), &EntityTreeRenderer::entityCollisionWithEntity); // connect the _entities (EntityTreeRenderer) to our script engine's EntityScriptingInterface for firing @@ -3404,22 +3405,22 @@ void Application::update(float deltaTime) { PerformanceTimer perfTimer("updateStates)"); static VectorOfMotionStates motionStates; - _entitySimulation.getObjectsToRemoveFromPhysics(motionStates); + _entitySimulation->getObjectsToRemoveFromPhysics(motionStates); _physicsEngine->removeObjects(motionStates); - _entitySimulation.deleteObjectsRemovedFromPhysics(); + _entitySimulation->deleteObjectsRemovedFromPhysics(); getEntities()->getTree()->withReadLock([&] { - _entitySimulation.getObjectsToAddToPhysics(motionStates); + _entitySimulation->getObjectsToAddToPhysics(motionStates); _physicsEngine->addObjects(motionStates); }); getEntities()->getTree()->withReadLock([&] { - _entitySimulation.getObjectsToChange(motionStates); + _entitySimulation->getObjectsToChange(motionStates); VectorOfMotionStates stillNeedChange = _physicsEngine->changeObjects(motionStates); - _entitySimulation.setObjectsToChange(stillNeedChange); + _entitySimulation->setObjectsToChange(stillNeedChange); }); - _entitySimulation.applyActionChanges(); + _entitySimulation->applyActionChanges(); avatarManager->getObjectsToRemoveFromPhysics(motionStates); _physicsEngine->removeObjects(motionStates); @@ -3447,7 +3448,7 @@ void Application::update(float deltaTime) { getEntities()->getTree()->withWriteLock([&] { PerformanceTimer perfTimer("handleOutgoingChanges"); const VectorOfMotionStates& outgoingChanges = _physicsEngine->getOutgoingChanges(); - _entitySimulation.handleOutgoingChanges(outgoingChanges); + _entitySimulation->handleOutgoingChanges(outgoingChanges); avatarManager->handleOutgoingChanges(outgoingChanges); }); @@ -3460,7 +3461,7 @@ void Application::update(float deltaTime) { PerformanceTimer perfTimer("entities"); // Collision events (and their scripts) must not be handled when we're locked, above. (That would risk // deadlock.) - _entitySimulation.handleCollisionEvents(collisionEvents); + _entitySimulation->handleCollisionEvents(collisionEvents); // NOTE: the getEntities()->update() call below will wait for lock // and will simulate entity motion (the EntityTree has been given an EntitySimulation). diff --git a/interface/src/Application.h b/interface/src/Application.h index 2911d42b65..528539307e 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -408,7 +408,7 @@ private: QElapsedTimer _lastTimeUpdated; ShapeManager _shapeManager; - PhysicalEntitySimulation _entitySimulation; + PhysicalEntitySimulationPointer _entitySimulation; PhysicsEnginePointer _physicsEngine; EntityTreeRenderer _entityClipboardRenderer; diff --git a/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp b/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp index 6c4e3994c6..1d5570b8b7 100644 --- a/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp +++ b/libraries/entities-renderer/src/RenderablePolyVoxEntityItem.cpp @@ -977,8 +977,8 @@ void RenderablePolyVoxEntityItem::compressVolumeDataAndSendEditPacket() { properties.setVoxelDataDirty(); properties.setLastEdited(now); - EntitySimulation* simulation = tree ? tree->getSimulation() : nullptr; - PhysicalEntitySimulation* peSimulation = static_cast(simulation); + EntitySimulationPointer simulation = tree ? tree->getSimulation() : nullptr; + PhysicalEntitySimulationPointer peSimulation = std::static_pointer_cast(simulation); EntityEditPacketSender* packetSender = peSimulation ? peSimulation->getPacketSender() : nullptr; if (packetSender) { packetSender->queueEditEntityMessage(PacketType::EntityEdit, entity->getID(), properties); diff --git a/libraries/entities/src/EntityActionInterface.h b/libraries/entities/src/EntityActionInterface.h index c634326a31..a872bc71a4 100644 --- a/libraries/entities/src/EntityActionInterface.h +++ b/libraries/entities/src/EntityActionInterface.h @@ -20,6 +20,8 @@ class EntityItem; class EntitySimulation; using EntityItemPointer = std::shared_ptr; using EntityItemWeakPointer = std::weak_ptr; +class EntitySimulation; +using EntitySimulationPointer = std::shared_ptr; enum EntityActionType { // keep these synchronized with actionTypeFromString and actionTypeToString @@ -39,7 +41,7 @@ public: bool isActive() { return _active; } - virtual void removeFromSimulation(EntitySimulation* simulation) const = 0; + virtual void removeFromSimulation(EntitySimulationPointer simulation) const = 0; virtual EntityItemWeakPointer getOwnerEntity() const = 0; virtual void setOwnerEntity(const EntityItemPointer ownerEntity) = 0; virtual bool updateArguments(QVariantMap arguments) = 0; diff --git a/libraries/entities/src/EntityItem.cpp b/libraries/entities/src/EntityItem.cpp index bdedbbce77..5476b48af0 100644 --- a/libraries/entities/src/EntityItem.cpp +++ b/libraries/entities/src/EntityItem.cpp @@ -89,7 +89,7 @@ EntityItem::EntityItem(const EntityItemID& entityItemID) : EntityItem::~EntityItem() { // clear out any left-over actions EntityTreePointer entityTree = _element ? _element->getTree() : nullptr; - EntitySimulation* simulation = entityTree ? entityTree->getSimulation() : nullptr; + EntitySimulationPointer simulation = entityTree ? entityTree->getSimulation() : nullptr; if (simulation) { clearActions(simulation); } @@ -1700,7 +1700,7 @@ void EntityItem::setPendingOwnershipPriority(quint8 priority, const quint64& tim _simulationOwner.setPendingPriority(priority, timestamp); } -bool EntityItem::addAction(EntitySimulation* simulation, EntityActionPointer action) { +bool EntityItem::addAction(EntitySimulationPointer simulation, EntityActionPointer action) { bool result; withWriteLock([&] { checkWaitingToRemove(simulation); @@ -1716,7 +1716,7 @@ bool EntityItem::addAction(EntitySimulation* simulation, EntityActionPointer act return result; } -bool EntityItem::addActionInternal(EntitySimulation* simulation, EntityActionPointer action) { +bool EntityItem::addActionInternal(EntitySimulationPointer simulation, EntityActionPointer action) { assert(action); assert(simulation); auto actionOwnerEntity = action->getOwnerEntity().lock(); @@ -1740,7 +1740,7 @@ bool EntityItem::addActionInternal(EntitySimulation* simulation, EntityActionPoi return success; } -bool EntityItem::updateAction(EntitySimulation* simulation, const QUuid& actionID, const QVariantMap& arguments) { +bool EntityItem::updateAction(EntitySimulationPointer simulation, const QUuid& actionID, const QVariantMap& arguments) { bool success = false; withWriteLock([&] { checkWaitingToRemove(simulation); @@ -1763,7 +1763,7 @@ bool EntityItem::updateAction(EntitySimulation* simulation, const QUuid& actionI return success; } -bool EntityItem::removeAction(EntitySimulation* simulation, const QUuid& actionID) { +bool EntityItem::removeAction(EntitySimulationPointer simulation, const QUuid& actionID) { bool success = false; withWriteLock([&] { checkWaitingToRemove(simulation); @@ -1772,7 +1772,7 @@ bool EntityItem::removeAction(EntitySimulation* simulation, const QUuid& actionI return success; } -bool EntityItem::removeActionInternal(const QUuid& actionID, EntitySimulation* simulation) { +bool EntityItem::removeActionInternal(const QUuid& actionID, EntitySimulationPointer simulation) { _previouslyDeletedActions.insert(actionID, usecTimestampNow()); if (_objectActions.contains(actionID)) { if (!simulation) { @@ -1798,7 +1798,7 @@ bool EntityItem::removeActionInternal(const QUuid& actionID, EntitySimulation* s return false; } -bool EntityItem::clearActions(EntitySimulation* simulation) { +bool EntityItem::clearActions(EntitySimulationPointer simulation) { withWriteLock([&] { QHash::iterator i = _objectActions.begin(); while (i != _objectActions.end()) { @@ -1834,7 +1834,7 @@ void EntityItem::deserializeActionsInternal() { EntityTreePointer entityTree = getTree(); assert(entityTree); - EntitySimulation* simulation = entityTree ? entityTree->getSimulation() : nullptr; + EntitySimulationPointer simulation = entityTree ? entityTree->getSimulation() : nullptr; assert(simulation); QVector serializedActions; @@ -1912,7 +1912,7 @@ void EntityItem::deserializeActionsInternal() { return; } -void EntityItem::checkWaitingToRemove(EntitySimulation* simulation) { +void EntityItem::checkWaitingToRemove(EntitySimulationPointer simulation) { foreach(QUuid actionID, _actionsToRemove) { removeActionInternal(actionID, simulation); } diff --git a/libraries/entities/src/EntityItem.h b/libraries/entities/src/EntityItem.h index ecb9800e70..b066fcff6a 100644 --- a/libraries/entities/src/EntityItem.h +++ b/libraries/entities/src/EntityItem.h @@ -378,10 +378,10 @@ public: void grabSimulationOwnership(); void flagForMotionStateChange() { _dirtyFlags |= Simulation::DIRTY_MOTION_TYPE; } - bool addAction(EntitySimulation* simulation, EntityActionPointer action); - bool updateAction(EntitySimulation* simulation, const QUuid& actionID, const QVariantMap& arguments); - bool removeAction(EntitySimulation* simulation, const QUuid& actionID); - bool clearActions(EntitySimulation* simulation); + bool addAction(EntitySimulationPointer simulation, EntityActionPointer action); + bool updateAction(EntitySimulationPointer simulation, const QUuid& actionID, const QVariantMap& arguments); + bool removeAction(EntitySimulationPointer simulation, const QUuid& actionID); + bool clearActions(EntitySimulationPointer simulation); void setActionData(QByteArray actionData); const QByteArray getActionData() const; bool hasActions() const { return !_objectActions.empty(); } @@ -516,8 +516,8 @@ protected: void* _physicsInfo = nullptr; // set by EntitySimulation bool _simulated; // set by EntitySimulation - bool addActionInternal(EntitySimulation* simulation, EntityActionPointer action); - bool removeActionInternal(const QUuid& actionID, EntitySimulation* simulation = nullptr); + bool addActionInternal(EntitySimulationPointer simulation, EntityActionPointer action); + bool removeActionInternal(const QUuid& actionID, EntitySimulationPointer simulation = nullptr); void deserializeActionsInternal(); void serializeActions(bool& success, QByteArray& result) const; QHash _objectActions; @@ -528,7 +528,7 @@ protected: // when an entity-server starts up, EntityItem::setActionData is called before the entity-tree is // ready. This means we can't find our EntityItemPointer or add the action to the simulation. These // are used to keep track of and work around this situation. - void checkWaitingToRemove(EntitySimulation* simulation = nullptr); + void checkWaitingToRemove(EntitySimulationPointer simulation = nullptr); mutable QSet _actionsToRemove; mutable bool _actionDataDirty = false; mutable bool _actionDataNeedsTransmit = false; diff --git a/libraries/entities/src/EntityScriptingInterface.cpp b/libraries/entities/src/EntityScriptingInterface.cpp index 093fa73ace..01f444491d 100644 --- a/libraries/entities/src/EntityScriptingInterface.cpp +++ b/libraries/entities/src/EntityScriptingInterface.cpp @@ -766,7 +766,7 @@ bool EntityScriptingInterface::appendPoint(QUuid entityID, const glm::vec3& poin bool EntityScriptingInterface::actionWorker(const QUuid& entityID, - std::function actor) { + std::function actor) { if (!_entityTree) { return false; } @@ -774,7 +774,7 @@ bool EntityScriptingInterface::actionWorker(const QUuid& entityID, EntityItemPointer entity; bool doTransmit = false; _entityTree->withWriteLock([&] { - EntitySimulation* simulation = _entityTree->getSimulation(); + EntitySimulationPointer simulation = _entityTree->getSimulation(); entity = _entityTree->findEntityByEntityItemID(entityID); if (!entity) { qDebug() << "actionWorker -- unknown entity" << entityID; @@ -815,7 +815,7 @@ QUuid EntityScriptingInterface::addAction(const QString& actionTypeString, QUuid actionID = QUuid::createUuid(); auto actionFactory = DependencyManager::get(); bool success = false; - actionWorker(entityID, [&](EntitySimulation* simulation, EntityItemPointer entity) { + actionWorker(entityID, [&](EntitySimulationPointer simulation, EntityItemPointer entity) { // create this action even if the entity doesn't have physics info. it will often be the // case that a script adds an action immediately after an object is created, and the physicsInfo // is computed asynchronously. @@ -843,7 +843,7 @@ QUuid EntityScriptingInterface::addAction(const QString& actionTypeString, bool EntityScriptingInterface::updateAction(const QUuid& entityID, const QUuid& actionID, const QVariantMap& arguments) { - return actionWorker(entityID, [&](EntitySimulation* simulation, EntityItemPointer entity) { + return actionWorker(entityID, [&](EntitySimulationPointer simulation, EntityItemPointer entity) { bool success = entity->updateAction(simulation, actionID, arguments); if (success) { entity->grabSimulationOwnership(); @@ -854,7 +854,7 @@ bool EntityScriptingInterface::updateAction(const QUuid& entityID, const QUuid& bool EntityScriptingInterface::deleteAction(const QUuid& entityID, const QUuid& actionID) { bool success = false; - actionWorker(entityID, [&](EntitySimulation* simulation, EntityItemPointer entity) { + actionWorker(entityID, [&](EntitySimulationPointer simulation, EntityItemPointer entity) { success = entity->removeAction(simulation, actionID); if (success) { // reduce from grab to poke @@ -867,7 +867,7 @@ bool EntityScriptingInterface::deleteAction(const QUuid& entityID, const QUuid& QVector EntityScriptingInterface::getActionIDs(const QUuid& entityID) { QVector result; - actionWorker(entityID, [&](EntitySimulation* simulation, EntityItemPointer entity) { + actionWorker(entityID, [&](EntitySimulationPointer simulation, EntityItemPointer entity) { QList actionIDs = entity->getActionIDs(); result = QVector::fromList(actionIDs); return false; // don't send an edit packet @@ -877,7 +877,7 @@ QVector EntityScriptingInterface::getActionIDs(const QUuid& entityID) { QVariantMap EntityScriptingInterface::getActionArguments(const QUuid& entityID, const QUuid& actionID) { QVariantMap result; - actionWorker(entityID, [&](EntitySimulation* simulation, EntityItemPointer entity) { + actionWorker(entityID, [&](EntitySimulationPointer simulation, EntityItemPointer entity) { result = entity->getActionArguments(actionID); return false; // don't send an edit packet }); diff --git a/libraries/entities/src/EntityScriptingInterface.h b/libraries/entities/src/EntityScriptingInterface.h index e5f913dbf8..5f80b7abb2 100644 --- a/libraries/entities/src/EntityScriptingInterface.h +++ b/libraries/entities/src/EntityScriptingInterface.h @@ -200,11 +200,11 @@ signals: void debitEnergySource(float value); private: - bool actionWorker(const QUuid& entityID, std::function actor); + bool actionWorker(const QUuid& entityID, std::function actor); bool setVoxels(QUuid entityID, std::function actor); bool setPoints(QUuid entityID, std::function actor); void queueEntityMessage(PacketType packetType, EntityItemID entityID, const EntityItemProperties& properties); - + EntityItemPointer checkForTreeEntityAndTypeMatch(const QUuid& entityID, EntityTypes::EntityType entityType = EntityTypes::Unknown); diff --git a/libraries/entities/src/EntitySimulation.cpp b/libraries/entities/src/EntitySimulation.cpp index 524f52e815..893ae83cc5 100644 --- a/libraries/entities/src/EntitySimulation.cpp +++ b/libraries/entities/src/EntitySimulation.cpp @@ -64,7 +64,7 @@ void EntitySimulation::prepareEntityForDelete(EntityItemPointer entity) { assert(entity->isDead()); if (entity->isSimulated()) { QMutexLocker lock(&_mutex); - entity->clearActions(this); + entity->clearActions(getThisPointer()); removeEntityInternal(entity); _entitiesToDelete.insert(entity); } diff --git a/libraries/entities/src/EntitySimulation.h b/libraries/entities/src/EntitySimulation.h index cbedbbd868..f8f506ac70 100644 --- a/libraries/entities/src/EntitySimulation.h +++ b/libraries/entities/src/EntitySimulation.h @@ -22,8 +22,9 @@ #include "EntityItem.h" #include "EntityTree.h" -typedef QSet SetOfEntities; -typedef QVector VectorOfEntities; +using EntitySimulationPointer = std::shared_ptr; +using SetOfEntities = QSet; +using VectorOfEntities = QVector; // the EntitySimulation needs to know when these things change on an entity, // so it can sort EntityItem or relay its state to the PhysicsEngine. @@ -41,12 +42,16 @@ const int DIRTY_SIMULATION_FLAGS = Simulation::DIRTY_MATERIAL | Simulation::DIRTY_SIMULATOR_ID; -class EntitySimulation : public QObject { +class EntitySimulation : public QObject, public std::enable_shared_from_this { Q_OBJECT public: EntitySimulation() : _mutex(QMutex::Recursive), _entityTree(NULL), _nextExpiry(quint64(-1)) { } virtual ~EntitySimulation() { setEntityTree(NULL); } + inline EntitySimulationPointer getThisPointer() const { + return std::const_pointer_cast(shared_from_this()); + } + /// \param tree pointer to EntityTree which is stored internally void setEntityTree(EntityTreePointer tree); diff --git a/libraries/entities/src/EntityTree.cpp b/libraries/entities/src/EntityTree.cpp index b4f0c484d5..0066dfac7e 100644 --- a/libraries/entities/src/EntityTree.cpp +++ b/libraries/entities/src/EntityTree.cpp @@ -368,7 +368,7 @@ void EntityTree::maybeNotifyNewCollisionSoundURL(const QString& previousCollisio } } -void EntityTree::setSimulation(EntitySimulation* simulation) { +void EntityTree::setSimulation(EntitySimulationPointer simulation) { this->withWriteLock([&] { if (simulation) { // assert that the simulation's backpointer has already been properly connected diff --git a/libraries/entities/src/EntityTree.h b/libraries/entities/src/EntityTree.h index 54e516d01d..3fb21832da 100644 --- a/libraries/entities/src/EntityTree.h +++ b/libraries/entities/src/EntityTree.h @@ -194,8 +194,8 @@ public: void emitEntityScriptChanging(const EntityItemID& entityItemID, const bool reload); - void setSimulation(EntitySimulation* simulation); - EntitySimulation* getSimulation() const { return _simulation; } + void setSimulation(EntitySimulationPointer simulation); + EntitySimulationPointer getSimulation() const { return _simulation; } bool wantEditLogging() const { return _wantEditLogging; } void setWantEditLogging(bool value) { _wantEditLogging = value; } @@ -297,7 +297,7 @@ protected: mutable QReadWriteLock _entityToElementLock; QHash _entityToElementMap; - EntitySimulation* _simulation; + EntitySimulationPointer _simulation; bool _wantEditLogging = false; bool _wantTerseEditLogging = false; diff --git a/libraries/entities/src/EntityTreeHeadlessViewer.cpp b/libraries/entities/src/EntityTreeHeadlessViewer.cpp index 149f92adad..81c42cda1e 100644 --- a/libraries/entities/src/EntityTreeHeadlessViewer.cpp +++ b/libraries/entities/src/EntityTreeHeadlessViewer.cpp @@ -22,7 +22,7 @@ EntityTreeHeadlessViewer::~EntityTreeHeadlessViewer() { void EntityTreeHeadlessViewer::init() { OctreeHeadlessViewer::init(); if (!_simulation) { - SimpleEntitySimulation* simpleSimulation = new SimpleEntitySimulation(); + SimpleEntitySimulationPointer simpleSimulation { new SimpleEntitySimulation() }; EntityTreePointer entityTree = std::static_pointer_cast(_tree); simpleSimulation->setEntityTree(entityTree); entityTree->setSimulation(simpleSimulation); diff --git a/libraries/entities/src/EntityTreeHeadlessViewer.h b/libraries/entities/src/EntityTreeHeadlessViewer.h index 40e428b655..0e0d4f9726 100644 --- a/libraries/entities/src/EntityTreeHeadlessViewer.h +++ b/libraries/entities/src/EntityTreeHeadlessViewer.h @@ -49,7 +49,7 @@ protected: return newTree; } - EntitySimulation* _simulation; + EntitySimulationPointer _simulation; }; #endif // hifi_EntityTreeHeadlessViewer_h diff --git a/libraries/entities/src/SimpleEntitySimulation.h b/libraries/entities/src/SimpleEntitySimulation.h index 12ded8a30d..9c7c9a374e 100644 --- a/libraries/entities/src/SimpleEntitySimulation.h +++ b/libraries/entities/src/SimpleEntitySimulation.h @@ -14,6 +14,10 @@ #include "EntitySimulation.h" +class SimpleEntitySimulation; +using SimpleEntitySimulationPointer = std::shared_ptr; + + /// provides simple velocity + gravity extrapolation of EntityItem's class SimpleEntitySimulation : public EntitySimulation { diff --git a/libraries/physics/src/ObjectAction.cpp b/libraries/physics/src/ObjectAction.cpp index 8d33c69d1c..ca64cabe5f 100644 --- a/libraries/physics/src/ObjectAction.cpp +++ b/libraries/physics/src/ObjectAction.cpp @@ -138,7 +138,7 @@ QVariantMap ObjectAction::getArguments() { void ObjectAction::debugDraw(btIDebugDraw* debugDrawer) { } -void ObjectAction::removeFromSimulation(EntitySimulation* simulation) const { +void ObjectAction::removeFromSimulation(EntitySimulationPointer simulation) const { QUuid myID; withReadLock([&]{ myID = _id; diff --git a/libraries/physics/src/ObjectAction.h b/libraries/physics/src/ObjectAction.h index efc5808f14..43330269ac 100644 --- a/libraries/physics/src/ObjectAction.h +++ b/libraries/physics/src/ObjectAction.h @@ -29,7 +29,7 @@ public: ObjectAction(EntityActionType type, const QUuid& id, EntityItemPointer ownerEntity); virtual ~ObjectAction(); - virtual void removeFromSimulation(EntitySimulation* simulation) const override; + virtual void removeFromSimulation(EntitySimulationPointer simulation) const override; virtual EntityItemWeakPointer getOwnerEntity() const override { return _ownerEntity; } virtual void setOwnerEntity(const EntityItemPointer ownerEntity) override { _ownerEntity = ownerEntity; } diff --git a/libraries/physics/src/PhysicalEntitySimulation.cpp b/libraries/physics/src/PhysicalEntitySimulation.cpp index 70ca512646..3fbf8ffaf5 100644 --- a/libraries/physics/src/PhysicalEntitySimulation.cpp +++ b/libraries/physics/src/PhysicalEntitySimulation.cpp @@ -156,7 +156,7 @@ void PhysicalEntitySimulation::clearEntitiesInternal() { void PhysicalEntitySimulation::prepareEntityForDelete(EntityItemPointer entity) { assert(entity); assert(entity->isDead()); - entity->clearActions(this); + entity->clearActions(getThisPointer()); removeEntityInternal(entity); } // end EntitySimulation overrides diff --git a/libraries/physics/src/PhysicalEntitySimulation.h b/libraries/physics/src/PhysicalEntitySimulation.h index 935ee57115..24b9ba95f0 100644 --- a/libraries/physics/src/PhysicalEntitySimulation.h +++ b/libraries/physics/src/PhysicalEntitySimulation.h @@ -23,7 +23,9 @@ #include "PhysicsEngine.h" #include "EntityMotionState.h" -typedef QSet SetOfEntityMotionStates; +class PhysicalEntitySimulation; +using PhysicalEntitySimulationPointer = std::shared_ptr; +using SetOfEntityMotionStates = QSet; class PhysicalEntitySimulation :public EntitySimulation { public: