(NDK) First commit

-Still missing a build file
-This is an Entity Component System, without any component nor system,
so this is an Entity. Yay.


Former-commit-id: f04d2fdfe8819826f940469c2618140a26afa637
This commit is contained in:
Lynix
2015-02-06 13:56:27 +01:00
parent d6c08db282
commit 5067767074
5 changed files with 345 additions and 0 deletions

49
SDK/src/NDK/Entity.cpp Normal file
View File

@@ -0,0 +1,49 @@
// This file was automatically generated on 26 May 2014 at 01:05:31
#include <NDK/Entity.hpp>
#include <NDK/World.hpp>
namespace Ndk
{
Entity::Entity() :
m_world(nullptr)
{
m_id.value = 0;
}
Entity::Entity(Id id, World* world) :
m_id(id),
m_world(world)
{
}
void Entity::Kill()
{
m_world->KillEntity(*this);
}
Entity::Id Entity::GetId() const
{
return m_id;
}
World* Entity::GetWorld() const
{
return m_world;
}
bool Entity::IsValid() const
{
return m_world != nullptr && m_world->IsEntityIdValid(m_id);
}
bool Entity::operator==(const Entity& other) const
{
return m_world == other.m_world && m_id == other.m_id;
}
bool Entity::operator!=(const Entity& other) const
{
return !operator==(other);
}
}

119
SDK/src/NDK/World.cpp Normal file
View File

@@ -0,0 +1,119 @@
// This file was automatically generated on 26 May 2014 at 01:05:31
#include <NDK/World.hpp>
#include <Nazara/Core/Error.hpp>
namespace Ndk
{
World::World() :
m_nextIndex(0)
{
}
World::~World() = default;
Entity World::CreateEntity()
{
Entity::Id id;
if (!m_freeIdList.empty())
{
// On récupère un identifiant
id = m_freeIdList.back();
m_freeIdList.pop_back();
}
else
{
// On alloue un nouvel identifiant
m_entitiesCounter.resize(m_entitiesCounter.size() + 1);
auto& counter = m_entitiesCounter.back();
counter = 1;
id.part.counter = counter;
id.part.index = m_nextIndex;
m_nextIndex++;
}
Entity entity(id, this);
m_aliveEntities.push_back(entity);
return entity;
}
World::EntityList World::CreateEntities(unsigned int count)
{
EntityList list;
for (unsigned int i = 0; i < count; ++i)
list.push_back(CreateEntity());
return list;
}
void World::KillEntity(Entity& entity)
{
m_killedEntities.push_back(entity);
}
void World::KillEntities(EntityList& list)
{
m_killedEntities.reserve(m_killedEntities.size() + list.size());
for (Entity& entity : list)
KillEntity(entity);
}
Entity World::GetEntity(Entity::Id id)
{
if (IsEntityIdValid(id))
return Entity(id, this);
else
{
NazaraError("Invalid ID");
return Entity();
}
}
bool World::IsEntityValid(const Entity& entity) const
{
return entity.GetWorld() == this && IsEntityIdValid(entity.GetId());
}
bool World::IsEntityIdValid(Entity::Id id) const
{
return m_entitiesCounter[id.part.index] == id.part.counter;
}
void World::Update()
{
if (!m_killedEntities.empty())
{
///FIXME: Inverser les deux boucles ?
for (unsigned int i = 0; i < m_aliveEntities.size(); ++i)
{
Entity::Id e1 = m_aliveEntities[i].GetId();
for (unsigned int j = 0; j < m_killedEntities.size(); ++j)
{
Entity::Id e2 = m_killedEntities[j].GetId();
if (e1 == e2)
{
// Remise en file de l'identifiant d'entité
nzUInt32& counter = m_entitiesCounter[e1.part.index];
counter++;
e1.part.counter = counter;
m_freeIdList.push_back(e1);
// Suppression de l'entité des deux tableaux
m_aliveEntities.erase(m_aliveEntities.begin() + i);
m_killedEntities.erase(m_killedEntities.begin() + j);
break;
}
}
if (m_killedEntities.empty())
break;
}
}
}
}