mirror of
https://github.com/zoriya/Bomberman.git
synced 2026-06-04 10:44:42 +00:00
Adding some tests
This commit is contained in:
@@ -17,6 +17,6 @@ add_library(wal
|
||||
sources/Component/Component.cpp
|
||||
sources/System/System.cpp
|
||||
sources/Models/Callback.hpp
|
||||
sources/View/BaseView.hpp)
|
||||
sources/View/View.hpp)
|
||||
|
||||
target_include_directories(wal PUBLIC sources)
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include <View/BaseView.hpp>
|
||||
#include <View/View.hpp>
|
||||
#include "Entity/Entity.hpp"
|
||||
|
||||
namespace WAL
|
||||
|
||||
@@ -1,50 +0,0 @@
|
||||
//
|
||||
// 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>() && ...);
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
//
|
||||
// Created by Zoe Roux on 2021-06-03.
|
||||
//
|
||||
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <tuple>
|
||||
#include <typeindex>
|
||||
#include <functional>
|
||||
#include "Entity/Entity.hpp"
|
||||
|
||||
namespace WAL
|
||||
{
|
||||
//! @brief A basic view used to manipulate view without knowing their type at compile time.
|
||||
class BaseView
|
||||
{
|
||||
public:
|
||||
//! @brief The list of entities in the view.
|
||||
std::vector<std::reference_wrapper<Entity>> entities = {};
|
||||
|
||||
//! @brief The list of types that every entity of the view has.
|
||||
std::vector<std::type_index> types = {};
|
||||
|
||||
size_t size()
|
||||
{
|
||||
return entities.size();
|
||||
}
|
||||
|
||||
//! @brief A default destructor
|
||||
~BaseView() = default;
|
||||
protected:
|
||||
//! @brief A basic view can't be constructed, you should use the View templated class.
|
||||
BaseView() = default;
|
||||
//! @brief A basic view can't be constructed, you should use the View templated class.
|
||||
BaseView(const BaseView &) = default;
|
||||
//! @brief A basic view can't be assigned. See the View template class.
|
||||
BaseView &operator=(const BaseView &) = default;
|
||||
};
|
||||
|
||||
//! @brief A view allowing one to easily access entities containing a set list of component.
|
||||
//! A view is always updated and only references to entities are kept.
|
||||
template<typename ...Components>
|
||||
class View : public BaseView
|
||||
{
|
||||
public:
|
||||
//! @brief Construct a view from a list of entities.
|
||||
//! Those entities are never copied but references to them are kept internally.
|
||||
explicit View(std::vector<Entity> &scene)
|
||||
{
|
||||
this->types = {typeid(Components)...};
|
||||
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;
|
||||
//! @brief A view is not assignable.
|
||||
View &operator=(const View &) = delete;
|
||||
};
|
||||
}
|
||||
+42
-6
@@ -4,15 +4,10 @@
|
||||
|
||||
#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;
|
||||
|
||||
@@ -29,4 +24,45 @@ TEST_CASE("View creation", "[View]")
|
||||
Entity &entity = *scene.getEntities().begin();
|
||||
Entity &firstView = *scene.view<PositionComponent, ControllableComponent>().entities.begin();
|
||||
REQUIRE(&entity == &firstView);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("View update", "[View]")
|
||||
{
|
||||
Scene scene;
|
||||
scene.addEntity("player")
|
||||
.addComponent<PositionComponent>()
|
||||
.addComponent<ControllableComponent>();
|
||||
auto &view = scene.view<PositionComponent>();
|
||||
auto &entity = scene.addEntity("Box")
|
||||
.addComponent<PositionComponent>();
|
||||
REQUIRE(view.size() == 2);
|
||||
entity.removeComponent<PositionComponent>();
|
||||
REQUIRE(view.size() == 1);
|
||||
}
|
||||
|
||||
TEST_CASE("View cache", "[View]")
|
||||
{
|
||||
Scene scene;
|
||||
scene.addEntity("player")
|
||||
.addComponent<PositionComponent>()
|
||||
.addComponent<ControllableComponent>();
|
||||
auto &view = scene.view<PositionComponent>();
|
||||
REQUIRE(&view == &scene.view<PositionComponent>());
|
||||
}
|
||||
|
||||
//TEST_CASE("View iteration", "[View]")
|
||||
//{
|
||||
// Scene scene;
|
||||
// scene.addEntity("player")
|
||||
// .addComponent<PositionComponent>()
|
||||
// .addComponent<ControllableComponent>();
|
||||
// scene.addEntity("Box")
|
||||
// .addComponent<PositionComponent>();
|
||||
// int i = 0;
|
||||
// for (auto &entity : scene.view<PositionComponent>()) {
|
||||
// if (i == 0)
|
||||
// REQUIRE(entity.getName() == "player");
|
||||
// else
|
||||
// REQUIRE(entity.getName() == "Box");
|
||||
// }
|
||||
//}
|
||||
Reference in New Issue
Block a user