diff --git a/SDK/include/NDK/BaseSystem.hpp b/SDK/include/NDK/BaseSystem.hpp index 2a0157102..202095be4 100644 --- a/SDK/include/NDK/BaseSystem.hpp +++ b/SDK/include/NDK/BaseSystem.hpp @@ -33,10 +33,15 @@ namespace Ndk inline const std::vector& 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 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; }; diff --git a/SDK/include/NDK/BaseSystem.inl b/SDK/include/NDK/BaseSystem.inl index 2d43bc15c..ceacd1e78 100644 --- a/SDK/include/NDK/BaseSystem.inl +++ b/SDK/include/NDK/BaseSystem.inl @@ -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 void BaseSystem::Excludes() { diff --git a/SDK/include/NDK/Systems/ListenerSystem.hpp b/SDK/include/NDK/Systems/ListenerSystem.hpp index a3de7b149..6f40fcdf9 100644 --- a/SDK/include/NDK/Systems/ListenerSystem.hpp +++ b/SDK/include/NDK/Systems/ListenerSystem.hpp @@ -17,9 +17,10 @@ namespace Ndk ListenerSystem(); ~ListenerSystem() = default; - void Update(float elapsedTime); - static SystemIndex systemIndex; + + private: + void OnUpdate(float elapsedTime) override; }; } diff --git a/SDK/include/NDK/Systems/PhysicsSystem.hpp b/SDK/include/NDK/Systems/PhysicsSystem.hpp index 227ee1f9d..95d4bfe13 100644 --- a/SDK/include/NDK/Systems/PhysicsSystem.hpp +++ b/SDK/include/NDK/Systems/PhysicsSystem.hpp @@ -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; diff --git a/SDK/include/NDK/Systems/RenderSystem.hpp b/SDK/include/NDK/Systems/RenderSystem.hpp index 522f1c775..6952abc3f 100644 --- a/SDK/include/NDK/Systems/RenderSystem.hpp +++ b/SDK/include/NDK/Systems/RenderSystem.hpp @@ -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; diff --git a/SDK/include/NDK/Systems/VelocitySystem.hpp b/SDK/include/NDK/Systems/VelocitySystem.hpp index b297d1959..71edcbe7e 100644 --- a/SDK/include/NDK/Systems/VelocitySystem.hpp +++ b/SDK/include/NDK/Systems/VelocitySystem.hpp @@ -17,9 +17,10 @@ namespace Ndk VelocitySystem(); ~VelocitySystem() = default; - void Update(float elapsedTime); - static SystemIndex systemIndex; + + private: + void OnUpdate(float elapsedTime) override; }; } diff --git a/SDK/include/NDK/World.hpp b/SDK/include/NDK/World.hpp index 55c06d0ea..794f5fb2a 100644 --- a/SDK/include/NDK/World.hpp +++ b/SDK/include/NDK/World.hpp @@ -58,6 +58,7 @@ namespace Ndk template void RemoveSystem(); void Update(); + inline void Update(float elapsedTime); private: inline void Invalidate(); diff --git a/SDK/include/NDK/World.inl b/SDK/include/NDK/World.inl index f15cbed47..3040e3d6c 100644 --- a/SDK/include/NDK/World.inl +++ b/SDK/include/NDK/World.inl @@ -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); diff --git a/SDK/src/NDK/Systems/ListenerSystem.cpp b/SDK/src/NDK/Systems/ListenerSystem.cpp index d7cede524..a943b55e5 100644 --- a/SDK/src/NDK/Systems/ListenerSystem.cpp +++ b/SDK/src/NDK/Systems/ListenerSystem.cpp @@ -15,7 +15,7 @@ namespace Ndk Requires(); } - void ListenerSystem::Update(float elapsedTime) + void ListenerSystem::OnUpdate(float elapsedTime) { NazaraUnused(elapsedTime); diff --git a/SDK/src/NDK/Systems/PhysicsSystem.cpp b/SDK/src/NDK/Systems/PhysicsSystem.cpp index a54c776a2..150aaaac7 100644 --- a/SDK/src/NDK/Systems/PhysicsSystem.cpp +++ b/SDK/src/NDK/Systems/PhysicsSystem.cpp @@ -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()) ? m_staticObjects : m_dynamicObjects; + entities.Remove(entity); + } + + auto& entities = (entity->HasComponent()) ? 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()) ? m_staticObjects : m_dynamicObjects; - entities.Remove(entity); - } - - auto& entities = (entity->HasComponent()) ? m_dynamicObjects : m_staticObjects; - entities.Insert(entity); - } - SystemIndex PhysicsSystem::systemIndex; } diff --git a/SDK/src/NDK/Systems/RenderSystem.cpp b/SDK/src/NDK/Systems/RenderSystem.cpp index 1f781e951..41efa7462 100644 --- a/SDK/src/NDK/Systems/RenderSystem.cpp +++ b/SDK/src/NDK/Systems/RenderSystem.cpp @@ -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() && entity->HasComponent()) + { + m_cameras.Insert(entity); + std::sort(m_cameras.begin(), m_cameras.end(), [](const EntityHandle& handle1, const EntityHandle& handle2) + { + return handle1->GetComponent().GetLayer() < handle2->GetComponent().GetLayer(); + }); + } + else + m_cameras.Remove(entity); + + if (entity->HasComponent() && entity->HasComponent()) + m_drawables.Insert(entity); + else + m_drawables.Remove(entity); + + if (entity->HasComponent() && entity->HasComponent()) + 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() && entity->HasComponent()) - { - m_cameras.Insert(entity); - std::sort(m_cameras.begin(), m_cameras.end(), [](const EntityHandle& handle1, const EntityHandle& handle2) - { - return handle1->GetComponent().GetLayer() < handle2->GetComponent().GetLayer(); - }); - } - else - m_cameras.Remove(entity); - - if (entity->HasComponent() && entity->HasComponent()) - m_drawables.Insert(entity); - else - m_drawables.Remove(entity); - - if (entity->HasComponent() && entity->HasComponent()) - m_lights.Insert(entity); - else - m_lights.Remove(entity); - } - SystemIndex RenderSystem::systemIndex; } diff --git a/SDK/src/NDK/Systems/VelocitySystem.cpp b/SDK/src/NDK/Systems/VelocitySystem.cpp index a3968a053..882b45728 100644 --- a/SDK/src/NDK/Systems/VelocitySystem.cpp +++ b/SDK/src/NDK/Systems/VelocitySystem.cpp @@ -15,7 +15,7 @@ namespace Ndk Excludes(); } - void VelocitySystem::Update(float elapsedTime) + void VelocitySystem::OnUpdate(float elapsedTime) { for (const Ndk::EntityHandle& entity : GetEntities()) {