Adding an iterator

This commit is contained in:
Zoe Roux
2021-06-04 18:59:42 +02:00
parent eac5c2c847
commit a40b61845a
12 changed files with 220 additions and 98 deletions
+10
View File
@@ -65,6 +65,16 @@ namespace WAL
return *static_cast<T *>(existing->second.get());
}
template<typename T>
T *getComponentOrDefault()
{
const std::type_index &type = typeid(T);
auto existing = this->_components.find(type);
if (existing == this->_components.end())
return nullptr;
return *static_cast<T *>(existing->second.get());
}
//! @brief Check if this entity has a component.
//! @tparam T The type of the component
template<typename T>
+5 -8
View File
@@ -26,25 +26,22 @@ namespace WAL
void Scene::_componentAdded(Entity &entity, const std::type_index &type)
{
for (auto &view : this->_views) {
if (std::find(view->types.begin(), view->types.end(), type) == view->types.end())
if (std::find(view->getTypes().begin(), view->getTypes().end(), type) == view->getTypes().end())
continue;
bool valid = std::all_of(view->types.begin(), view->types.end(), [&entity](const auto &type){
bool valid = std::all_of(view->getTypes().begin(), view->getTypes().end(), [&entity](const auto &type){
return entity.hasComponent(type);
});
if (valid)
view->entities.emplace_back(entity);
view->emplace_back(entity);
}
}
void Scene::_componentRemoved(const Entity &entity, const std::type_index &type)
{
for (auto &view : this->_views) {
if (std::find(view->types.begin(), view->types.end(), type) == view->types.end())
if (std::find(view->getTypes().begin(), view->getTypes().end(), type) == view->getTypes().end())
continue;
view->entities.erase(std::remove_if(view->entities.begin(), view->entities.end(), [&entity](const auto &ref)
{
return &ref.get() == &entity;
}), view->entities.end());
view->erase(entity);
}
}
} // namespace WAL
+1 -1
View File
@@ -24,7 +24,7 @@ namespace WAL
//! @brief The list of registered entities
std::list<Entity> _entities = {};
//! @brief The list of cached views to update.
std::vector<std::shared_ptr<BaseView>> _views = {};
std::vector<std::shared_ptr<IView>> _views = {};
//! @brief Notify this scene that a component has been added to the given entity.
//! @param entity The entity with the new component
+6 -11
View File
@@ -15,21 +15,16 @@ namespace WAL
class ISystem
{
public:
//! @brief Update the corresponding component of the given entity
//! @param entity The entity to update.
//! @param dtime The delta time.
virtual void onUpdate(Entity &entity, std::chrono::nanoseconds dtime) = 0;
//! @brief Update the whole system (every entities that this system is responsible can be updated.
//! @param dtime The delta time since the last call to this method.
virtual void update(std::chrono::nanoseconds dtime) = 0;
//! @brief An alternative of onUpdate that is called every 8ms (120 times per seconds). If the system slow down, it will try to catch up.
//! @brief An alternative of update that is called every 8ms (120 times per seconds). If the system slow down, it will try to catch up.
//! @remark This should be used for Physics, AI and everything that could be imprecise due to float rounding.
//! @param entity The entity to update.
virtual void onFixedUpdate(Entity &entity) = 0;
//! @brief A method called after all entities that this system manage has been updated.
virtual void onSelfUpdate() = 0;
virtual void fixedUpdate() = 0;
//! @brief Get a view containing every entity this system should update.
virtual BaseView &getView() = 0;
virtual IView &getView() = 0;
//! @brief A virtual default destructor.
virtual ~ISystem() = default;
+21 -3
View File
@@ -35,15 +35,33 @@ namespace WAL
//! @brief Update the corresponding component of the given entity
//! @param entity The entity to update.
//! @param dtime The delta time.
void onUpdate(Entity &entity, std::chrono::nanoseconds dtime) override {}
virtual void onUpdate(ViewEntity<Dependencies...> &entity, std::chrono::nanoseconds dtime) {}
//! @brief An alternative of onUpdate that is called every 8ms (120 times per seconds). If the system slow down, it will try to catch up.
//! @remark This should be used for Physics, AI and everything that could be imprecise due to float rounding.
//! @param entity The entity to update.
void onFixedUpdate(Entity &entity) override {}
virtual void onFixedUpdate(ViewEntity<Dependencies...> &entity) {}
//! @brief A method called after all entities that this system manage has been updated.
void onSelfUpdate() override {}
virtual void onSelfUpdate() {}
//! @brief Update the whole system (every entities that this system is responsible can be updated.
//! @param dtime The delta time since the last call to this method.
void update(std::chrono::nanoseconds dtime) final
{
for (auto entity : this->getView())
this->onUpdate(entity, dtime);
this->onSelfUpdate();
}
//! @brief An alternative of update that is called every 8ms (120 times per seconds). If the system slow down, it will try to catch up.
//! @remark This should be used for Physics, AI and everything that could be imprecise due to float rounding.
void fixedUpdate() final
{
for (auto entity : this->getView())
this->onFixedUpdate(entity);
}
protected:
//! @brief A reference to the ECS.
Wal &_wal;
+128 -22
View File
@@ -13,46 +13,152 @@
namespace WAL
{
//! @brief A basic view used to manipulate view without knowing their type at compile time.
class BaseView
template<typename ...Components>
class ViewEntity
{
private:
std::tuple<std::reference_wrapper<Entity>, std::reference_wrapper<Components>...> _value;
public:
//! @brief The list of entities in the view.
std::vector<std::reference_wrapper<Entity>> entities = {};
explicit ViewEntity(std::tuple<std::reference_wrapper<Entity>, std::reference_wrapper<Components>...> value)
: _value(value)
{}
//! @brief The list of types that every entity of the view has.
std::vector<std::type_index> types = {};
size_t size() const
Entity *operator->()
{
return entities.size();
return &(std::get<0>(this->_value).get());
}
Entity &operator*()
{
return std::get<0>(this->_value);
}
operator Entity &()
{
return std::get<0>(this->_value);
}
template<typename T>
T &get()
{
return std::get<std::reference_wrapper<T>>(this->_value);
}
};
template<typename It, typename ...Components>
class ViewIterator
{
private:
It _it;
public:
ViewEntity<Components...> operator*()
{
ViewEntity<Components...> entity(*this->_it);
return entity;
}
ViewEntity<Components...> operator->()
{
ViewEntity<Components...> entity(*this->_it);
return entity;
}
ViewIterator &operator++()
{
this->_it++;
return *this;
}
ViewIterator operator++(int)
{
ViewIterator copy = *this;
this->_it++;
return *this;
}
bool operator==(const ViewIterator &other) const
{
return this->_it == other._it;
}
bool operator!=(const ViewIterator &other) const
{
return !this->operator==(other);
}
explicit ViewIterator(It current)
: _it(current)
{}
};
//! @brief A basic view used to manipulate view without knowing their type at compile time.
class IView
{
public:
//! @brief The list of types that every entity of the view has.
virtual const std::vector<std::type_index> &getTypes() const = 0;
virtual void emplace_back(Entity &) = 0;
virtual void erase(const Entity &) = 0;
//! @brief A default destructor
~BaseView() = default;
protected:
//! @brief A basic view can't be constructed, you should use the View templated class.
BaseView() = default;
//! @brief A basic view can't be constructed, you should use the View templated class.
BaseView(const BaseView &) = default;
//! @brief A basic view can't be assigned. See the View template class.
BaseView &operator=(const BaseView &) = default;
virtual ~IView() = default;
};
//! @brief A view allowing one to easily access entities containing a set list of component.
//! A view is always updated and only references to entities are kept.
template<typename ...Components>
class View : public BaseView
class View : public IView
{
private:
using entity_type = std::tuple<std::reference_wrapper<Entity>, std::reference_wrapper<Components>...>;
//! @brief The list of entities in the view.
std::vector<entity_type> _entities = {};
//! @brief The list of types that every entity of the view has.
std::vector<std::type_index> _types = {};
public:
using iterator = ViewIterator<typename std::vector<entity_type>::iterator, Components...>;
iterator begin()
{
return iterator(this->_entities.begin());
}
iterator end()
{
return iterator(this->_entities.begin());
}
const std::vector<std::type_index> &getTypes() const override
{
return this->_types;
}
void emplace_back(Entity &) override
{
}
void erase(const Entity &) override
{
}
//! @brief Construct a view from a list of entities.
//! Those entities are never copied but references to them are kept internally.
explicit View(std::list<Entity> &scene)
{
this->types = {typeid(Components)...};
std::copy_if(scene.begin(), scene.end(), std::back_inserter(this->entities), [](Entity &entity) {
return (entity.hasComponent<Components>() && ...);
});
this->_types = {typeid(Components)...};
for (auto &entity : scene) {
auto tuple = std::make_tuple<Entity &, Components *...>(entity, entity.getComponentOrDefault<Components>()...);
std::apply(&this->_entities.emplace_back, tuple);
}
// std::copy_if(scene.begin(), scene.end(), std::back_inserter(this->entities), [](Entity &entity) {
// return (entity.hasComponent<Components>() && ...);
// });
}
//! @brief Copying a view is not possible since a view must be managed by a scene.
+4 -9
View File
@@ -129,16 +129,11 @@ namespace WAL
while (fBehind > Wal::timestep) {
fBehind -= Wal::timestep;
for (auto &system : this->_systems) {
for (auto &entity : system->getView().entities)
system->onFixedUpdate(entity);
}
}
for (auto &system : this->_systems) {
for (auto &entity : system->getView().entities)
system->onUpdate(entity, dtime);
system->onSelfUpdate();
for (auto &system : this->_systems)
system->fixedUpdate();
}
for (auto &system : this->_systems)
system->update(dtime);
callback(*this, state);
}
}