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>
@@ -5,6 +5,7 @@
#include "ShaderComponent.hpp"
#include <utility>
#include <Component/Renderer/Drawable3DComponent.hpp>
namespace BBM
{
@@ -20,7 +21,6 @@ namespace BBM
ShaderComponent::ShaderComponent(WAL::Entity &entity, const std::string &fragmentFilePath, const std::string &vertexFilePath)
: WAL::Component(entity),
_refEntity(entity),
_shader(vertexFilePath, fragmentFilePath),
_fragmentFilePath(fragmentFilePath),
_vertexFilePath(vertexFilePath)
@@ -42,6 +42,14 @@ namespace BBM
{
}
void ShaderComponentModel::onStart()
{
auto &drawable = this->_entity.getComponent<Drawable3DComponent>();
this->model = dynamic_cast<RAY::Drawables::Drawables3D::Model *>(drawable.drawable.get());
if (!this->model)
throw std::runtime_error("No model available with a shader model component. This is unsupported.");
}
ShaderComponentDrawable2D::ShaderComponentDrawable2D(WAL::Entity &entity, std::string fragmentFilePath, std::string vertexFilePath)
: ShaderComponent(entity, std::move(fragmentFilePath), std::move(vertexFilePath))
{
@@ -8,15 +8,13 @@
#include <Component/Component.hpp>
#include <Entity/Entity.hpp>
#include <Shaders/Shaders.hpp>
#include <Model/Model.hpp>
namespace BBM
{
class ShaderComponent : public WAL::Component
{
private:
//! @brief efefefefez
WAL::Entity &_refEntity;
//! @brief The shader to be applied
RAY::Shader _shader;
//! @brief The path to the fragment file
@@ -51,6 +49,10 @@ namespace BBM
class ShaderComponentModel : public ShaderComponent
{
public:
RAY::Drawables::Drawables3D::Model *model = nullptr;
void onStart() override;
//! @brief ctor
//! @note use empty string to omit a file
ShaderComponentModel(WAL::Entity &entity, std::string fragmentFilePath, std::string vertexFilePath = "");
+3 -5
View File
@@ -132,12 +132,10 @@ namespace BBM
{UPPERFLOOR, &createUpperFloor},
};
try {
auto element = elements.at(blockType);
element(coords, scene);
} catch (std::exception const &err) {
if (blockType == NOTHING || blockType == SPAWNER)
return;
}
auto element = elements.at(blockType);
element(coords, std::move(scene));
}
void MapGenerator::createBreakable(Vector3f coords, std::shared_ptr<WAL::Scene> scene)
+1 -1
View File
@@ -126,7 +126,7 @@ namespace BBM
WAL::Wal wal;
addSystems(wal);
enableRaylib(wal);
wal.scene = loadGameScene();
wal.changeScene(loadGameScene());
try {
wal.run<GameState>(updateState);
@@ -39,7 +39,7 @@ namespace BBM
void BombHolderSystem::_spawnBomb(Vector3f position)
{
this->_wal.scene->scheduleNewEntity("Bomb")
this->_wal.getScene()->scheduleNewEntity("Bomb")
.addComponent<PositionComponent>(position)
.addComponent<TimerComponent>(BombHolderSystem::explosionTimer, &BombHolderSystem::_bombExplosion)
.addComponent<Drawable3DComponent, RAY3D::Model>("assets/bombs/bomb.obj",
+6 -10
View File
@@ -33,24 +33,20 @@ namespace BBM
this->_window.clear();
this->_window.useCamera(this->_camera);
for (auto &[entity, pos, drawable] : this->_wal.scene->view<PositionComponent, Drawable3DComponent>()) {
for (auto &[entity, pos, drawable] : this->_wal.getScene()->view<PositionComponent, Drawable3DComponent>()) {
auto *modelShader = entity.tryGetComponent<ShaderComponentModel>();
if (modelShader) {
auto &model = dynamic_cast<RAY::Drawables::Drawables3D::Model &>(*drawable.drawable);
model.setShader(modelShader->getShader());
}
if (modelShader)
modelShader->model->setShader(modelShader->getShader());
drawable.drawable->setPosition(pos.position);
drawable.drawable->drawOn(this->_window);
if (modelShader) {
auto &model = dynamic_cast<RAY::Drawables::Drawables3D::Model &>(*drawable.drawable);
model.resetShader();
}
if (modelShader)
modelShader->model->resetShader();
}
this->_window.unuseCamera();
// TODO sort entities based on the Z axis
for (auto &[entity, pos, drawable] : this->_wal.scene->view<PositionComponent, Drawable2DComponent>()) {
for (auto &[entity, pos, drawable] : this->_wal.getScene()->view<PositionComponent, Drawable2DComponent>()) {
auto *shader = entity.tryGetComponent<ShaderComponentDrawable2D>();
if (shader) {
+10 -10
View File
@@ -22,8 +22,8 @@ TEST_CASE("Collision test", "[Component][System]")
{
Wal wal;
CollisionSystem collision(wal);
wal.scene = std::make_shared<Scene>();
wal.scene->addEntity("player")
wal.changeScene(std::make_shared<Scene>());
wal.getScene()->addEntity("player")
.addComponent<PositionComponent>()
.addComponent<CollisionComponent>([](Entity &actual, const Entity &, int _) {
try {
@@ -33,7 +33,7 @@ TEST_CASE("Collision test", "[Component][System]")
pos.position.z = 1;
} catch (std::exception &e) {};
}, [](Entity &, const Entity &, int) {}, 0, 5.0);
Entity &entity = wal.scene->getEntities().front();
Entity &entity = wal.getScene()->getEntities().front();
REQUIRE(entity.getComponent<PositionComponent>().position == Vector3f());
entity.getComponent<CollisionComponent>().bound.x = 5;
@@ -46,14 +46,14 @@ TEST_CASE("Collision test", "[Component][System]")
REQUIRE(entity.getComponent<PositionComponent>().position.y == 0.0);
REQUIRE(entity.getComponent<PositionComponent>().position.z == 0.0);
wal.scene->addEntity("block")
wal.getScene()->addEntity("block")
.addComponent<PositionComponent>(2, 2, 2)
.addComponent<CollisionComponent>(0, 1);
Entity &player = wal.scene->getEntities().front();
Entity &player = wal.getScene()->getEntities().front();
collision.update(std::chrono::nanoseconds(1));
REQUIRE(player.hasComponent(typeid(PositionComponent)));
collision.fixedUpdate();
REQUIRE(wal.scene->getEntities().size() == 2);
REQUIRE(wal.getScene()->getEntities().size() == 2);
REQUIRE(player.hasComponent(typeid(PositionComponent)));
REQUIRE(player.getComponent<PositionComponent>().position.x == 1.0);
REQUIRE(player.getComponent<PositionComponent>().position.y == 1);
@@ -66,14 +66,14 @@ TEST_CASE("Collsion test with movable", "[Component][System]")
Wal wal;
CollisionSystem collision(wal);
MovableSystem movable(wal);
wal.scene = std::make_shared<Scene>();
wal.scene->addEntity("player")
wal.changeScene(std::make_shared<Scene>());
wal.getScene()->addEntity("player")
.addComponent<PositionComponent>()
.addComponent<CollisionComponent>([](Entity &actual, const Entity &, int) {},
[](Entity &actual, const Entity &, int) {}, 0, 5.0)
.addComponent<MovableComponent>();
wal.scene->addEntity("block")
wal.getScene()->addEntity("block")
.addComponent<PositionComponent>(0, 0, 0)
.addComponent<CollisionComponent>([](Entity &actual, const Entity &, int) {},
[](Entity &actual, const Entity &, int) {
@@ -82,7 +82,7 @@ TEST_CASE("Collsion test with movable", "[Component][System]")
mov._velocity = Vector3f();
} catch (std::exception &e) {};
}, 0, 1);
Entity &entity = wal.scene->getEntities().front();
Entity &entity = wal.getScene()->getEntities().front();
REQUIRE(entity.getComponent<PositionComponent>().position == Vector3f());
entity.getComponent<CollisionComponent>().bound.x = 5;
+3 -3
View File
@@ -20,12 +20,12 @@ using namespace BBM;
TEST_CASE("Move test", "[Component][System]")
{
Wal wal;
wal.scene = std::make_shared<Scene>();
wal.scene->addEntity("player")
wal.changeScene(std::make_shared<Scene>());
wal.getScene()->addEntity("player")
.addComponent<ControllableComponent>()
.addComponent<MovableComponent>()
.addComponent<PositionComponent>();
Entity &entity = wal.scene->getEntities().front();
Entity &entity = wal.getScene()->getEntities().front();
REQUIRE(entity.getComponent<PositionComponent>().position == Vector3f());