From 762e33aab431eef632b733a10ebc5d5ff565b09d Mon Sep 17 00:00:00 2001 From: Lynix Date: Thu, 11 Aug 2016 22:00:05 +0200 Subject: [PATCH] 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 --- SDK/include/NDK/Entity.hpp | 2 ++ SDK/include/NDK/World.hpp | 2 ++ SDK/src/NDK/Entity.cpp | 8 ++++++++ SDK/src/NDK/World.cpp | 22 ++++++++++++++++++++++ 4 files changed, 34 insertions(+) diff --git a/SDK/include/NDK/Entity.hpp b/SDK/include/NDK/Entity.hpp index ae6b1acf5..33661eb50 100644 --- a/SDK/include/NDK/Entity.hpp +++ b/SDK/include/NDK/Entity.hpp @@ -34,6 +34,8 @@ namespace Ndk BaseComponent& AddComponent(std::unique_ptr&& component); template ComponentType& AddComponent(Args&&... args); + const EntityHandle& Clone() const; + inline void Enable(bool enable); inline BaseComponent& GetComponent(ComponentIndex index); diff --git a/SDK/include/NDK/World.hpp b/SDK/include/NDK/World.hpp index 40508e8e4..2e20d03a9 100644 --- a/SDK/include/NDK/World.hpp +++ b/SDK/include/NDK/World.hpp @@ -44,6 +44,8 @@ namespace Ndk void Clear() noexcept; + const EntityHandle& CloneEntity(EntityId id); + const EntityHandle& GetEntity(EntityId id); inline const EntityList& GetEntities(); inline BaseSystem& GetSystem(SystemIndex index); diff --git a/SDK/src/NDK/Entity.cpp b/SDK/src/NDK/Entity.cpp index 680ff56d9..ecd74ebc0 100644 --- a/SDK/src/NDK/Entity.cpp +++ b/SDK/src/NDK/Entity.cpp @@ -61,6 +61,14 @@ namespace Ndk 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() { m_world->KillEntity(this); diff --git a/SDK/src/NDK/World.cpp b/SDK/src/NDK/World.cpp index e5b2d874a..8e5ff99f0 100644 --- a/SDK/src/NDK/World.cpp +++ b/SDK/src/NDK/World.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #include @@ -74,6 +75,27 @@ namespace Ndk 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 component(original->GetComponent(ComponentIndex(i)).Clone()); + clone->AddComponent(std::move(component)); + } + + return GetEntity(clone->GetId()); + } + void World::KillEntity(Entity* entity) { ///DOC: Ignoré si l'entité est invalide