fix incorrect/premature destruction of entities

This commit is contained in:
Nick Fisher
2023-12-15 23:38:38 +08:00
parent 59d0e64d04
commit f5244975df
2 changed files with 13 additions and 6 deletions

View File

@@ -111,8 +111,6 @@ namespace polyvox
vector<SceneAsset> _assets; vector<SceneAsset> _assets;
tsl::robin_map<EntityId, int> _entityIdLookup; tsl::robin_map<EntityId, int> _entityIdLookup;
utils::Entity findEntityByName( utils::Entity findEntityByName(
SceneAsset asset, SceneAsset asset,
const char *entityName); const char *entityName);

View File

@@ -510,6 +510,7 @@ namespace polyvox
void AssetManager::remove(EntityId entityId) void AssetManager::remove(EntityId entityId)
{ {
std::lock_guard lock(_animationMutex);
const auto &pos = _entityIdLookup.find(entityId); const auto &pos = _entityIdLookup.find(entityId);
if (pos == _entityIdLookup.end()) if (pos == _entityIdLookup.end())
{ {
@@ -518,10 +519,14 @@ namespace polyvox
} }
SceneAsset &sceneAsset = _assets[pos->second]; SceneAsset &sceneAsset = _assets[pos->second];
_assets.erase(std::remove_if(_assets.begin(), _assets.end(),
[=](SceneAsset &asset) _entityIdLookup.erase(entityId);
{ return asset.asset == sceneAsset.asset; }),
_assets.end()); for(auto entityPos : _entityIdLookup) {
if(entityPos.second > pos->second) {
_entityIdLookup[entityPos.first] = entityPos.second-1;
}
}
_scene->removeEntities(sceneAsset.asset->getEntities(), _scene->removeEntities(sceneAsset.asset->getEntities(),
sceneAsset.asset->getEntityCount()); sceneAsset.asset->getEntityCount());
@@ -537,6 +542,10 @@ namespace polyvox
} }
EntityManager &em = EntityManager::get(); EntityManager &em = EntityManager::get();
em.destroy(Entity::import(entityId)); em.destroy(Entity::import(entityId));
_assets.erase(std::remove_if(_assets.begin(), _assets.end(),
[=](SceneAsset &asset)
{ return asset.asset == sceneAsset.asset; }),
_assets.end());
} }
void AssetManager::setMorphTargetWeights(EntityId entityId, const char *const entityName, const float *const weights, const int count) void AssetManager::setMorphTargetWeights(EntityId entityId, const char *const entityName, const float *const weights, const int count)