Finishing views

This commit is contained in:
Zoe Roux
2021-06-05 16:42:53 +02:00
parent a40b61845a
commit acb3935c7c
2 changed files with 18 additions and 13 deletions
+11 -7
View File
@@ -54,25 +54,29 @@ namespace WAL
void setDisable(bool disabled);
//! @brief Get a component of a specific type
//! @tparam The type of the component
//! @throw NotFoundError if the component could not be found
//! @return The component of the requested type.
template<typename T>
T &getComponent()
{
const std::type_index &type = typeid(T);
auto existing = this->_components.find(type);
if (existing == this->_components.end())
throw NotFoundError("No component could be found with the type \"" + std::string(type.name()) + "\".");
return *static_cast<T *>(existing->second.get());
T *ret = this->tryGetComponent<T>();
if (ret == nullptr)
throw NotFoundError("No component could be found with the type \"" + std::string(typeid(T).name()) + "\".");
return *ret;
}
//! @brief Get a component of a specific type or null if not found.
//! @tparam The type of the component
//! @return The component or nullptr if not found.
template<typename T>
T *getComponentOrDefault()
T *tryGetComponent()
{
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());
return static_cast<T *>(existing->second.get());
}
//! @brief Check if this entity has a component.
+7 -6
View File
@@ -153,18 +153,19 @@ namespace WAL
{
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);
auto tuple = std::make_tuple<Components *...>(entity.tryGetComponent<Components>()...);
if (std::apply([](const auto *...component) {return ((component == nullptr) || ...);}, tuple))
continue;
std::apply([&](auto *...component) {
this->_entities.emplace_back(entity, *component...);
}, 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.
View(const View &) = delete;
//! @brief A default destructor
~View() = default;
~View() override = default;
//! @brief A view is not assignable.
View &operator=(const View &) = delete;
};