SDK/Entity: Add entity cloning

Former-commit-id: 6ba33c15d30cb9e385eaa2dd115e680962aad364 [formerly e5bf85dfef3def45ea47b9a8e46f2b5c61a6ca4f] [formerly aa4cd7ce6637f9559ede8530119d2d96b30a4b47 [formerly aa7c268cc1ed04a8e484b5a6317d967121939067]]
Former-commit-id: 24cf44420c029ec2ccc02f8b88873555af0aa88f [formerly efb642be02646b410003e5972c8494af773ef097]
Former-commit-id: 4d30a3ad59e3a30b0ff48eb984c20e3c098fbee1
This commit is contained in:
Lynix 2016-08-11 22:00:05 +02:00
parent 039447c127
commit 762e33aab4
4 changed files with 34 additions and 0 deletions

View File

@ -34,6 +34,8 @@ namespace Ndk
BaseComponent& AddComponent(std::unique_ptr<BaseComponent>&& component); BaseComponent& AddComponent(std::unique_ptr<BaseComponent>&& component);
template<typename ComponentType, typename... Args> ComponentType& AddComponent(Args&&... args); template<typename ComponentType, typename... Args> ComponentType& AddComponent(Args&&... args);
const EntityHandle& Clone() const;
inline void Enable(bool enable); inline void Enable(bool enable);
inline BaseComponent& GetComponent(ComponentIndex index); inline BaseComponent& GetComponent(ComponentIndex index);

View File

@ -44,6 +44,8 @@ namespace Ndk
void Clear() noexcept; void Clear() noexcept;
const EntityHandle& CloneEntity(EntityId id);
const EntityHandle& GetEntity(EntityId id); const EntityHandle& GetEntity(EntityId id);
inline const EntityList& GetEntities(); inline const EntityList& GetEntities();
inline BaseSystem& GetSystem(SystemIndex index); inline BaseSystem& GetSystem(SystemIndex index);

View File

@ -61,6 +61,14 @@ namespace Ndk
return component; return component;
} }
const EntityHandle& Entity::Clone() const
{
///DOC: The clone is enabled by default, even if the original entity is disabled
NazaraAssert(IsValid(), "Invalid entity");
return m_world->CloneEntity(m_id);
}
void Entity::Kill() void Entity::Kill()
{ {
m_world->KillEntity(this); m_world->KillEntity(this);

View File

@ -4,6 +4,7 @@
#include <NDK/World.hpp> #include <NDK/World.hpp>
#include <Nazara/Core/Error.hpp> #include <Nazara/Core/Error.hpp>
#include <NDK/BaseComponent.hpp>
#include <NDK/Systems/PhysicsSystem.hpp> #include <NDK/Systems/PhysicsSystem.hpp>
#include <NDK/Systems/VelocitySystem.hpp> #include <NDK/Systems/VelocitySystem.hpp>
@ -74,6 +75,27 @@ namespace Ndk
m_killedEntities.Clear(); m_killedEntities.Clear();
} }
const EntityHandle& World::CloneEntity(EntityId id)
{
EntityHandle original = GetEntity(id);
if (!original)
{
NazaraError("Invalid entity ID");
return EntityHandle::InvalidHandle;
}
EntityHandle clone = CreateEntity();
const Nz::Bitset<>& componentBits = original->GetComponentBits();
for (std::size_t i = componentBits.FindFirst(); i != componentBits.npos; i = componentBits.FindNext(i))
{
std::unique_ptr<BaseComponent> component(original->GetComponent(ComponentIndex(i)).Clone());
clone->AddComponent(std::move(component));
}
return GetEntity(clone->GetId());
}
void World::KillEntity(Entity* entity) void World::KillEntity(Entity* entity)
{ {
///DOC: Ignoré si l'entité est invalide ///DOC: Ignoré si l'entité est invalide