void EntitySimulation::changeEntity(EntityItemPointer entity) { assert(entity); if (!entity->_simulated) { // This entity was either never added to the simulation or has been removed // (probably for pending delete), so we don't want to keep a pointer to it // on any internal lists. return; } // Although it is not the responsibility of the EntitySimulation to sort the tree for EXTERNAL changes // it IS responsibile for triggering deletes for entities that leave the bounds of the domain, hence // we must check for that case here, however we rely on the change event to have set DIRTY_POSITION flag. bool wasRemoved = false; uint32_t dirtyFlags = entity->getDirtyFlags(); if (dirtyFlags & EntityItem::DIRTY_POSITION) { AACube domainBounds(glm::vec3(0.0f,0.0f,0.0f), (float)TREE_SCALE); AACube newCube = entity->getMaximumAACube(); if (!domainBounds.touches(newCube)) { qCDebug(entities) << "Entity " << entity->getEntityItemID() << " moved out of domain bounds."; _entitiesToDelete.insert(entity); _mortalEntities.remove(entity); _entitiesToUpdate.remove(entity); _entitiesToSort.remove(entity); _simpleKinematicEntities.remove(entity); removeEntityInternal(entity); entity->_simulated = false; wasRemoved = true; } } if (!wasRemoved) { if (dirtyFlags & EntityItem::DIRTY_LIFETIME) { if (entity->isMortal()) { _mortalEntities.insert(entity); quint64 expiry = entity->getExpiry(); if (expiry < _nextExpiry) { _nextExpiry = expiry; } } else { _mortalEntities.remove(entity); } entity->clearDirtyFlags(EntityItem::DIRTY_LIFETIME); } if (entity->needsToCallUpdate()) { _entitiesToUpdate.insert(entity); } else { _entitiesToUpdate.remove(entity); } changeEntityInternal(entity); } }
// protected void EntitySimulation::callUpdateOnEntitiesThatNeedIt(const quint64& now) { PerformanceTimer perfTimer("updatingEntities"); SetOfEntities::iterator itemItr = _entitiesToUpdate.begin(); while (itemItr != _entitiesToUpdate.end()) { EntityItemPointer entity = *itemItr; // TODO: catch transition from needing update to not as a "change" // so we don't have to scan for it here. if (!entity->needsToCallUpdate()) { itemItr = _entitiesToUpdate.erase(itemItr); } else { entity->update(now); ++itemItr; } } }
void EntitySimulation::addEntity(EntityItemPointer entity) { assert(entity); entity->deserializeActions(); if (entity->isMortal()) { _mortalEntities.insert(entity); quint64 expiry = entity->getExpiry(); if (expiry < _nextExpiry) { _nextExpiry = expiry; } } if (entity->needsToCallUpdate()) { _entitiesToUpdate.insert(entity); } addEntityInternal(entity); _allEntities.insert(entity); entity->_simulated = true; // 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. entity->clearDirtyFlags(); }