Merge remote-tracking branch 'refs/remotes/origin/master' into reflection-mapping
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
|
||||
#include <Nazara/Core/Bitset.hpp>
|
||||
#include <NDK/Entity.hpp>
|
||||
#include <NDK/EntityList.hpp>
|
||||
#include <vector>
|
||||
|
||||
namespace Ndk
|
||||
@@ -33,7 +34,7 @@ namespace Ndk
|
||||
|
||||
bool Filters(const Entity* entity) const;
|
||||
|
||||
inline const std::vector<EntityHandle>& GetEntities() const;
|
||||
inline const EntityList& GetEntities() const;
|
||||
inline SystemIndex GetIndex() const;
|
||||
inline int GetUpdateOrder() const;
|
||||
inline float GetUpdateRate() const;
|
||||
@@ -84,12 +85,11 @@ namespace Ndk
|
||||
static inline bool Initialize();
|
||||
static inline void Uninitialize();
|
||||
|
||||
std::vector<EntityHandle> m_entities;
|
||||
Nz::Bitset<Nz::UInt64> m_entityBits;
|
||||
Nz::Bitset<> m_excludedComponents;
|
||||
mutable Nz::Bitset<> m_filterResult;
|
||||
Nz::Bitset<> m_requiredAnyComponents;
|
||||
Nz::Bitset<> m_requiredComponents;
|
||||
EntityList m_entities;
|
||||
SystemIndex m_systemIndex;
|
||||
World* m_world;
|
||||
bool m_updateEnabled;
|
||||
|
||||
@@ -56,7 +56,7 @@ namespace Ndk
|
||||
* \return A constant reference to the list of entities
|
||||
*/
|
||||
|
||||
inline const std::vector<EntityHandle>& BaseSystem::GetEntities() const
|
||||
inline const EntityList& BaseSystem::GetEntities() const
|
||||
{
|
||||
return m_entities;
|
||||
}
|
||||
@@ -121,10 +121,7 @@ namespace Ndk
|
||||
|
||||
inline bool BaseSystem::HasEntity(const Entity* entity) const
|
||||
{
|
||||
if (!entity)
|
||||
return false;
|
||||
|
||||
return m_entityBits.UnboundedTest(entity->GetId());
|
||||
return m_entities.Has(entity);
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -288,9 +285,7 @@ namespace Ndk
|
||||
{
|
||||
NazaraAssert(entity, "Invalid entity");
|
||||
|
||||
m_entities.emplace_back(entity);
|
||||
m_entityBits.UnboundedSet(entity->GetId(), true);
|
||||
|
||||
m_entities.Insert(entity);
|
||||
entity->RegisterSystem(m_systemIndex);
|
||||
|
||||
OnEntityAdded(entity);
|
||||
@@ -308,14 +303,7 @@ namespace Ndk
|
||||
{
|
||||
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");
|
||||
|
||||
// To avoid moving a lot of handles, we swap and pop
|
||||
std::swap(*it, m_entities.back());
|
||||
m_entities.pop_back(); // We get it out of the vector
|
||||
|
||||
m_entityBits.Reset(entity->GetId());
|
||||
m_entities.Remove(entity);
|
||||
entity->UnregisterSystem(m_systemIndex);
|
||||
|
||||
OnEntityRemoved(entity); // And we alert our callback
|
||||
@@ -360,7 +348,7 @@ namespace Ndk
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Uninitializes the BaseSystem
|
||||
* \brief Uninitialize the BaseSystem
|
||||
*/
|
||||
|
||||
inline void BaseSystem::Uninitialize()
|
||||
|
||||
@@ -44,7 +44,7 @@ namespace Ndk
|
||||
const Nz::Frustumf& GetFrustum() const override;
|
||||
inline unsigned int GetLayer() const;
|
||||
const Nz::Matrix4f& GetProjectionMatrix() const override;
|
||||
inline Nz::ProjectionType GetProjectionType() const;
|
||||
Nz::ProjectionType GetProjectionType() const override;
|
||||
inline const Nz::Vector2f& GetSize() const;
|
||||
const Nz::RenderTarget* GetTarget() const override;
|
||||
inline const Nz::Rectf& GetTargetRegion() const;
|
||||
|
||||
@@ -117,16 +117,6 @@ namespace Ndk
|
||||
return m_layer;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the projection type of the camera
|
||||
* \return Projection type of the camera
|
||||
*/
|
||||
|
||||
inline Nz::ProjectionType CameraComponent::GetProjectionType() const
|
||||
{
|
||||
return m_projectionType;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the size of the camera
|
||||
* \return Size of the camera
|
||||
|
||||
@@ -54,6 +54,9 @@ namespace Ndk
|
||||
|
||||
inline void RemoveFromCullingList(GraphicsComponentCullingList* cullingList) const;
|
||||
|
||||
inline void UpdateLocalMatrix(const Nz::InstancedRenderable* instancedRenderable, const Nz::Matrix4f& localMatrix);
|
||||
inline void UpdateRenderOrder(const Nz::InstancedRenderable* instancedRenderable, int renderOrder);
|
||||
|
||||
static ComponentIndex componentIndex;
|
||||
|
||||
private:
|
||||
|
||||
@@ -170,6 +170,32 @@ namespace Ndk
|
||||
}
|
||||
}
|
||||
|
||||
inline void GraphicsComponent::UpdateLocalMatrix(const Nz::InstancedRenderable* instancedRenderable, const Nz::Matrix4f& localMatrix)
|
||||
{
|
||||
for (auto& renderable : m_renderables)
|
||||
{
|
||||
if (renderable.renderable == instancedRenderable)
|
||||
{
|
||||
renderable.data.localMatrix = localMatrix;
|
||||
|
||||
InvalidateBoundingVolume();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline void GraphicsComponent::UpdateRenderOrder(const Nz::InstancedRenderable* instancedRenderable, int renderOrder)
|
||||
{
|
||||
for (auto& renderable : m_renderables)
|
||||
{
|
||||
if (renderable.renderable == instancedRenderable)
|
||||
{
|
||||
renderable.data.renderOrder = renderOrder;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Invalidates the bounding volume
|
||||
*/
|
||||
|
||||
@@ -16,45 +16,64 @@ namespace Ndk
|
||||
class NDK_API EntityList
|
||||
{
|
||||
public:
|
||||
using Container = std::vector<EntityHandle>;
|
||||
class iterator;
|
||||
friend iterator;
|
||||
using size_type = std::size_t;
|
||||
|
||||
EntityList() = default;
|
||||
inline EntityList();
|
||||
inline EntityList(const EntityList& entityList);
|
||||
inline EntityList(EntityList&& entityList) noexcept;
|
||||
~EntityList() = default;
|
||||
|
||||
inline void Clear();
|
||||
|
||||
inline bool Has(const Entity* entity);
|
||||
inline bool Has(EntityId entity);
|
||||
inline bool Has(const Entity* entity) const;
|
||||
inline bool Has(EntityId entity) const;
|
||||
|
||||
inline void Insert(Entity* entity);
|
||||
|
||||
inline void Remove(Entity* entity);
|
||||
|
||||
// STL API
|
||||
inline Container::iterator begin();
|
||||
inline Container::const_iterator begin() const;
|
||||
|
||||
inline Container::const_iterator cbegin() const;
|
||||
inline Container::const_iterator cend() const;
|
||||
inline Container::const_reverse_iterator crbegin() const;
|
||||
inline Container::const_reverse_iterator crend() const;
|
||||
|
||||
inline iterator begin() const;
|
||||
inline bool empty() const;
|
||||
inline iterator end() const;
|
||||
inline size_type size() const;
|
||||
|
||||
inline Container::iterator end();
|
||||
inline Container::const_iterator end() const;
|
||||
|
||||
inline Container::reverse_iterator rbegin();
|
||||
inline Container::const_reverse_iterator rbegin() const;
|
||||
|
||||
inline Container::reverse_iterator rend();
|
||||
inline Container::const_reverse_iterator rend() const;
|
||||
|
||||
inline Container::size_type size() const;
|
||||
inline EntityList& operator=(const EntityList& entityList);
|
||||
inline EntityList& operator=(EntityList&& entityList) noexcept;
|
||||
|
||||
private:
|
||||
std::vector<EntityHandle> m_entities;
|
||||
inline std::size_t FindNext(std::size_t currentId) const;
|
||||
inline World* GetWorld() const;
|
||||
|
||||
Nz::Bitset<Nz::UInt64> m_entityBits;
|
||||
World* m_world;
|
||||
};
|
||||
|
||||
class NDK_API EntityList::iterator : public std::iterator<std::forward_iterator_tag, const EntityHandle>
|
||||
{
|
||||
friend EntityList;
|
||||
|
||||
public:
|
||||
inline iterator(const iterator& iterator);
|
||||
|
||||
const EntityHandle& operator*() const;
|
||||
|
||||
inline iterator& operator=(const iterator& iterator);
|
||||
inline iterator& operator++();
|
||||
inline iterator operator++(int);
|
||||
|
||||
friend inline bool operator==(const iterator& lhs, const iterator& rhs);
|
||||
friend inline bool operator!=(const iterator& lhs, const iterator& rhs);
|
||||
|
||||
friend inline void swap(iterator& lhs, iterator& rhs);
|
||||
|
||||
private:
|
||||
inline iterator(const EntityList* world, std::size_t nextId);
|
||||
|
||||
std::size_t m_nextEntityId;
|
||||
const EntityList* m_list;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -2,8 +2,10 @@
|
||||
// This file is part of the "Nazara Development Kit"
|
||||
// For conditions of distribution and use, see copyright notice in Prerequesites.hpp
|
||||
|
||||
#include <NDK/EntityList.hpp>
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <algorithm>
|
||||
#include "EntityList.hpp"
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
@@ -14,24 +16,55 @@ namespace Ndk
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \brief Clears the set from every entities
|
||||
* \brief Construct a new entity list
|
||||
*/
|
||||
|
||||
inline void EntityList::Clear()
|
||||
inline EntityList::EntityList() :
|
||||
m_world(nullptr)
|
||||
{
|
||||
m_entities.clear();
|
||||
m_entityBits.Clear();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Checks whether or not the set contains the entity
|
||||
* \brief Construct a new entity list by copying another one
|
||||
*/
|
||||
inline EntityList::EntityList(const EntityList& entityList) :
|
||||
m_entityBits(entityList.m_entityBits),
|
||||
m_world(entityList.m_world)
|
||||
{
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Construct a new entity list by moving a list into this one
|
||||
*/
|
||||
inline EntityList::EntityList(EntityList&& entityList) noexcept :
|
||||
m_entityBits(std::move(entityList.m_entityBits)),
|
||||
m_world(entityList.m_world)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/*!
|
||||
* \brief Clears the set from every entities
|
||||
*
|
||||
* \remark This resets the implicit world member, allowing you to insert entities from a different world than previously
|
||||
*/
|
||||
inline void EntityList::Clear()
|
||||
{
|
||||
m_entityBits.Clear();
|
||||
m_world = nullptr;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Checks whether or not the EntityList contains the entity
|
||||
* \return true If it is the case
|
||||
*
|
||||
* \param entity Pointer to the entity
|
||||
*
|
||||
* \remark If the Insert function was called since the EntityList construction (or last call to Clear), the entity passed by parameter must belong to the same world as the previously inserted entities.
|
||||
*/
|
||||
|
||||
inline bool EntityList::Has(const Entity* entity)
|
||||
inline bool EntityList::Has(const Entity* entity) const
|
||||
{
|
||||
NazaraAssert(!m_world || !entity || entity->GetWorld() == m_world, "Incompatible world");
|
||||
|
||||
return entity && entity->IsValid() && Has(entity->GetId());
|
||||
}
|
||||
|
||||
@@ -41,8 +74,7 @@ namespace Ndk
|
||||
*
|
||||
* \param id Identifier of the entity
|
||||
*/
|
||||
|
||||
inline bool EntityList::Has(EntityId entity)
|
||||
inline bool EntityList::Has(EntityId entity) const
|
||||
{
|
||||
return m_entityBits.UnboundedTest(entity);
|
||||
}
|
||||
@@ -50,18 +82,21 @@ namespace Ndk
|
||||
/*!
|
||||
* \brief Inserts the entity into the set
|
||||
*
|
||||
* \param entity Pointer to the entity
|
||||
* Marks an entity as present in this entity list, it must belongs to the same world as others entities contained in this list.
|
||||
*
|
||||
* \param entity Valid pointer to an entity
|
||||
*
|
||||
* \remark If entity is already contained, no action is performed
|
||||
* \remark If any entity has been inserted since construction (or last Clear call), the entity must belong to the same world as the previously inserted entities
|
||||
* \remark It's up to the programmer to remove an entity from this list before its deletion
|
||||
*/
|
||||
|
||||
inline void EntityList::Insert(Entity* entity)
|
||||
{
|
||||
if (!Has(entity))
|
||||
{
|
||||
m_entities.emplace_back(entity);
|
||||
m_entityBits.UnboundedSet(entity->GetId(), true);
|
||||
}
|
||||
NazaraAssert(entity, "Invalid entity");
|
||||
NazaraAssert(!m_world || entity->GetWorld() == m_world, "Incompatible world");
|
||||
|
||||
m_entityBits.UnboundedSet(entity->GetId(), true);
|
||||
m_world = entity->GetWorld();
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -70,89 +105,117 @@ namespace Ndk
|
||||
* \param entity Pointer to the entity
|
||||
*
|
||||
* \remark If entity is not contained, no action is performed
|
||||
* \remark This function never resets the implicit world member, even if it empties the list. Use the Clear method if you want to reset it.
|
||||
*
|
||||
* \see Clear
|
||||
*/
|
||||
|
||||
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(); // We get it out of the vector
|
||||
m_entityBits.UnboundedSet(entity->GetId(), false);
|
||||
}
|
||||
m_entityBits.UnboundedSet(entity->GetId(), false);
|
||||
}
|
||||
|
||||
// Nz::Interface STD
|
||||
inline EntityList::Container::iterator EntityList::begin()
|
||||
// STL Interface
|
||||
inline EntityList::iterator EntityList::begin() const
|
||||
{
|
||||
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();
|
||||
return EntityList::iterator(this, m_entityBits.FindFirst());
|
||||
}
|
||||
|
||||
inline bool EntityList::empty() const
|
||||
{
|
||||
return m_entities.empty();
|
||||
return !m_entityBits.TestAny();
|
||||
}
|
||||
|
||||
inline EntityList::Container::iterator EntityList::end()
|
||||
inline EntityList::iterator EntityList::end() const
|
||||
{
|
||||
return m_entities.end();
|
||||
return EntityList::iterator(this, m_entityBits.npos);
|
||||
}
|
||||
|
||||
inline EntityList::Container::const_iterator EntityList::end() const
|
||||
inline EntityList::size_type EntityList::size() const
|
||||
{
|
||||
return m_entities.end();
|
||||
return m_entityBits.Count();
|
||||
}
|
||||
|
||||
inline EntityList::Container::reverse_iterator EntityList::rbegin()
|
||||
inline EntityList& EntityList::operator=(const EntityList& entityList)
|
||||
{
|
||||
return m_entities.rbegin();
|
||||
m_entityBits = entityList.m_entityBits;
|
||||
m_world = entityList.m_world;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline EntityList::Container::const_reverse_iterator EntityList::rbegin() const
|
||||
inline EntityList& EntityList::operator=(EntityList && entityList) noexcept
|
||||
{
|
||||
return m_entities.rbegin();
|
||||
m_entityBits = std::move(entityList.m_entityBits);
|
||||
m_world = entityList.m_world;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline EntityList::Container::reverse_iterator EntityList::rend()
|
||||
inline std::size_t EntityList::FindNext(std::size_t currentId) const
|
||||
{
|
||||
return m_entities.rend();
|
||||
return m_entityBits.FindNext(currentId);
|
||||
}
|
||||
|
||||
inline EntityList::Container::const_reverse_iterator EntityList::rend() const
|
||||
inline World* EntityList::GetWorld() const
|
||||
{
|
||||
return m_entities.rend();
|
||||
return m_world;
|
||||
}
|
||||
|
||||
inline EntityList::Container::size_type EntityList::size() const
|
||||
|
||||
inline EntityList::iterator::iterator(const EntityList* list, std::size_t nextId) :
|
||||
m_nextEntityId(nextId),
|
||||
m_list(list)
|
||||
{
|
||||
return m_entities.size();
|
||||
}
|
||||
|
||||
inline EntityList::iterator::iterator(const iterator& iterator) :
|
||||
m_nextEntityId(iterator.m_nextEntityId),
|
||||
m_list(iterator.m_list)
|
||||
{
|
||||
}
|
||||
|
||||
inline EntityList::iterator& EntityList::iterator::operator=(const iterator& iterator)
|
||||
{
|
||||
m_nextEntityId = iterator.m_nextEntityId;
|
||||
m_list = iterator.m_list;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline EntityList::iterator& EntityList::iterator::operator++()
|
||||
{
|
||||
m_nextEntityId = m_list->FindNext(m_nextEntityId);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline EntityList::iterator EntityList::iterator::operator++(int)
|
||||
{
|
||||
std::size_t previousId = m_nextEntityId;
|
||||
|
||||
m_nextEntityId = m_list->FindNext(m_nextEntityId);
|
||||
|
||||
return iterator(m_list, previousId);
|
||||
}
|
||||
|
||||
inline bool operator==(const EntityList::iterator& lhs, const EntityList::iterator& rhs)
|
||||
{
|
||||
NazaraAssert(lhs.m_list == rhs.m_list, "Cannot compare iterator coming from different lists");
|
||||
|
||||
return lhs.m_nextEntityId == rhs.m_nextEntityId;
|
||||
}
|
||||
|
||||
inline bool operator!=(const EntityList::iterator& lhs, const EntityList::iterator& rhs)
|
||||
{
|
||||
return !operator==(lhs, rhs);
|
||||
}
|
||||
|
||||
inline void swap(EntityList::iterator& lhs, EntityList::iterator& rhs)
|
||||
{
|
||||
NazaraAssert(lhs.m_list == rhs.m_list, "Cannot compare iterator coming from different lists");
|
||||
|
||||
using std::swap;
|
||||
|
||||
std::swap(lhs.m_nextEntityId, rhs.m_nextEntityId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// Copyright (C) 2017 Jérôme Leclercq
|
||||
// Copyright (C) 2017 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
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include <NDK/Prerequesites.hpp>
|
||||
#include <NDK/State.hpp>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
namespace Ndk
|
||||
{
|
||||
@@ -25,14 +26,21 @@ namespace Ndk
|
||||
|
||||
inline const std::shared_ptr<State>& GetCurrentState() const;
|
||||
|
||||
inline bool IsTopState(const State* state) const;
|
||||
|
||||
inline std::shared_ptr<State> PopState();
|
||||
inline bool PopStatesUntil(std::shared_ptr<State> state);
|
||||
inline void PushState(std::shared_ptr<State> state);
|
||||
|
||||
inline void SetState(std::shared_ptr<State> state);
|
||||
|
||||
inline bool Update(float elapsedTime);
|
||||
|
||||
inline StateMachine& operator=(StateMachine&& fsm) = default;
|
||||
StateMachine& operator=(const StateMachine&) = delete;
|
||||
|
||||
private:
|
||||
std::shared_ptr<State> m_currentState;
|
||||
std::shared_ptr<State> m_nextState;
|
||||
std::vector<std::shared_ptr<State>> m_states;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
// For conditions of distribution and use, see copyright notice in Prerequesites.hpp
|
||||
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <NDK/StateMachine.hpp>
|
||||
#include <utility>
|
||||
|
||||
namespace Ndk
|
||||
@@ -11,7 +10,7 @@ namespace Ndk
|
||||
/*!
|
||||
* \ingroup NDK
|
||||
* \class Ndk::StateMachine
|
||||
* \brief NDK class that represents a state machine, to represent the multiple states of your program
|
||||
* \brief NDK class that represents a state machine, to represent the multiple states of your program as a stack
|
||||
*/
|
||||
|
||||
/*!
|
||||
@@ -23,61 +22,153 @@ namespace Ndk
|
||||
* \remark Produces a NazaraAssert if nullptr is given
|
||||
*/
|
||||
|
||||
inline StateMachine::StateMachine(std::shared_ptr<State> originalState) :
|
||||
m_currentState(std::move(originalState))
|
||||
inline StateMachine::StateMachine(std::shared_ptr<State> originalState)
|
||||
{
|
||||
NazaraAssert(m_currentState, "StateMachine must have a state to begin with");
|
||||
m_currentState->Enter(*this);
|
||||
NazaraAssert(originalState, "StateMachine must have a state to begin with");
|
||||
PushState(std::move(originalState));
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Destructs the object
|
||||
*
|
||||
* \remark Calls "Leave" on the state
|
||||
* \remark Calls "Leave" on all the states
|
||||
*/
|
||||
|
||||
inline StateMachine::~StateMachine()
|
||||
{
|
||||
m_currentState->Leave(*this);
|
||||
for (std::shared_ptr<State>& state : m_states)
|
||||
state->Leave(*this);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Changes the current state of the machine
|
||||
* \brief Replaces the current state on the top of the machine
|
||||
*
|
||||
* \param state Next state to represent
|
||||
* \param state State to replace the top one if it is nullptr, no action is performed
|
||||
*/
|
||||
|
||||
inline void StateMachine::ChangeState(std::shared_ptr<State> state)
|
||||
{
|
||||
m_nextState = std::move(state);
|
||||
if (state)
|
||||
{
|
||||
PopState();
|
||||
PushState(std::move(state));
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the current state of the machine
|
||||
* \brief Gets the current state on the top of the machine
|
||||
* \return A constant reference to the state
|
||||
*
|
||||
* \remark The stack is supposed to be non empty, otherwise it is undefined behaviour
|
||||
*
|
||||
* \see PopStatesUntil
|
||||
*/
|
||||
|
||||
inline const std::shared_ptr<State>& StateMachine::GetCurrentState() const
|
||||
{
|
||||
return m_currentState;
|
||||
return m_states.back();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Updates the state
|
||||
* \return True if update is successful
|
||||
* \brief Checks whether the state is on the top of the machine
|
||||
* \return true If it is the case
|
||||
*
|
||||
* \param state State to compare the top with
|
||||
*/
|
||||
|
||||
inline bool StateMachine::IsTopState(const State* state) const
|
||||
{
|
||||
if (m_states.empty())
|
||||
return false;
|
||||
|
||||
return m_states.back().get() == state;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Pops the state on the top of the machine
|
||||
* \return Old state on the top, nullptr if stack was empty
|
||||
*
|
||||
* \remark This method can completely empty the stack
|
||||
*/
|
||||
|
||||
inline std::shared_ptr<State> StateMachine::PopState()
|
||||
{
|
||||
if (m_states.empty())
|
||||
return nullptr;
|
||||
|
||||
m_states.back()->Leave(*this);
|
||||
std::shared_ptr<State> oldTopState = std::move(m_states.back());
|
||||
m_states.pop_back();
|
||||
return oldTopState;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Pops all the states of the machine until a specific one is reached
|
||||
* \return true If that specific state is on top, false if stack is empty
|
||||
*
|
||||
* \param state State to find on the stack if it is nullptr, no action is performed
|
||||
*
|
||||
* \remark This method can completely empty the stack
|
||||
*/
|
||||
|
||||
inline bool StateMachine::PopStatesUntil(std::shared_ptr<State> state)
|
||||
{
|
||||
if (!state)
|
||||
return false;
|
||||
|
||||
while (!m_states.empty() && !IsTopState(state.get()))
|
||||
PopState();
|
||||
|
||||
return !m_states.empty();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Pushes a new state on the top of the machine
|
||||
*
|
||||
* \param state Next state to represent if it is nullptr, it performs no action
|
||||
*
|
||||
* \remark Produces a NazaraAssert if the same state is pushed two times on the stack
|
||||
*/
|
||||
|
||||
inline void StateMachine::PushState(std::shared_ptr<State> state)
|
||||
{
|
||||
if (state)
|
||||
{
|
||||
NazaraAssert(std::find(m_states.begin(), m_states.end(), state) == m_states.end(), "The same state was pushed two times");
|
||||
|
||||
m_states.push_back(std::move(state));
|
||||
m_states.back()->Enter(*this);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Pops every states of the machine to put a new one
|
||||
*
|
||||
* \param state State to reset the stack with if it is nullptr, no action is performed
|
||||
*/
|
||||
|
||||
inline void StateMachine::SetState(std::shared_ptr<State> state)
|
||||
{
|
||||
if (state)
|
||||
{
|
||||
while (!m_states.empty())
|
||||
PopState();
|
||||
|
||||
PushState(std::move(state));
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Updates all the states
|
||||
* \return true If update is successful for everyone of them
|
||||
*
|
||||
* \param elapsedTime Delta time used for the update
|
||||
*/
|
||||
|
||||
inline bool StateMachine::Update(float elapsedTime)
|
||||
{
|
||||
if (m_nextState)
|
||||
{
|
||||
m_currentState->Leave(*this);
|
||||
m_currentState = std::move(m_nextState);
|
||||
m_currentState->Enter(*this);
|
||||
}
|
||||
|
||||
return m_currentState->Update(*this, elapsedTime);
|
||||
return std::all_of(m_states.begin(), m_states.end(), [=](std::shared_ptr<State>& state) {
|
||||
return state->Update(*this, elapsedTime);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ namespace Ndk
|
||||
|
||||
std::unique_ptr<Nz::AbstractRenderTechnique> m_renderTechnique;
|
||||
std::vector<GraphicsComponentCullingList::VolumeEntry> m_volumeEntries;
|
||||
EntityList m_cameras;
|
||||
std::vector<EntityHandle> m_cameras;
|
||||
EntityList m_drawables;
|
||||
EntityList m_directionalLights;
|
||||
EntityList m_lights;
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include <Nazara/Core/Bitset.hpp>
|
||||
#include <Nazara/Core/HandledObject.hpp>
|
||||
#include <NDK/Entity.hpp>
|
||||
#include <NDK/EntityList.hpp>
|
||||
#include <NDK/System.hpp>
|
||||
#include <algorithm>
|
||||
#include <memory>
|
||||
@@ -28,7 +29,7 @@ namespace Ndk
|
||||
friend Entity;
|
||||
|
||||
public:
|
||||
using EntityList = std::vector<EntityHandle>;
|
||||
using EntityVector = std::vector<EntityHandle>;
|
||||
|
||||
inline World(bool addDefaultSystems = true);
|
||||
World(const World&) = delete;
|
||||
@@ -41,21 +42,21 @@ namespace Ndk
|
||||
template<typename SystemType, typename... Args> SystemType& AddSystem(Args&&... args);
|
||||
|
||||
const EntityHandle& CreateEntity();
|
||||
inline EntityList CreateEntities(unsigned int count);
|
||||
inline EntityVector CreateEntities(unsigned int count);
|
||||
|
||||
void Clear() noexcept;
|
||||
const EntityHandle& CloneEntity(EntityId id);
|
||||
|
||||
const EntityHandle& GetEntity(EntityId id);
|
||||
inline const EntityList& GetEntities();
|
||||
inline const EntityHandle& GetEntity(EntityId id);
|
||||
inline const EntityList& GetEntities() const;
|
||||
inline BaseSystem& GetSystem(SystemIndex index);
|
||||
template<typename SystemType> SystemType& GetSystem();
|
||||
|
||||
inline bool HasSystem(SystemIndex index) const;
|
||||
template<typename SystemType> bool HasSystem() const;
|
||||
|
||||
void KillEntity(Entity* entity);
|
||||
inline void KillEntities(const EntityList& list);
|
||||
inline void KillEntity(Entity* entity);
|
||||
inline void KillEntities(const EntityVector& list);
|
||||
|
||||
inline bool IsEntityValid(const Entity* entity) const;
|
||||
inline bool IsEntityIdValid(EntityId id) const;
|
||||
@@ -79,19 +80,22 @@ namespace Ndk
|
||||
struct EntityBlock
|
||||
{
|
||||
EntityBlock(Entity&& e) :
|
||||
entity(std::move(e))
|
||||
entity(std::move(e)),
|
||||
handle(&entity)
|
||||
{
|
||||
}
|
||||
|
||||
EntityBlock(EntityBlock&& block) = default;
|
||||
|
||||
Entity entity;
|
||||
std::size_t aliveIndex;
|
||||
EntityHandle handle;
|
||||
};
|
||||
|
||||
std::vector<std::unique_ptr<BaseSystem>> m_systems;
|
||||
std::vector<BaseSystem*> m_orderedSystems;
|
||||
std::vector<EntityBlock> m_entities;
|
||||
std::vector<EntityBlock*> m_entityBlocks;
|
||||
std::vector<std::unique_ptr<EntityBlock>> m_waitingEntities;
|
||||
std::vector<EntityId> m_freeIdList;
|
||||
EntityList m_aliveEntities;
|
||||
Nz::Bitset<Nz::UInt64> m_dirtyEntities;
|
||||
|
||||
@@ -82,9 +82,9 @@ namespace Ndk
|
||||
* \param count Number of entities to create
|
||||
*/
|
||||
|
||||
inline World::EntityList World::CreateEntities(unsigned int count)
|
||||
inline World::EntityVector World::CreateEntities(unsigned int count)
|
||||
{
|
||||
EntityList list;
|
||||
EntityVector list;
|
||||
list.reserve(count);
|
||||
|
||||
for (unsigned int i = 0; i < count; ++i)
|
||||
@@ -98,7 +98,7 @@ namespace Ndk
|
||||
* \return A constant reference to the entities
|
||||
*/
|
||||
|
||||
inline const World::EntityList& World::GetEntities()
|
||||
inline const EntityList& World::GetEntities() const
|
||||
{
|
||||
return m_aliveEntities;
|
||||
}
|
||||
@@ -164,18 +164,53 @@ namespace Ndk
|
||||
return HasSystem(index);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Marks an entity for deletion
|
||||
*
|
||||
* \param Pointer to the entity
|
||||
*
|
||||
* \remark If the entity pointer is invalid, nothing is done
|
||||
* \remark For safety, entities are not killed until the next world update
|
||||
*/
|
||||
inline void World::KillEntity(Entity* entity)
|
||||
{
|
||||
if (IsEntityValid(entity))
|
||||
m_killedEntities.UnboundedSet(entity->GetId(), true);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Kills a set of entities
|
||||
*
|
||||
* This function has the same effect as calling KillEntity for every entity contained in the vector
|
||||
*
|
||||
* \param list Set of entities to kill
|
||||
*/
|
||||
|
||||
inline void World::KillEntities(const EntityList& list)
|
||||
inline void World::KillEntities(const EntityVector& list)
|
||||
{
|
||||
for (const EntityHandle& entity : list)
|
||||
KillEntity(entity);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets an entity
|
||||
* \return A constant reference to a handle of the entity
|
||||
*
|
||||
* \param id Identifier of the entity
|
||||
*
|
||||
* \remark Handle referenced by this function can move in memory when updating the world, do not keep a reference to a handle from a world update to another
|
||||
* \remark If an invalid identifier is provided, an error got triggered and an invalid handle is returned
|
||||
*/
|
||||
inline const EntityHandle& World::GetEntity(EntityId id)
|
||||
{
|
||||
if (IsEntityIdValid(id))
|
||||
return m_entityBlocks[id]->handle;
|
||||
else
|
||||
{
|
||||
NazaraError("Invalid ID");
|
||||
return EntityHandle::InvalidHandle;
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Checks whether or not an entity is valid
|
||||
* \return true If it is the case
|
||||
@@ -197,7 +232,7 @@ namespace Ndk
|
||||
|
||||
inline bool World::IsEntityIdValid(EntityId id) const
|
||||
{
|
||||
return id < m_entities.size() && m_entities[id].entity.IsValid();
|
||||
return id < m_entityBlocks.size() && m_entityBlocks[id]->entity.IsValid();
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -266,10 +301,12 @@ namespace Ndk
|
||||
{
|
||||
m_aliveEntities = std::move(world.m_aliveEntities);
|
||||
m_dirtyEntities = std::move(world.m_dirtyEntities);
|
||||
m_entityBlocks = std::move(world.m_entityBlocks);
|
||||
m_freeIdList = std::move(world.m_freeIdList);
|
||||
m_killedEntities = std::move(world.m_killedEntities);
|
||||
m_orderedSystems = std::move(world.m_orderedSystems);
|
||||
m_orderedSystemsUpdated = world.m_orderedSystemsUpdated;
|
||||
m_waitingEntities = std::move(world.m_waitingEntities);
|
||||
|
||||
m_entities = std::move(world.m_entities);
|
||||
for (EntityBlock& block : m_entities)
|
||||
@@ -284,7 +321,7 @@ namespace Ndk
|
||||
|
||||
inline void World::Invalidate()
|
||||
{
|
||||
m_dirtyEntities.Resize(m_entities.size(), false);
|
||||
m_dirtyEntities.Resize(m_entityBlocks.size(), false);
|
||||
m_dirtyEntities.Set(true); // Activation of all bits
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user