Remade Entities
Former-commit-id: 25f7bc84279fdf58b44cf78e2d94b4cbb78a8410
This commit is contained in:
@@ -8,57 +8,50 @@
|
||||
#define NDK_ENTITY_HPP
|
||||
|
||||
#include <NDK/Prerequesites.hpp>
|
||||
#include <set>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
class EntityHandle;
|
||||
class World;
|
||||
|
||||
class NDK_API Entity
|
||||
{
|
||||
friend EntityHandle;
|
||||
friend World;
|
||||
|
||||
public:
|
||||
class Id;
|
||||
using Id = nzUInt32;
|
||||
|
||||
Entity();
|
||||
Entity(const Entity&) = default;
|
||||
~Entity() = default;
|
||||
Entity(const Entity&) = delete;
|
||||
Entity(Entity&& entity);
|
||||
~Entity();
|
||||
|
||||
void Kill();
|
||||
EntityHandle CreateHandle();
|
||||
|
||||
Id GetId() const;
|
||||
World* GetWorld() const;
|
||||
|
||||
void Kill();
|
||||
|
||||
bool IsValid() const;
|
||||
|
||||
Entity& operator=(const Entity&) = default;
|
||||
|
||||
bool operator==(const Entity& other) const;
|
||||
bool operator!=(const Entity& other) const;
|
||||
|
||||
// Identifiant
|
||||
struct Id
|
||||
{
|
||||
struct Part
|
||||
{
|
||||
nzUInt32 counter, index;
|
||||
};
|
||||
|
||||
union
|
||||
{
|
||||
Part part;
|
||||
nzUInt64 value;
|
||||
};
|
||||
|
||||
bool operator==(const Id& other) const;
|
||||
bool operator!=(const Id& other) const;
|
||||
};
|
||||
Entity& operator=(const Entity&) = delete;
|
||||
Entity& operator=(Entity&&) = delete;
|
||||
|
||||
private:
|
||||
Entity(Id id, World* world);
|
||||
Entity(World& world, Id id);
|
||||
|
||||
void Create();
|
||||
void Destroy();
|
||||
|
||||
void RegisterHandle(EntityHandle* handle);
|
||||
void UnregisterHandle(EntityHandle* handle);
|
||||
|
||||
std::set<EntityHandle*> m_handles;
|
||||
Id m_id;
|
||||
World* m_world;
|
||||
bool m_valid;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -6,15 +6,9 @@
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
inline Entity::Entity() :
|
||||
m_world(nullptr)
|
||||
{
|
||||
m_id.value = 0;
|
||||
}
|
||||
|
||||
inline Entity::Entity(Id id, World* world) :
|
||||
inline Entity::Entity(World& world, Id id) :
|
||||
m_id(id),
|
||||
m_world(world)
|
||||
m_world(&world)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -28,23 +22,13 @@ namespace Ndk
|
||||
return m_world;
|
||||
}
|
||||
|
||||
inline bool Entity::operator==(const Entity& other) const
|
||||
inline void Entity::RegisterHandle(EntityHandle* handle)
|
||||
{
|
||||
return m_world == other.m_world && m_id == other.m_id;
|
||||
m_handles.insert(handle);
|
||||
}
|
||||
|
||||
inline bool Entity::operator!=(const Entity& other) const
|
||||
inline void Entity::UnregisterHandle(EntityHandle* handle)
|
||||
{
|
||||
return !operator==(other);
|
||||
}
|
||||
|
||||
inline bool Entity::Id::operator==(const Id& other) const
|
||||
{
|
||||
return value == other.value;
|
||||
}
|
||||
|
||||
inline bool Entity::Id::operator!=(const Id& other) const
|
||||
{
|
||||
return !operator==(other);
|
||||
m_handles.erase(handle);
|
||||
}
|
||||
}
|
||||
|
||||
71
SDK/include/NDK/EntityHandle.hpp
Normal file
71
SDK/include/NDK/EntityHandle.hpp
Normal file
@@ -0,0 +1,71 @@
|
||||
// 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_ENTITYHANDLE_HPP
|
||||
#define NDK_ENTITYHANDLE_HPP
|
||||
|
||||
#include <NDK/Prerequesites.hpp>
|
||||
#include <NDK/Entity.hpp>
|
||||
#include <ostream>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
class Entity;
|
||||
|
||||
class EntityHandle
|
||||
{
|
||||
friend Entity;
|
||||
|
||||
public:
|
||||
EntityHandle();
|
||||
explicit EntityHandle(Entity* entity);
|
||||
EntityHandle(const EntityHandle& handle);
|
||||
EntityHandle(EntityHandle&& handle);
|
||||
~EntityHandle();
|
||||
|
||||
Entity* GetEntity() const;
|
||||
|
||||
bool IsValid() const;
|
||||
|
||||
void Reset(Entity* entity = nullptr);
|
||||
void Reset(const EntityHandle& handle);
|
||||
void Reset(EntityHandle&& handle);
|
||||
|
||||
EntityHandle& Swap(EntityHandle& handle);
|
||||
|
||||
operator bool() const;
|
||||
operator Entity*() const;
|
||||
Entity* operator->() const;
|
||||
|
||||
EntityHandle& operator=(Entity* entity);
|
||||
EntityHandle& operator=(const EntityHandle& handle);
|
||||
EntityHandle& operator=(EntityHandle&& handle);
|
||||
|
||||
friend std::ostream& operator<<(std::ostream& out, const EntityHandle& handle);
|
||||
|
||||
friend bool operator==(const EntityHandle& lhs, const EntityHandle& rhs);
|
||||
friend bool operator!=(const EntityHandle& lhs, const EntityHandle& rhs);
|
||||
friend bool operator<(const EntityHandle& lhs, const EntityHandle& rhs);
|
||||
friend bool operator<=(const EntityHandle& lhs, const EntityHandle& rhs);
|
||||
friend bool operator>(const EntityHandle& lhs, const EntityHandle& rhs);
|
||||
friend bool operator>=(const EntityHandle& lhs, const EntityHandle& rhs);
|
||||
|
||||
private:
|
||||
void OnEntityDestroyed();
|
||||
void OnEntityMoved(Entity* newEntity);
|
||||
|
||||
Entity* m_entity;
|
||||
};
|
||||
}
|
||||
|
||||
namespace std
|
||||
{
|
||||
void swap(Ndk::EntityHandle& lhs, Ndk::EntityHandle& rhs);
|
||||
}
|
||||
|
||||
#include <NDK/EntityHandle.inl>
|
||||
|
||||
#endif // NDK_ENTITYHANDLE_HPP
|
||||
177
SDK/include/NDK/EntityHandle.inl
Normal file
177
SDK/include/NDK/EntityHandle.inl
Normal file
@@ -0,0 +1,177 @@
|
||||
// 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 <functional>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
inline EntityHandle::EntityHandle() :
|
||||
m_entity(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
inline EntityHandle::EntityHandle(Entity* entity) :
|
||||
EntityHandle()
|
||||
{
|
||||
Reset(entity);
|
||||
}
|
||||
|
||||
inline EntityHandle::EntityHandle(const EntityHandle& handle) :
|
||||
EntityHandle()
|
||||
{
|
||||
Reset(handle);
|
||||
}
|
||||
|
||||
inline EntityHandle::EntityHandle(EntityHandle&& handle) :
|
||||
EntityHandle()
|
||||
{
|
||||
Reset(handle);
|
||||
}
|
||||
|
||||
inline EntityHandle::~EntityHandle()
|
||||
{
|
||||
Reset(nullptr);
|
||||
}
|
||||
|
||||
inline Entity* EntityHandle::GetEntity() const
|
||||
{
|
||||
return m_entity;
|
||||
}
|
||||
|
||||
inline bool EntityHandle::IsValid() const
|
||||
{
|
||||
return m_entity != nullptr;
|
||||
}
|
||||
|
||||
inline void EntityHandle::Reset(Entity* entity)
|
||||
{
|
||||
// Si nous avions déjà une entité, nous devons l'informer que nous ne pointons plus vers elle
|
||||
if (m_entity)
|
||||
m_entity->UnregisterHandle(this);
|
||||
|
||||
m_entity = entity;
|
||||
if (m_entity)
|
||||
// On informe la nouvelle entité que nous pointons vers elle
|
||||
m_entity->RegisterHandle(this);
|
||||
}
|
||||
|
||||
inline void EntityHandle::Reset(const EntityHandle& handle)
|
||||
{
|
||||
Reset(handle.GetEntity());
|
||||
}
|
||||
|
||||
inline void EntityHandle::Reset(EntityHandle&& handle)
|
||||
{
|
||||
Reset(handle.GetEntity());
|
||||
}
|
||||
|
||||
inline EntityHandle& EntityHandle::Swap(EntityHandle& handle)
|
||||
{
|
||||
std::swap(m_entity, handle.m_entity);
|
||||
}
|
||||
|
||||
inline EntityHandle::operator bool() const
|
||||
{
|
||||
return IsValid();
|
||||
}
|
||||
|
||||
inline EntityHandle::operator Entity*() const
|
||||
{
|
||||
return m_entity;
|
||||
}
|
||||
|
||||
inline Entity* EntityHandle::operator->() const
|
||||
{
|
||||
return m_entity;
|
||||
}
|
||||
|
||||
inline EntityHandle& EntityHandle::operator=(Entity* entity)
|
||||
{
|
||||
Reset(entity);
|
||||
}
|
||||
|
||||
inline EntityHandle& EntityHandle::operator=(const EntityHandle& handle)
|
||||
{
|
||||
Reset(handle);
|
||||
}
|
||||
|
||||
inline EntityHandle& EntityHandle::operator=(EntityHandle&& handle)
|
||||
{
|
||||
Reset(handle);
|
||||
}
|
||||
|
||||
inline void EntityHandle::OnEntityDestroyed()
|
||||
{
|
||||
// Un raccourci, un appel à Reset nous enlèverait de la liste des handles que nous ne pouvons pas modifier
|
||||
// maintenant car elle est actuellement parcourue
|
||||
m_entity = nullptr;
|
||||
}
|
||||
|
||||
inline void EntityHandle::OnEntityMoved(Entity* newEntity)
|
||||
{
|
||||
// L'entité a été déplacée (peut arriver lors d'un changement de taille du conteneur du monde)
|
||||
// nous mettons à jour notre pointeur
|
||||
m_entity = newEntity;
|
||||
}
|
||||
|
||||
inline std::ostream& operator<<(std::ostream& out, const EntityHandle& handle)
|
||||
{
|
||||
out << "EntityHandle(";
|
||||
if (handle.IsValid())
|
||||
out << "Entity(" << handle->GetId() << ")";
|
||||
else
|
||||
out << "Null entity";
|
||||
|
||||
out << ')';
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
inline bool operator==(const EntityHandle& lhs, const EntityHandle& rhs)
|
||||
{
|
||||
return lhs.m_entity == rhs.m_entity;
|
||||
}
|
||||
|
||||
inline bool operator!=(const EntityHandle& lhs, const EntityHandle& rhs)
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
inline bool operator<(const EntityHandle& lhs, const EntityHandle& rhs)
|
||||
{
|
||||
return lhs.m_entity < rhs.m_entity;
|
||||
}
|
||||
|
||||
inline bool operator<=(const EntityHandle& lhs, const EntityHandle& rhs)
|
||||
{
|
||||
return !(lhs > rhs);
|
||||
}
|
||||
|
||||
inline bool operator>(const EntityHandle& lhs, const EntityHandle& rhs)
|
||||
{
|
||||
return rhs < lhs;
|
||||
}
|
||||
|
||||
inline bool operator>=(const EntityHandle& lhs, const EntityHandle& rhs)
|
||||
{
|
||||
return !(lhs < rhs);
|
||||
}
|
||||
}
|
||||
|
||||
namespace std
|
||||
{
|
||||
template<>
|
||||
struct hash<Ndk::EntityHandle>
|
||||
{
|
||||
size_t operator()(const Ndk::EntityHandle& handle) const
|
||||
{
|
||||
return hash<Ndk::Entity*>()(handle.GetEntity());
|
||||
}
|
||||
};
|
||||
|
||||
inline void swap(Ndk::EntityHandle& lhs, Ndk::EntityHandle& rhs)
|
||||
{
|
||||
lhs.Swap(rhs);
|
||||
}
|
||||
}
|
||||
@@ -10,39 +10,41 @@
|
||||
#include <Nazara/Core/NonCopyable.hpp>
|
||||
#include <NDK/Prerequesites.hpp>
|
||||
#include <NDK/Entity.hpp>
|
||||
#include <NDK/EntityHandle.hpp>
|
||||
#include <vector>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
class EntityHandle;
|
||||
|
||||
class NDK_API World : NzNonCopyable
|
||||
{
|
||||
public:
|
||||
using EntityList = std::vector<Entity>;
|
||||
using EntityList = std::vector<EntityHandle>;
|
||||
|
||||
World();
|
||||
~World() = default;
|
||||
World() = default;
|
||||
~World();
|
||||
|
||||
Entity CreateEntity();
|
||||
EntityHandle CreateEntity();
|
||||
EntityList CreateEntities(unsigned int count);
|
||||
|
||||
void Clear();
|
||||
|
||||
void KillEntity(Entity& entity);
|
||||
void KillEntities(EntityList& list);
|
||||
void KillEntity(Entity* entity);
|
||||
void KillEntities(const EntityList& list);
|
||||
|
||||
Entity GetEntity(Entity::Id id);
|
||||
Entity* GetEntity(Entity::Id id);
|
||||
|
||||
bool IsEntityValid(const Entity& entity) const;
|
||||
bool IsEntityValid(Entity* entity) const;
|
||||
bool IsEntityIdValid(Entity::Id id) const;
|
||||
|
||||
void Update();
|
||||
|
||||
private:
|
||||
std::vector<nzUInt32> m_entitiesCounter;
|
||||
std::vector<Entity::Id> m_freeIdList;
|
||||
std::vector<Entity> m_entities;
|
||||
EntityList m_aliveEntities;
|
||||
EntityList m_killedEntities;
|
||||
nzUInt32 m_nextIndex;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -4,40 +4,31 @@
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
inline World::World() :
|
||||
m_nextIndex(0)
|
||||
{
|
||||
}
|
||||
|
||||
inline World::EntityList World::CreateEntities(unsigned int count)
|
||||
{
|
||||
EntityList list;
|
||||
list.reserve(count);
|
||||
|
||||
for (unsigned int i = 0; i < count; ++i)
|
||||
list.push_back(CreateEntity());
|
||||
list.emplace_back(CreateEntity());
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
inline void World::KillEntities(EntityList& list)
|
||||
inline void World::KillEntities(const EntityList& list)
|
||||
{
|
||||
m_killedEntities.reserve(m_killedEntities.size() + list.size());
|
||||
for (Entity& entity : list)
|
||||
for (const EntityHandle& entity : list)
|
||||
KillEntity(entity);
|
||||
}
|
||||
|
||||
inline bool World::IsEntityValid(const Entity& entity) const
|
||||
inline bool World::IsEntityValid(Entity* entity) const
|
||||
{
|
||||
///DOC: Cette méthode vérifie également l'appartenance de l'entité au monde (et est donc plus sûre)
|
||||
return entity.GetWorld() == this && IsEntityIdValid(entity.GetId());
|
||||
return entity != nullptr && entity->GetWorld() == this && IsEntityIdValid(entity->GetId());
|
||||
}
|
||||
|
||||
inline bool World::IsEntityIdValid(Entity::Id id) const
|
||||
{
|
||||
///DOC: Il est possible que si l'identifiant vienne d'un autre monde, il soit considéré valide
|
||||
/// alors qu'aucune entité de ce monde-ci ne l'utilise (encore)
|
||||
|
||||
return m_entitiesCounter[id.part.index] == id.part.counter;
|
||||
return id < m_entities.size() && m_entities[id].IsValid();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user