This commit is contained in:
Lynix 2018-04-09 20:05:36 +02:00
commit 1e88f5ddf8
3 changed files with 76 additions and 4 deletions

View File

@ -121,6 +121,8 @@ Nazara Development Kit:
- CollisionComponent[2D|3D] and PhysicsComponent[2D|3D] now configures their internal RigidBody userdata to the entity ID they belong to (useful for callbacks). - CollisionComponent[2D|3D] and PhysicsComponent[2D|3D] now configures their internal RigidBody userdata to the entity ID they belong to (useful for callbacks).
- Fixed EntityList copy/movement assignment operator which was not properly unregistering contained entities. - Fixed EntityList copy/movement assignment operator which was not properly unregistering contained entities.
- ListenerSystem now handles velocity in a generic way (no longer require a VelocityComponent and is compatible with physics) - ListenerSystem now handles velocity in a generic way (no longer require a VelocityComponent and is compatible with physics)
- World now has const getters for systems
- Add World::ForEachSystem method, allowing iteration on every active system on a specific world
# 0.4: # 0.4:

View File

@ -50,11 +50,16 @@ namespace Ndk
inline void DisableProfiler(); inline void DisableProfiler();
inline void EnableProfiler(bool enable = true); inline void EnableProfiler(bool enable = true);
template<typename F> void ForEachSystem(const F& iterationFunc);
template<typename F> void ForEachSystem(const F& iterationFunc) const;
inline const EntityHandle& GetEntity(EntityId id); inline const EntityHandle& GetEntity(EntityId id);
inline const EntityList& GetEntities() const; inline const EntityList& GetEntities() const;
inline const ProfilerData& GetProfilerData() const; inline const ProfilerData& GetProfilerData() const;
inline BaseSystem& GetSystem(SystemIndex index); inline BaseSystem& GetSystem(SystemIndex index);
inline const BaseSystem& GetSystem(SystemIndex index) const;
template<typename SystemType> SystemType& GetSystem(); template<typename SystemType> SystemType& GetSystem();
template<typename SystemType> const SystemType& GetSystem() const;
inline bool HasSystem(SystemIndex index) const; inline bool HasSystem(SystemIndex index) const;
template<typename SystemType> bool HasSystem() const; template<typename SystemType> bool HasSystem() const;

View File

@ -133,6 +133,40 @@ namespace Ndk
} }
} }
/*!
* \brief Executes a function on every present system
*
* Calls iterationFunc on every previously added system, in the same order as their indexes
*
* \param iterationFunc Function to be called
*/
template<typename F>
void World::ForEachSystem(const F& iterationFunc)
{
for (const auto& systemPtr : m_systems)
{
if (systemPtr)
iterationFunc(*systemPtr);
}
}
/*!
* \brief Executes a function on every present system
*
* Calls iterationFunc on every previously added system, in the same order as their indexes
*
* \param iterationFunc Function to be called
*/
template<typename F>
void World::ForEachSystem(const F& iterationFunc) const
{
for (const auto& systemPtr : m_systems)
{
if (systemPtr)
iterationFunc(static_cast<const Ndk::BaseSystem&>(*systemPtr)); //< Force const reference
}
}
/*! /*!
* \brief Gets an entity * \brief Gets an entity
* \return A constant reference to a handle of the entity * \return A constant reference to a handle of the entity
@ -177,9 +211,8 @@ namespace Ndk
* *
* \param index Index of the system * \param index Index of the system
* *
* \remark Produces a NazaraAssert if system is not available in this world * \remark The world must have the system before calling this function
*/ */
inline BaseSystem& World::GetSystem(SystemIndex index) inline BaseSystem& World::GetSystem(SystemIndex index)
{ {
NazaraAssert(HasSystem(index), "This system is not part of the world"); NazaraAssert(HasSystem(index), "This system is not part of the world");
@ -190,13 +223,30 @@ namespace Ndk
return *system; return *system;
} }
/*!
* \brief Gets a system in the world by index
* \return A const reference to the system
*
* \param index Index of the system
*
* \remark The world must have the system before calling this function
*/
inline const BaseSystem& World::GetSystem(SystemIndex index) const
{
NazaraAssert(HasSystem(index), "This system is not part of the world");
const BaseSystem* system = m_systems[index].get();
NazaraAssert(system, "Invalid system pointer");
return *system;
}
/*! /*!
* \brief Gets a system in the world by type * \brief Gets a system in the world by type
* \return A reference to the system * \return A reference to the system
* *
* \remark Produces a NazaraAssert if system is not available in this world * \remark Produces a NazaraAssert if system is not available in this world
*/ */
template<typename SystemType> template<typename SystemType>
SystemType& World::GetSystem() SystemType& World::GetSystem()
{ {
@ -206,6 +256,21 @@ namespace Ndk
return static_cast<SystemType&>(GetSystem(index)); return static_cast<SystemType&>(GetSystem(index));
} }
/*!
* \brief Gets a system in the world by type
* \return A const reference to the system
*
* \remark Produces a NazaraAssert if system is not available in this world
*/
template<typename SystemType>
const SystemType& World::GetSystem() const
{
static_assert(std::is_base_of<BaseSystem, SystemType>::value, "SystemType is not a system");
SystemIndex index = GetSystemIndex<SystemType>();
return static_cast<const SystemType&>(GetSystem(index));
}
/*! /*!
* \brief Checks whether or not a system is present in the world by index * \brief Checks whether or not a system is present in the world by index
* \return true If it is the case * \return true If it is the case
@ -361,7 +426,7 @@ namespace Ndk
m_orderedSystems = std::move(world.m_orderedSystems); m_orderedSystems = std::move(world.m_orderedSystems);
m_orderedSystemsUpdated = world.m_orderedSystemsUpdated; m_orderedSystemsUpdated = world.m_orderedSystemsUpdated;
m_profilerData = std::move(world.m_profilerData); m_profilerData = std::move(world.m_profilerData);
m_isProfilerEnabled = world.m_isProfilerEnabled; m_isProfilerEnabled = m_isProfilerEnabled;
m_entities = std::move(world.m_entities); m_entities = std::move(world.m_entities);
for (EntityBlock& block : m_entities) for (EntityBlock& block : m_entities)