mirror of
https://github.com/zoriya/Bomberman.git
synced 2026-06-03 18:31:17 +00:00
Reworking the view
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
}
|
||||
@@ -5,6 +5,7 @@
|
||||
#include <chrono>
|
||||
#include <algorithm>
|
||||
#include "Wal.hpp"
|
||||
#include "Scene/Scene.hpp"
|
||||
|
||||
namespace WAL
|
||||
{
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user