master
ブランチ master
Tetris / src / scene / gamemain.cc
31.8 KB、968行 生 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/gamemain.hh>
36#include <tetris/sys/imgman.hh>
37#include <tetris/sys/sndman.hh>
38#include <tetris/sys/assman.hh>
39#include <raylib.h>
40#include <cmath>
41#include <algorithm>
42
43#include <tetris/ass/tex_block.hh>
44#include <tetris/ass/bg_beach.hh>
45#include <tetris/ass/bg_city.hh>
46#include <tetris/ass/bg_mount.hh>
47#include <tetris/ass/bg_road.hh>
48#include <tetris/ass/bg_sunset.hh>
49#include <tetris/ass/bg_waterfall.hh>
50#include <tetris/ass/vfx_smoke.hh>
51#include <tetris/ass/vfx_sparkle.hh>
52
53#include <tetris/ass/bgm_tetorisu_start.hh>
54#include <tetris/ass/bgm_tetorisu_loop.hh>
55
56namespace tetris::scene {
57 i8 levelUpThreshold = 10;
58
59 Color tetrominoColors[7] = {
60 BLUE,
61 YELLOW,
62 GREEN,
63 RED,
64 PURPLE,
65 ORANGE,
66 SKYBLUE,
67 };
68
69 // [ピース][回転][4ブロック][x,y]
70 static const i32 tetrominoShapes[7][4][4][2] = {
71 {{{0,1},{1,1},{2,1},{3,1}}, {{2,0},{2,1},{2,2},{2,3}}, {{0,2},{1,2},{2,2},{3,2}}, {{1,0},{1,1},{1,2},{1,3}}}, // I
72 {{{0,0},{1,0},{0,1},{1,1}}, {{0,0},{1,0},{0,1},{1,1}}, {{0,0},{1,0},{0,1},{1,1}}, {{0,0},{1,0},{0,1},{1,1}}}, // O
73 {{{1,0},{2,0},{0,1},{1,1}}, {{0,0},{0,1},{1,1},{1,2}}, {{1,0},{2,0},{0,1},{1,1}}, {{0,0},{0,1},{1,1},{1,2}}}, // S
74 {{{0,0},{1,0},{1,1},{2,1}}, {{1,0},{0,1},{1,1},{0,2}}, {{0,0},{1,0},{1,1},{2,1}}, {{1,0},{0,1},{1,1},{0,2}}}, // Z
75 {{{1,0},{0,1},{1,1},{2,1}}, {{1,0},{2,1},{1,2},{1,1}}, {{0,1},{1,1},{2,1},{1,2}}, {{1,0},{1,1},{1,2},{0,1}}}, // T
76 {{{0,1},{1,1},{2,1},{2,0}}, {{0,0},{0,1},{0,2},{1,2}}, {{0,0},{1,0},{2,0},{0,1}}, {{0,0},{1,0},{1,1},{1,2}}}, // L
77 {{{0,0},{0,1},{1,1},{2,1}}, {{0,0},{0,1},{0,2},{1,0}}, {{0,0},{1,0},{2,0},{2,1}}, {{1,0},{1,1},{1,2},{0,2}}}, // J
78 };
79
80 Vector2 boardPos = { 0, 0 };
81 Texture2D blockTexture;
82
83 i8 nextPiece = 0;
84 Vector2 nextPos = { 0, 0 };
85 f32 nextSquareSize = 0.f;
86
87 Vector2 holdPos = { 0, 0 };
88 f32 holdSquareSize;
89
90 std::vector<std::vector<i32>> board = {};
91
92 void GameMain::Init(Game *game) {
93 m_Score = 0;
94 m_Level = 1;
95 m_Lines = 0;
96
97 m_Game = game;
98 boardPos = {
99 .x = (GetScreenWidth() - m_HSize * m_SquareSize) / 2.f - 150,
100 .y = (GetScreenHeight() - m_VSize * m_SquareSize) / 2.f,
101 };
102
103 board.assign(m_VSize, std::vector<i32>(m_HSize, 0));
104
105 blockTexture = sys::ImgMan::Get().LoadTexture("tx_block", __ass_tetris_block_png, __ass_tetris_block_png_len);
106 sys::ImgMan::Get().LoadTexture("bg_beach", __ass_bg_beach_png, __ass_bg_beach_png_len);
107 sys::ImgMan::Get().LoadTexture("bg_city", __ass_bg_city_png, __ass_bg_city_png_len);
108 sys::ImgMan::Get().LoadTexture("bg_mount", __ass_bg_mount_png, __ass_bg_mount_png_len);
109 sys::ImgMan::Get().LoadTexture("bg_road", __ass_bg_road_png, __ass_bg_road_png_len);
110 sys::ImgMan::Get().LoadTexture("bg_sunset", __ass_bg_sunset_png, __ass_bg_sunset_png_len);
111 sys::ImgMan::Get().LoadTexture("bg_waterfall", __ass_bg_waterfall_png, __ass_bg_waterfall_png_len);
112 sys::ImgMan::Get().SetBackground(m_Bg[0]);
113
114 m_Sparkles.push_back(sys::ImgMan::Get().LoadTexture("vfx_sparkle1", __ass_sparkle1_png, __ass_sparkle1_png_len));
115 m_Sparkles.push_back(sys::ImgMan::Get().LoadTexture("vfx_sparkle2", __ass_sparkle2_png, __ass_sparkle2_png_len));
116 m_Sparkles.push_back(sys::ImgMan::Get().LoadTexture("vfx_sparkle3", __ass_sparkle3_png, __ass_sparkle3_png_len));
117 m_Sparkles.push_back(sys::ImgMan::Get().LoadTexture("vfx_sparkle4", __ass_sparkle4_png, __ass_sparkle4_png_len));
118 m_Sparkles.push_back(sys::ImgMan::Get().LoadTexture("vfx_sparkle5", __ass_sparkle5_png, __ass_sparkle5_png_len));
119 m_Sparkles.push_back(sys::ImgMan::Get().LoadTexture("vfx_sparkle6", __ass_sparkle6_png, __ass_sparkle6_png_len));
120 m_Sparkles.push_back(sys::ImgMan::Get().LoadTexture("vfx_sparkle7", __ass_sparkle7_png, __ass_sparkle7_png_len));
121 m_Sparkles.push_back(sys::ImgMan::Get().LoadTexture("vfx_sparkle8", __ass_sparkle8_png, __ass_sparkle8_png_len));
122 m_Sparkles.push_back(sys::ImgMan::Get().LoadTexture("vfx_sparkle9", __ass_sparkle9_png, __ass_sparkle9_png_len));
123 m_Sparkles.push_back(sys::ImgMan::Get().LoadTexture("vfx_sparkle10", __ass_sparkle10_png, __ass_sparkle10_png_len));
124 m_Sparkles.push_back(sys::ImgMan::Get().LoadTexture("vfx_sparkle11", __ass_sparkle11_png, __ass_sparkle11_png_len));
125 m_Sparkles.push_back(sys::ImgMan::Get().LoadTexture("vfx_sparkle12", __ass_sparkle12_png, __ass_sparkle12_png_len));
126 m_Sparkles.push_back(sys::ImgMan::Get().LoadTexture("vfx_sparkle13", __ass_sparkle13_png, __ass_sparkle13_png_len));
127 m_Sparkles.push_back(sys::ImgMan::Get().LoadTexture("vfx_sparkle14", __ass_sparkle14_png, __ass_sparkle14_png_len));
128 m_Sparkles.push_back(sys::ImgMan::Get().LoadTexture("vfx_sparkle15", __ass_sparkle15_png, __ass_sparkle15_png_len));
129 m_Sparkles.push_back(sys::ImgMan::Get().LoadTexture("vfx_sparkle16", __ass_sparkle16_png, __ass_sparkle16_png_len));
130
131 m_Smokes.push_back(sys::ImgMan::Get().LoadTexture("vfx_smoke1", __ass_whitePuff01_png, __ass_whitePuff01_png_len));
132 m_Smokes.push_back(sys::ImgMan::Get().LoadTexture("vfx_smoke2", __ass_whitePuff02_png, __ass_whitePuff02_png_len));
133 m_Smokes.push_back(sys::ImgMan::Get().LoadTexture("vfx_smoke3", __ass_whitePuff03_png, __ass_whitePuff03_png_len));
134 m_Smokes.push_back(sys::ImgMan::Get().LoadTexture("vfx_smoke4", __ass_whitePuff04_png, __ass_whitePuff04_png_len));
135 m_Smokes.push_back(sys::ImgMan::Get().LoadTexture("vfx_smoke5", __ass_whitePuff05_png, __ass_whitePuff05_png_len));
136 m_Smokes.push_back(sys::ImgMan::Get().LoadTexture("vfx_smoke6", __ass_whitePuff06_png, __ass_whitePuff06_png_len));
137 m_Smokes.push_back(sys::ImgMan::Get().LoadTexture("vfx_smoke7", __ass_whitePuff07_png, __ass_whitePuff07_png_len));
138 m_Smokes.push_back(sys::ImgMan::Get().LoadTexture("vfx_smoke8", __ass_whitePuff08_png, __ass_whitePuff08_png_len));
139 m_Smokes.push_back(sys::ImgMan::Get().LoadTexture("vfx_smoke9", __ass_whitePuff09_png, __ass_whitePuff09_png_len));
140 m_Smokes.push_back(sys::ImgMan::Get().LoadTexture("vfx_smoke10", __ass_whitePuff10_png, __ass_whitePuff10_png_len));
141 m_Smokes.push_back(sys::ImgMan::Get().LoadTexture("vfx_smoke11", __ass_whitePuff11_png, __ass_whitePuff11_png_len));
142 m_Smokes.push_back(sys::ImgMan::Get().LoadTexture("vfx_smoke12", __ass_whitePuff12_png, __ass_whitePuff12_png_len));
143 m_Smokes.push_back(sys::ImgMan::Get().LoadTexture("vfx_smoke13", __ass_whitePuff13_png, __ass_whitePuff13_png_len));
144 m_Smokes.push_back(sys::ImgMan::Get().LoadTexture("vfx_smoke14", __ass_whitePuff14_png, __ass_whitePuff14_png_len));
145 m_Smokes.push_back(sys::ImgMan::Get().LoadTexture("vfx_smoke15", __ass_whitePuff15_png, __ass_whitePuff15_png_len));
146 m_Smokes.push_back(sys::ImgMan::Get().LoadTexture("vfx_smoke16", __ass_whitePuff16_png, __ass_whitePuff16_png_len));
147
148 auto &snd = sys::SndMan::Get();
149 if (!snd.LoadBgm("bgm_tetorisu", __ass_tetorisu_start_ogg, __ass_tetorisu_start_ogg_len, __ass_tetorisu_loop_ogg, __ass_tetorisu_loop_ogg_len)) {
150 TraceLog(LOG_WARNING, "Failed to load BGM!");
151 } else {
152 snd.PlayBgm("bgm_tetorisu");
153 }
154 snd.LoadSfx("sfx_drop", __ass_drop_ogg, __ass_drop_ogg_len);
155 snd.LoadSfx("sfx_line_clear", __ass_line_clear_ogg, __ass_line_clear_ogg_len);
156 snd.LoadSfx("sfx_level_up", __ass_level_up_ogg, __ass_level_up_ogg_len);
157 snd.LoadSfx("sfx_hold_block", __ass_hold_block_ogg, __ass_hold_block_ogg_len);
158 snd.LoadSfx("sfx_countdown", __ass_countdown_ogg, __ass_countdown_ogg_len);
159 snd.LoadSfx("sfx_confirm", __ass_confirm_ogg, __ass_confirm_ogg_len);
160 snd.LoadSfx("sfx_pause", __ass_pause_ogg, __ass_pause_ogg_len);
161 snd.LoadSfx("sfx_quit", __ass_quit_ogg, __ass_quit_ogg_len);
162 snd.LoadSfx("sfx_resume", __ass_resume_ogg, __ass_resume_ogg_len);
163 snd.LoadSfx("sfx_select", __ass_select_ogg, __ass_select_ogg_len);
164 sys::SndMan::Get().PlaySfx("sfx_countdown");
165
166 nextPiece = GetRandomValue(0, 6);
167 spawn();
168 updateBoardLayout();
169 }
170
171 void GameMain::FuncUpdate() {
172 if (m_State == GameState::Countdown) {
173 countdown();
174 return;
175 }
176
177 if (m_GameOver) {
178 m_Game->SetNextScene(Scene::GameOver);
179 return;
180 }
181
182 if (m_State == GameState::Playing && IsKeyPressed(KEY_ESCAPE)) {
183 m_State = GameState::Paused;
184 m_PauseSelect = PauseOption::Resume;
185
186 sys::SndMan::Get().PlaySfx("sfx_pause");
187 }
188
189 if (m_State == GameState::Paused) {
190 pause();
191 return;
192 }
193
194 updateBoardLayout();
195 handleInput();
196 updateLandingEffects();
197 updateClearEffects();
198 updateHoldAnimation();
199 sys::SndMan::Get().Update(GetFrameTime());
200
201 m_FallTimer += GetFrameTime();
202
203 f32 curFallSpeed = m_FallSpeed;
204 if (IsKeyDown(KEY_DOWN) || IsKeyDown(KEY_S)) {
205 curFallSpeed = .05f;
206 }
207
208 if (m_FallTimer >= curFallSpeed) {
209 if (isValidPos(m_CurPiece, m_CurRot, m_CurX, m_CurY + 1)) {
210 m_CurY++;
211 } else {
212 lock();
213 }
214
215 m_FallTimer = 0.f;
216 }
217
218 f32 target = targetDangerPitch();
219 m_DangerPitch = std::lerp(m_DangerPitch, target, GetFrameTime() * 3.5f);
220
221 sys::SndMan::Get().SetBgmPitch(m_DangerPitch);
222 }
223
224 void GameMain::DrawUpdate() {
225 sys::ImgMan::Get().DrawBackground();
226
227 Rectangle gameArea = {
228 boardPos.x - 160,
229 boardPos.y - 30,
230 (f32)m_HSize * (f32)m_SquareSize + 380,
231 (f32)m_VSize * (f32)m_SquareSize + 60,
232 };
233 DrawRectangleRec(gameArea, Fade(BLACK, 0.65f));
234
235 i32 titleSize = std::max(30, (i32)(m_SquareSize * 1.2f));
236 DrawText("TETRIS", GetScreenWidth() / 2 - titleSize * 2, 30, titleSize, WHITE);
237
238#ifndef NDEBUG
239 if (m_ShakeTime > 0.f) {
240 DrawText(TextFormat("SHAKE: %.2f", m_ShakeTime), 20, 20, 20, RED);
241 }
242 DrawText(TextFormat("Height: %d Pitch: %.2f", stackHeight(), m_DangerPitch), 10, 10, 20, WHITE);
243#endif
244
245 Vector2 drawPos = boardPos;
246 if (m_ShakeTime > 0.f) {
247 drawPos.x += m_ShakeOffset.x;
248 drawPos.y += m_ShakeOffset.y;
249 }
250
251 Rectangle rect = {
252 .x = drawPos.x - 5,
253 .y = drawPos.y - 5,
254 .width = (f32)(m_HSize * m_SquareSize + 10),
255 .height = (f32)(m_VSize * m_SquareSize + 10),
256 };
257 DrawRectangleLinesEx(rect, 5, LIGHTGRAY);
258
259 // グリッド
260 for (u8 j = 0; j < m_VSize; ++j) {
261 for (u8 i = 0; i < m_HSize; ++i) {
262 Color bCol = tetrominoColors[(board[j][i] - 1) % 7];
263 Rectangle rect = {
264 .x = drawPos.x + i * (f32)m_SquareSize,
265 .y = drawPos.y + j * (f32)m_SquareSize,
266 .width = (f32)m_SquareSize,
267 .height = (f32)m_SquareSize,
268 };
269
270 if (board[j][i] > 0) {
271 DrawTexturePro(blockTexture, { 0, 0, (f32)blockTexture.width, (f32)blockTexture.height }, rect, { 0, 0 }, 0.f, bCol);
272 DrawRectangleLines(rect.x, rect.y, rect.width, rect.height, DARKGRAY);
273 } else {
274 DrawRectangleLines(rect.x, rect.y, rect.width, rect.height, Fade(GRAY, .3f));
275 }
276 }
277 }
278
279 // VFX
280 drawLandingEffects();
281 drawClearEffects(drawPos);
282
283 // ゴースト
284 drawGhostPiece(drawPos);
285
286 // 持ち
287 drawHoldAnimation();
288
289 // 現在のピース
290 Color pieceCol = tetrominoColors[m_CurPiece];
291
292 if (m_HoldAnim == HoldAnimState::Beam) {
293 f32 t = std::clamp(m_HoldAnimTimer / 0.15f, 0.0f, 1.0f);
294 pieceCol.a = static_cast<u8>(255.0f * (1.0f - t));
295 }
296
297 for (i8 i = 0; i < 4; ++i) {
298 i8 px = m_CurX + tetrominoShapes[m_CurPiece][m_CurRot][i][0];
299 i8 py = m_CurY + tetrominoShapes[m_CurPiece][m_CurRot][i][1];
300
301 if (py >= 0 && px >= 0 && px < m_HSize && py < m_VSize) {
302 Rectangle rect = {
303 .x = drawPos.x + px * (f32)m_SquareSize,
304 .y = drawPos.y + py * (f32)m_SquareSize,
305 .width = (f32)m_SquareSize,
306 .height = (f32)m_SquareSize,
307 };
308
309 f32 scale = 1.0f;
310
311 if (m_HoldAnim == HoldAnimState::Beam) {
312 f32 t = std::clamp(m_HoldAnimTimer / 0.15f, 0.0f, 1.0f);
313 scale = std::lerp(1.0f, 0.85f, t);
314 }
315
316 f32 cx = rect.x + rect.width * 0.5f;
317 f32 cy = rect.y + rect.height * 0.5f;
318
319 rect.width *= scale;
320 rect.height *= scale;
321
322 rect.x = cx - rect.width * 0.5f;
323 rect.y = cy - rect.height * 0.5f;
324
325 DrawTexturePro(blockTexture, { 0, 0, (f32)blockTexture.width, (f32)blockTexture.height }, rect, { 0, 0 }, 0.f, pieceCol);
326 DrawRectangleLines(rect.x, rect.y, rect.width, rect.height, BLACK);
327 }
328 }
329
330 // 次のピース
331 Rectangle nextPanel = {
332 .x = nextPos.x - 10,
333 .y = nextPos.y - 10,
334 .width = 4 * nextSquareSize + 20,
335 .height = 4 * nextSquareSize + 20,
336 };
337
338 DrawRectangleRec(nextPanel, DARKGRAY);
339 DrawRectangleLinesEx(nextPanel, 3, LIGHTGRAY);
340 DrawText("NEXT", nextPos.x + 10, nextPos.y - 30, 20, WHITE);
341
342 drawNextPiece();
343
344 // 持ち
345 Rectangle holdPanel = {
346 .x = holdPos.x - 10,
347 .y = holdPos.y - 10,
348 .width = 4 * holdSquareSize + 20,
349 .height = 4 * holdSquareSize + 20,
350 };
351
352 DrawRectangleRec(holdPanel, DARKGRAY);
353 DrawRectangleLinesEx(holdPanel, 3, LIGHTGRAY);
354 DrawText("HOLD", holdPos.x + 10, holdPos.y - 45, 20, WHITE);
355
356 drawHoldPiece();
357
358 f32 uiX = holdPos.x;
359 f32 uiY = holdPos.y + 120;
360
361 DrawText(TextFormat("SCORE"), uiX, uiY, 24, WHITE);
362 DrawText(TextFormat("%08d", m_Score), uiX, uiY + 35, 28, YELLOW);
363
364 DrawText(TextFormat("LEVEL"), uiX, uiY + 90, 24, WHITE);
365 DrawText(TextFormat("%02d", m_Level), uiX, uiY + 125, 32, ORANGE);
366
367 DrawText(TextFormat("LINES"), uiX, uiY + 180, 24, WHITE);
368 DrawText(TextFormat("%04d", m_Lines), uiX, uiY + 215, 28, SKYBLUE);
369
370 if (m_State == GameState::Countdown) {
371 cstr text;
372
373 switch (m_CountValue) {
374 case 3: text = "3"; break;
375 case 2: text = "2"; break;
376 case 1: text = "1"; break;
377 case 0: text = "GO!!"; break;
378 default: text = ""; break;
379 };
380
381 Font font = GetFontDefault();
382 Vector2 size = MeasureTextEx(font, text, 120.f * m_TextScale, 0.f);
383 DrawTextPro(font, text, { GetScreenWidth() / 2.f, GetScreenHeight() / 2.f }, { size.x / 2.f, size.y / 2.f }, 0.f, 120 * m_TextScale, 4.f, Fade(WHITE, m_TextAlpha));
384 }
385
386 if (m_State == GameState::Paused) {
387 DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(BLACK, .55f));
388 m_PauseAlpha = std::min(m_PauseAlpha + GetFrameTime() * 6.f, 1.f);
389
390 Font font = GetFontDefault();
391 cstr title = "PAUSE";
392 f32 titleSize = 60.f;
393 Vector2 size = MeasureTextEx(font, title, titleSize, 0);
394
395 DrawTextPro(font, title, { GetScreenWidth() / 2.f, GetScreenHeight() / 2.f - 100.f }, { size.x / 2.f, size.y / 2.f }, 0.f, titleSize, 4.f, WHITE);
396 Vector2 resumePos = {
397 GetScreenWidth() / 2.f - 120.f,
398 GetScreenHeight() / 2.f + 20.f
399 };
400
401 Vector2 quitPos = {
402 GetScreenWidth() / 2.f + 120.f,
403 GetScreenHeight() / 2.f + 20.f
404 };
405
406 auto DrawButton = [](Font font, cstr text, Vector2 pos, bool selected) {
407 f32 scale = selected ? 1.05f + 0.03f * sinf(GetTime() * 8.f) : 1.f;
408 Color color = selected ? WHITE : Fade(WHITE, 0.5f);
409 f32 sizePx = 40.f * scale;
410 Vector2 textSize = MeasureTextEx(font, text, sizePx, 0);
411
412 DrawTextPro(font, text, pos, { textSize.x / 2.f, textSize.y / 2.f }, 0.f, sizePx, 2.f, color);
413 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);
414 };
415
416 DrawButton(font, "RESUME", resumePos, m_PauseSelect == PauseOption::Resume);
417 DrawButton(font, "QUIT", quitPos, m_PauseSelect == PauseOption::Quit);
418 }
419 }
420
421 void GameMain::Close() {
422 sys::SndMan::Get().StopBgm();
423 sys::ImgMan::Get().UnloadAll();
424 m_State = GameState::Countdown;
425 m_CurBg = 0;
426 m_FallTimer = 0.f;
427 m_FallSpeed = .8f;
428 m_GameOver = false;
429 m_CanHold = true;
430 m_Gravity = 30;
431 m_HSize = 10;
432 m_VSize = 20;
433 m_SquareSize = 20;
434 m_Hold = -1;
435 m_CurPiece = 0;
436 m_CurRot = 0;
437 m_CurX = 0;
438 m_CurY = 0;
439 m_ShakeTime = 0.f;
440 m_HoldAnimTimer = 0.f;
441 m_Countdown = 4.f;
442 m_CountValue = 3;
443 m_TextScale = 1.f;
444 m_TextAlpha = 1.f;
445 m_PauseAlpha = 0.f;
446 m_DangerPitch = 1.f;
447 m_Particles.clear();
448 m_ClearEffects.clear();
449 m_Sparkles.clear();
450 m_Smokes.clear();
451 }
452
453 //////////
454
455 void GameMain::spawn() {
456 m_CurPiece = nextPiece;
457 nextPiece = GetRandomValue(0, 6);
458
459 m_CurRot = 0;
460 m_CurX = m_HSize / 2 - 2;
461 m_CurY = 0;
462 m_CanHold = true;
463
464 if (!isValidPos(m_CurPiece, m_CurRot, m_CurX, m_CurY)) {
465 m_GameOver = true;
466 }
467 }
468
469 void GameMain::lock() {
470 for (i8 i = 0; i < 4; ++i) {
471 i8 px = m_CurX + tetrominoShapes[m_CurPiece][m_CurRot][i][0];
472 i8 py = m_CurY + tetrominoShapes[m_CurPiece][m_CurRot][i][1];
473 if (py >= 0) board[py][px] = m_CurPiece + 1;
474 }
475
476 clearLines();
477 spawn();
478 }
479
480 void GameMain::updateBoardLayout() {
481 i32 screenW = GetScreenWidth();
482 i32 screenH = GetScreenHeight();
483
484 f32 maxW = (screenW * .55f) / (f32)m_HSize;
485 f32 maxH = (screenH * .85f) / (f32)m_VSize;
486
487 m_SquareSize = std::min(maxW, maxH);
488 m_SquareSize = std::clamp((f32)m_SquareSize, 20.f, 60.f);
489
490 boardPos.x = (screenW * .5f - m_HSize * m_SquareSize) / 2.f - 40;
491 boardPos.y = (screenH - m_VSize * m_SquareSize) / 2.f;
492
493 nextSquareSize = m_SquareSize * .75f;
494 nextPos = {
495 .x = boardPos.x + m_HSize * m_SquareSize + 80,
496 .y = boardPos.y + 60,
497 };
498
499 holdSquareSize = m_SquareSize * .75f;
500 holdPos = {
501 .x = boardPos.x + m_HSize * m_SquareSize + 80,
502 .y = boardPos.y + 220,
503 };
504 }
505
506 void GameMain::drawNextPiece() {
507 Color col = tetrominoColors[nextPiece];
508
509 for (i8 i = 0; i < 4; ++i) {
510 f32 x = nextPos.x + tetrominoShapes[nextPiece][0][i][0] * nextSquareSize;
511 f32 y = nextPos.y + tetrominoShapes[nextPiece][0][i][1] * nextSquareSize;
512
513 Rectangle rect = { x, y, nextSquareSize, nextSquareSize };
514 DrawTexturePro(blockTexture, { 0, 0, (f32)blockTexture.width, (f32)blockTexture.height }, rect, { 0, 0 }, 0.f, col);
515 DrawRectangleLines(x, y, nextSquareSize, nextSquareSize, BLACK);
516 }
517 }
518
519 void GameMain::drawHoldPiece() {
520 if (m_Hold == -1) return;
521
522 Color col = tetrominoColors[m_Hold];
523
524 for (i8 i = 0; i < 4; ++i) {
525 f32 x = holdPos.x + tetrominoShapes[m_Hold][0][i][0] * holdSquareSize;
526 f32 y = holdPos.y + tetrominoShapes[m_Hold][0][i][1] * holdSquareSize;
527 Rectangle rect = { x, y, holdSquareSize, holdSquareSize };
528
529 DrawTexturePro(blockTexture, { 0, 0, (f32)blockTexture.width, (f32)blockTexture.height }, rect, { 0, 0 }, 0.f, col);
530 DrawRectangleLines(x, y, holdSquareSize, holdSquareSize, BLACK);
531 }
532 }
533
534 void GameMain::drawGhostPiece(Vector2 offset) {
535 i8 ghostY = m_CurY;
536 while (isValidPos(m_CurPiece, m_CurRot, m_CurX, ghostY + 1)) ghostY++;
537
538 Color ghostCol = tetrominoColors[m_CurPiece];
539 ghostCol.a = 90;
540
541 for (i8 i = 0; i < 4; ++i) {
542 i8 px = m_CurX + tetrominoShapes[m_CurPiece][m_CurRot][i][0];
543 i8 py = ghostY + tetrominoShapes[m_CurPiece][m_CurRot][i][1];
544
545 if (py >= 0 && px >= 0 && px < m_HSize && py < m_VSize) {
546 Rectangle rect = {
547 offset.x + px * (f32)m_SquareSize,
548 offset.y + py * (f32)m_SquareSize,
549 (f32)m_SquareSize,
550 (f32)m_SquareSize,
551 };
552
553 DrawTexturePro(blockTexture, { 0, 0, (f32)blockTexture.width, (f32)blockTexture.height }, rect, { 0, 0 }, 0.f, ghostCol);
554 DrawRectangleLines(rect.x, rect.y, rect.width, rect.height, Fade(BLACK, .3f));
555 }
556 }
557 }
558
559 void GameMain::clearLines() {
560 i32 clearLine = 0;
561
562 for (i32 j = m_VSize - 1; j >= 0; --j) {
563 bool isFull = true;
564
565 for (i32 i = 0; i < m_HSize; ++i) {
566 if (board[j][i] == 0) {
567 isFull = false;
568 break;
569 }
570 }
571
572 if (isFull) {
573 createClearEffect(j);
574 sys::SndMan::Get().PlaySfx("sfx_line_clear");
575 clearLine++;
576
577 for (i32 y = j; y > 0; --y)
578 for (i32 x = 0; x < m_HSize; ++x)
579 board[y][x] = board[y - 1][x];
580
581 for (i32 x = 0; x < m_HSize; ++x) board[0][x] = 0;
582
583 j++;
584 }
585 }
586
587 if (clearLine > 0) {
588 m_Lines += clearLine;
589 m_Score += clearLine * clearLine * 100;
590
591 i8 oldLevel = m_Level;
592 m_Level = 1 + (m_Lines / levelUpThreshold);
593
594 if (m_Level > oldLevel) {
595 m_CurBg = (m_Level - 1) % (i8)m_Bg.size();
596 sys::SndMan::Get().PlaySfx("sfx_level_up");
597 sys::ImgMan::Get().SetBackground(m_Bg[m_CurBg]);
598 }
599
600 m_FallSpeed = std::max(.15f, .8f - (m_Level - 1) * .07f);
601 }
602 }
603
604 void GameMain::handleInput() {
605 if (m_GameOver) return;
606
607 if ((IsKeyPressed(KEY_LEFT) || IsKeyPressedRepeat(KEY_LEFT)) || (IsKeyPressed(KEY_A) || IsKeyPressedRepeat(KEY_A))) {
608 if (isValidPos(m_CurPiece, m_CurRot, m_CurX - 1, m_CurY)) {
609 m_CurX--;
610 }
611 }
612
613 if ((IsKeyPressed(KEY_RIGHT) || IsKeyPressedRepeat(KEY_RIGHT)) || (IsKeyPressed(KEY_D) || IsKeyPressedRepeat(KEY_D))) {
614 if (isValidPos(m_CurPiece, m_CurRot, m_CurX + 1, m_CurY)) {
615 m_CurX++;
616 }
617 }
618
619 if (IsKeyPressed(KEY_UP) || IsKeyPressed(KEY_W)) {
620 i8 startY = m_CurY;
621 while (isValidPos(m_CurPiece, m_CurRot, m_CurX, m_CurY + 1)) {
622 m_CurY++;
623 m_Score += 2;
624 }
625
626 if (m_CurY > startY) {
627 createLandingEffect();
628 sys::SndMan::Get().PlaySfx("sfx_drop");
629 }
630
631 lock();
632 m_FallTimer = 0.f;
633 return;
634 }
635
636 if (IsKeyPressed(KEY_SPACE)) {
637 u8 newRot = (m_CurRot + 1) % 4;
638 if (isValidPos(m_CurPiece, newRot, m_CurX, m_CurY)) {
639 m_CurRot = newRot;
640 } else if (isValidPos(m_CurPiece, newRot, m_CurX - 1, m_CurY)) {
641 m_CurX--;
642 m_CurRot = newRot;
643 } else if (isValidPos(m_CurPiece, newRot, m_CurX + 1, m_CurY)) {
644 m_CurX++;
645 m_CurRot = newRot;
646 }
647 }
648
649 if ((IsKeyPressed(KEY_LEFT_CONTROL) || IsKeyPressed(KEY_RIGHT_CONTROL)) && m_CanHold) {
650 startHoldAnimation();
651 sys::SndMan::Get().PlaySfx("sfx_hold_block");
652 }
653 }
654
655 bool GameMain::isValidPos(i8 piece, i8 rot, i8 x, i8 y) {
656 for (i8 i = 0; i < 4; ++i) {
657 i8 px = x + tetrominoShapes[piece][rot][i][0];
658 i8 py = y + tetrominoShapes[piece][rot][i][1];
659
660 if (px < 0 || px >= m_HSize || py >= m_VSize) return false;
661 if (py >= 0 && board[py][px] != 0) return false;
662 }
663 return true;
664 }
665
666 i32 GameMain::stackHeight() const {
667 i32 highestRow = m_VSize;
668
669 for (i32 y = 0; y < m_VSize; ++y) {
670 for (i32 x = 0; x < m_HSize; ++x) {
671 if (board[y][x] != 0) {
672 highestRow = y;
673 goto found;
674 }
675 }
676 }
677
678found:
679
680 return m_VSize - highestRow;
681 }
682
683 f32 GameMain::targetDangerPitch() const {
684 constexpr i32 beginDanger = 9;
685 constexpr i32 maxDanger = 20;
686
687 i32 h = stackHeight();
688 f32 t = std::clamp((h - beginDanger) / f32(maxDanger - beginDanger), 0.0f, 1.0f);
689 t = t * t * (3.0f - 2.0f * t);
690
691 return std::lerp(1.0f, 1.20f, t);
692 }
693
694 ///////////
695
696 void GameMain::createLandingEffect() {
697 Color baseCol = tetrominoColors[m_CurPiece];
698
699 for (i8 i = 0; i < 28; ++i) {
700 Particle p;
701 p.pos.x = boardPos.x + GetRandomValue(0, m_HSize * (i8)m_SquareSize);
702 p.pos.y = boardPos.y + (m_CurY + 2) * m_SquareSize;
703
704 p.vel.x = GetRandomValue(-180, 180);
705 p.vel.y = GetRandomValue(-80, 40);
706
707 p.color = baseCol;
708 p.color.a = GetRandomValue(160, 255);
709 p.life = GetRandomValue(25, 45) / 60.f;
710 p.size = GetRandomValue(8, 18);
711
712 if (!m_Smokes.empty()) {
713 p.txIdx = GetRandomValue(0, (i8)m_Smokes.size() - 1);
714 p.type = ParticleType::Smoke;
715 } else {
716 p.txIdx = -1;
717 }
718
719 m_Particles.push_back(p);
720 }
721
722 m_ShakeTime = .18f;
723 }
724
725 void GameMain::updateLandingEffects() {
726 for (auto it = m_Particles.begin(); it != m_Particles.end();) {
727 it->pos.x += it->vel.x * GetFrameTime();
728 it->pos.y += it->vel.y * GetFrameTime();
729 it->vel.y += 280.f * GetFrameTime();
730 it->life -= GetFrameTime();
731
732 if (it->life <= 0.f) it = m_Particles.erase(it);
733 else ++it;
734 }
735
736 if (m_ShakeTime > 0.f) {
737 m_ShakeTime -= GetFrameTime();
738 m_ShakeOffset.x = GetRandomValue(-6, 6);
739 m_ShakeOffset.y = GetRandomValue(-4, 4);
740 } else {
741 m_ShakeOffset = { 0, 0 };
742 }
743 }
744
745 void GameMain::drawLandingEffects() {
746 for (const auto &p : m_Particles) {
747 f32 alpha = p.life * 2.f;
748 if (alpha > 1.f) alpha = 1.f;
749
750 Color c = p.color;
751 c.a = (u8)(p.color.a * alpha);
752
753 if (p.txIdx >= 0) {
754 Texture2D *tx = (p.type == ParticleType::Sparkle) ? &m_Sparkles[p.txIdx] : &m_Smokes[p.txIdx];
755
756 if (tx && tx->id != 0) {
757 Rectangle rect = { p.pos.x - p.size / 2, p.pos.y - p.size / 2, p.size, p.size };
758 DrawTexturePro(*tx,
759 { 0, 0, (f32)tx->width, (f32)tx->height },
760 rect, { 0, 0 }, 0.f, c);
761 } else {
762 DrawRectangle((i8)(p.pos.x - p.size / 2),
763 (i8)(p.pos.y - p.size / 2),
764 (i8)p.size, (i8)p.size, c);
765 }
766 }
767 }
768 }
769
770 void GameMain::createClearEffect(i8 row) {
771 for (i8 i = 0; i < 42; ++i) {
772 Particle p;
773 p.pos.x = boardPos.x + GetRandomValue(0, m_HSize * (i8)m_SquareSize);
774 p.pos.y = boardPos.y + row * m_SquareSize + GetRandomValue(0, (i8)m_SquareSize);
775
776 p.vel.x = GetRandomValue(-120, 120);
777 p.vel.y = GetRandomValue(-220, 60);
778
779 p.color = GOLD;
780 p.color.a = GetRandomValue(200, 255);
781 p.life = GetRandomValue(35, 65) / 60.f;
782 p.size = GetRandomValue(8, 18);
783
784 if (!m_Sparkles.empty()) {
785 p.txIdx = GetRandomValue(0, (i8)m_Sparkles.size() - 1);
786 p.type = ParticleType::Sparkle;
787 } else {
788 p.txIdx = -1;
789 }
790
791 m_Particles.push_back(p);
792 }
793
794 ClearEffect vfx;
795 vfx.row = row;
796 vfx.timer = .18f;
797 m_ClearEffects.push_back(vfx);
798 }
799
800 void GameMain::updateClearEffects() {
801 for (auto it = m_ClearEffects.begin(); it != m_ClearEffects.end();) {
802 it->timer -= GetFrameTime();
803 if (it->timer <= 0.f) it = m_ClearEffects.erase(it);
804 else ++it;
805 }
806 }
807
808 void GameMain::drawClearEffects(Vector2 offset) {
809 for (const auto &e : m_ClearEffects) {
810 f32 alpha = e.timer / .18f;
811 Color flash = Fade(WHITE, alpha * .85f);
812
813 Rectangle r = {
814 .x = offset.x - 4,
815 .y = offset.y + e.row * (f32)m_SquareSize - 4,
816 .width = (f32)m_HSize * (f32)m_SquareSize + 8,
817 .height = (f32)m_SquareSize + 8,
818 };
819
820 DrawRectangleRec(r, flash);
821 }
822 }
823
824 void GameMain::startHoldAnimation() {
825 if (m_HoldAnim != HoldAnimState::None) return;
826
827 m_HoldAnim = HoldAnimState::FadeOut;
828 m_HoldAnimTimer = 0.f;
829
830 f32 cx = 0.f;
831 f32 cy = 0.f;
832
833 for (int i = 0; i < 4; ++i) {
834 cx += m_CurX + tetrominoShapes[m_CurPiece][m_CurRot][i][0] + 0.5f;
835 cy += m_CurY + tetrominoShapes[m_CurPiece][m_CurRot][i][1] + 0.5f;
836 }
837
838 cx /= 4.f;
839 cy /= 4.f;
840
841 m_BeamStart = {
842 boardPos.x + cx * m_SquareSize,
843 boardPos.y + cy * m_SquareSize,
844 };
845
846 m_BeamEnd = {
847 holdPos.x + holdSquareSize * 2.f,
848 holdPos.y + holdSquareSize * 2.f,
849 };
850 }
851
852 void GameMain::updateHoldAnimation() {
853 if (m_HoldAnim == HoldAnimState::None) return;
854 m_HoldAnimTimer += GetFrameTime();
855
856 switch (m_HoldAnim) {
857 case HoldAnimState::FadeOut:
858 if (m_HoldAnimTimer >= 0.08f) {
859 m_HoldAnim = HoldAnimState::Beam;
860 m_HoldAnimTimer = 0.f;
861 }
862 break;
863 case HoldAnimState::Beam:
864 if (m_HoldAnimTimer >= 0.15f) {
865 if (m_Hold == -1) {
866 m_Hold = m_CurPiece;
867 spawn();
868 } else {
869 std::swap(m_CurPiece, m_Hold);
870 m_CurRot = 0;
871 m_CurX = m_HSize / 2 - 2;
872 m_CurY = 0;
873 }
874
875 m_CanHold = false;
876
877 m_HoldAnim = HoldAnimState::FadeIn;
878 m_HoldAnimTimer = 0.f;
879 }
880 break;
881 case HoldAnimState::FadeIn:
882 if (m_HoldAnimTimer >= 0.08f)
883 m_HoldAnim = HoldAnimState::None;
884 break;
885 default:
886 break;
887 }
888 }
889
890 void GameMain::drawHoldAnimation() {
891 if (m_HoldAnim == HoldAnimState::None) return;
892 f32 t = m_HoldAnimTimer / 0.15f;
893 t = std::clamp(t, 0.f, 1.f);
894 t = t * t * (3.f - 2.f * t);
895
896 Vector2 head = {
897 std::lerp(m_BeamStart.x, m_BeamEnd.x, t),
898 std::lerp(m_BeamStart.y, m_BeamEnd.y, t),
899 };
900
901 DrawLineEx(m_BeamStart, head, 18.f, Fade(SKYBLUE, 0.20f));
902 DrawLineEx(m_BeamStart, head, 8.f, Fade(SKYBLUE, 0.80f));
903 DrawLineEx(m_BeamStart, head, 3.f, SKYBLUE);
904
905 DrawCircleV(head, 12.f, SKYBLUE);
906 DrawCircleV(head, 24.f, Fade(SKYBLUE, 0.25f));
907
908
909
910 if (m_HoldAnim != HoldAnimState::None) {
911 f32 alpha = 0.0f;
912
913 switch (m_HoldAnim) {
914 case HoldAnimState::FadeOut:
915 alpha = (m_HoldAnimTimer / 0.08f) * 0.75f;
916 break;
917 case HoldAnimState::Beam:
918 alpha = 0.65f;
919 break;
920 case HoldAnimState::FadeIn:
921 alpha = (1.0f - m_HoldAnimTimer / 0.08f) * 0.75f;
922 break;
923 default:
924 break;
925 }
926
927 DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(BLACK, alpha));
928 }
929 }
930
931 void GameMain::countdown() {
932 f32 dt = GetFrameTime() * 1.65f;
933 m_Countdown -= dt;
934
935 i32 val = (i32)ceilf(m_Countdown);
936 if (val != m_CountValue) m_CountValue = val;
937
938 if (m_Countdown <= -1.f) {
939 m_State = GameState::Playing;
940 return;
941 }
942
943 f32 t = 1.f - (m_Countdown - floorf(m_Countdown));
944 m_TextScale = 1.4f - t * .4f;
945 m_TextAlpha = 1.f - t * .3f;
946 }
947
948 void GameMain::pause() {
949 if (IsKeyPressed(KEY_LEFT) || IsKeyPressed(KEY_A) || IsKeyPressed(KEY_RIGHT) || IsKeyPressed(KEY_D)) {
950 m_PauseSelect = m_PauseSelect == PauseOption::Resume ? PauseOption::Quit : PauseOption::Resume;
951 sys::SndMan::Get().PlaySfx("sfx_select");
952 }
953
954 if (IsKeyPressed(KEY_ENTER)) {
955 switch (m_PauseSelect) {
956 case PauseOption::Resume:
957 m_State = GameState::Playing;
958 m_PauseAlpha = 0.f;
959 sys::SndMan::Get().PlaySfx("sfx_resume");
960 break;
961 case PauseOption::Quit:
962 m_Game->SetNextScene(Scene::Title);
963 sys::SndMan::Get().PlaySfx("sfx_quit");
964 break;
965 }
966 }
967 }
968} // namespace tetris::scene