master
ブランチ master
Tetris / src / scene / gameover.cc
6.2 KB、163行 生 Blame 履歴
1/**
2Copyright (c) テクニカル諏訪子
3
4Permission is hereby granted to any person obtaining a copy of the software
5Tetris (the "Software") to use, modify, merge, copy, publish, distribute,
6sublicense, and/or sell copies of the Software, subject to the following conditions:
7
8 1. **Origin Attribution**:
9 - You must not misrepresent the origin of the Software; you must not claim
10 you created the original Software.
11 - If the Software is used in a product, you must either:
12 a. Provide clear attribution in the product's documentation, user interface,
13 or other visible areas, **OR**
14 b. Pay the original developers a fee they specify in writing.
15 2. **Usage Restriction**:
16 - The Software, or any derivative works, dependencies, or libraries
17 incorporating it, must not be used for censorship or to suppress freedom of
18 speech, expression, or creativity. Prohibited uses include, but are not
19 limited to:
20 - Censorship of so-called "hate speech", visuals, non-mainstream opinions,
21 ideas, or objective reality.
22 - Tools or systems designed to restrict access to information or
23 artistic works.
24 3. **Notice Preservation**:
25 - This license and the above copyright notice must remain intact in all copies
26 of the source code.
27
28THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
29INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
30PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
31HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
32CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
33OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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
49namespace 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} // namespace tetris::scene