| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | #include <tetris/scene/gameover.hh> |
| 36 | #include <tetris/sys/imgman.hh> |
| 37 | #include <tetris/sys/assman.hh> |
| 38 | #include <tetris/sys/sndman.hh> |
| 39 | #include <tetris/sys/save.hh> |
| 40 | #include <raylib.h> |
| 41 | |
| 42 | #include <tetris/ass/bgm_gameover.hh> |
| 43 | #include <tetris/ass/sfx_gameover.hh> |
| 44 | |
| 45 | #include <cassert> |
| 46 | #include <iostream> |
| 47 | #include <cmath> |
| 48 | |
| 49 | namespace tetris::scene { |
| 50 | constexpr f32 MinTimePassed = 5.f; |
| 51 | |
| 52 | void GameOver::Init(Game *game) { |
| 53 | m_Game = game; |
| 54 | auto &snd = sys::SndMan::Get(); |
| 55 | if (!snd.LoadBgm("bgm_gameover", ass_gameover_bgm_ogg, ass_gameover_bgm_ogg_len, ass_gameover_bgm_ogg, ass_gameover_bgm_ogg_len)) { |
| 56 | TraceLog(LOG_WARNING, "Failed to load BGM!"); |
| 57 | } else { |
| 58 | snd.PlayBgm("bgm_gameover"); |
| 59 | } |
| 60 | |
| 61 | snd.LoadSfx("sfx_confirm", __ass_confirm_ogg, __ass_confirm_ogg_len); |
| 62 | snd.LoadSfx("sfx_quit", __ass_quit_ogg, __ass_quit_ogg_len); |
| 63 | snd.LoadSfx("sfx_select", __ass_select_ogg, __ass_select_ogg_len); |
| 64 | snd.LoadSfx("sfx_gameover", ass_gameover_sfx_ogg, ass_gameover_sfx_ogg_len); |
| 65 | |
| 66 | sys::SndMan::Get().PlaySfx("sfx_gameover"); |
| 67 | |
| 68 | sys::SaveData saveData = sys::GetScore(); |
| 69 | m_CurScore = game->GetScore(); |
| 70 | m_HighScore = m_CurScore > saveData.highScore ? m_CurScore : saveData.highScore; |
| 71 | sys::SaveScore(game->GetScore(), game->GetLines(), game->GetLevel()); |
| 72 | } |
| 73 | |
| 74 | void GameOver::FuncUpdate() { |
| 75 | m_InputTimer += GetFrameTime(); |
| 76 | #ifndef NDEBUG |
| 77 | DrawText(TextFormat("Timer: %.2f", m_InputTimer), 10, 10, 20, MAGENTA); |
| 78 | #endif |
| 79 | if (m_InputTimer > MinTimePassed) handleInput(); |
| 80 | sys::SndMan::Get().Update(GetFrameTime()); |
| 81 | } |
| 82 | |
| 83 | void GameOver::DrawUpdate() { |
| 84 | DrawRectangleRec({ 0, 0, (f32)GetScreenWidth(), (f32)GetScreenHeight() }, Fade(BLACK, 1.f)); |
| 85 | |
| 86 | Font font = GetFontDefault(); |
| 87 | |
| 88 | string score = "SCORE: " + std::to_string(m_CurScore); |
| 89 | string hScore = "HIGH SCORE: " + std::to_string(m_HighScore); |
| 90 | int textWidthGO = MeasureText("GAME OVER", 36.f); |
| 91 | int textWidthSC = MeasureText(score.c_str(), 24.f); |
| 92 | int textWidthHS = MeasureText(hScore.c_str(), 24.f); |
| 93 | |
| 94 | DrawText("GAME OVER", GetScreenWidth() / 2 - textWidthGO / 2, GetScreenHeight() / 2 - 300.f + 72.f, 36.f, RED); |
| 95 | DrawText(score.c_str(), GetScreenWidth() / 2 - textWidthSC / 2, GetScreenHeight() / 2 - 300.f + 128.f, 24.f, RED); |
| 96 | DrawText(hScore.c_str(), GetScreenWidth() / 2 - textWidthHS / 2, GetScreenHeight() / 2 - 300.f + 160.f, 24.f, RED); |
| 97 | |
| 98 | Vector2 menuPos = { |
| 99 | GetScreenWidth() / 2.f, |
| 100 | GetScreenHeight() / 2.f + 20.f, |
| 101 | }; |
| 102 | |
| 103 | constexpr f32 spacing = 40.f; |
| 104 | |
| 105 | auto DrawButton = [&](Font font, cstr text, Vector2 pos, bool selected) { |
| 106 | f32 scale = selected ? 1.05f + 0.03f * std::sinf(GetTime() * 8.f) : 1.f; |
| 107 | Color canColor = m_InputTimer > MinTimePassed ? RED : GRAY; |
| 108 | Color color = selected ? canColor : Fade(canColor, 0.5f); |
| 109 | f32 sizePx = 20.f * scale; |
| 110 | Vector2 textSize = MeasureTextEx(font, text, sizePx, 0); |
| 111 | |
| 112 | DrawTextPro(font, text, pos, { textSize.x / 2.f, textSize.y / 2.f }, 0.f, sizePx, 2.f, color); |
| 113 | if (selected) DrawTextPro(font, ">", { pos.x - textSize.x / 2.f - 30.f, pos.y }, { 0.f, textSize.y / 2.f }, 0.f, sizePx, 4.f, color); |
| 114 | }; |
| 115 | |
| 116 | DrawButton(font, "RETRY", { menuPos.x, menuPos.y + spacing * 0 }, m_Selection == Selection::Retry); |
| 117 | DrawButton(font, "QUIT", { menuPos.x, menuPos.y + spacing * 1 }, m_Selection == Selection::Quit); |
| 118 | } |
| 119 | |
| 120 | void GameOver::Close() { |
| 121 | sys::SndMan::Get().StopBgm(); |
| 122 | m_Selection = Selection::Retry; |
| 123 | m_CurSelect = Selection::Retry; |
| 124 | m_HighScore = 0; |
| 125 | m_CurScore = 0; |
| 126 | m_InputTimer = 0.f; |
| 127 | } |
| 128 | |
| 129 | |
| 130 | |
| 131 | void GameOver::handleInput() { |
| 132 | switch (m_Selection) { |
| 133 | case Selection::Retry: { |
| 134 | if (IsKeyPressed(KEY_DOWN) || IsKeyPressed(KEY_S)) { |
| 135 | sys::SndMan::Get().PlaySfx("sfx_select"); |
| 136 | m_Selection = Selection::Quit; |
| 137 | } |
| 138 | |
| 139 | if (IsKeyPressed(KEY_ENTER)) { |
| 140 | sys::SndMan::Get().PlaySfx("sfx_confirm"); |
| 141 | assert(m_Game != nullptr); |
| 142 | m_Game->SetNextScene(Scene::GameMain); |
| 143 | } |
| 144 | break; |
| 145 | } |
| 146 | case Selection::Quit: { |
| 147 | if (IsKeyPressed(KEY_UP) || IsKeyPressed(KEY_W)) { |
| 148 | sys::SndMan::Get().PlaySfx("sfx_select"); |
| 149 | m_Selection = Selection::Retry; |
| 150 | } |
| 151 | |
| 152 | if (IsKeyPressed(KEY_ENTER)) { |
| 153 | sys::SndMan::Get().PlaySfx("sfx_quit"); |
| 154 | assert(m_Game != nullptr); |
| 155 | m_Game->SetNextScene(Scene::Title); |
| 156 | } |
| 157 | break; |
| 158 | } |
| 159 | default: |
| 160 | break; |
| 161 | } |
| 162 | } |
| 163 | } |