simplify add/removal to EntitySimulation

This commit is contained in:
Andrew Meadows 2019-09-25 14:13:47 -07:00
parent c8e875ff72
commit 03db88009f
6 changed files with 34 additions and 37 deletions

View file

@ -47,7 +47,7 @@ void EntitySimulation::takeDeadEntities(SetOfEntities& entitiesToDelete) {
_deadEntities.clear(); _deadEntities.clear();
} }
void EntitySimulation::removeEntityInternal(EntityItemPointer entity) { void EntitySimulation::removeEntityFromInternalLists(EntityItemPointer entity) {
// remove from all internal lists except _deadEntities // remove from all internal lists except _deadEntities
_mortalEntities.remove(entity); _mortalEntities.remove(entity);
_entitiesToUpdate.remove(entity); _entitiesToUpdate.remove(entity);
@ -62,7 +62,7 @@ void EntitySimulation::prepareEntityForDelete(EntityItemPointer entity) {
assert(entity->isDead()); assert(entity->isDead());
if (entity->isSimulated()) { if (entity->isSimulated()) {
QMutexLocker lock(&_mutex); QMutexLocker lock(&_mutex);
removeEntityInternal(entity); removeEntityFromInternalLists(entity);
if (entity->getElement()) { if (entity->getElement()) {
_deadEntities.insert(entity); _deadEntities.insert(entity);
_entityTree->cleanupCloneIDs(entity->getEntityItemID()); _entityTree->cleanupCloneIDs(entity->getEntityItemID());
@ -148,9 +148,7 @@ void EntitySimulation::sortEntitiesThatMoved() {
_entitiesToSort.clear(); _entitiesToSort.clear();
} }
void EntitySimulation::addEntity(EntityItemPointer entity) { void EntitySimulation::addEntityToInternalLists(EntityItemPointer entity) {
QMutexLocker lock(&_mutex);
assert(entity);
if (entity->isMortal()) { if (entity->isMortal()) {
_mortalEntities.insert(entity); _mortalEntities.insert(entity);
uint64_t expiry = entity->getExpiry(); uint64_t expiry = entity->getExpiry();
@ -161,10 +159,14 @@ void EntitySimulation::addEntity(EntityItemPointer entity) {
if (entity->needsToCallUpdate()) { if (entity->needsToCallUpdate()) {
_entitiesToUpdate.insert(entity); _entitiesToUpdate.insert(entity);
} }
addEntityInternal(entity);
_allEntities.insert(entity); _allEntities.insert(entity);
entity->setSimulated(true); entity->setSimulated(true);
}
void EntitySimulation::addEntity(EntityItemPointer entity) {
QMutexLocker lock(&_mutex);
assert(entity);
addEntityToInternalLists(entity);
// DirtyFlags are used to signal changes to entities that have already been added, // DirtyFlags are used to signal changes to entities that have already been added,
// so we can clear them for this entity which has just been added. // so we can clear them for this entity which has just been added.

View file

@ -88,8 +88,8 @@ protected:
// These pure virtual methods are protected because they are not to be called will-nilly. The base class // These pure virtual methods are protected because they are not to be called will-nilly. The base class
// calls them in the right places. // calls them in the right places.
virtual void updateEntitiesInternal(uint64_t now) = 0; virtual void updateEntitiesInternal(uint64_t now) = 0;
virtual void addEntityInternal(EntityItemPointer entity) = 0; virtual void addEntityToInternalLists(EntityItemPointer entity);
virtual void removeEntityInternal(EntityItemPointer entity); virtual void removeEntityFromInternalLists(EntityItemPointer entity);
virtual void processChangedEntity(const EntityItemPointer& entity); virtual void processChangedEntity(const EntityItemPointer& entity);
virtual void clearEntitiesInternal() = 0; virtual void clearEntitiesInternal() = 0;

View file

@ -52,9 +52,9 @@ void SimpleEntitySimulation::updateEntitiesInternal(uint64_t now) {
stopOwnerlessEntities(now); stopOwnerlessEntities(now);
} }
void SimpleEntitySimulation::addEntityInternal(EntityItemPointer entity) { void SimpleEntitySimulation::addEntityToInternalLists(EntityItemPointer entity) {
EntitySimulation::addEntityToInternalLists(entity);
if (entity->getSimulatorID().isNull()) { if (entity->getSimulatorID().isNull()) {
QMutexLocker lock(&_mutex);
if (entity->getDynamic()) { if (entity->getDynamic()) {
// we don't allow dynamic objects to move without an owner so nothing to do here // we don't allow dynamic objects to move without an owner so nothing to do here
} else if (entity->isMovingRelativeToParent()) { } else if (entity->isMovingRelativeToParent()) {
@ -65,7 +65,6 @@ void SimpleEntitySimulation::addEntityInternal(EntityItemPointer entity) {
} }
} }
} else { } else {
QMutexLocker lock(&_mutex);
_entitiesWithSimulationOwner.insert(entity); _entitiesWithSimulationOwner.insert(entity);
_nextStaleOwnershipExpiry = glm::min(_nextStaleOwnershipExpiry, entity->getSimulationOwnershipExpiry()); _nextStaleOwnershipExpiry = glm::min(_nextStaleOwnershipExpiry, entity->getSimulationOwnershipExpiry());
@ -79,10 +78,10 @@ void SimpleEntitySimulation::addEntityInternal(EntityItemPointer entity) {
} }
} }
void SimpleEntitySimulation::removeEntityInternal(EntityItemPointer entity) { void SimpleEntitySimulation::removeEntityFromInternalLists(EntityItemPointer entity) {
EntitySimulation::removeEntityInternal(entity);
_entitiesWithSimulationOwner.remove(entity); _entitiesWithSimulationOwner.remove(entity);
_entitiesThatNeedSimulationOwner.remove(entity); _entitiesThatNeedSimulationOwner.remove(entity);
EntitySimulation::removeEntityFromInternalLists(entity);
} }
void SimpleEntitySimulation::processChangedEntity(const EntityItemPointer& entity) { void SimpleEntitySimulation::processChangedEntity(const EntityItemPointer& entity) {

View file

@ -29,8 +29,8 @@ public:
protected: protected:
void updateEntitiesInternal(uint64_t now) override; void updateEntitiesInternal(uint64_t now) override;
void addEntityInternal(EntityItemPointer entity) override; void addEntityToInternalLists(EntityItemPointer entity) override;
void removeEntityInternal(EntityItemPointer entity) override; void removeEntityFromInternalLists(EntityItemPointer entity) override;
void processChangedEntity(const EntityItemPointer& entity) override; void processChangedEntity(const EntityItemPointer& entity) override;
void clearEntitiesInternal() override; void clearEntitiesInternal() override;

View file

@ -44,11 +44,9 @@ void PhysicalEntitySimulation::updateEntitiesInternal(uint64_t now) {
// Do nothing here because the "internal" update the PhysicsEngine::stepSimulation() which is done elsewhere. // Do nothing here because the "internal" update the PhysicsEngine::stepSimulation() which is done elsewhere.
} }
void PhysicalEntitySimulation::addEntityInternal(EntityItemPointer entity) { void PhysicalEntitySimulation::addEntityToInternalLists(EntityItemPointer entity) {
QMutexLocker lock(&_mutex); EntitySimulation::addEntityToInternalLists(entity);
assert(entity); entity->deserializeActions(); // TODO: do this elsewhere
assert(!entity->isDead());
entity->deserializeActions();
uint8_t region = _space->getRegion(entity->getSpaceIndex()); uint8_t region = _space->getRegion(entity->getSpaceIndex());
bool maybeShouldBePhysical = (region < workload::Region::R3 || region == workload::Region::UNKNOWN) && entity->shouldBePhysical(); bool maybeShouldBePhysical = (region < workload::Region::R3 || region == workload::Region::UNKNOWN) && entity->shouldBePhysical();
bool canBeKinematic = region <= workload::Region::R3; bool canBeKinematic = region <= workload::Region::R3;
@ -67,23 +65,19 @@ void PhysicalEntitySimulation::addEntityInternal(EntityItemPointer entity) {
} }
} }
void PhysicalEntitySimulation::removeEntityInternal(EntityItemPointer entity) { void PhysicalEntitySimulation::removeEntityFromInternalLists(EntityItemPointer entity) {
if (entity->isSimulated()) { _entitiesToAddToPhysics.remove(entity);
EntitySimulation::removeEntityInternal(entity); EntityMotionState* motionState = static_cast<EntityMotionState*>(entity->getPhysicsInfo());
_entitiesToAddToPhysics.remove(entity); if (motionState) {
removeOwnershipData(motionState);
EntityMotionState* motionState = static_cast<EntityMotionState*>(entity->getPhysicsInfo()); _entitiesToRemoveFromPhysics.insert(entity);
if (motionState) { } else if (entity->isDead() && entity->getElement()) {
removeOwnershipData(motionState); _deadEntities.insert(entity);
_entitiesToRemoveFromPhysics.insert(entity);
}
if (entity->isDead() && entity->getElement()) {
_deadEntities.insert(entity);
}
} }
if (entity->isAvatarEntity()) { if (entity->isAvatarEntity()) {
_deadAvatarEntities.insert(entity); _deadAvatarEntities.insert(entity);
} }
EntitySimulation::removeEntityFromInternalLists(entity);
} }
void PhysicalEntitySimulation::removeOwnershipData(EntityMotionState* motionState) { void PhysicalEntitySimulation::removeOwnershipData(EntityMotionState* motionState) {
@ -233,7 +227,9 @@ void PhysicalEntitySimulation::removeDeadEntities() {
QMutexLocker lock(&_mutex); QMutexLocker lock(&_mutex);
for (auto& entity : _entitiesToDeleteLater) { for (auto& entity : _entitiesToDeleteLater) {
entity->clearActions(getThisPointer()); entity->clearActions(getThisPointer());
removeEntityInternal(entity); if (entity->isSimulated()) {
removeEntityFromInternalLists(entity);
}
} }
_entitiesToDeleteLater.clear(); _entitiesToDeleteLater.clear();
} }

View file

@ -72,8 +72,8 @@ signals:
protected: // only called by EntitySimulation protected: // only called by EntitySimulation
// overrides for EntitySimulation // overrides for EntitySimulation
virtual void updateEntitiesInternal(uint64_t now) override; virtual void updateEntitiesInternal(uint64_t now) override;
virtual void addEntityInternal(EntityItemPointer entity) override; virtual void addEntityToInternalLists(EntityItemPointer entity) override;
virtual void removeEntityInternal(EntityItemPointer entity) override; virtual void removeEntityFromInternalLists(EntityItemPointer entity) override;
void processChangedEntity(const EntityItemPointer& entity) override; void processChangedEntity(const EntityItemPointer& entity) override;
virtual void clearEntitiesInternal() override; virtual void clearEntitiesInternal() override;