all delete pass through EntitySimulation::prepareEntityForDelete()

This commit is contained in:
Andrew Meadows 2019-09-25 16:31:21 -07:00
parent 03db88009f
commit 9557ba80c9
3 changed files with 15 additions and 16 deletions

View file

@ -19,11 +19,11 @@
void EntitySimulation::setEntityTree(EntityTreePointer tree) { void EntitySimulation::setEntityTree(EntityTreePointer tree) {
if (_entityTree && _entityTree != tree) { if (_entityTree && _entityTree != tree) {
_mortalEntities.clear();
_nextExpiry = std::numeric_limits<uint64_t>::max();
_entitiesToUpdate.clear();
_entitiesToSort.clear(); _entitiesToSort.clear();
_simpleKinematicEntities.clear(); _simpleKinematicEntities.clear();
_entitiesToUpdate.clear();
_mortalEntities.clear();
_nextExpiry = std::numeric_limits<uint64_t>::max();
} }
_entityTree = tree; _entityTree = tree;
} }
@ -49,11 +49,11 @@ void EntitySimulation::takeDeadEntities(SetOfEntities& entitiesToDelete) {
void EntitySimulation::removeEntityFromInternalLists(EntityItemPointer entity) { void EntitySimulation::removeEntityFromInternalLists(EntityItemPointer entity) {
// remove from all internal lists except _deadEntities // remove from all internal lists except _deadEntities
_mortalEntities.remove(entity);
_entitiesToUpdate.remove(entity);
_entitiesToSort.remove(entity); _entitiesToSort.remove(entity);
_simpleKinematicEntities.remove(entity); _simpleKinematicEntities.remove(entity);
_allEntities.remove(entity); _allEntities.remove(entity);
_entitiesToUpdate.remove(entity);
_mortalEntities.remove(entity);
entity->setSimulated(false); entity->setSimulated(false);
} }
@ -218,11 +218,11 @@ void EntitySimulation::processChangedEntity(const EntityItemPointer& entity) {
void EntitySimulation::clearEntities() { void EntitySimulation::clearEntities() {
QMutexLocker lock(&_mutex); QMutexLocker lock(&_mutex);
_mortalEntities.clear();
_nextExpiry = std::numeric_limits<uint64_t>::max();
_entitiesToUpdate.clear();
_entitiesToSort.clear(); _entitiesToSort.clear();
_simpleKinematicEntities.clear(); _simpleKinematicEntities.clear();
_entitiesToUpdate.clear();
_mortalEntities.clear();
_nextExpiry = std::numeric_limits<uint64_t>::max();
clearEntitiesInternal(); clearEntitiesInternal();

View file

@ -115,10 +115,9 @@ private:
// An entity may be in more than one list. // An entity may be in more than one list.
std::unordered_set<EntityItemPointer> _changedEntities; // all changes this frame std::unordered_set<EntityItemPointer> _changedEntities; // all changes this frame
SetOfEntities _allEntities; // tracks all entities added the simulation SetOfEntities _allEntities; // tracks all entities added the simulation
SetOfEntities _entitiesToUpdate; // entities that need to call EntityItem::update()
SetOfEntities _mortalEntities; // entities that have an expiry SetOfEntities _mortalEntities; // entities that have an expiry
uint64_t _nextExpiry; uint64_t _nextExpiry;
SetOfEntities _entitiesToUpdate; // entities that need to call EntityItem::update()
}; };
#endif // hifi_EntitySimulation_h #endif // hifi_EntitySimulation_h

View file

@ -71,7 +71,8 @@ void PhysicalEntitySimulation::removeEntityFromInternalLists(EntityItemPointer e
if (motionState) { if (motionState) {
removeOwnershipData(motionState); removeOwnershipData(motionState);
_entitiesToRemoveFromPhysics.insert(entity); _entitiesToRemoveFromPhysics.insert(entity);
} else if (entity->isDead() && entity->getElement()) { }
if (entity->isDead() && entity->getElement()) {
_deadEntities.insert(entity); _deadEntities.insert(entity);
} }
if (entity->isAvatarEntity()) { if (entity->isAvatarEntity()) {
@ -215,7 +216,8 @@ void PhysicalEntitySimulation::clearEntitiesInternal() {
// virtual // virtual
void PhysicalEntitySimulation::prepareEntityForDelete(EntityItemPointer entity) { void PhysicalEntitySimulation::prepareEntityForDelete(EntityItemPointer entity) {
// this can be called on any thread // DANGER! this can be called on any thread
// do no dirty deeds here --> assemble list for later
assert(entity); assert(entity);
assert(entity->isDead()); assert(entity->isDead());
QMutexLocker lock(&_mutex); QMutexLocker lock(&_mutex);
@ -223,13 +225,11 @@ void PhysicalEntitySimulation::prepareEntityForDelete(EntityItemPointer entity)
} }
void PhysicalEntitySimulation::removeDeadEntities() { void PhysicalEntitySimulation::removeDeadEntities() {
// only ever call this on the main thread // DANGER! only ever call this on the main thread
QMutexLocker lock(&_mutex); QMutexLocker lock(&_mutex);
for (auto& entity : _entitiesToDeleteLater) { for (auto& entity : _entitiesToDeleteLater) {
entity->clearActions(getThisPointer()); entity->clearActions(getThisPointer());
if (entity->isSimulated()) { EntitySimulation::prepareEntityForDelete(entity);
removeEntityFromInternalLists(entity);
}
} }
_entitiesToDeleteLater.clear(); _entitiesToDeleteLater.clear();
} }