From e51044a8872a05313fa92cad15eedf1f93f26aee Mon Sep 17 00:00:00 2001 From: Aki Date: Mon, 25 Apr 2022 02:02:11 +0200 Subject: Moved flash to own class and used it as transition to death screen --- CMakeLists.txt | 1 + Flash.cpp | 37 +++++++++++++++++++++++++++++++++++++ Flash.h | 15 +++++++++++++++ OverScreen.cpp | 5 ++++- OverScreen.h | 2 ++ TestStage.cpp | 9 +++------ TestStage.h | 3 ++- 7 files changed, 64 insertions(+), 8 deletions(-) create mode 100644 Flash.cpp create mode 100644 Flash.h diff --git a/CMakeLists.txt b/CMakeLists.txt index c3e431d..772f0d6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,6 +23,7 @@ add_executable( EnemyFactory.cpp Falling.cpp FallingAndOscillating.cpp + Flash.cpp Game.cpp GameScreen.cpp Generator.cpp diff --git a/Flash.cpp b/Flash.cpp new file mode 100644 index 0000000..ba9f836 --- /dev/null +++ b/Flash.cpp @@ -0,0 +1,37 @@ +#include "Flash.h" + +#include + + +Flash::Flash() : + m_color {250, 190, 130, 255}, + m_duration {0.23}, + m_flash {0} +{ +} + + +void +Flash::update(const float dt) +{ + if (m_flash > 0) + m_flash -= dt; +} + + +void +Flash::draw() +{ + if (m_flash > 0) { + Color color = m_color; + color.a *= m_flash / m_duration; + DrawRectangle(0, 0, 800, 600, color); + } +} + + +void +Flash::start() +{ + m_flash = m_duration; +} diff --git a/Flash.h b/Flash.h new file mode 100644 index 0000000..58cd547 --- /dev/null +++ b/Flash.h @@ -0,0 +1,15 @@ +#pragma once + +#include + + +struct Flash +{ + Flash(); + void update(float dt); + void draw(); + void start(); + Color m_color; + float m_duration; + float m_flash; +}; diff --git a/OverScreen.cpp b/OverScreen.cpp index 844a9dd..dcf5866 100644 --- a/OverScreen.cpp +++ b/OverScreen.cpp @@ -18,12 +18,14 @@ OverScreen::OverScreen(std::shared_ptr stats) : const int screen_width = GetScreenWidth(); const int died_width = MeasureText(DIED, 20); m_died_x = (screen_width - died_width) / 2; + m_flash.start(); } void -OverScreen::update(const float) +OverScreen::update(const float dt) { + m_flash.update(dt); if (IsKeyPressed(KEY_SPACE)) g_game.set(std::make_unique()); } @@ -34,4 +36,5 @@ OverScreen::draw() { ClearBackground(BLACK); DrawText(DIED, m_died_x, 160, 20, RED); + m_flash.draw(); } diff --git a/OverScreen.h b/OverScreen.h index 3dd7fa2..dc7ab1a 100644 --- a/OverScreen.h +++ b/OverScreen.h @@ -2,6 +2,7 @@ #include +#include "Flash.h" #include "Screen.h" #include "Stats.h" @@ -15,4 +16,5 @@ public: private: std::shared_ptr m_stats; int m_died_x; + Flash m_flash; }; diff --git a/TestStage.cpp b/TestStage.cpp index ccc1f35..b851b0b 100644 --- a/TestStage.cpp +++ b/TestStage.cpp @@ -8,7 +8,6 @@ static constexpr Color DEEPSPACE {3, 5, 22, 255}; -static constexpr float FLASH {0.23f}; TestStage::TestStage() : @@ -38,14 +37,13 @@ TestStage::TestStage() : void TestStage::update(const float dt) { - if (m_flash > 0) - m_flash -= dt; + m_flash.update(dt); m_player.update(dt); for (auto& enemy : m_enemies) enemy.update(dt); m_const.update(dt); bool collided = m_player.collide(m_const.m_bullets); - if (collided) m_flash = FLASH; + if (collided) m_flash.start(); if (m_stats) { m_stats->total_bullets = m_const.m_bullets.size(); if (collided) m_stats->lifes--; @@ -61,6 +59,5 @@ TestStage::draw() for (auto& enemy : m_enemies) enemy.draw(); m_player.draw(); - if (m_flash > 0) - DrawRectangle(0, 0, 800, 600, Color{250, 190, 130, static_cast(m_flash / FLASH * 250)}); + m_flash.draw(); } diff --git a/TestStage.h b/TestStage.h index 3398a89..44a1967 100644 --- a/TestStage.h +++ b/TestStage.h @@ -4,6 +4,7 @@ #include "ConstantVelocity.h" #include "Enemy.h" +#include "Flash.h" #include "Player.h" #include "Stage.h" @@ -17,5 +18,5 @@ public: private: ConstantVelocitySystem m_const; std::vector m_enemies; - float m_flash; + Flash m_flash; }; -- cgit v1.1