Bomberman
Scene.hpp
Go to the documentation of this file.
1 //
2 // Created by Zoe Roux on 2021-05-14.
3 //
4 
5 
6 #pragma once
7 
8 #include <vector>
9 #include <list>
10 #include <functional>
11 #include <View/View.hpp>
12 #include "Entity/Entity.hpp"
13 
14 namespace WAL
15 {
17  class Scene
18  {
19  private:
20  static int _nextID;
22  int _id = _nextID++;
23 
25  std::list<Entity> _entities = {};
27  std::list<Entity> _newEntities = {};
29  std::vector<std::shared_ptr<IView>> _views = {};
30 
34  void _componentAdded(Entity &entity, const std::type_index &type);
38  void _componentRemoved(const Entity &entity, const std::type_index &type);
41  void _entityRemoved(const Entity &entity);
42  public:
44  int getID() const;
45 
47  std::list<Entity> &getEntities();
48 
52  Entity &addEntity(const std::string &name);
53 
57  Entity &scheduleNewEntity(const std::string &name);
58 
59  template<typename ...Components>
60  View<Components...> &view()
61  {
62  static std::unordered_map<int, std::weak_ptr<View<Components...>>> cache;
63  auto existing = cache.find(this->_id);
64  if (existing != cache.end() && !existing->second.expired())
65  return *existing->second.lock();
66  auto view = std::make_shared<View<Components...>>(this->_entities);
67  this->_views.emplace_back(view);
68  cache.emplace(this->_id, view);
69  return *view;
70  }
71 
73  void applyChanges();
74 
76  Scene() = default;
78  Scene(const Scene &) = delete;
80  ~Scene() = default;
82  Scene &operator=(const Scene &);
83 
84  friend Entity;
85  };
86 } // namespace WAL
WAL::View::emplace_back
void emplace_back(Entity &entity) override
Definition: View.hpp:175
WAL::Scene::Entity
friend Entity
Definition: Scene.hpp:84
WAL::Scene::operator=
Scene & operator=(const Scene &)
A scene is assignable.
Definition: Scene.cpp:17
WAL::Scene::applyChanges
void applyChanges()
Delete entities marked as deleted and create scheduled entities.
Definition: Scene.cpp:65
WAL::Scene::_newEntities
std::list< Entity > _newEntities
The list of entities to add on the next call to applyChanges.
Definition: Scene.hpp:27
WAL
Definition: Component.cpp:7
WAL::Scene::addEntity
Entity & addEntity(const std::string &name)
Add a new entity to the scene.
Definition: Scene.cpp:22
View.hpp
WAL::Scene::_componentAdded
void _componentAdded(Entity &entity, const std::type_index &type)
Notify this scene that a component has been added to the given entity.
Definition: Scene.cpp:32
Entity.hpp
WAL::Entity
An entity of the WAL's ECS.
Definition: Entity.hpp:20
WAL::Scene::_nextID
static int _nextID
Definition: Scene.hpp:20
WAL::Scene::getID
int getID() const
Get the ID of this scene.
Definition: Scene.cpp:54
WAL::Scene::_componentRemoved
void _componentRemoved(const Entity &entity, const std::type_index &type)
Notify this scene that a component has been removed to the given entity.
Definition: Scene.cpp:45
WAL::Scene::getEntities
std::list< Entity > & getEntities()
Get the list of entities.
Definition: Scene.cpp:12
WAL::Scene::Scene
Scene()=default
A default constructor.
WAL::Scene::_entities
std::list< Entity > _entities
The list of registered entities.
Definition: Scene.hpp:25
WAL::Scene::_id
int _id
An ID representing this scene.
Definition: Scene.hpp:22
WAL::View
A view allowing one to easily access entities containing a set list of component. A view is always up...
Definition: View.hpp:133
WAL::Scene
Represent a single scene that contains entities.
Definition: Scene.hpp:17
WAL::Scene::_entityRemoved
void _entityRemoved(const Entity &entity)
Remove an entity from every views.
Definition: Scene.cpp:59
WAL::Scene::view
View< Components... > & view()
Definition: Scene.hpp:60
WAL::Scene::_views
std::vector< std::shared_ptr< IView > > _views
The list of cached views to update.
Definition: Scene.hpp:29
WAL::Scene::~Scene
~Scene()=default
A default destructor.
WAL::Scene::scheduleNewEntity
Entity & scheduleNewEntity(const std::string &name)
Add a new entity to the scene, this entity will be added on the next call to applyChanges.
Definition: Scene.cpp:27