Sdk/PhysicsSystem: Create PhysWorld only if required by entities

This commit is contained in:
Lynix 2016-10-13 05:29:48 +02:00
parent 9f771f8c01
commit debf87e739
3 changed files with 13 additions and 6 deletions

View File

@ -10,6 +10,7 @@
#include <Nazara/Physics/PhysWorld.hpp>
#include <NDK/EntityList.hpp>
#include <NDK/System.hpp>
#include <memory>
namespace Ndk
{
@ -31,7 +32,7 @@ namespace Ndk
EntityList m_dynamicObjects;
EntityList m_staticObjects;
Nz::PhysWorld m_world;
std::unique_ptr<Nz::PhysWorld> m_world; ///TODO: std::optional (Should I make a Nz::Optional class?)
};
}

View File

@ -11,7 +11,7 @@ namespace Ndk
inline Nz::PhysWorld& PhysicsSystem::GetWorld()
{
return m_world;
return *m_world;
}
/*!
@ -21,6 +21,6 @@ namespace Ndk
inline const Nz::PhysWorld& PhysicsSystem::GetWorld() const
{
return m_world;
return *m_world;
}
}

View File

@ -50,16 +50,19 @@ namespace Ndk
void PhysicsSystem::OnEntityValidation(Entity* entity, bool justAdded)
{
// If entity has not been just added to the system, it is possible that it does not own to the right array
// It's possible our entity got revalidated because of the addition/removal of a PhysicsComponent
if (!justAdded)
{
// We take the inverted array from which the entity should belong to
// We take the opposite array from which the entity should belong to
auto& entities = (entity->HasComponent<PhysicsComponent>()) ? m_staticObjects : m_dynamicObjects;
entities.Remove(entity);
}
auto& entities = (entity->HasComponent<PhysicsComponent>()) ? m_dynamicObjects : m_staticObjects;
entities.Insert(entity);
if (!m_world)
m_world = std::make_unique<Nz::PhysWorld>();
}
/*!
@ -70,7 +73,10 @@ namespace Ndk
void PhysicsSystem::OnUpdate(float elapsedTime)
{
m_world.Step(elapsedTime);
if (!m_world)
return;
m_world->Step(elapsedTime);
for (const Ndk::EntityHandle& entity : m_dynamicObjects)
{