Sdk/BaseComponent: Add OnEntityDisabled and OnEntityEnabled callbacks

This commit is contained in:
Lynix 2017-12-10 14:06:43 +01:00
parent 33b3b2feaf
commit 498bd77d8a
7 changed files with 59 additions and 20 deletions

View File

@ -63,6 +63,7 @@ Nazara Development Kit:
- Add linear and angular damping accessor to PhysicsComponent3D - Add linear and angular damping accessor to PhysicsComponent3D
- Fix GraphicsComponent cloning not copying renderable local matrices - Fix GraphicsComponent cloning not copying renderable local matrices
- ⚠️ Rename PhysicsComponent3D::[Get|Set]Velocity to [Get|Set]LinearVelocity - ⚠️ Rename PhysicsComponent3D::[Get|Set]Velocity to [Get|Set]LinearVelocity
- Add OnEntityDisabled and OnEntityEnabled callbacks to BaseComponent
# 0.4: # 0.4:

View File

@ -50,6 +50,8 @@ namespace Ndk
virtual void OnComponentDetached(BaseComponent& component); virtual void OnComponentDetached(BaseComponent& component);
virtual void OnDetached(); virtual void OnDetached();
virtual void OnEntityDestruction(); virtual void OnEntityDestruction();
virtual void OnEntityDisabled();
virtual void OnEntityEnabled();
void SetEntity(Entity* entity); void SetEntity(Entity* entity);

View File

@ -42,7 +42,7 @@ namespace Ndk
const EntityHandle& Clone() const; const EntityHandle& Clone() const;
inline void Disable(); inline void Disable();
inline void Enable(bool enable = true); void Enable(bool enable = true);
inline BaseComponent& GetComponent(ComponentIndex index); inline BaseComponent& GetComponent(ComponentIndex index);
template<typename ComponentType> ComponentType& GetComponent(); template<typename ComponentType> ComponentType& GetComponent();

View File

@ -38,20 +38,6 @@ namespace Ndk
Enable(false); Enable(false);
} }
/*!
* \brief Enables the entity
*
* \param enable Should the entity be enabled
*/
inline void Entity::Enable(bool enable)
{
if (m_enabled != enable)
{
m_enabled = enable;
Invalidate();
}
}
/*! /*!
* \brief Gets a component in the entity by index * \brief Gets a component in the entity by index
* \return A reference to the component * \return A reference to the component

View File

@ -63,6 +63,26 @@ namespace Ndk
{ {
} }
/*!
* \brief Operation to perform when the entity is disabled
*
* \remark Disabling an entity will remove it from systems it belongs to, but sometimes the entity will need to do
* additional work in order to be properly disabled (i.e.: disabling physics simulation & collisions)
*/
void BaseComponent::OnEntityDisabled()
{
}
/*!
* \brief Operation to perform when the entity is disabled
*
* \remark Enabling an entity will add it back to systems it belongs to, but sometimes the entity will need to do
* additional work in order to be properly re-enabled (i.e.: enabling physics simulation & collisions)
*/
void BaseComponent::OnEntityEnabled()
{
}
std::vector<BaseComponent::ComponentEntry> BaseComponent::s_entries; std::vector<BaseComponent::ComponentEntry> BaseComponent::s_entries;
std::unordered_map<ComponentId, ComponentIndex> BaseComponent::s_idToIndex; std::unordered_map<ComponentId, ComponentIndex> BaseComponent::s_idToIndex;
} }

View File

@ -96,6 +96,10 @@ namespace Ndk
m_components[i]->OnComponentAttached(component); m_components[i]->OnComponentAttached(component);
} }
// If we are currently disabled, inform the component
if (!m_enabled)
component.OnEntityDisabled();
return component; return component;
} }
@ -106,7 +110,6 @@ namespace Ndk
* \remark The close is enable by default, even if the original is disabled * \remark The close is enable by default, even if the original is disabled
* \remark Produces a NazaraAssert if the entity is not valid * \remark Produces a NazaraAssert if the entity is not valid
*/ */
const EntityHandle& Entity::Clone() const const EntityHandle& Entity::Clone() const
{ {
NazaraAssert(IsValid(), "Invalid entity"); NazaraAssert(IsValid(), "Invalid entity");
@ -114,10 +117,34 @@ namespace Ndk
return m_world->CloneEntity(m_id); return m_world->CloneEntity(m_id);
} }
/*!
* \brief Enables the entity
*
* \param enable Should the entity be enabled
*/
void Entity::Enable(bool enable)
{
if (m_enabled != enable)
{
m_enabled = enable;
if (m_enabled)
{
for (std::size_t i = m_componentBits.FindFirst(); i != m_componentBits.npos; i = m_componentBits.FindNext(i))
m_components[i]->OnEntityEnabled();
}
else
{
for (std::size_t i = m_componentBits.FindFirst(); i != m_componentBits.npos; i = m_componentBits.FindNext(i))
m_components[i]->OnEntityDisabled();
}
Invalidate();
}
}
/*! /*!
* \brief Kills the entity * \brief Kills the entity
*/ */
void Entity::Kill() void Entity::Kill()
{ {
m_world->KillEntity(this); m_world->KillEntity(this);

View File

@ -138,12 +138,11 @@ namespace Ndk
* *
* \param id Identifier of the entity * \param id Identifier of the entity
* *
* \remark Produces a NazaraError if the entity to clone does not exist * \remark Cloning a disabled entity will produce an enabled clone
*/ */
const EntityHandle& World::CloneEntity(EntityId id) const EntityHandle& World::CloneEntity(EntityId id)
{ {
EntityHandle original = GetEntity(id); const EntityHandle& original = GetEntity(id);
if (!original) if (!original)
{ {
NazaraError("Invalid entity ID"); NazaraError("Invalid entity ID");
@ -151,6 +150,8 @@ namespace Ndk
} }
const EntityHandle& clone = CreateEntity(); const EntityHandle& clone = CreateEntity();
if (!original->IsEnabled())
clone->Disable();
const Nz::Bitset<>& componentBits = original->GetComponentBits(); const Nz::Bitset<>& componentBits = original->GetComponentBits();
for (std::size_t i = componentBits.FindFirst(); i != componentBits.npos; i = componentBits.FindNext(i)) for (std::size_t i = componentBits.FindFirst(); i != componentBits.npos; i = componentBits.FindNext(i))
@ -159,6 +160,8 @@ namespace Ndk
clone->AddComponent(std::move(component)); clone->AddComponent(std::move(component));
} }
clone->Enable();
return clone; return clone;
} }