diff --git a/CMakeLists.txt b/CMakeLists.txt index 38fa4806..ab2f7c6f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -176,7 +176,7 @@ set(SOURCES sources/Component/Color/ColorComponent.cpp sources/Component/Stat/StatComponent.cpp sources/Component/Stat/StatComponent.hpp - ) + sources/Component/Shaders/Items/AlphaCtxShaderComponent.cpp sources/Component/Shaders/Items/AlphaCtxShaderComponent.hpp) add_executable(bomberman sources/main.cpp diff --git a/sources/Component/Shaders/Items/AlphaCtxShaderComponent.cpp b/sources/Component/Shaders/Items/AlphaCtxShaderComponent.cpp new file mode 100644 index 00000000..49634922 --- /dev/null +++ b/sources/Component/Shaders/Items/AlphaCtxShaderComponent.cpp @@ -0,0 +1,20 @@ +// +// Created by cbihan on 18/06/2021. +// + +#include "AlphaCtxShaderComponent.hpp" + + +namespace BBM +{ + + AlphaVarShaderComponent::AlphaVarShaderComponent(WAL::Entity &entity) : + WAL::Component(entity) + { + } + + WAL::Component *AlphaVarShaderComponent::clone(WAL::Entity &entity) const + { + return new AlphaVarShaderComponent(this->_entity); + } +} \ No newline at end of file diff --git a/sources/Component/Shaders/Items/AlphaCtxShaderComponent.hpp b/sources/Component/Shaders/Items/AlphaCtxShaderComponent.hpp new file mode 100644 index 00000000..5408d091 --- /dev/null +++ b/sources/Component/Shaders/Items/AlphaCtxShaderComponent.hpp @@ -0,0 +1,51 @@ +// +// Created by cbihan on 18/06/2021. +// + +#pragma once + +#include +#include + +using namespace std::chrono_literals; + +namespace BBM +{ + class AlphaVarShaderComponent : public WAL::Component + { + public: + //! @brief Transparency + float alpha = 1; + + //! @brief minimum transparency + float minAlpha = 0.2; + //! @brief maximum transparency + float maxAlpha = 1; + //! @brief inital step value + float initalStepValue = 0.04; + //! @brief how fast the alpha will vary + float step = 0.04; + //! @brief if the alpha should increase or decrease + float balance = -1; + + //! @brief The clock to use + std::chrono::nanoseconds clock = 0ns; + + + //! @inherit + WAL::Component *clone(WAL::Entity &entity) const override; + + //! @brief ctor + explicit AlphaVarShaderComponent(WAL::Entity &entity); + + //! @brief Default copy ctor + AlphaVarShaderComponent(const AlphaVarShaderComponent &) = default; + + //! @brief Default dtor + ~AlphaVarShaderComponent() override = default; + + //! @brief Default assignment operator + AlphaVarShaderComponent &operator=(const AlphaVarShaderComponent &) = delete; + }; + +} \ No newline at end of file diff --git a/sources/Items/Bonus.cpp b/sources/Items/Bonus.cpp index 6a579d43..0ebfcb25 100644 --- a/sources/Items/Bonus.cpp +++ b/sources/Items/Bonus.cpp @@ -58,6 +58,7 @@ namespace BBM { if (!playerBonus) return; playerBonus->nextNoClipRate = playerBonus->noClipBonusRate; + playerBonus->isNoClipOn = true; const_cast(bonus).scheduleDeletion(); } @@ -70,7 +71,7 @@ namespace BBM { {SPEEDUP, 45.0f}, {BOMBSTOCK, 30.0f}, {EXPLOSIONINC, 15.0f}, - {NOCLIP, 1.5f}, + {NOCLIP, 70.5f}, }; std::uniform_int_distribution distribution(1,1000); float value = (distribution(generator) / 10); diff --git a/sources/Runner/GameScene.cpp b/sources/Runner/GameScene.cpp index d4e5a382..6c7f8fd7 100644 --- a/sources/Runner/GameScene.cpp +++ b/sources/Runner/GameScene.cpp @@ -16,8 +16,10 @@ #include "Component/BombHolder/BombHolderComponent.hpp" #include "Component/Tag/TagComponent.hpp" #include "Component/Renderer/Drawable3DComponent.hpp" +#include "Component/Shaders/Items/AlphaCtxShaderComponent.hpp" #include "Component/Renderer/Drawable2DComponent.hpp" #include +#include "Component/Shaders/ShaderComponent.hpp" #include "Drawables/Texture.hpp" #include "Component/Gravity/GravityComponent.hpp" #include "Component/BumperTimer/BumperTimerComponent.hpp" @@ -66,6 +68,44 @@ namespace BBM .addComponent("assets/player/player.iqm", 3) .addComponent(BBM::Vector3f{0.25, 0, 0.25}, BBM::Vector3f{.75, 2, .75}) .addComponent() + .addComponent() + .addComponent("assets/shaders/alpha.fs", "", [](WAL::Entity &myEntity, WAL::Wal &wal, std::chrono::nanoseconds dtime) { + auto &ctx = myEntity.getComponent(); + + ctx.clock += dtime; + if (duration_cast(ctx.clock).count() <= 10) + return; + ctx.clock = 0ns; + auto &bonus = myEntity.getComponent(); + auto &shader = myEntity.getComponent(); + + if (!bonus.isNoClipOn) { + ctx.alpha = ctx.maxAlpha; + shader.shader.setShaderUniformVar("alpha", ctx.alpha); + return; + } + + auto nbMilliSec = duration_cast(bonus.nextNoClipRate).count(); + + if (nbMilliSec > 1000) { + ctx.step = ctx.initalStepValue; + } else if (nbMilliSec > 500) { + ctx.step = 0.15; + } else if (nbMilliSec > 100) { + ctx.step = 0.30; + } else { + ctx.step = 0.5; + } + ctx.alpha += static_cast(ctx.step * ctx.balance); + + if (ctx.alpha <= ctx.minAlpha) { + ctx.balance = 1; + } + if (ctx.alpha >= ctx.maxAlpha) { + ctx.balance = -1; + } + shader.shader.setShaderUniformVar("alpha", ctx.alpha); + }, true) .addComponent(soundPath) .addComponent("assets/musics/music_battle.ogg") .addComponent()