mirror of
https://github.com/zoriya/Bomberman.git
synced 2025-12-20 05:15:10 +00:00
Adding an iterator
This commit is contained in:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user