Ndk: Added EntityList

Former-commit-id: f68c3830e59f460b06e23dbacc150c17937491df
This commit is contained in:
Lynix 2015-05-04 00:25:54 +02:00
parent da36f95b7c
commit 58416694d4
4 changed files with 186 additions and 17 deletions

View File

@ -0,0 +1,63 @@
// 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_ENTITYLIST_HPP
#define NDK_ENTITYLIST_HPP
#include <Nazara/Core/Bitset.hpp>
#include <NDK/Prerequesites.hpp>
#include <NDK/EntityHandle.hpp>
namespace Ndk
{
class NDK_API EntityList
{
public:
using Container = std::vector<EntityHandle>;
EntityList() = default;
~EntityList() = default;
void Clear();
bool Has(const Entity* entity);
bool Has(EntityId entity);
void Insert(Entity* entity);
void Remove(Entity* entity);
// Interface STD
Container::iterator begin();
Container::const_iterator begin() const;
Container::const_iterator cbegin() const;
Container::const_iterator cend() const;
Container::const_reverse_iterator crbegin() const;
Container::const_reverse_iterator crend() const;
bool empty() const;
Container::iterator end();
Container::const_iterator end() const;
Container::reverse_iterator rbegin();
Container::const_reverse_iterator rbegin() const;
Container::reverse_iterator rend();
Container::const_reverse_iterator rend() const;
Container::size_type size() const;
private:
std::vector<EntityHandle> m_entities;
NzBitset<nzUInt64> m_entityBits;
};
}
#include <NDK/EntityList.inl>
#endif // NDK_ENTITYLIST_HPP

View File

@ -0,0 +1,117 @@
// 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/Error.hpp>
#include <algorithm>
namespace Ndk
{
inline void EntityList::Clear()
{
m_entities.clear();
m_entityBits.Clear();
}
inline bool EntityList::Has(const Entity* entity)
{
return entity && entity->IsValid() && Has(entity->GetId());
}
inline bool EntityList::Has(EntityId entity)
{
return m_entityBits.UnboundedTest(entity);
}
inline void EntityList::Insert(Entity* entity)
{
if (!Has(entity))
{
m_entities.emplace_back(entity);
m_entityBits.UnboundedSet(entity->GetId(), true);
}
}
inline void EntityList::Remove(Entity* entity)
{
if (Has(entity))
{
auto it = std::find(m_entities.begin(), m_entities.end(), *entity);
NazaraAssert(it != m_entities.end(), "Entity should be part of the vector");
std::swap(*it, m_entities.back());
m_entities.pop_back(); // On le sort du vector
}
}
// Interface STD
inline EntityList::Container::iterator EntityList::begin()
{
return m_entities.begin();
}
inline EntityList::Container::const_iterator EntityList::begin() const
{
return m_entities.begin();
}
inline EntityList::Container::const_iterator EntityList::cbegin() const
{
return m_entities.cbegin();
}
inline EntityList::Container::const_iterator EntityList::cend() const
{
return m_entities.cend();
}
inline EntityList::Container::const_reverse_iterator EntityList::crbegin() const
{
return m_entities.crbegin();
}
inline EntityList::Container::const_reverse_iterator EntityList::crend() const
{
return m_entities.crend();
}
inline bool EntityList::empty() const
{
return m_entities.empty();
}
inline EntityList::Container::iterator EntityList::end()
{
return m_entities.end();
}
inline EntityList::Container::const_iterator EntityList::end() const
{
return m_entities.end();
}
inline EntityList::Container::reverse_iterator EntityList::rbegin()
{
return m_entities.rbegin();
}
inline EntityList::Container::const_reverse_iterator EntityList::rbegin() const
{
return m_entities.rbegin();
}
inline EntityList::Container::reverse_iterator EntityList::rend()
{
return m_entities.rend();
}
inline EntityList::Container::const_reverse_iterator EntityList::rend() const
{
return m_entities.rend();
}
inline EntityList::Container::size_type EntityList::size() const
{
return m_entities.size();
}
}

View File

@ -8,6 +8,7 @@
#define NDK_SYSTEMS_PHYSICSSYSTEM_HPP
#include <Nazara/Physics/PhysWorld.hpp>
#include <NDK/EntityList.hpp>
#include <NDK/System.hpp>
namespace Ndk
@ -29,8 +30,8 @@ namespace Ndk
private:
void OnEntityValidation(Entity* entity, bool justAdded) override;
std::vector<EntityHandle> m_dynamicObjects;
std::vector<EntityHandle> m_staticObjects;
EntityList m_dynamicObjects;
EntityList m_staticObjects;
NzPhysWorld m_world;
};
}

View File

@ -82,23 +82,11 @@ namespace Ndk
{
// On prend le tableau inverse de celui dont l'entité devrait faire partie
auto& entities = (entity->HasComponent<PhysicsComponent>()) ? m_staticObjects : m_dynamicObjects;
auto it = std::find(entities.begin(), entities.end(), *entity);
if (it != entities.end())
{
// Pour éviter de déplacer beaucoup de handles, on swap le dernier avec celui à supprimer
std::swap(*it, entities.back());
entities.pop_back(); // On le sort du vector
}
entities.Remove(entity);
}
if (entity->HasComponent<PhysicsComponent>())
m_dynamicObjects.emplace_back(entity);
else
{
NazaraAssert(entity->HasComponent<CollisionComponent>(), "Validated entity should have component CollisionComponent");
m_staticObjects.emplace_back(entity);
}
auto& entities = (entity->HasComponent<PhysicsComponent>()) ? m_dynamicObjects : m_staticObjects;
entities.Insert(entity);
}
SystemIndex PhysicsSystem::systemIndex;