Sdk/Entity: Add IsDying method

This commit is contained in:
Jérôme Leclercq 2018-07-17 10:51:35 +02:00
parent 6dfc866a4d
commit 7b6cc47e03
5 changed files with 62 additions and 3 deletions

View File

@ -61,6 +61,7 @@ namespace Ndk
void Invalidate(); void Invalidate();
inline bool IsEnabled() const; inline bool IsEnabled() const;
bool IsDying() const;
inline bool IsValid() const; inline bool IsValid() const;
inline void RemoveAllComponents(); inline void RemoveAllComponents();

View File

@ -67,6 +67,8 @@ namespace Ndk
inline void KillEntity(Entity* entity); inline void KillEntity(Entity* entity);
inline void KillEntities(const EntityVector& list); inline void KillEntities(const EntityVector& list);
inline bool IsEntityDying(const Entity* entity) const;
inline bool IsEntityDying(EntityId id) const;
inline bool IsEntityValid(const Entity* entity) const; inline bool IsEntityValid(const Entity* entity) const;
inline bool IsEntityIdValid(EntityId id) const; inline bool IsEntityIdValid(EntityId id) const;
inline bool IsProfilerEnabled() const; inline bool IsProfilerEnabled() const;

View File

@ -324,13 +324,34 @@ namespace Ndk
KillEntity(entity); KillEntity(entity);
} }
/*!
* \brief Checks whether or not an entity is dying (has been killed this update)
* \return true If the entity exists and is dying
*
* \param entity Pointer to the entity
*/
inline bool World::IsEntityDying(const Entity* entity) const
{
return entity && IsEntityDying(entity->GetId());
}
/*!
* \brief Checks whether or not an entity is dying (has been killed this update)
* \return true If it is the case, false if the entity is alive (and hasn't been killed yet) or if the entity id is invalid
*
* \param id Identifier of the entity
*/
inline bool World::IsEntityDying(EntityId id) const
{
return m_killedEntities.UnboundedTest(id);
}
/*! /*!
* \brief Checks whether or not an entity is valid * \brief Checks whether or not an entity is valid
* \return true If it is the case * \return true If it is the case
* *
* \param entity Pointer to the entity * \param entity Pointer to the entity
*/ */
inline bool World::IsEntityValid(const Entity* entity) const inline bool World::IsEntityValid(const Entity* entity) const
{ {
return entity && entity->GetWorld() == this && IsEntityIdValid(entity->GetId()); return entity && entity->GetWorld() == this && IsEntityIdValid(entity->GetId());

View File

@ -133,13 +133,22 @@ namespace Ndk
/*! /*!
* \brief Invalidates the entity * \brief Invalidates the entity
*/ */
void Entity::Invalidate() void Entity::Invalidate()
{ {
// We alert everyone that we have been updated // We alert everyone that we have been updated
m_world->Invalidate(m_id); m_world->Invalidate(m_id);
} }
/*!
* \brief Checks if the entity has been killed this update
* \return True if the entity is currently dying and will be dead at next world refresh
*/
bool Entity::IsDying() const
{
return m_world->IsEntityDying(m_id);
}
/*! /*!
* \brief Creates the entity * \brief Creates the entity
*/ */

View File

@ -100,4 +100,30 @@ SCENARIO("World", "[NDK][WORLD]")
} }
} }
} }
}
GIVEN("A newly created entity")
{
Ndk::World world(false);
Ndk::EntityHandle entity = world.CreateEntity();
REQUIRE(entity.IsValid());
REQUIRE(entity->IsValid());
CHECK_FALSE(entity->IsDying());
WHEN("We kill it")
{
entity->Kill();
CHECK(entity.IsValid());
CHECK(entity->IsValid());
CHECK(entity->IsDying());
THEN("We refresh the world")
{
world.Refresh();
CHECK_FALSE(entity.IsValid());
}
}
}
}