Sdk: Add automatic system update

Former-commit-id: ce129cc945e7373ac5b9f48960894712d66b097a
This commit is contained in:
Lynix 2015-06-22 00:58:46 +02:00
parent 4b5c08e739
commit 90363406a6
12 changed files with 101 additions and 58 deletions

View File

@ -33,10 +33,15 @@ namespace Ndk
inline const std::vector<EntityHandle>& GetEntities() const;
inline SystemIndex GetIndex() const;
inline float GetUpdateRate() const;
inline World& GetWorld() const;
inline bool HasEntity(const Entity* entity) const;
inline void SetUpdateRate(float updatePerSecond);
inline void Update(float elapsedTime);
BaseSystem& operator=(const BaseSystem&) = delete;
BaseSystem& operator=(BaseSystem&&) noexcept = default;
@ -55,6 +60,8 @@ namespace Ndk
template<typename ComponentType1, typename ComponentType2, typename... Rest> void RequiresAny();
inline void RequiresAnyComponent(ComponentIndex index);
virtual void OnUpdate(float elapsedTime) = 0;
private:
inline void AddEntity(Entity* entity);
@ -79,6 +86,8 @@ namespace Ndk
NzBitset<> m_requiredComponents;
SystemIndex m_systemIndex;
World* m_world;
float m_updateCounter;
float m_updateRate;
static SystemIndex s_nextIndex;
};

View File

@ -10,12 +10,15 @@ namespace Ndk
inline BaseSystem::BaseSystem(SystemIndex systemId) :
m_systemIndex(systemId)
{
SetUpdateRate(30);
}
inline BaseSystem::BaseSystem(const BaseSystem& system) :
m_excludedComponents(system.m_excludedComponents),
m_requiredComponents(system.m_requiredComponents),
m_systemIndex(system.m_systemIndex)
m_systemIndex(system.m_systemIndex),
m_updateCounter(0.f),
m_updateRate(system.m_updateRate)
{
}
@ -29,6 +32,11 @@ namespace Ndk
return m_systemIndex;
}
inline float BaseSystem::GetUpdateRate() const
{
return 1.f / m_updateRate;
}
inline World& BaseSystem::GetWorld() const
{
return *m_world;
@ -42,6 +50,22 @@ namespace Ndk
return m_entityBits.UnboundedTest(entity->GetId());
}
inline void BaseSystem::SetUpdateRate(float updatePerSecond)
{
m_updateCounter = 0.f;
m_updateRate = (updatePerSecond > 0.f) ? 1.f / updatePerSecond : 0.f; // 0.f means no limit
}
inline void BaseSystem::Update(float elapsedTime)
{
m_updateCounter -= elapsedTime;
if (m_updateCounter < 0.f)
{
m_updateCounter += m_updateRate;
OnUpdate(elapsedTime);
}
}
template<typename ComponentType>
void BaseSystem::Excludes()
{

View File

@ -17,9 +17,10 @@ namespace Ndk
ListenerSystem();
~ListenerSystem() = default;
void Update(float elapsedTime);
static SystemIndex systemIndex;
private:
void OnUpdate(float elapsedTime) override;
};
}

View File

@ -23,12 +23,11 @@ namespace Ndk
NzPhysWorld& GetWorld();
const NzPhysWorld& GetWorld() const;
void Update(float elapsedTime);
static SystemIndex systemIndex;
private:
void OnEntityValidation(Entity* entity, bool justAdded) override;
void OnUpdate(float elapsedTime) override;
EntityList m_dynamicObjects;
EntityList m_staticObjects;

View File

@ -24,13 +24,12 @@ namespace Ndk
inline RenderSystem(const RenderSystem& renderSystem);
~RenderSystem() = default;
void Update(float elapsedTime);
static SystemIndex systemIndex;
private:
void OnEntityRemoved(Entity* entity) override;
void OnEntityValidation(Entity* entity, bool justAdded) override;
void OnUpdate(float elapsedTime) override;
EntityList m_cameras;
EntityList m_drawables;

View File

@ -17,9 +17,10 @@ namespace Ndk
VelocitySystem();
~VelocitySystem() = default;
void Update(float elapsedTime);
static SystemIndex systemIndex;
private:
void OnUpdate(float elapsedTime) override;
};
}

View File

@ -58,6 +58,7 @@ namespace Ndk
template<typename SystemType> void RemoveSystem();
void Update();
inline void Update(float elapsedTime);
private:
inline void Invalidate();

View File

@ -130,6 +130,15 @@ namespace Ndk
RemoveSystem(index);
}
inline void World::Update(float elapsedTime)
{
Update(); //< Update entities
// And then update systems
for (auto& systemPtr : m_systems)
systemPtr->Update(elapsedTime);
}
inline void World::Invalidate()
{
m_dirtyEntities.Resize(m_entities.size(), false);

View File

@ -15,7 +15,7 @@ namespace Ndk
Requires<ListenerComponent, NodeComponent>();
}
void ListenerSystem::Update(float elapsedTime)
void ListenerSystem::OnUpdate(float elapsedTime)
{
NazaraUnused(elapsedTime);

View File

@ -22,7 +22,21 @@ namespace Ndk
{
}
void PhysicsSystem::Update(float elapsedTime)
void PhysicsSystem::OnEntityValidation(Entity* entity, bool justAdded)
{
// Si l'entité ne vient pas d'être ajoutée au système, il est possible qu'elle fasse partie du mauvais tableau
if (!justAdded)
{
// On prend le tableau inverse de celui dont l'entité devrait faire partie
auto& entities = (entity->HasComponent<PhysicsComponent>()) ? m_staticObjects : m_dynamicObjects;
entities.Remove(entity);
}
auto& entities = (entity->HasComponent<PhysicsComponent>()) ? m_dynamicObjects : m_staticObjects;
entities.Insert(entity);
}
void PhysicsSystem::OnUpdate(float elapsedTime)
{
m_world.Step(elapsedTime);
@ -75,19 +89,5 @@ namespace Ndk
}
}
void PhysicsSystem::OnEntityValidation(Entity* entity, bool justAdded)
{
// Si l'entité ne vient pas d'être ajoutée au système, il est possible qu'elle fasse partie du mauvais tableau
if (!justAdded)
{
// On prend le tableau inverse de celui dont l'entité devrait faire partie
auto& entities = (entity->HasComponent<PhysicsComponent>()) ? m_staticObjects : m_dynamicObjects;
entities.Remove(entity);
}
auto& entities = (entity->HasComponent<PhysicsComponent>()) ? m_dynamicObjects : m_staticObjects;
entities.Insert(entity);
}
SystemIndex PhysicsSystem::systemIndex;
}

View File

@ -15,7 +15,38 @@ namespace Ndk
{
}
void RenderSystem::Update(float elapsedTime)
void RenderSystem::OnEntityRemoved(Entity* entity)
{
m_cameras.Remove(entity);
m_drawables.Remove(entity);
m_lights.Remove(entity);
}
void RenderSystem::OnEntityValidation(Entity* entity, bool justAdded)
{
if (entity->HasComponent<CameraComponent>() && entity->HasComponent<NodeComponent>())
{
m_cameras.Insert(entity);
std::sort(m_cameras.begin(), m_cameras.end(), [](const EntityHandle& handle1, const EntityHandle& handle2)
{
return handle1->GetComponent<CameraComponent>().GetLayer() < handle2->GetComponent<CameraComponent>().GetLayer();
});
}
else
m_cameras.Remove(entity);
if (entity->HasComponent<GraphicsComponent>() && entity->HasComponent<NodeComponent>())
m_drawables.Insert(entity);
else
m_drawables.Remove(entity);
if (entity->HasComponent<LightComponent>() && entity->HasComponent<NodeComponent>())
m_lights.Insert(entity);
else
m_lights.Remove(entity);
}
void RenderSystem::OnUpdate(float elapsedTime)
{
for (const Ndk::EntityHandle& camera : m_cameras)
{
@ -52,36 +83,5 @@ namespace Ndk
}
}
void RenderSystem::OnEntityRemoved(Entity* entity)
{
m_cameras.Remove(entity);
m_drawables.Remove(entity);
m_lights.Remove(entity);
}
void RenderSystem::OnEntityValidation(Entity* entity, bool justAdded)
{
if (entity->HasComponent<CameraComponent>() && entity->HasComponent<NodeComponent>())
{
m_cameras.Insert(entity);
std::sort(m_cameras.begin(), m_cameras.end(), [](const EntityHandle& handle1, const EntityHandle& handle2)
{
return handle1->GetComponent<CameraComponent>().GetLayer() < handle2->GetComponent<CameraComponent>().GetLayer();
});
}
else
m_cameras.Remove(entity);
if (entity->HasComponent<GraphicsComponent>() && entity->HasComponent<NodeComponent>())
m_drawables.Insert(entity);
else
m_drawables.Remove(entity);
if (entity->HasComponent<LightComponent>() && entity->HasComponent<NodeComponent>())
m_lights.Insert(entity);
else
m_lights.Remove(entity);
}
SystemIndex RenderSystem::systemIndex;
}

View File

@ -15,7 +15,7 @@ namespace Ndk
Excludes<PhysicsComponent>();
}
void VelocitySystem::Update(float elapsedTime)
void VelocitySystem::OnUpdate(float elapsedTime)
{
for (const Ndk::EntityHandle& entity : GetEntities())
{