Clean up faraway projectiles

experimental
MiroslavR 7 years ago
parent f15de6d3ca
commit b5f5268ff3

@ -149,6 +149,7 @@ namespace MWWorld
, mResourceSystem(resourceSystem) , mResourceSystem(resourceSystem)
, mRendering(rendering) , mRendering(rendering)
, mPhysics(physics) , mPhysics(physics)
, mCleanupTimer(0.0f)
{ {
} }
@ -326,10 +327,49 @@ namespace MWWorld
void ProjectileManager::update(float dt) void ProjectileManager::update(float dt)
{ {
periodicCleanup(dt);
moveProjectiles(dt); moveProjectiles(dt);
moveMagicBolts(dt); moveMagicBolts(dt);
} }
void ProjectileManager::periodicCleanup(float dt)
{
mCleanupTimer -= dt;
if (mCleanupTimer <= 0.0f)
{
mCleanupTimer = 2.0f;
auto isCleanable = [](const ProjectileManager::State& state) -> bool
{
const float farawayThreshold = 72000.0f;
osg::Vec3 playerPos = MWMechanics::getPlayer().getRefData().getPosition().asVec3();
return (state.mNode->getPosition() - playerPos).length2() >= farawayThreshold*farawayThreshold;
};
for (std::vector<ProjectileState>::iterator it = mProjectiles.begin(); it != mProjectiles.end();)
{
if (isCleanable(*it))
{
cleanupProjectile(*it);
it = mProjectiles.erase(it);
}
else
++it;
}
for (std::vector<MagicBoltState>::iterator it = mMagicBolts.begin(); it != mMagicBolts.end();)
{
if (isCleanable(*it))
{
cleanupMagicBolt(*it);
it = mMagicBolts.erase(it);
}
else
++it;
}
}
}
void ProjectileManager::moveMagicBolts(float duration) void ProjectileManager::moveMagicBolts(float duration)
{ {
for (std::vector<MagicBoltState>::iterator it = mMagicBolts.begin(); it != mMagicBolts.end();) for (std::vector<MagicBoltState>::iterator it = mMagicBolts.begin(); it != mMagicBolts.end();)
@ -468,20 +508,30 @@ namespace MWWorld
} }
} }
void ProjectileManager::cleanupProjectile(ProjectileManager::ProjectileState& state)
{
mParent->removeChild(state.mNode);
}
void ProjectileManager::cleanupMagicBolt(ProjectileManager::MagicBoltState& state)
{
mParent->removeChild(state.mNode);
for (size_t soundIter = 0; soundIter != state.mSounds.size(); soundIter++)
{
MWBase::Environment::get().getSoundManager()->stopSound(state.mSounds.at(soundIter));
}
}
void ProjectileManager::clear() void ProjectileManager::clear()
{ {
for (std::vector<ProjectileState>::iterator it = mProjectiles.begin(); it != mProjectiles.end(); ++it) for (std::vector<ProjectileState>::iterator it = mProjectiles.begin(); it != mProjectiles.end(); ++it)
{ {
mParent->removeChild(it->mNode); cleanupProjectile(*it);
} }
mProjectiles.clear(); mProjectiles.clear();
for (std::vector<MagicBoltState>::iterator it = mMagicBolts.begin(); it != mMagicBolts.end(); ++it) for (std::vector<MagicBoltState>::iterator it = mMagicBolts.begin(); it != mMagicBolts.end(); ++it)
{ {
mParent->removeChild(it->mNode); cleanupMagicBolt(*it);
for (size_t soundIter = 0; soundIter != it->mSounds.size(); soundIter++)
{
MWBase::Environment::get().getSoundManager()->stopSound(it->mSounds.at(soundIter));
}
} }
mMagicBolts.clear(); mMagicBolts.clear();
} }

@ -68,6 +68,7 @@ namespace MWWorld
Resource::ResourceSystem* mResourceSystem; Resource::ResourceSystem* mResourceSystem;
MWRender::RenderingManager* mRendering; MWRender::RenderingManager* mRendering;
MWPhysics::PhysicsSystem* mPhysics; MWPhysics::PhysicsSystem* mPhysics;
float mCleanupTimer;
struct State struct State
{ {
@ -116,6 +117,10 @@ namespace MWWorld
std::vector<MagicBoltState> mMagicBolts; std::vector<MagicBoltState> mMagicBolts;
std::vector<ProjectileState> mProjectiles; std::vector<ProjectileState> mProjectiles;
void cleanupProjectile(ProjectileState& state);
void cleanupMagicBolt(MagicBoltState& state);
void periodicCleanup(float dt);
void moveProjectiles(float dt); void moveProjectiles(float dt);
void moveMagicBolts(float dt); void moveMagicBolts(float dt);

Loading…
Cancel
Save