Sdk: Add automatic system update
Former-commit-id: ce129cc945e7373ac5b9f48960894712d66b097a
This commit is contained in:
parent
4b5c08e739
commit
90363406a6
|
|
@ -33,10 +33,15 @@ namespace Ndk
|
||||||
|
|
||||||
inline const std::vector<EntityHandle>& GetEntities() const;
|
inline const std::vector<EntityHandle>& GetEntities() const;
|
||||||
inline SystemIndex GetIndex() const;
|
inline SystemIndex GetIndex() const;
|
||||||
|
inline float GetUpdateRate() const;
|
||||||
inline World& GetWorld() const;
|
inline World& GetWorld() const;
|
||||||
|
|
||||||
inline bool HasEntity(const Entity* entity) 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=(const BaseSystem&) = delete;
|
||||||
BaseSystem& operator=(BaseSystem&&) noexcept = default;
|
BaseSystem& operator=(BaseSystem&&) noexcept = default;
|
||||||
|
|
||||||
|
|
@ -55,6 +60,8 @@ namespace Ndk
|
||||||
template<typename ComponentType1, typename ComponentType2, typename... Rest> void RequiresAny();
|
template<typename ComponentType1, typename ComponentType2, typename... Rest> void RequiresAny();
|
||||||
inline void RequiresAnyComponent(ComponentIndex index);
|
inline void RequiresAnyComponent(ComponentIndex index);
|
||||||
|
|
||||||
|
virtual void OnUpdate(float elapsedTime) = 0;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
inline void AddEntity(Entity* entity);
|
inline void AddEntity(Entity* entity);
|
||||||
|
|
||||||
|
|
@ -79,6 +86,8 @@ namespace Ndk
|
||||||
NzBitset<> m_requiredComponents;
|
NzBitset<> m_requiredComponents;
|
||||||
SystemIndex m_systemIndex;
|
SystemIndex m_systemIndex;
|
||||||
World* m_world;
|
World* m_world;
|
||||||
|
float m_updateCounter;
|
||||||
|
float m_updateRate;
|
||||||
|
|
||||||
static SystemIndex s_nextIndex;
|
static SystemIndex s_nextIndex;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -10,12 +10,15 @@ namespace Ndk
|
||||||
inline BaseSystem::BaseSystem(SystemIndex systemId) :
|
inline BaseSystem::BaseSystem(SystemIndex systemId) :
|
||||||
m_systemIndex(systemId)
|
m_systemIndex(systemId)
|
||||||
{
|
{
|
||||||
|
SetUpdateRate(30);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline BaseSystem::BaseSystem(const BaseSystem& system) :
|
inline BaseSystem::BaseSystem(const BaseSystem& system) :
|
||||||
m_excludedComponents(system.m_excludedComponents),
|
m_excludedComponents(system.m_excludedComponents),
|
||||||
m_requiredComponents(system.m_requiredComponents),
|
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;
|
return m_systemIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline float BaseSystem::GetUpdateRate() const
|
||||||
|
{
|
||||||
|
return 1.f / m_updateRate;
|
||||||
|
}
|
||||||
|
|
||||||
inline World& BaseSystem::GetWorld() const
|
inline World& BaseSystem::GetWorld() const
|
||||||
{
|
{
|
||||||
return *m_world;
|
return *m_world;
|
||||||
|
|
@ -42,6 +50,22 @@ namespace Ndk
|
||||||
return m_entityBits.UnboundedTest(entity->GetId());
|
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>
|
template<typename ComponentType>
|
||||||
void BaseSystem::Excludes()
|
void BaseSystem::Excludes()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -17,9 +17,10 @@ namespace Ndk
|
||||||
ListenerSystem();
|
ListenerSystem();
|
||||||
~ListenerSystem() = default;
|
~ListenerSystem() = default;
|
||||||
|
|
||||||
void Update(float elapsedTime);
|
|
||||||
|
|
||||||
static SystemIndex systemIndex;
|
static SystemIndex systemIndex;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void OnUpdate(float elapsedTime) override;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -23,12 +23,11 @@ namespace Ndk
|
||||||
NzPhysWorld& GetWorld();
|
NzPhysWorld& GetWorld();
|
||||||
const NzPhysWorld& GetWorld() const;
|
const NzPhysWorld& GetWorld() const;
|
||||||
|
|
||||||
void Update(float elapsedTime);
|
|
||||||
|
|
||||||
static SystemIndex systemIndex;
|
static SystemIndex systemIndex;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void OnEntityValidation(Entity* entity, bool justAdded) override;
|
void OnEntityValidation(Entity* entity, bool justAdded) override;
|
||||||
|
void OnUpdate(float elapsedTime) override;
|
||||||
|
|
||||||
EntityList m_dynamicObjects;
|
EntityList m_dynamicObjects;
|
||||||
EntityList m_staticObjects;
|
EntityList m_staticObjects;
|
||||||
|
|
|
||||||
|
|
@ -24,13 +24,12 @@ namespace Ndk
|
||||||
inline RenderSystem(const RenderSystem& renderSystem);
|
inline RenderSystem(const RenderSystem& renderSystem);
|
||||||
~RenderSystem() = default;
|
~RenderSystem() = default;
|
||||||
|
|
||||||
void Update(float elapsedTime);
|
|
||||||
|
|
||||||
static SystemIndex systemIndex;
|
static SystemIndex systemIndex;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void OnEntityRemoved(Entity* entity) override;
|
void OnEntityRemoved(Entity* entity) override;
|
||||||
void OnEntityValidation(Entity* entity, bool justAdded) override;
|
void OnEntityValidation(Entity* entity, bool justAdded) override;
|
||||||
|
void OnUpdate(float elapsedTime) override;
|
||||||
|
|
||||||
EntityList m_cameras;
|
EntityList m_cameras;
|
||||||
EntityList m_drawables;
|
EntityList m_drawables;
|
||||||
|
|
|
||||||
|
|
@ -17,9 +17,10 @@ namespace Ndk
|
||||||
VelocitySystem();
|
VelocitySystem();
|
||||||
~VelocitySystem() = default;
|
~VelocitySystem() = default;
|
||||||
|
|
||||||
void Update(float elapsedTime);
|
|
||||||
|
|
||||||
static SystemIndex systemIndex;
|
static SystemIndex systemIndex;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void OnUpdate(float elapsedTime) override;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -58,6 +58,7 @@ namespace Ndk
|
||||||
template<typename SystemType> void RemoveSystem();
|
template<typename SystemType> void RemoveSystem();
|
||||||
|
|
||||||
void Update();
|
void Update();
|
||||||
|
inline void Update(float elapsedTime);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
inline void Invalidate();
|
inline void Invalidate();
|
||||||
|
|
|
||||||
|
|
@ -130,6 +130,15 @@ namespace Ndk
|
||||||
RemoveSystem(index);
|
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()
|
inline void World::Invalidate()
|
||||||
{
|
{
|
||||||
m_dirtyEntities.Resize(m_entities.size(), false);
|
m_dirtyEntities.Resize(m_entities.size(), false);
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ namespace Ndk
|
||||||
Requires<ListenerComponent, NodeComponent>();
|
Requires<ListenerComponent, NodeComponent>();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ListenerSystem::Update(float elapsedTime)
|
void ListenerSystem::OnUpdate(float elapsedTime)
|
||||||
{
|
{
|
||||||
NazaraUnused(elapsedTime);
|
NazaraUnused(elapsedTime);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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);
|
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;
|
SystemIndex PhysicsSystem::systemIndex;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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)
|
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;
|
SystemIndex RenderSystem::systemIndex;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ namespace Ndk
|
||||||
Excludes<PhysicsComponent>();
|
Excludes<PhysicsComponent>();
|
||||||
}
|
}
|
||||||
|
|
||||||
void VelocitySystem::Update(float elapsedTime)
|
void VelocitySystem::OnUpdate(float elapsedTime)
|
||||||
{
|
{
|
||||||
for (const Ndk::EntityHandle& entity : GetEntities())
|
for (const Ndk::EntityHandle& entity : GetEntities())
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue