Creating a basic view

This commit is contained in:
Zoe Roux
2021-06-03 22:42:38 +02:00
parent 0d37a560d7
commit a11bd21ec3
15 changed files with 121 additions and 63 deletions
+1 -1
View File
@@ -17,6 +17,6 @@ add_library(wal
sources/Component/Component.cpp
sources/System/System.cpp
sources/Models/Callback.hpp
)
sources/View/BaseView.hpp)
target_include_directories(wal PUBLIC sources)
+11
View File
@@ -3,6 +3,7 @@
//
#include "Entity/Entity.hpp"
#include "Scene/Scene.hpp"
#include <string>
#include <utility>
@@ -65,4 +66,14 @@ namespace WAL
{
return this->_components.contains(type);
}
void Entity::_componentAdded(const std::type_index &type)
{
this->_scene._componentAdded(*this, type);
}
void Entity::_componentRemoved(const std::type_index &type)
{
this->_scene._componentRemoved(*this, type);
}
} // namespace WAL
+10 -15
View File
@@ -10,22 +10,10 @@
#include <memory>
#include "Component/Component.hpp"
#include "Exception/WalError.hpp"
#include "Wal.hpp"
namespace WAL
{
class Scene {
public:
//! @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. namespace WAL
void _componentRemoved(const Entity &entity, std::type_index type);
};
class Scene;
//! @brief An entity of the WAL's ECS.
class Entity
@@ -42,6 +30,13 @@ namespace WAL
//! @brief This ID will be the one of the next entity created.
static unsigned nextID;
//! @brief Callback called when a component is added
//! @param type The type of component
void _componentAdded(const std::type_index &type);
//! @brief Callback called when a component is removed
//! @param type The type of component
void _componentRemoved(const std::type_index &type);
protected:
//! @brief A reference to the ECS.
Scene &_scene;
@@ -96,7 +91,7 @@ namespace WAL
if (this->hasComponent(type))
throw DuplicateError("A component of the type \"" + std::string(type.name()) + "\" already exists.");
this->_components[type] = std::make_unique<T>(*this, std::forward<Types>(params)...);
this->_scene._componentAdded(*this, type);
this->_componentAdded(type);
return *this;
}
@@ -115,7 +110,7 @@ namespace WAL
if (existing == this->_components.end())
throw NotFoundError("No component could be found with the type \"" + std::string(type.name()) + "\".");
this->_components.erase(existing);
this->_scene._componentRemoved(*this, type);
this->_componentRemoved(type);
return *this;
}
+19 -4
View File
@@ -21,13 +21,28 @@ namespace WAL
return this->_entities.emplace_back(*this, name);
}
void Scene::_componentAdded(const Entity &entity, std::type_index type)
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())
continue;
bool valid = std::all_of(view->types.begin(), view->types.end(), [&entity](const auto &type){
return entity.hasComponent(type);
});
if (valid)
view->entities.emplace_back(entity);
}
}
void Scene::_componentRemoved(const Entity &entity, std::type_index type)
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())
continue;
view->entities.erase(std::remove_if(view->entities.begin(), view->entities.end(), [&entity](const auto &ref)
{
return &ref.get() == &entity;
}), view->entities.end());
}
}
} // namespace WAL
+11 -25
View File
@@ -3,11 +3,11 @@
//
#ifndef WAL_SCENE
#define WAL_SCENE
#pragma once
#include <vector>
#include <functional>
#include <View/BaseView.hpp>
#include "Entity/Entity.hpp"
namespace WAL
@@ -18,15 +18,17 @@ namespace WAL
private:
//! @brief The list of registered entities
std::vector<Entity> _entities = {};
//! @brief The list of cached views to update.
std::vector<std::shared_ptr<BaseView>> _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);
void _componentAdded(Entity &entity, const 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);
void _componentRemoved(const Entity &entity, const std::type_index &type);
public:
//! @brief Get the list of entities.
std::vector<Entity> &getEntities();
@@ -37,25 +39,13 @@ namespace WAL
Entity &addEntity(const std::string &name);
template<typename ...Components>
std::vector<std::reference_wrapper<Entity>> &view()
View<Components...> &view()
{
return this->view(typeid(Components)...);
static auto view = std::make_shared<View<Components...>>(this->_entities);
this->_views.emplace_back(view);
return *view;
}
#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
@@ -68,8 +58,4 @@ namespace WAL
friend Entity;
};
} // namespace WAL
#else
#endif
} // namespace WAL
+1 -2
View File
@@ -8,11 +8,10 @@
#include <vector>
#include <typeinfo>
#include <typeindex>
#include "Entity/Entity.hpp"
namespace WAL
{
class Entity;
//! @brief A base system of WAL
class System
{
+50
View File
@@ -0,0 +1,50 @@
//
// Created by Zoe Roux on 2021-06-03.
//
#pragma once
#include <vector>
#include <tuple>
#include <typeindex>
#include <functional>
#include "Entity/Entity.hpp"
namespace WAL
{
class BaseView
{
public:
std::vector<std::reference_wrapper<Entity>> entities = {};
std::vector<std::type_index> types = {};
size_t size()
{
return entities.size();
}
// 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;
// }
};
template<typename ...Components>
class View : public BaseView
{
public:
explicit View(std::vector<Entity> &scene)
{
std::copy_if(scene.begin(), scene.end(), std::back_inserter(this->entities), [](Entity &entity) {
return (entity.hasComponent<Components>() && ...);
});
}
};
}
+1
View File
@@ -12,6 +12,7 @@
#include "Exception/WalError.hpp"
#include "System/System.hpp"
#include "Models/Callback.hpp"
#include "Scene/Scene.hpp"
namespace WAL
{
+1 -1
View File
@@ -66,7 +66,7 @@ namespace BBM
std::shared_ptr<WAL::Scene> loadGameScene(WAL::Wal &wal)
{
auto scene = std::make_shared<WAL::Scene>(wal);
auto scene = std::make_shared<WAL::Scene>();
scene->addEntity("player")
.addComponent<PositionComponent>()
.addComponent<Drawable3DComponent<RAY3D::Model>>("assets/player/player.iqm", std::make_pair(MAP_DIFFUSE, "assets/player/blue.png"))
@@ -6,6 +6,7 @@
#include "Component/Position/PositionComponent.hpp"
#include "Component/Collision/CollisionComponent.hpp"
#include "System/Collision/CollisionSystem.hpp"
#include "Scene/Scene.hpp"
namespace BBM
{
+3 -2
View File
@@ -5,6 +5,7 @@
#include <catch2/catch.hpp>
#include <stdexcept>
#include <Wal.hpp>
#include <Scene/Scene.hpp>
#include "Entity/Entity.hpp"
#include "Models/Callback.hpp"
@@ -37,7 +38,7 @@ TEST_CASE("Callback multiple arguments", "[Callback]")
callback.addCallback([](const std::string& str, int a, unsigned *value, Entity &entity) {
throw std::runtime_error("");
});
Wal wal;
Entity entity(wal, "name");
Scene scene;
Entity entity(scene, "name");
REQUIRE_THROWS_AS(callback("1", 0, nullptr, entity), std::runtime_error);
}
+2 -2
View File
@@ -21,7 +21,7 @@ TEST_CASE("Collision test", "[Component][System]")
{
Wal wal;
CollisionSystem collision(wal);
wal.scene = std::make_shared<Scene>(wal);
wal.scene = std::make_shared<Scene>();
wal.scene->addEntity("player")
.addComponent<PositionComponent>()
.addComponent<CollisionComponent>([](Entity &actual, const Entity &) {
@@ -65,7 +65,7 @@ TEST_CASE("Collsion test with movable", "[Component][System]")
Wal wal;
CollisionSystem collision(wal);
MovableSystem movable;
wal.scene = std::make_shared<Scene>(wal);
wal.scene = std::make_shared<Scene>();
wal.scene->addEntity("player")
.addComponent<PositionComponent>()
.addComponent<CollisionComponent>([](Entity &actual, const Entity &) {}, [](Entity &actual, const Entity &) {}, 5.0)
+7 -6
View File
@@ -6,14 +6,15 @@
#include "Component/Position/PositionComponent.hpp"
#include <catch2/catch.hpp>
#include <Wal.hpp>
#include <Scene/Scene.hpp>
using namespace WAL;
using namespace BBM;
TEST_CASE("Component", "[Entity]")
{
Wal wal;
Entity entity(wal, "Bob");
Scene scene;
Entity entity(scene, "Bob");
entity.addComponent<PositionComponent>(2, 3, 4);
SECTION("Check value") {
@@ -33,15 +34,15 @@ TEST_CASE("Component", "[Entity]")
TEST_CASE("ComponentNotFound", "[Entity]")
{
Wal wal;
Entity entity(wal, "Bob");
Scene scene;
Entity entity(scene, "Bob");
REQUIRE_THROWS_AS(entity.getComponent<PositionComponent>(), NotFoundError);
}
TEST_CASE("Add component by reference", "[Entity]")
{
Wal wal;
Entity entity(wal, "Bob");
Scene scene;
Entity entity(scene, "Bob");
PositionComponent component(entity, 4, 5, 6);
REQUIRE(&entity.addComponent(component) == &entity);
+1 -2
View File
@@ -19,8 +19,7 @@ using namespace BBM;
TEST_CASE("Move test", "[Component][System]")
{
Wal wal;
Scene scene(wal);
Scene scene;
scene.addEntity("player")
.addComponent<ControllableComponent>()
.addComponent<MovableComponent>()
+2 -3
View File
@@ -18,8 +18,7 @@ using namespace BBM;
TEST_CASE("View creation", "[View]")
{
Wal wal;
Scene scene(wal);
Scene scene;
scene.addEntity("player")
.addComponent<PositionComponent>()
.addComponent<ControllableComponent>();
@@ -28,6 +27,6 @@ TEST_CASE("View creation", "[View]")
REQUIRE(scene.view<PositionComponent>().size() == 2);
REQUIRE(scene.view<PositionComponent, ControllableComponent>().size() == 1);
Entity &entity = *scene.getEntities().begin();
Entity &firstView = *scene.view<PositionComponent, ControllableComponent>().begin();
Entity &firstView = *scene.view<PositionComponent, ControllableComponent>().entities.begin();
REQUIRE(&entity == &firstView);
}