From 1c636fe6b281f4e4549e1bcb82529691123b7893 Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Mon, 24 Nov 2014 20:37:09 -0800 Subject: [PATCH 1/2] more correct entity list maintenance --- libraries/entities/src/EntityTree.cpp | 49 ++++++++++++++++++++------- 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/libraries/entities/src/EntityTree.cpp b/libraries/entities/src/EntityTree.cpp index b25b153f44..bb201b6f86 100644 --- a/libraries/entities/src/EntityTree.cpp +++ b/libraries/entities/src/EntityTree.cpp @@ -653,7 +653,6 @@ void EntityTree::update() { } void EntityTree::updateChangedEntities(quint64 now, QSet& entitiesToDelete) { - // TODO: switch these to iterators so we can remove items that get deleted foreach (EntityItem* thisEntity, _changedEntities) { // check to see if the lifetime has expired, for immortal entities this is always false if (thisEntity->lifetimeHasExpired()) { @@ -675,15 +674,17 @@ void EntityTree::updateMovingEntities(quint64 now, QSet& entitiesT { PerformanceTimer perfTimer("_movingEntities"); - // TODO: switch these to iterators so we can remove items that get deleted - for (int i = 0; i < _movingEntities.size(); i++) { - EntityItem* thisEntity = _movingEntities[i]; + QList::iterator item_itr = _movingEntities.begin(); + while (item_itr != _movingEntities.end()) { + EntityItem* thisEntity = *item_itr; // always check to see if the lifetime has expired, for immortal entities this is always false if (thisEntity->lifetimeHasExpired()) { qDebug() << "Lifetime has expired for entity:" << thisEntity->getEntityItemID(); entitiesToDelete << thisEntity->getEntityItemID(); - clearEntityState(thisEntity); + // remove thisEntity from the list + item_itr = _movingEntities.erase(item_itr); + thisEntity->setSimulationState(EntityItem::Static); } else { AACube oldCube = thisEntity->getMaximumAACube(); thisEntity->update(now); @@ -694,10 +695,22 @@ void EntityTree::updateMovingEntities(quint64 now, QSet& entitiesT if (!domainBounds.touches(newCube)) { qDebug() << "Entity " << thisEntity->getEntityItemID() << " moved out of domain bounds."; entitiesToDelete << thisEntity->getEntityItemID(); - clearEntityState(thisEntity); + // remove thisEntity from the list + item_itr = _movingEntities.erase(item_itr); + thisEntity->setSimulationState(EntityItem::Static); } else { moveOperator.addEntityToMoveList(thisEntity, oldCube, newCube); - updateEntityState(thisEntity); + EntityItem::SimulationState newState = thisEntity->computeSimulationState(); + if (newState != EntityItem::Moving) { + if (newState == EntityItem::Mortal) { + _mortalEntities.push_back(thisEntity); + } + // remove thisEntity from the list + item_itr = _movingEntities.erase(item_itr); + thisEntity->setSimulationState(newState); + } else { + ++item_itr; + } } } } @@ -710,18 +723,30 @@ void EntityTree::updateMovingEntities(quint64 now, QSet& entitiesT } void EntityTree::updateMortalEntities(quint64 now, QSet& entitiesToDelete) { - // TODO: switch these to iterators so we can remove items that get deleted - for (int i = 0; i < _mortalEntities.size(); i++) { - EntityItem* thisEntity = _mortalEntities[i]; + QList::iterator item_itr = _mortalEntities.begin(); + while (item_itr != _mortalEntities.end()) { + EntityItem* thisEntity = *item_itr; thisEntity->update(now); // always check to see if the lifetime has expired, for immortal entities this is always false if (thisEntity->lifetimeHasExpired()) { qDebug() << "Lifetime has expired for entity:" << thisEntity->getEntityItemID(); entitiesToDelete << thisEntity->getEntityItemID(); - clearEntityState(thisEntity); + // remove thisEntity from the list + item_itr = _mortalEntities.erase(item_itr); + thisEntity->setSimulationState(EntityItem::Static); } else { // check to see if this entity is no longer moving - updateEntityState(thisEntity); + EntityItem::SimulationState newState = thisEntity->computeSimulationState(); + if (newState != EntityItem::Mortal) { + if (newState == EntityItem::Moving) { + _movingEntities.push_back(thisEntity); + } + // remove thisEntity from the list + item_itr = _mortalEntities.erase(item_itr); + thisEntity->setSimulationState(newState); + } else { + ++item_itr; + } } } } From 8966ab32d8cb0b40f77b19d65c15ef3d2f748ed1 Mon Sep 17 00:00:00 2001 From: Andrew Meadows Date: Mon, 24 Nov 2014 20:37:52 -0800 Subject: [PATCH 2/2] add lifetime to gun bullets --- examples/gun.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/examples/gun.js b/examples/gun.js index fff78496b2..2386e61539 100644 --- a/examples/gun.js +++ b/examples/gun.js @@ -96,6 +96,7 @@ function printVector(string, vector) { function shootBullet(position, velocity) { var BULLET_SIZE = 0.01; + var BULLET_LIFETIME = 20.0; var BULLET_GRAVITY = -0.02; Entities.addEntity( { type: "Sphere", @@ -103,6 +104,7 @@ function shootBullet(position, velocity) { dimensions: { x: BULLET_SIZE, y: BULLET_SIZE, z: BULLET_SIZE }, color: { red: 10, green: 10, blue: 10 }, velocity: velocity, + lifetime: BULLET_LIFETIME, gravity: { x: 0, y: BULLET_GRAVITY, z: 0 }, damping: 0 }); @@ -118,6 +120,7 @@ function shootBullet(position, velocity) { function shootTarget() { var TARGET_SIZE = 0.25; var TARGET_GRAVITY = -0.6; + var TARGET_LIFETIME = 300.0; var TARGET_UP_VELOCITY = 3.0; var TARGET_FWD_VELOCITY = 5.0; var DISTANCE_TO_LAUNCH_FROM = 3.0; @@ -140,7 +143,7 @@ function shootTarget() { color: { red: 0, green: 200, blue: 200 }, velocity: velocity, gravity: { x: 0, y: TARGET_GRAVITY, z: 0 }, - lifetime: 1000.0, + lifetime: TARGET_LIFETIME, damping: 0.99 }); // Record start time