Reworking dependencies

This commit is contained in:
Zoe Roux
2021-05-21 16:51:18 +02:00
parent dee56a52c7
commit e5d3823d0c
12 changed files with 53 additions and 35 deletions
-5
View File
@@ -20,11 +20,6 @@ namespace WAL
this->_disabled = disabled;
}
const std::vector<std::type_index> &Component::getDependencies() const
{
return this->_dependencies;
}
void Component::onStart()
{
//TODO handle events here
-5
View File
@@ -22,8 +22,6 @@ namespace WAL
protected:
//! @brief The entity that own this component
Entity &_entity;
//! @brief The list of dependencies of this component.
std::vector<std::type_index> _dependencies;
//! @brief A component can't be instantiated, it should be derived.
explicit Component(Entity &entity);
@@ -44,9 +42,6 @@ namespace WAL
//! @brief Disable this component.
void setDisable(bool disabled);
//! @brief Get the dependencies of this component.
const std::vector<std::type_index> &getDependencies() const;
//! @brief The entity or this component has just been enabled.
virtual void onStart();
+8
View File
@@ -59,4 +59,12 @@ namespace WAL
});
return existing != this->_components.end();
}
bool Entity::hasComponent(const std::type_index &type) const
{
auto existing = std::find_if(this->_components.begin(), this->_components.end(), [&type] (const auto &cmp) {
return std::type_index(typeid(*cmp)) == type;
});
return existing != this->_components.end();
}
}
+4
View File
@@ -67,6 +67,10 @@ namespace WAL
//! @param type The type of the component
bool hasComponent(const std::type_info &type) const;
//! @brief Check if this entity has a component.
//! @param type The type of the component
bool hasComponent(const std::type_index &type) const;
//! @brief Add a component to this entity. The component is constructed in place.
//! @throw DuplicateError is thrown if a component with the same type already exist.
//! @return This entity is returned
-5
View File
@@ -10,10 +10,5 @@ namespace WAL
{
return this->_entities;
}
void Scene::removeAll(std::function<bool (const Entity &)> &predicate)
{
// this->_entities.erase(std::remove_if(this->_entities.begin(), this->_entities.end(), predicate), this->_entities.end());
}
}
-4
View File
@@ -30,10 +30,6 @@ namespace WAL
return *this;
}
//! @brief Remove every entity of this scene that matches the given predicate
//! @param predicate The predicate used to filer entities
void removeAll(std::function<bool (const Entity &)> &predicate);
//! @brief A default constructor
Scene() = default;
//! @brief A scene is copy constructable
@@ -9,10 +9,12 @@
namespace WAL
{
const std::type_info &MovableSystem::getComponent() const
{
return typeid(MovableComponent);
}
MovableSystem::MovableSystem()
: System({
typeid(MovableComponent),
typeid(PositionComponent)
})
{}
void MovableSystem::onFixedUpdate(Entity &entity)
{
@@ -13,13 +13,11 @@ namespace WAL
class MovableSystem : public System
{
public:
//! @inherit
const std::type_info &getComponent() const override;
//! @inherit
void onFixedUpdate(Entity &entity) override;
//! @brief A default constructor
MovableSystem() = default;
MovableSystem();
//! @brief A movable system is copy constructable
MovableSystem(const MovableSystem &) = default;
//! @brief A default destructor
+10
View File
@@ -4,8 +4,13 @@
#include "System.hpp"
#include <utility>
namespace WAL
{
System::System(std::vector<std::type_index> dependencies)
: _dependencies(std::move(dependencies))
{}
void System::onUpdate(Entity &entity, std::chrono::nanoseconds dtime)
{}
@@ -15,4 +20,9 @@ namespace WAL
void System::onSelfUpdate()
{}
const std::vector<std::type_index> &System::getDependencies() const
{
return this->_dependencies;
}
}
+6 -3
View File
@@ -12,6 +12,9 @@ namespace WAL
//! @brief A base system of WAL
class System
{
private:
//! @brief The list of dependencies of this system
std::vector<std::type_index> _dependencies = {};
public:
//! @brief A virtual, default, destructor
virtual ~System() = default;
@@ -19,8 +22,8 @@ namespace WAL
System(System &&) = default;
//! @brief Get the name of the component corresponding to this system.
virtual const std::type_info &getComponent() const = 0;
const std::vector<std::type_index> &getDependencies() const;
//! @brief Update the corresponding component of the given entity
//! @param entity The entity to update.
//! @param dtime The delta time.
@@ -35,7 +38,7 @@ namespace WAL
virtual void onSelfUpdate();
protected:
//! @brief A system can't be instantiated, it should be derived.
System() = default;
explicit System(std::vector<std::type_index> dependencies);
//! @brief A system can't be instantiated, it should be derived.
System(const System &) = default;
//! @brief A system can't be instantiated, it should be derived.
+12 -6
View File
@@ -3,6 +3,7 @@
//
#include <chrono>
#include <algorithm>
#include "Wal.hpp"
using namespace std::chrono_literals;
@@ -36,10 +37,8 @@ namespace WAL
for (auto &system : this->_systems) {
for (auto &entity : entities) {
const auto &cmp = system->getComponent();
if (!entity.hasComponent(cmp))
if (!Wal::_hasDependencies(entity, *system))
continue;
// TODO handle dependencies.
system->onUpdate(entity, dtime);
}
system->onSelfUpdate();
@@ -52,12 +51,19 @@ namespace WAL
for (auto &system : this->_systems) {
for (auto &entity : entities) {
auto &cmp = system->getComponent();
if (!entity.hasComponent(cmp))
if (!Wal::_hasDependencies(entity, *system))
continue;
// TODO handle dependencies.
system->onFixedUpdate(entity);
}
}
}
bool Wal::_hasDependencies(const Entity &entity, const System &system)
{
// TODO use an hashmap to cache results.
const auto &dependency = system.getDependencies();
return std::ranges::all_of(dependency.begin(), dependency.end(), [&entity](const auto &dependency) {
return entity.hasComponent(dependency);
});
}
}
+6
View File
@@ -31,6 +31,12 @@ namespace WAL
//! @brief Call the onFixedUpdate of every system with every component
void _fixedUpdate();
//! @brief Check if an entity met a system's dependencies.
//! @param entity The entity to check
//! @param system The system that will list dependencies
//! @return True if all dependencies are met, false otherwise.
static bool _hasDependencies(const Entity &entity, const System &system);
public:
//! @brief The time between each fixed update.
static std::chrono::nanoseconds timestep;