Improved interface

EntityHandle are no longer required to pass Entity as arguments
World::CreateEntity() now returns a const EntityHandle&

Former-commit-id: 6fc53ce5759a2a508094bdc61b4471f13f0844ec
This commit is contained in:
Lynix 2015-03-18 00:49:44 +01:00
parent e91313b62d
commit be8f6edeb4
9 changed files with 117 additions and 33 deletions

View File

@ -27,13 +27,13 @@ namespace Ndk
virtual BaseSystem* Clone() const = 0;
bool Filters(const EntityHandle& entity) const;
bool Filters(const Entity* entity) const;
const std::vector<EntityHandle>& GetEntities() const;
SystemId GetId() const;
World& GetWorld() const;
bool HasEntity(const EntityHandle& entity) const;
bool HasEntity(const Entity* entity) const;
protected:
template<typename ComponentType> void Excludes();
@ -45,12 +45,12 @@ namespace Ndk
void RequiresComponent(ComponentId componentId);
private:
void AddEntity(const EntityHandle& entity);
void AddEntity(Entity* entity);
virtual void OnEntityAdded(const EntityHandle& entity);
virtual void OnEntityRemoved(const EntityHandle& entity);
virtual void OnEntityAdded(Entity* entity);
virtual void OnEntityRemoved(Entity* entity);
void RemoveEntity(const EntityHandle& entity);
void RemoveEntity(Entity* entity);
void SetWorld(World& world);

View File

@ -27,8 +27,11 @@ namespace Ndk
return *m_world;
}
inline bool BaseSystem::HasEntity(const EntityHandle& entity) const
inline bool BaseSystem::HasEntity(const Entity* entity) const
{
if (!entity)
return false;
return m_entityBits.UnboundedTest(entity->GetId());
}
@ -72,9 +75,11 @@ namespace Ndk
m_requiredComponents.insert(componentId);
}
inline void BaseSystem::AddEntity(const EntityHandle& entity)
inline void BaseSystem::AddEntity(Entity* entity)
{
m_entities.push_back(entity);
NazaraAssert(entity, "Invalid entity");
m_entities.push_back(entity->CreateHandle());
m_entityBits.UnboundedSet(entity->GetId(), true);
entity->RegisterSystem(m_systemId);
@ -82,9 +87,11 @@ namespace Ndk
OnEntityAdded(entity);
}
inline void BaseSystem::RemoveEntity(const EntityHandle& entity)
inline void BaseSystem::RemoveEntity(Entity* entity)
{
auto it = std::find(m_entities.begin(), m_entities.end(), entity);
NazaraAssert(entity, "Invalid entity");
auto it = std::find(m_entities.begin(), m_entities.end(), *entity);
NazaraAssert(it != m_entities.end(), "Entity is not part of this system");
// Pour éviter de déplacer beaucoup de handles, on swap le dernier avec celui à supprimer

View File

@ -44,11 +44,28 @@ namespace Ndk
friend std::ostream& operator<<(std::ostream& out, const EntityHandle& handle);
friend bool operator==(const EntityHandle& lhs, const EntityHandle& rhs);
friend bool operator==(const Entity& lhs, const EntityHandle& rhs);
friend bool operator==(const EntityHandle& lhs, const Entity& rhs);
friend bool operator!=(const EntityHandle& lhs, const EntityHandle& rhs);
friend bool operator!=(const Entity& lhs, const EntityHandle& rhs);
friend bool operator!=(const EntityHandle& lhs, const Entity& rhs);
friend bool operator<(const EntityHandle& lhs, const EntityHandle& rhs);
friend bool operator<(const Entity& lhs, const EntityHandle& rhs);
friend bool operator<(const EntityHandle& lhs, const Entity& rhs);
friend bool operator<=(const EntityHandle& lhs, const EntityHandle& rhs);
friend bool operator<=(const Entity& lhs, const EntityHandle& rhs);
friend bool operator<=(const EntityHandle& lhs, const Entity& rhs);
friend bool operator>(const EntityHandle& lhs, const EntityHandle& rhs);
friend bool operator>(const Entity& lhs, const EntityHandle& rhs);
friend bool operator>(const EntityHandle& lhs, const Entity& rhs);
friend bool operator>=(const EntityHandle& lhs, const EntityHandle& rhs);
friend bool operator>=(const Entity& lhs, const EntityHandle& rhs);
friend bool operator>=(const EntityHandle& lhs, const Entity& rhs);
private:
void OnEntityDestroyed();

View File

@ -149,30 +149,90 @@ namespace Ndk
return lhs.m_entity == rhs.m_entity;
}
inline bool operator==(const Entity& lhs, const EntityHandle& rhs)
{
return &lhs == rhs.m_entity;
}
inline bool operator==(const EntityHandle& lhs, const Entity& rhs)
{
return lhs.m_entity == &rhs;
}
inline bool operator!=(const EntityHandle& lhs, const EntityHandle& rhs)
{
return !(lhs == rhs);
}
inline bool operator!=(const Entity& lhs, const EntityHandle& rhs)
{
return !(lhs == rhs);
}
inline bool operator!=(const EntityHandle& lhs, const Entity& rhs)
{
return !(lhs == rhs);
}
inline bool operator<(const EntityHandle& lhs, const EntityHandle& rhs)
{
return lhs.m_entity < rhs.m_entity;
}
inline bool operator<(const Entity& lhs, const EntityHandle& rhs)
{
return &lhs < rhs.m_entity;
}
inline bool operator<(const EntityHandle& lhs, const Entity& rhs)
{
return lhs.m_entity < &rhs;
}
inline bool operator<=(const EntityHandle& lhs, const EntityHandle& rhs)
{
return !(lhs > rhs);
}
inline bool operator<=(const Entity& lhs, const EntityHandle& rhs)
{
return !(lhs > rhs);
}
inline bool operator<=(const EntityHandle& lhs, const Entity& rhs)
{
return !(lhs > rhs);
}
inline bool operator>(const EntityHandle& lhs, const EntityHandle& rhs)
{
return rhs < lhs;
}
inline bool operator>(const Entity& lhs, const EntityHandle& rhs)
{
return rhs < lhs;
}
inline bool operator>(const EntityHandle& lhs, const Entity& rhs)
{
return rhs < lhs;
}
inline bool operator>=(const EntityHandle& lhs, const EntityHandle& rhs)
{
return !(lhs < rhs);
}
inline bool operator>=(const Entity& lhs, const EntityHandle& rhs)
{
return !(lhs < rhs);
}
inline bool operator>=(const EntityHandle& lhs, const Entity& rhs)
{
return !(lhs < rhs);
}
}
namespace std

View File

@ -32,7 +32,7 @@ namespace Ndk
BaseSystem& AddSystem(std::unique_ptr<BaseSystem>&& system);
template<typename SystemType, typename... Args> SystemType& AddSystem(Args&&... args);
EntityHandle CreateEntity();
const EntityHandle& CreateEntity();
EntityList CreateEntities(unsigned int count);
void Clear();
@ -44,10 +44,10 @@ namespace Ndk
bool HasSystem(SystemId systemId) const;
template<typename SystemType> bool HasSystem() const;
void KillEntity(const EntityHandle& entity);
void KillEntity(Entity* entity);
void KillEntities(const EntityList& list);
bool IsEntityValid(const EntityHandle& entity) const;
bool IsEntityValid(const Entity* entity) const;
bool IsEntityIdValid(EntityId id) const;
void RemoveAllSystems();

View File

@ -84,9 +84,9 @@ namespace Ndk
KillEntity(entity);
}
inline bool World::IsEntityValid(const EntityHandle& entity) const
inline bool World::IsEntityValid(const Entity* entity) const
{
return entity.IsValid() && entity->GetWorld() == this && IsEntityIdValid(entity->GetId());
return entity && entity->GetWorld() == this && IsEntityIdValid(entity->GetId());
}
inline bool World::IsEntityIdValid(EntityId id) const

View File

@ -12,8 +12,11 @@ namespace Ndk
entity->UnregisterSystem(m_systemId);
}
bool BaseSystem::Filters(const EntityHandle& entity) const
bool BaseSystem::Filters(const Entity* entity) const
{
if (!entity)
return false;
for (ComponentId component : m_requiredComponents)
{
if (!entity->HasComponent(component))
@ -29,12 +32,12 @@ namespace Ndk
return true;
}
void BaseSystem::OnEntityAdded(const EntityHandle& entity)
void BaseSystem::OnEntityAdded(Entity* entity)
{
NazaraUnused(entity);
}
void BaseSystem::OnEntityRemoved(const EntityHandle& entity)
void BaseSystem::OnEntityRemoved(Entity* entity)
{
NazaraUnused(entity);
}

View File

@ -45,7 +45,7 @@ namespace Ndk
void Entity::Kill()
{
m_world->KillEntity(CreateHandle());
m_world->KillEntity(this);
}
bool Entity::IsValid() const
@ -88,7 +88,7 @@ namespace Ndk
if (m_world->HasSystem(systemId))
{
BaseSystem& system = m_world->GetSystem(systemId);
system.RemoveEntity(CreateHandle());
system.RemoveEntity(this);
}
}
m_systems.clear();

View File

@ -13,7 +13,7 @@ namespace Ndk
Clear();
}
EntityHandle World::CreateEntity()
const EntityHandle& World::CreateEntity()
{
EntityId id;
if (!m_freeIdList.empty())
@ -35,11 +35,10 @@ namespace Ndk
Entity& entity = m_entities[id].entity;
entity.Create();
EntityHandle handle = entity.CreateHandle();
m_aliveEntities.push_back(handle);
m_aliveEntities.emplace_back(&entity);
m_entities[id].aliveIndex = m_aliveEntities.size()-1;
return handle;
return m_aliveEntities.back();
}
void World::Clear()
@ -54,7 +53,7 @@ namespace Ndk
m_killedEntities.Clear();
}
void World::KillEntity(const EntityHandle& entity)
void World::KillEntity(Entity* entity)
{
///DOC: Ignoré si l'entité est invalide
@ -111,9 +110,7 @@ namespace Ndk
{
NazaraAssert(i < m_entities.size(), "Entity index out of range");
EntityBlock& block = m_entities[i];
Entity& entity = block.entity;
EntityHandle& handle = m_aliveEntities[block.aliveIndex];
Entity& entity = m_entities[i].entity;
// Aucun intérêt de traiter une entité n'existant plus
if (entity.IsValid())
@ -123,20 +120,20 @@ namespace Ndk
BaseSystem* system = systemPair.second.get();
// L'entité est-elle enregistrée comme faisant partie du système ?
bool partOfSystem = system->HasEntity(handle);
if (system->Filters(handle))
bool partOfSystem = system->HasEntity(&entity);
if (system->Filters(&entity))
{
// L'entité doit faire partie du système, est-ce que c'est déjà le cas ?
if (!partOfSystem)
// Non, rajoutons-là
system->AddEntity(handle);
system->AddEntity(&entity);
}
else
{
// L'entité ne doit pas faire partie du système, était-ce le cas ?
if (partOfSystem)
// Oui, enlevons-là
system->RemoveEntity(handle);
system->RemoveEntity(&entity);
}
}
}