cleanup motionstate on EntityItem delete

This commit is contained in:
Andrew Meadows 2014-11-14 09:04:09 -08:00
parent 1a094f2b51
commit 40b9416810
3 changed files with 16 additions and 4 deletions

View file

@ -37,7 +37,7 @@ EntityTreeElement* EntityTree::createNewElement(unsigned char * octalCode) {
void EntityTree::eraseAllOctreeElements(bool createNewRoot) {
// this would be a good place to clean up our entities...
foreach (EntityTreeElement* element, _entityToElementMap) {
element->cleanupEntities();
element->cleanupEntities(_physicsWorld);
}
_entityToElementMap.clear();
Octree::eraseAllOctreeElements(createNewRoot);
@ -91,6 +91,7 @@ void EntityTree::addEntityItem(EntityItem* entityItem) {
recurseTreeWithOperator(&theOperator);
// check to see if we need to simulate this entity..
// BOOKMARK -- add entity to physics engine here
changeEntityState(entityItem, EntityItem::Static, entityItem->getSimulationState());
_isDirty = true;
@ -133,7 +134,9 @@ bool EntityTree::updateEntity(const EntityItemID& entityID, const EntityItemProp
_isDirty = true;
EntityItem::SimulationState newState = existingEntity->getSimulationState();
changeEntityState(existingEntity, oldState, newState);
if (newState != oldState) {
changeEntityState(existingEntity, oldState, newState);
}
QString entityScriptAfter = existingEntity->getScript();
if (entityScriptBefore != entityScriptAfter) {

View file

@ -16,7 +16,9 @@
#include <FBXReader.h>
#include <GeometryUtil.h>
#include <PhysicsWorld.h>
#include "EntityMotionState.h"
#include "EntityTree.h"
#include "EntityTreeElement.h"
@ -653,10 +655,16 @@ EntityItem* EntityTreeElement::getEntityWithEntityItemID(const EntityItemID& id)
return foundEntity;
}
void EntityTreeElement::cleanupEntities() {
void EntityTreeElement::cleanupEntities(PhysicsWorld* physicsWorld) {
uint16_t numberOfEntities = _entityItems->size();
for (uint16_t i = 0; i < numberOfEntities; i++) {
EntityItem* entity = (*_entityItems)[i];
EntityMotionState* motionState = entity->getMotionState();
if (motionState) {
assert(physicsWorld);
physicsWorld->removeEntity(static_cast<CustomMotionState*>(motionState));
entity->destroyMotionState();
}
delete entity;
}
_entityItems->clear();

View file

@ -21,6 +21,7 @@
class EntityTree;
class EntityTreeElement;
class PhysicsWorld;
class EntityTreeUpdateArgs {
public:
@ -175,7 +176,7 @@ public:
EntityItem* getEntityWithEntityItemID(const EntityItemID& id);
void cleanupEntities(); /// called by EntityTree on cleanup this will free all entities
void cleanupEntities(PhysicsWorld* physicsWorld);
bool removeEntityWithEntityItemID(const EntityItemID& id);
bool removeEntityItem(EntityItem* entity);