mirror of
https://github.com/zoriya/Bomberman.git
synced 2026-06-04 18:46:22 +00:00
Trying to merge with develop
This commit is contained in:
@@ -3,15 +3,62 @@
|
||||
//
|
||||
|
||||
#include "Scene.hpp"
|
||||
#include <algorithm>
|
||||
|
||||
namespace WAL
|
||||
{
|
||||
std::vector<Entity> &Scene::getEntities()
|
||||
int Scene::_nextID = 0;
|
||||
|
||||
std::list<Entity> &Scene::getEntities()
|
||||
{
|
||||
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(Entity &entity, const std::type_index &type)
|
||||
{
|
||||
for (auto &view : this->_views) {
|
||||
if (std::find(view->getTypes().begin(), view->getTypes().end(), type) == view->getTypes().end())
|
||||
continue;
|
||||
bool valid = std::all_of(view->getTypes().begin(), view->getTypes().end(), [&entity](const auto &type){
|
||||
return entity.hasComponent(type);
|
||||
});
|
||||
if (valid)
|
||||
view->emplace_back(entity);
|
||||
}
|
||||
}
|
||||
|
||||
void Scene::_componentRemoved(const Entity &entity, const std::type_index &type)
|
||||
{
|
||||
for (auto &view : this->_views) {
|
||||
if (std::find(view->getTypes().begin(), view->getTypes().end(), type) == view->getTypes().end())
|
||||
continue;
|
||||
view->erase(entity);
|
||||
}
|
||||
}
|
||||
|
||||
void Scene::_entityRemoved(const Entity &entity)
|
||||
{
|
||||
for (auto &view : this->_views)
|
||||
view->erase(entity);
|
||||
}
|
||||
|
||||
void Scene::deleteMarkedEntities()
|
||||
{
|
||||
this->_entities.remove_if([this](auto &entity) {
|
||||
if (!entity.shouldDelete())
|
||||
return false;
|
||||
this->_entityRemoved(entity);
|
||||
return true;
|
||||
});
|
||||
}
|
||||
} // namespace WAL
|
||||
@@ -6,7 +6,9 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <list>
|
||||
#include <functional>
|
||||
#include <View/View.hpp>
|
||||
#include "Entity/Entity.hpp"
|
||||
|
||||
namespace WAL
|
||||
@@ -15,20 +17,51 @@ 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<IView>> _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(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, const std::type_index &type);
|
||||
//! @brief Remove an entity from every views.
|
||||
//! @param entity The entity to remove.
|
||||
void _entityRemoved(const Entity &entity);
|
||||
public:
|
||||
//! @brief Get the list of entities.
|
||||
std::vector<Entity> &getEntities();
|
||||
std::list<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>
|
||||
View<Components...> &view()
|
||||
{
|
||||
return this->_entities.emplace_back(std::forward<Params>(params)...);
|
||||
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;
|
||||
}
|
||||
|
||||
//! @brief Delete entities marked as deleted.
|
||||
void deleteMarkedEntities();
|
||||
|
||||
//! @brief A default constructor
|
||||
Scene() = default;
|
||||
//! @brief A scene is copy constructable
|
||||
@@ -38,5 +71,7 @@ namespace WAL
|
||||
//! @brief A scene is assignable
|
||||
Scene &operator=(const Scene &);
|
||||
Scene(Scene &&) = default;
|
||||
|
||||
friend Entity;
|
||||
};
|
||||
} // namespace WAL
|
||||
Reference in New Issue
Block a user