Reworking the view

This commit is contained in:
Zoe Roux
2021-06-03 18:20:37 +02:00
parent ae2e419832
commit 0d37a560d7
16 changed files with 141 additions and 62 deletions
+40 -10
View File
@@ -3,11 +3,11 @@
//
#pragma once
#ifndef WAL_SCENE
#define WAL_SCENE
#include <vector>
#include <functional>
#include "View/View.hpp"
#include "Entity/Entity.hpp"
namespace WAL
@@ -18,20 +18,44 @@ namespace WAL
private:
//! @brief The list of registered entities
std::vector<Entity> _entities = {};
//! @brief A list of cached views.
// std::vector<View> _views = {};
//! @brief Notify this scene that a component has been added to the given entity.
//! @param entity The entity with the new component
//! @param type The type of the component added.
void _componentAdded(const Entity &entity, std::type_index type);
//! @brief Notify this scene that a component has been removed to the given entity.
//! @param entity The entity with the removed component
//! @param type The type of the component removed.
void _componentRemoved(const Entity &entity, std::type_index type);
public:
//! @brief Get the list of entities.
std::vector<Entity> &getEntities();
//! @brief Add a new entity to the scene, you can use this method with the same arguments as the entity's constructor.
//! @return The current scene is returned to allow you to chain call.
template <class ...Params>
Entity &addEntity(Params &&...params)
//! @brief Add a new entity to the scene.
//! @param name The name of the created entity.
//! @return The created entity is returned.
Entity &addEntity(const std::string &name);
template<typename ...Components>
std::vector<std::reference_wrapper<Entity>> &view()
{
return this->_entities.emplace_back(std::forward<Params>(params)...);
return this->view(typeid(Components)...);
}
#pragma clang diagnostic push
#pragma ide diagnostic ignored "NotImplementedFunctions"
template<typename ...Components>
std::vector<std::reference_wrapper<Entity>> &view(const Components &...index) requires(std::is_same_v<Components...>)
{
static std::vector<std::reference_wrapper<Entity>> view;
std::copy_if(this->_entities.begin(), this->_entities.end(), std::back_inserter(view), [&index...](Entity &entity) {
return (entity.hasComponent(index) && ...);
});
return view;
}
#pragma clang diagnostic pop
//! @brief A default constructor
Scene() = default;
//! @brief A scene is copy constructable
@@ -41,5 +65,11 @@ namespace WAL
//! @brief A scene is assignable
Scene &operator=(const Scene &);
Scene(Scene &&) = default;
friend Entity;
};
} // namespace WAL
} // namespace WAL
#else
#endif