(Entity) Added components bits
Moved [Add/Remove]Component implementation to .cpp Former-commit-id: e61e8c57911c2e106e6c0959b692a006b8f58c40
This commit is contained in:
parent
99e0912163
commit
0ba034f7e9
|
|
@ -7,6 +7,7 @@
|
||||||
#ifndef NDK_ENTITY_HPP
|
#ifndef NDK_ENTITY_HPP
|
||||||
#define NDK_ENTITY_HPP
|
#define NDK_ENTITY_HPP
|
||||||
|
|
||||||
|
#include <Nazara/Core/Bitset.hpp>
|
||||||
#include <NDK/Component.hpp>
|
#include <NDK/Component.hpp>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
@ -35,6 +36,7 @@ namespace Ndk
|
||||||
|
|
||||||
BaseComponent& GetComponent(nzUInt32 componentId);
|
BaseComponent& GetComponent(nzUInt32 componentId);
|
||||||
template<typename ComponentType> ComponentType& GetComponent();
|
template<typename ComponentType> ComponentType& GetComponent();
|
||||||
|
const NzBitset<>& GetComponentBits() const;
|
||||||
Id GetId() const;
|
Id GetId() const;
|
||||||
World* GetWorld() const;
|
World* GetWorld() const;
|
||||||
|
|
||||||
|
|
@ -64,6 +66,7 @@ namespace Ndk
|
||||||
std::vector<std::unique_ptr<BaseComponent>> m_components;
|
std::vector<std::unique_ptr<BaseComponent>> m_components;
|
||||||
std::vector<EntityHandle*> m_handles;
|
std::vector<EntityHandle*> m_handles;
|
||||||
Id m_id;
|
Id m_id;
|
||||||
|
NzBitset<> m_componentBits;
|
||||||
World* m_world;
|
World* m_world;
|
||||||
bool m_valid;
|
bool m_valid;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -15,25 +15,6 @@ namespace Ndk
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
inline BaseComponent& Entity::AddComponent(std::unique_ptr<BaseComponent>&& component)
|
|
||||||
{
|
|
||||||
NazaraAssert(component, "Component must be valid");
|
|
||||||
|
|
||||||
nzUInt32 componentId = component->GetId();
|
|
||||||
|
|
||||||
// Nous supprimons l'ancien component, s'il existe
|
|
||||||
RemoveComponent(componentId);
|
|
||||||
|
|
||||||
// Nous nous assurons que le vecteur de component est suffisamment grand pour contenir le nouveau component
|
|
||||||
if (m_components.size() <= componentId)
|
|
||||||
m_components.resize(componentId + 1);
|
|
||||||
|
|
||||||
// Affectation et retour du component
|
|
||||||
m_components[componentId] = std::move(component);
|
|
||||||
|
|
||||||
return *m_components[componentId].get();
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename ComponentType, typename... Args>
|
template<typename ComponentType, typename... Args>
|
||||||
ComponentType& Entity::AddComponent(Args&&... args)
|
ComponentType& Entity::AddComponent(Args&&... args)
|
||||||
{
|
{
|
||||||
|
|
@ -65,6 +46,11 @@ namespace Ndk
|
||||||
return static_cast<ComponentType&>(GetComponent(componentId));
|
return static_cast<ComponentType&>(GetComponent(componentId));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline const NzBitset<>& Entity::GetComponentBits() const
|
||||||
|
{
|
||||||
|
return m_componentBits;
|
||||||
|
}
|
||||||
|
|
||||||
inline Entity::Id Entity::GetId() const
|
inline Entity::Id Entity::GetId() const
|
||||||
{
|
{
|
||||||
return m_id;
|
return m_id;
|
||||||
|
|
@ -89,18 +75,6 @@ namespace Ndk
|
||||||
return HasComponent(componentId);
|
return HasComponent(componentId);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void Entity::RemoveAllComponents()
|
|
||||||
{
|
|
||||||
m_components.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void Entity::RemoveComponent(nzUInt32 componentId)
|
|
||||||
{
|
|
||||||
///DOC: N'a aucun effet si le component n'est pas présent
|
|
||||||
if (HasComponent(componentId))
|
|
||||||
m_components[componentId].reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
template<typename ComponentType>
|
template<typename ComponentType>
|
||||||
void Entity::RemoveComponent()
|
void Entity::RemoveComponent()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,26 @@ namespace Ndk
|
||||||
Destroy();
|
Destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BaseComponent& Entity::AddComponent(std::unique_ptr<BaseComponent>&& component)
|
||||||
|
{
|
||||||
|
NazaraAssert(component, "Component must be valid");
|
||||||
|
|
||||||
|
nzUInt32 componentId = component->GetId();
|
||||||
|
|
||||||
|
// Nous supprimons l'ancien component, s'il existe
|
||||||
|
RemoveComponent(componentId);
|
||||||
|
|
||||||
|
// Nous nous assurons que le vecteur de component est suffisamment grand pour contenir le nouveau component
|
||||||
|
if (m_components.size() <= componentId)
|
||||||
|
m_components.resize(componentId + 1);
|
||||||
|
|
||||||
|
// Affectation et retour du component
|
||||||
|
m_components[componentId] = std::move(component);
|
||||||
|
m_componentBits.UnboundedSet(componentId);
|
||||||
|
|
||||||
|
return *m_components[componentId].get();
|
||||||
|
}
|
||||||
|
|
||||||
EntityHandle Entity::CreateHandle()
|
EntityHandle Entity::CreateHandle()
|
||||||
{
|
{
|
||||||
return EntityHandle(this);
|
return EntityHandle(this);
|
||||||
|
|
@ -38,6 +58,22 @@ namespace Ndk
|
||||||
return m_valid;
|
return m_valid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Entity::RemoveAllComponents()
|
||||||
|
{
|
||||||
|
m_components.clear();
|
||||||
|
m_componentBits.Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Entity::RemoveComponent(nzUInt32 componentId)
|
||||||
|
{
|
||||||
|
///DOC: N'a aucun effet si le component n'est pas présent
|
||||||
|
if (HasComponent(componentId))
|
||||||
|
{
|
||||||
|
m_components[componentId].reset();
|
||||||
|
m_componentBits.Reset(componentId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Entity::Create()
|
void Entity::Create()
|
||||||
{
|
{
|
||||||
m_valid = true;
|
m_valid = true;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue