Adding a onStart onStop

This commit is contained in:
Zoe Roux
2021-06-09 16:00:06 +02:00
parent c544a83ee8
commit c94237dedc
13 changed files with 79 additions and 38 deletions
+1
View File
@@ -44,6 +44,7 @@ namespace WAL
void _componentRemoved(const std::type_index &type);
friend Scene;
friend class Wal;
protected:
//! @brief A reference to the ECS.
Scene &_scene;
+9
View File
@@ -62,6 +62,8 @@ namespace WAL
this->_entities.remove_if([this](auto &entity) {
if (!entity.shouldDelete())
return false;
for (auto &cmp : entity._components)
cmp.second->onStop();
this->_entityRemoved(entity);
return true;
});
@@ -74,7 +76,14 @@ namespace WAL
view->emplace_back(entity);
}
entity._notifyScene = true;
for (auto &cmp : entity._components)
cmp.second->onStart();
}
this->_entities.splice(this->_entities.end(), this->_newEntities);
}
int Scene::getID() const
{
return this->_id;
}
} // namespace WAL
+3
View File
@@ -40,6 +40,9 @@ namespace WAL
//! @param entity The entity to remove.
void _entityRemoved(const Entity &entity);
public:
//! @brief Get the ID of this scene.
int getID() const;
//! @brief Get the list of entities.
std::list<Entity> &getEntities();
+1 -1
View File
@@ -30,7 +30,7 @@ namespace WAL
//! @brief Get a view of all entities containing every dependencies of this system.
View<Dependencies...> &getView() override
{
return this->_wal.scene->template view<Dependencies...>();
return this->_wal.getScene()->template view<Dependencies...>();
}
//! @brief Update the corresponding component of the given entity
+27 -3
View File
@@ -5,6 +5,7 @@
#pragma once
#include <utility>
#include <vector>
#include <string>
#include <memory>
@@ -29,6 +30,9 @@ namespace WAL
//! @brief The list of registered systems
std::vector<std::unique_ptr<ISystem>> _systems = {};
//! @brief The scene that contains entities.
std::shared_ptr<Scene> _scene;
//! @brief Start the game loop
//! @param callback A callback called after each update of the game. It allow you to update the engine based on a specific game state. (you can also update the game state here)
//! @param state An initial game state. If not specified, it will be defaulted.
@@ -52,7 +56,7 @@ namespace WAL
}
for (auto &system : this->_systems)
system->update(dtime);
this->scene->applyChanges();
this->_scene->applyChanges();
callback(*this, state);
}
}
@@ -81,13 +85,33 @@ namespace WAL
}
#endif
public:
//! @brief The scene that contains entities.
std::shared_ptr<Scene> scene;
//! @brief True if the engine should close after the end of the current tick.
bool shouldClose = false;
//! @brief The time between each fixed update.
static constexpr std::chrono::nanoseconds timestep = std::chrono::milliseconds(32);
//! @brief Retrieve the current scene
std::shared_ptr<Scene> getScene() const
{
return this->_scene;
}
//! @brief Change the current scene
void changeScene(std::shared_ptr<Scene> newScene)
{
if (this->_scene) {
for (auto &entity : this->_scene->getEntities()) {
for (auto &cmp : entity._components)
cmp.second->onStop();
}
}
this->_scene = std::move(newScene);
for (auto &entity : this->_scene->getEntities()) {
for (auto &cmp : entity._components)
cmp.second->onStart();
}
}
//! @brief Create a new system in place.
//! @return The wal instance used to call this function is returned. This allow method chaining.
template<typename T, class ...Types>