mirror of
https://github.com/zoriya/Bomberman.git
synced 2026-06-01 09:45:42 +00:00
Fixing view caches
This commit is contained in:
@@ -6,7 +6,9 @@
|
||||
|
||||
namespace WAL
|
||||
{
|
||||
std::vector<Entity> &Scene::getEntities()
|
||||
int Scene::_nextID = 0;
|
||||
|
||||
std::list<Entity> &Scene::getEntities()
|
||||
{
|
||||
return this->_entities;
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <list>
|
||||
#include <functional>
|
||||
#include <View/View.hpp>
|
||||
#include "Entity/Entity.hpp"
|
||||
@@ -16,8 +17,12 @@ namespace WAL
|
||||
class Scene
|
||||
{
|
||||
private:
|
||||
static int _nextID;
|
||||
//! @brief An ID representing this scene.
|
||||
int _id = _nextID++;
|
||||
|
||||
//! @brief The list of registered entities
|
||||
std::vector<Entity> _entities = {};
|
||||
std::list<Entity> _entities = {};
|
||||
//! @brief The list of cached views to update.
|
||||
std::vector<std::shared_ptr<BaseView>> _views = {};
|
||||
|
||||
@@ -31,7 +36,7 @@ namespace WAL
|
||||
void _componentRemoved(const Entity &entity, const std::type_index &type);
|
||||
public:
|
||||
//! @brief Get the list of entities.
|
||||
std::vector<Entity> &getEntities();
|
||||
std::list<Entity> &getEntities();
|
||||
|
||||
//! @brief Add a new entity to the scene.
|
||||
//! @param name The name of the created entity.
|
||||
@@ -41,8 +46,13 @@ namespace WAL
|
||||
template<typename ...Components>
|
||||
View<Components...> &view()
|
||||
{
|
||||
static auto view = std::make_shared<View<Components...>>(this->_entities);
|
||||
static std::unordered_map<int, std::weak_ptr<View<Components...>>> cache;
|
||||
auto existing = cache.find(this->_id);
|
||||
if (existing != cache.end() && !existing->second.expired())
|
||||
return *existing->second.lock();
|
||||
auto view = std::make_shared<View<Components...>>(this->_entities);
|
||||
this->_views.emplace_back(view);
|
||||
cache.emplace(this->_id, view);
|
||||
return *view;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <list>
|
||||
#include <tuple>
|
||||
#include <typeindex>
|
||||
#include <functional>
|
||||
@@ -47,7 +47,7 @@ namespace WAL
|
||||
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)
|
||||
explicit View(std::list<Entity> &scene)
|
||||
{
|
||||
this->types = {typeid(Components)...};
|
||||
std::copy_if(scene.begin(), scene.end(), std::back_inserter(this->entities), [](Entity &entity) {
|
||||
|
||||
@@ -32,7 +32,7 @@ TEST_CASE("Collision test", "[Component][System]")
|
||||
pos.position.z = 1;
|
||||
} catch (std::exception &e) {};
|
||||
}, [](Entity &, const Entity &){}, 5.0);
|
||||
Entity &entity = wal.scene->getEntities()[0];
|
||||
Entity &entity = wal.scene->getEntities().front();
|
||||
REQUIRE(entity.getComponent<PositionComponent>().position == Vector3f());
|
||||
|
||||
entity.getComponent<CollisionComponent>().bound.x = 5;
|
||||
@@ -48,7 +48,7 @@ TEST_CASE("Collision test", "[Component][System]")
|
||||
wal.scene->addEntity("block")
|
||||
.addComponent<PositionComponent>(2,2,2)
|
||||
.addComponent<CollisionComponent>(1);
|
||||
Entity &player = wal.scene->getEntities()[0];
|
||||
Entity &player = wal.scene->getEntities().front();
|
||||
collision.onUpdate(entity, std::chrono::nanoseconds(1));
|
||||
REQUIRE(player.hasComponent(typeid(PositionComponent)));
|
||||
collision.onFixedUpdate(player);
|
||||
@@ -79,7 +79,7 @@ TEST_CASE("Collsion test with movable", "[Component][System]")
|
||||
mov.resetVelocity();
|
||||
} catch (std::exception &e) {};
|
||||
}, 1);
|
||||
Entity &entity = wal.scene->getEntities()[0];
|
||||
Entity &entity = wal.scene->getEntities().front();
|
||||
REQUIRE(entity.getComponent<PositionComponent>().position == Vector3f());
|
||||
|
||||
entity.getComponent<CollisionComponent>().bound.x = 5;
|
||||
|
||||
+1
-1
@@ -25,7 +25,7 @@ TEST_CASE("Move test", "[Component][System]")
|
||||
.addComponent<ControllableComponent>()
|
||||
.addComponent<MovableComponent>()
|
||||
.addComponent<PositionComponent>();
|
||||
Entity &entity = scene.getEntities()[0];
|
||||
Entity &entity = *scene.getEntities().begin();
|
||||
|
||||
REQUIRE(entity.getComponent<PositionComponent>().position == Vector3f());
|
||||
|
||||
|
||||
@@ -50,6 +50,22 @@ TEST_CASE("View cache", "[View]")
|
||||
REQUIRE(&view == &scene.view<PositionComponent>());
|
||||
}
|
||||
|
||||
TEST_CASE("View cache switch", "[View]")
|
||||
{
|
||||
Scene scene;
|
||||
scene.addEntity("player")
|
||||
.addComponent<PositionComponent>()
|
||||
.addComponent<ControllableComponent>();
|
||||
auto &view = scene.view<PositionComponent>();
|
||||
Scene scene2;
|
||||
scene2.addEntity("box")
|
||||
.addComponent<PositionComponent>();
|
||||
|
||||
REQUIRE(&view == &scene.view<PositionComponent>());
|
||||
REQUIRE(view.entities.begin()->get().getName() == "player");
|
||||
REQUIRE(scene2.view<PositionComponent>().entities.begin()->get().getName() == "box");
|
||||
}
|
||||
|
||||
//TEST_CASE("View iteration", "[View]")
|
||||
//{
|
||||
// Scene scene;
|
||||
|
||||
Reference in New Issue
Block a user