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
+1
View File
@@ -77,6 +77,7 @@ add_executable(unit_tests EXCLUDE_FROM_ALL
tests/CallbackTest.cpp
tests/CollisionTest.cpp
tests/MoveTests.cpp
tests/ViewTest.cpp
)
target_include_directories(unit_tests PUBLIC sources)
target_link_libraries(unit_tests PUBLIC wal ray)
+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/View.hpp)
)
target_include_directories(wal PUBLIC sources)
+4 -1
View File
@@ -10,13 +10,15 @@ namespace WAL
{
unsigned Entity::nextID = 0;
Entity::Entity(std::string name)
Entity::Entity(Scene &scene, std::string name)
: _uid(Entity::nextID++),
_scene(scene),
_name(std::move(name))
{ }
Entity::Entity(const Entity &other)
: _uid(Entity::nextID++),
_scene(other._scene),
_name(other._name),
_disabled(other._disabled)
{
@@ -50,6 +52,7 @@ namespace WAL
if (this->hasComponent(type))
throw DuplicateError("A component of the type \"" + std::string(type.name()) + "\" already exists.");
this->_components.emplace(type, component.clone(*this));
this->_scene._componentAdded(*this, type);
return *this;
}
+20 -1
View File
@@ -10,9 +10,23 @@
#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);
};
//! @brief An entity of the WAL's ECS.
class Entity
{
@@ -28,6 +42,9 @@ namespace WAL
//! @brief This ID will be the one of the next entity created.
static unsigned nextID;
protected:
//! @brief A reference to the ECS.
Scene &_scene;
public:
//! @brief Get the ID of the entity.
unsigned getUid() const;
@@ -79,6 +96,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);
return *this;
}
@@ -97,11 +115,12 @@ 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);
return *this;
}
//! @brief A default constructor
explicit Entity(std::string name);
explicit Entity(Scene &wal, std::string name);
//! @brief An entity is copyable
Entity(const Entity &);
//! @brief An entity is movable.
+16
View File
@@ -10,8 +10,24 @@ namespace WAL
{
return this->_entities;
}
Scene &Scene::operator=(const Scene &)
{
return *this;
}
Entity &Scene::addEntity(const std::string &name)
{
return this->_entities.emplace_back(*this, name);
}
void Scene::_componentAdded(const Entity &entity, std::type_index type)
{
}
void Scene::_componentRemoved(const Entity &entity, std::type_index type)
{
}
} // namespace WAL
+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
+4 -1
View File
@@ -6,10 +6,13 @@
#include <chrono>
#include <vector>
#include "Entity/Entity.hpp"
#include <typeinfo>
#include <typeindex>
namespace WAL
{
class Entity;
//! @brief A base system of WAL
class System
{
-35
View File
@@ -1,35 +0,0 @@
//
// Created by Zoe Roux on 2021-06-02.
//
#pragma once
#include <vector>
#include <Entity/Entity.hpp>
namespace WAL
{
//! @brief A view caching entities containing requested components
template<typename ...Components>
class View
{
//! @brief A list of reference to entities that contains the
std::vector<std::reference_wrapper<Entity>> entities;
explicit View(std::vector<Entity> &entities)
: entities()
{
std::copy_if(entities.begin(), entities.end(), std::back_inserter(this->entities), [](Entity &entity) {
return (entity.hasComponent<Components>() && ...);
});
}
//! @brief A default copy constructor.
View(const View &) = default;
//! @brief A default destructor.
~View() = default;
//! @brief A View is assignable.
View &operator=(const View &) = default;
};
}
+1
View File
@@ -5,6 +5,7 @@
#include <chrono>
#include <algorithm>
#include "Wal.hpp"
#include "Scene/Scene.hpp"
namespace WAL
{
+3 -2
View File
@@ -10,13 +10,14 @@
#include <memory>
#include <typeinfo>
#include "Exception/WalError.hpp"
#include "Scene/Scene.hpp"
#include "Entity/Entity.hpp"
#include "System/System.hpp"
#include "Models/Callback.hpp"
namespace WAL
{
class Entity;
class Scene;
//! @brief The main WAL class, it is used to setup and run the ECS.
class Wal
{
+4 -4
View File
@@ -64,9 +64,9 @@ namespace BBM
wal.addSystem<RenderScreenSystem>(window);
}
std::shared_ptr<WAL::Scene> loadGameScene()
std::shared_ptr<WAL::Scene> loadGameScene(WAL::Wal &wal)
{
auto scene = std::make_shared<WAL::Scene>();
auto scene = std::make_shared<WAL::Scene>(wal);
scene->addEntity("player")
.addComponent<PositionComponent>()
.addComponent<Drawable3DComponent<RAY3D::Model>>("assets/player/player.iqm", std::make_pair(MAP_DIFFUSE, "assets/player/blue.png"))
@@ -90,7 +90,7 @@ namespace BBM
scene->addEntity("camera")
.addComponent<PositionComponent>(8, 20, 7)
.addComponent<CameraComponent>(Vector3f(8, 0, 8));
std::srand(std::time(NULL));
std::srand(std::time(nullptr));
MapGenerator::loadMap(16, 16, MapGenerator::createMap(16, 16), scene);
return scene;
}
@@ -100,7 +100,7 @@ namespace BBM
WAL::Wal wal;
addSystems(wal);
enableRaylib(wal);
wal.scene = loadGameScene();
wal.scene = loadGameScene(wal);
try {
wal.run<GameState>(updateState);
+3 -1
View File
@@ -4,6 +4,7 @@
#include <catch2/catch.hpp>
#include <stdexcept>
#include <Wal.hpp>
#include "Entity/Entity.hpp"
#include "Models/Callback.hpp"
@@ -36,6 +37,7 @@ TEST_CASE("Callback multiple arguments", "[Callback]")
callback.addCallback([](const std::string& str, int a, unsigned *value, Entity &entity) {
throw std::runtime_error("");
});
Entity entity("name");
Wal wal;
Entity entity(wal, "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::shared_ptr<Scene>(new Scene);
wal.scene = std::make_shared<Scene>(wal);
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::shared_ptr<Scene>(new Scene);
wal.scene = std::make_shared<Scene>(wal);
wal.scene->addEntity("player")
.addComponent<PositionComponent>()
.addComponent<CollisionComponent>([](Entity &actual, const Entity &) {}, [](Entity &actual, const Entity &) {}, 5.0)
+7 -3
View File
@@ -5,13 +5,15 @@
#include "Entity/Entity.hpp"
#include "Component/Position/PositionComponent.hpp"
#include <catch2/catch.hpp>
#include <Wal.hpp>
using namespace WAL;
using namespace BBM;
TEST_CASE("Component", "[Entity]")
{
Entity entity("Bob");
Wal wal;
Entity entity(wal, "Bob");
entity.addComponent<PositionComponent>(2, 3, 4);
SECTION("Check value") {
@@ -31,13 +33,15 @@ TEST_CASE("Component", "[Entity]")
TEST_CASE("ComponentNotFound", "[Entity]")
{
Entity entity("Bob");
Wal wal;
Entity entity(wal, "Bob");
REQUIRE_THROWS_AS(entity.getComponent<PositionComponent>(), NotFoundError);
}
TEST_CASE("Add component by reference", "[Entity]")
{
Entity entity("Bob");
Wal wal;
Entity entity(wal, "Bob");
PositionComponent component(entity, 4, 5, 6);
REQUIRE(&entity.addComponent(component) == &entity);
+2 -1
View File
@@ -19,7 +19,8 @@ using namespace BBM;
TEST_CASE("Move test", "[Component][System]")
{
Scene scene;
Wal wal;
Scene scene(wal);
scene.addEntity("player")
.addComponent<ControllableComponent>()
.addComponent<MovableComponent>()
+33
View File
@@ -0,0 +1,33 @@
//
// Created by Zoe Roux on 6/3/21.
//
#include "Entity/Entity.hpp"
#include "Component/Position/PositionComponent.hpp"
#include "System/Movable/MovableSystem.hpp"
#include "System/Controllable/ControllableSystem.hpp"
#include <catch2/catch.hpp>
#include <Wal.hpp>
#include <Component/Controllable/ControllableComponent.hpp>
#define private public
#include <Component/Movable/MovableComponent.hpp>
using namespace WAL;
using namespace BBM;
TEST_CASE("View creation", "[View]")
{
Wal wal;
Scene scene(wal);
scene.addEntity("player")
.addComponent<PositionComponent>()
.addComponent<ControllableComponent>();
scene.addEntity("Box")
.addComponent<PositionComponent>();
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();
REQUIRE(&entity == &firstView);
}