Remade component ids
No longer based on incrementing counter Former-commit-id: b875e17781d8bcda48ea9ada523adf0823b22a8b
This commit is contained in:
20
SDK/include/NDK/Algorithm.hpp
Normal file
20
SDK/include/NDK/Algorithm.hpp
Normal file
@@ -0,0 +1,20 @@
|
||||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequesites.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NDK_ALGORITHM_HPP
|
||||
#define NDK_ALGORITHM_HPP
|
||||
|
||||
#include <NDK/Prerequesites.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
template<unsigned int N> ComponentId BuildComponentId(const char (&id)[N]);
|
||||
template<typename ComponentType> constexpr ComponentId GetComponentId();
|
||||
}
|
||||
|
||||
#include <Ndk/Algorithm.inl>
|
||||
|
||||
#endif // NDK_ALGORITHM_HPP
|
||||
27
SDK/include/NDK/Algorithm.inl
Normal file
27
SDK/include/NDK/Algorithm.inl
Normal file
@@ -0,0 +1,27 @@
|
||||
// Copyright (C) 2015 Jérôme Leclercq
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequesites.hpp
|
||||
|
||||
#include <Nazara/Core/Endianness.hpp>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
///TODO: constexpr avec le C++14
|
||||
template<unsigned int N>
|
||||
ComponentId BuildComponentId(const char (&id)[N])
|
||||
{
|
||||
static_assert(N-1 <= sizeof(ComponentId), "ID too long for this size of component id");
|
||||
|
||||
ComponentId componentId = 0;
|
||||
for (int i = 0; i < N; ++i)
|
||||
componentId |= static_cast<ComponentId>(id[i]) << i*8;
|
||||
|
||||
return componentId;
|
||||
}
|
||||
|
||||
template<typename ComponentType>
|
||||
constexpr ComponentId GetComponentId()
|
||||
{
|
||||
return ComponentType::ComponentId;
|
||||
}
|
||||
}
|
||||
@@ -14,20 +14,15 @@ namespace Ndk
|
||||
class NDK_API BaseComponent
|
||||
{
|
||||
public:
|
||||
BaseComponent(nzUInt32 componentId);
|
||||
BaseComponent(ComponentId componentId);
|
||||
virtual ~BaseComponent();
|
||||
|
||||
virtual BaseComponent* Clone() const = 0;
|
||||
|
||||
nzUInt32 GetId() const;
|
||||
|
||||
static nzUInt32 GetNextId();
|
||||
ComponentId GetId() const;
|
||||
|
||||
protected:
|
||||
nzUInt32 m_componentId;
|
||||
|
||||
private:
|
||||
static nzUInt32 s_nextId;
|
||||
ComponentId m_componentId;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -4,18 +4,13 @@
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
inline BaseComponent::BaseComponent(nzUInt32 componentId) :
|
||||
inline BaseComponent::BaseComponent(ComponentId componentId) :
|
||||
m_componentId(componentId)
|
||||
{
|
||||
}
|
||||
|
||||
inline nzUInt32 BaseComponent::GetId() const
|
||||
inline ComponentId BaseComponent::GetId() const
|
||||
{
|
||||
return m_componentId;
|
||||
}
|
||||
|
||||
inline nzUInt32 BaseComponent::GetNextId()
|
||||
{
|
||||
return s_nextId++;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,8 +20,6 @@ namespace Ndk
|
||||
|
||||
BaseComponent* Clone() const override;
|
||||
};
|
||||
|
||||
template<typename ComponentType> constexpr nzUInt32 GetComponentId();
|
||||
}
|
||||
|
||||
#include <NDK/Component.inl>
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequesites.hpp
|
||||
|
||||
#include <Ndk/Algorithm.hpp>
|
||||
#include <type_traits>
|
||||
|
||||
namespace Ndk
|
||||
@@ -23,10 +24,4 @@ namespace Ndk
|
||||
|
||||
return new ComponentType(static_cast<const ComponentType&>(*this));
|
||||
}
|
||||
|
||||
template<typename ComponentType>
|
||||
constexpr nzUInt32 GetComponentId()
|
||||
{
|
||||
return ComponentType::ComponentId;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include <NDK/Component.hpp>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
@@ -23,8 +24,6 @@ namespace Ndk
|
||||
friend World;
|
||||
|
||||
public:
|
||||
using Id = nzUInt32;
|
||||
|
||||
Entity(const Entity&) = delete;
|
||||
Entity(Entity&& entity);
|
||||
~Entity();
|
||||
@@ -34,13 +33,12 @@ namespace Ndk
|
||||
|
||||
EntityHandle CreateHandle();
|
||||
|
||||
BaseComponent& GetComponent(nzUInt32 componentId);
|
||||
BaseComponent& GetComponent(ComponentId componentId);
|
||||
template<typename ComponentType> ComponentType& GetComponent();
|
||||
const NzBitset<>& GetComponentBits() const;
|
||||
Id GetId() const;
|
||||
EntityId GetId() const;
|
||||
World* GetWorld() const;
|
||||
|
||||
bool HasComponent(nzUInt32 componentId) const;
|
||||
bool HasComponent(ComponentId componentId) const;
|
||||
template<typename ComponentType> bool HasComponent() const;
|
||||
|
||||
void Kill();
|
||||
@@ -48,14 +46,14 @@ namespace Ndk
|
||||
bool IsValid() const;
|
||||
|
||||
void RemoveAllComponents();
|
||||
void RemoveComponent(nzUInt32 componentId);
|
||||
void RemoveComponent(ComponentId componentId);
|
||||
template<typename ComponentType> void RemoveComponent();
|
||||
|
||||
Entity& operator=(const Entity&) = delete;
|
||||
Entity& operator=(Entity&&) = delete;
|
||||
|
||||
private:
|
||||
Entity(World& world, Id id);
|
||||
Entity(World& world, EntityId id);
|
||||
|
||||
void Create();
|
||||
void Destroy();
|
||||
@@ -63,10 +61,9 @@ namespace Ndk
|
||||
void RegisterHandle(EntityHandle* handle);
|
||||
void UnregisterHandle(EntityHandle* handle);
|
||||
|
||||
std::vector<std::unique_ptr<BaseComponent>> m_components;
|
||||
std::vector<EntityHandle*> m_handles;
|
||||
Id m_id;
|
||||
NzBitset<> m_componentBits;
|
||||
std::unordered_map<ComponentId, std::unique_ptr<BaseComponent>> m_components;
|
||||
EntityId m_id;
|
||||
World* m_world;
|
||||
bool m_valid;
|
||||
};
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
inline Entity::Entity(World& world, Id id) :
|
||||
inline Entity::Entity(World& world, EntityId id) :
|
||||
m_id(id),
|
||||
m_world(&world)
|
||||
{
|
||||
@@ -25,7 +25,7 @@ namespace Ndk
|
||||
return static_cast<ComponentType&>(AddComponent(std::move(ptr)));
|
||||
}
|
||||
|
||||
inline BaseComponent& Entity::GetComponent(nzUInt32 componentId)
|
||||
inline BaseComponent& Entity::GetComponent(ComponentId componentId)
|
||||
{
|
||||
///DOC: Le component doit être présent
|
||||
NazaraAssert(HasComponent(componentId), "This component is not part of the entity");
|
||||
@@ -42,16 +42,11 @@ namespace Ndk
|
||||
///DOC: Le component doit être présent
|
||||
static_assert(std::is_base_of<BaseComponent, ComponentType>(), "ComponentType is not a component");
|
||||
|
||||
nzUInt32 componentId = GetComponentId<ComponentType>();
|
||||
ComponentId componentId = GetComponentId<ComponentType>();
|
||||
return static_cast<ComponentType&>(GetComponent(componentId));
|
||||
}
|
||||
|
||||
inline const NzBitset<>& Entity::GetComponentBits() const
|
||||
{
|
||||
return m_componentBits;
|
||||
}
|
||||
|
||||
inline Entity::Id Entity::GetId() const
|
||||
inline EntityId Entity::GetId() const
|
||||
{
|
||||
return m_id;
|
||||
}
|
||||
@@ -61,9 +56,9 @@ namespace Ndk
|
||||
return m_world;
|
||||
}
|
||||
|
||||
inline bool Entity::HasComponent(nzUInt32 componentId) const
|
||||
inline bool Entity::HasComponent(ComponentId componentId) const
|
||||
{
|
||||
return m_components.size() > componentId && m_components[componentId];
|
||||
return m_components.count(componentId) > 0;
|
||||
}
|
||||
|
||||
template<typename ComponentType>
|
||||
@@ -71,7 +66,7 @@ namespace Ndk
|
||||
{
|
||||
static_assert(std::is_base_of<BaseComponent, ComponentType>(), "ComponentType is not a component");
|
||||
|
||||
nzUInt32 componentId = GetComponentId<ComponentType>();
|
||||
ComponentId componentId = GetComponentId<ComponentType>();
|
||||
return HasComponent(componentId);
|
||||
}
|
||||
|
||||
@@ -80,7 +75,7 @@ namespace Ndk
|
||||
{
|
||||
static_assert(std::is_base_of<BaseComponent, ComponentType>(), "ComponentType is not a component");
|
||||
|
||||
nzUInt32 componentId = GetComponentId<ComponentType>();
|
||||
ComponentId componentId = GetComponentId<ComponentType>();
|
||||
RemoveComponent(componentId);
|
||||
}
|
||||
|
||||
|
||||
@@ -55,4 +55,10 @@
|
||||
#define NDK_API
|
||||
#endif
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
using ComponentId = nzUInt32;
|
||||
using EntityId = nzUInt32;
|
||||
}
|
||||
|
||||
#endif // NDK_PREREQUESITES_HPP
|
||||
|
||||
@@ -29,13 +29,13 @@ namespace Ndk
|
||||
|
||||
void Clear();
|
||||
|
||||
const EntityHandle& GetEntity(Entity::Id id);
|
||||
const EntityHandle& GetEntity(EntityId id);
|
||||
|
||||
void KillEntity(const EntityHandle& entity);
|
||||
void KillEntities(const EntityList& list);
|
||||
|
||||
bool IsEntityValid(const EntityHandle& entity) const;
|
||||
bool IsEntityIdValid(Entity::Id id) const;
|
||||
bool IsEntityIdValid(EntityId id) const;
|
||||
|
||||
void Update();
|
||||
|
||||
@@ -51,7 +51,7 @@ namespace Ndk
|
||||
unsigned int aliveIndex;
|
||||
};
|
||||
|
||||
std::vector<Entity::Id> m_freeIdList;
|
||||
std::vector<EntityId> m_freeIdList;
|
||||
std::vector<EntityBlock> m_entities;
|
||||
EntityList m_aliveEntities;
|
||||
NzBitset<nzUInt64> m_killedEntities;
|
||||
|
||||
@@ -28,7 +28,7 @@ namespace Ndk
|
||||
return entity.IsValid() && entity->GetWorld() == this && IsEntityIdValid(entity->GetId());
|
||||
}
|
||||
|
||||
inline bool World::IsEntityIdValid(Entity::Id id) const
|
||||
inline bool World::IsEntityIdValid(EntityId id) const
|
||||
{
|
||||
return id < m_entities.size() && m_entities[id].entity.IsValid();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user