/** Copyright (c) テクニカル諏訪子 Permission is hereby granted to any person obtaining a copy of the software Tetris (the "Software") to use, modify, merge, copy, publish, distribute, sublicense, and/or sell copies of the Software, subject to the following conditions: 1. **Origin Attribution**: - You must not misrepresent the origin of the Software; you must not claim you created the original Software. - If the Software is used in a product, you must either: a. Provide clear attribution in the product's documentation, user interface, or other visible areas, **OR** b. Pay the original developers a fee they specify in writing. 2. **Usage Restriction**: - The Software, or any derivative works, dependencies, or libraries incorporating it, must not be used for censorship or to suppress freedom of speech, expression, or creativity. Prohibited uses include, but are not limited to: - Censorship of so-called "hate speech", visuals, non-mainstream opinions, ideas, or objective reality. - Tools or systems designed to restrict access to information or artistic works. 3. **Notice Preservation**: - This license and the above copyright notice must remain intact in all copies of the source code. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace tetris::scene { i8 levelUpThreshold = 10; Color tetrominoColors[7] = { BLUE, YELLOW, GREEN, RED, PURPLE, ORANGE, SKYBLUE, }; // [ピース][回転][4ブロック][x,y] static const i32 tetrominoShapes[7][4][4][2] = { {{{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 {{{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 {{{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 {{{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 {{{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 {{{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 {{{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 }; Vector2 boardPos = { 0, 0 }; Texture2D blockTexture; i8 nextPiece = 0; Vector2 nextPos = { 0, 0 }; f32 nextSquareSize = 0.f; Vector2 holdPos = { 0, 0 }; f32 holdSquareSize; std::vector> board = {}; void GameMain::Init(Game *game) { m_Score = 0; m_Level = 1; m_Lines = 0; m_Game = game; boardPos = { .x = (GetScreenWidth() - m_HSize * m_SquareSize) / 2.f - 150, .y = (GetScreenHeight() - m_VSize * m_SquareSize) / 2.f, }; board.assign(m_VSize, std::vector(m_HSize, 0)); blockTexture = sys::ImgMan::Get().LoadTexture("tx_block", __ass_tetris_block_png, __ass_tetris_block_png_len); sys::ImgMan::Get().LoadTexture("bg_beach", __ass_bg_beach_png, __ass_bg_beach_png_len); sys::ImgMan::Get().LoadTexture("bg_city", __ass_bg_city_png, __ass_bg_city_png_len); sys::ImgMan::Get().LoadTexture("bg_mount", __ass_bg_mount_png, __ass_bg_mount_png_len); sys::ImgMan::Get().LoadTexture("bg_road", __ass_bg_road_png, __ass_bg_road_png_len); sys::ImgMan::Get().LoadTexture("bg_sunset", __ass_bg_sunset_png, __ass_bg_sunset_png_len); sys::ImgMan::Get().LoadTexture("bg_waterfall", __ass_bg_waterfall_png, __ass_bg_waterfall_png_len); sys::ImgMan::Get().SetBackground(m_Bg[0]); m_Sparkles.push_back(sys::ImgMan::Get().LoadTexture("vfx_sparkle1", __ass_sparkle1_png, __ass_sparkle1_png_len)); m_Sparkles.push_back(sys::ImgMan::Get().LoadTexture("vfx_sparkle2", __ass_sparkle2_png, __ass_sparkle2_png_len)); m_Sparkles.push_back(sys::ImgMan::Get().LoadTexture("vfx_sparkle3", __ass_sparkle3_png, __ass_sparkle3_png_len)); m_Sparkles.push_back(sys::ImgMan::Get().LoadTexture("vfx_sparkle4", __ass_sparkle4_png, __ass_sparkle4_png_len)); m_Sparkles.push_back(sys::ImgMan::Get().LoadTexture("vfx_sparkle5", __ass_sparkle5_png, __ass_sparkle5_png_len)); m_Sparkles.push_back(sys::ImgMan::Get().LoadTexture("vfx_sparkle6", __ass_sparkle6_png, __ass_sparkle6_png_len)); m_Sparkles.push_back(sys::ImgMan::Get().LoadTexture("vfx_sparkle7", __ass_sparkle7_png, __ass_sparkle7_png_len)); m_Sparkles.push_back(sys::ImgMan::Get().LoadTexture("vfx_sparkle8", __ass_sparkle8_png, __ass_sparkle8_png_len)); m_Sparkles.push_back(sys::ImgMan::Get().LoadTexture("vfx_sparkle9", __ass_sparkle9_png, __ass_sparkle9_png_len)); m_Sparkles.push_back(sys::ImgMan::Get().LoadTexture("vfx_sparkle10", __ass_sparkle10_png, __ass_sparkle10_png_len)); m_Sparkles.push_back(sys::ImgMan::Get().LoadTexture("vfx_sparkle11", __ass_sparkle11_png, __ass_sparkle11_png_len)); m_Sparkles.push_back(sys::ImgMan::Get().LoadTexture("vfx_sparkle12", __ass_sparkle12_png, __ass_sparkle12_png_len)); m_Sparkles.push_back(sys::ImgMan::Get().LoadTexture("vfx_sparkle13", __ass_sparkle13_png, __ass_sparkle13_png_len)); m_Sparkles.push_back(sys::ImgMan::Get().LoadTexture("vfx_sparkle14", __ass_sparkle14_png, __ass_sparkle14_png_len)); m_Sparkles.push_back(sys::ImgMan::Get().LoadTexture("vfx_sparkle15", __ass_sparkle15_png, __ass_sparkle15_png_len)); m_Sparkles.push_back(sys::ImgMan::Get().LoadTexture("vfx_sparkle16", __ass_sparkle16_png, __ass_sparkle16_png_len)); m_Smokes.push_back(sys::ImgMan::Get().LoadTexture("vfx_smoke1", __ass_whitePuff01_png, __ass_whitePuff01_png_len)); m_Smokes.push_back(sys::ImgMan::Get().LoadTexture("vfx_smoke2", __ass_whitePuff02_png, __ass_whitePuff02_png_len)); m_Smokes.push_back(sys::ImgMan::Get().LoadTexture("vfx_smoke3", __ass_whitePuff03_png, __ass_whitePuff03_png_len)); m_Smokes.push_back(sys::ImgMan::Get().LoadTexture("vfx_smoke4", __ass_whitePuff04_png, __ass_whitePuff04_png_len)); m_Smokes.push_back(sys::ImgMan::Get().LoadTexture("vfx_smoke5", __ass_whitePuff05_png, __ass_whitePuff05_png_len)); m_Smokes.push_back(sys::ImgMan::Get().LoadTexture("vfx_smoke6", __ass_whitePuff06_png, __ass_whitePuff06_png_len)); m_Smokes.push_back(sys::ImgMan::Get().LoadTexture("vfx_smoke7", __ass_whitePuff07_png, __ass_whitePuff07_png_len)); m_Smokes.push_back(sys::ImgMan::Get().LoadTexture("vfx_smoke8", __ass_whitePuff08_png, __ass_whitePuff08_png_len)); m_Smokes.push_back(sys::ImgMan::Get().LoadTexture("vfx_smoke9", __ass_whitePuff09_png, __ass_whitePuff09_png_len)); m_Smokes.push_back(sys::ImgMan::Get().LoadTexture("vfx_smoke10", __ass_whitePuff10_png, __ass_whitePuff10_png_len)); m_Smokes.push_back(sys::ImgMan::Get().LoadTexture("vfx_smoke11", __ass_whitePuff11_png, __ass_whitePuff11_png_len)); m_Smokes.push_back(sys::ImgMan::Get().LoadTexture("vfx_smoke12", __ass_whitePuff12_png, __ass_whitePuff12_png_len)); m_Smokes.push_back(sys::ImgMan::Get().LoadTexture("vfx_smoke13", __ass_whitePuff13_png, __ass_whitePuff13_png_len)); m_Smokes.push_back(sys::ImgMan::Get().LoadTexture("vfx_smoke14", __ass_whitePuff14_png, __ass_whitePuff14_png_len)); m_Smokes.push_back(sys::ImgMan::Get().LoadTexture("vfx_smoke15", __ass_whitePuff15_png, __ass_whitePuff15_png_len)); m_Smokes.push_back(sys::ImgMan::Get().LoadTexture("vfx_smoke16", __ass_whitePuff16_png, __ass_whitePuff16_png_len)); auto &snd = sys::SndMan::Get(); if (!snd.LoadBgm("bgm_tetorisu", __ass_tetorisu_start_ogg, __ass_tetorisu_start_ogg_len, __ass_tetorisu_loop_ogg, __ass_tetorisu_loop_ogg_len)) { TraceLog(LOG_WARNING, "Failed to load BGM!"); } else { snd.PlayBgm("bgm_tetorisu"); } snd.LoadSfx("sfx_drop", __ass_drop_ogg, __ass_drop_ogg_len); snd.LoadSfx("sfx_line_clear", __ass_line_clear_ogg, __ass_line_clear_ogg_len); snd.LoadSfx("sfx_level_up", __ass_level_up_ogg, __ass_level_up_ogg_len); snd.LoadSfx("sfx_hold_block", __ass_hold_block_ogg, __ass_hold_block_ogg_len); snd.LoadSfx("sfx_countdown", __ass_countdown_ogg, __ass_countdown_ogg_len); snd.LoadSfx("sfx_confirm", __ass_confirm_ogg, __ass_confirm_ogg_len); snd.LoadSfx("sfx_pause", __ass_pause_ogg, __ass_pause_ogg_len); snd.LoadSfx("sfx_quit", __ass_quit_ogg, __ass_quit_ogg_len); snd.LoadSfx("sfx_resume", __ass_resume_ogg, __ass_resume_ogg_len); snd.LoadSfx("sfx_select", __ass_select_ogg, __ass_select_ogg_len); sys::SndMan::Get().PlaySfx("sfx_countdown"); nextPiece = GetRandomValue(0, 6); spawn(); updateBoardLayout(); } void GameMain::FuncUpdate() { if (m_State == GameState::Countdown) { countdown(); return; } if (m_GameOver) { m_Game->SetNextScene(Scene::GameOver); return; } if (m_State == GameState::Playing && IsKeyPressed(KEY_ESCAPE)) { m_State = GameState::Paused; m_PauseSelect = PauseOption::Resume; sys::SndMan::Get().PlaySfx("sfx_pause"); } if (m_State == GameState::Paused) { pause(); return; } updateBoardLayout(); handleInput(); updateLandingEffects(); updateClearEffects(); updateHoldAnimation(); sys::SndMan::Get().Update(GetFrameTime()); m_FallTimer += GetFrameTime(); f32 curFallSpeed = m_FallSpeed; if (IsKeyDown(KEY_DOWN) || IsKeyDown(KEY_S)) { curFallSpeed = .05f; } if (m_FallTimer >= curFallSpeed) { if (isValidPos(m_CurPiece, m_CurRot, m_CurX, m_CurY + 1)) { m_CurY++; } else { lock(); } m_FallTimer = 0.f; } f32 target = targetDangerPitch(); m_DangerPitch = std::lerp(m_DangerPitch, target, GetFrameTime() * 3.5f); sys::SndMan::Get().SetBgmPitch(m_DangerPitch); } void GameMain::DrawUpdate() { sys::ImgMan::Get().DrawBackground(); Rectangle gameArea = { boardPos.x - 160, boardPos.y - 30, (f32)m_HSize * (f32)m_SquareSize + 380, (f32)m_VSize * (f32)m_SquareSize + 60, }; DrawRectangleRec(gameArea, Fade(BLACK, 0.65f)); i32 titleSize = std::max(30, (i32)(m_SquareSize * 1.2f)); DrawText("TETRIS", GetScreenWidth() / 2 - titleSize * 2, 30, titleSize, WHITE); #ifndef NDEBUG if (m_ShakeTime > 0.f) { DrawText(TextFormat("SHAKE: %.2f", m_ShakeTime), 20, 20, 20, RED); } DrawText(TextFormat("Height: %d Pitch: %.2f", stackHeight(), m_DangerPitch), 10, 10, 20, WHITE); #endif Vector2 drawPos = boardPos; if (m_ShakeTime > 0.f) { drawPos.x += m_ShakeOffset.x; drawPos.y += m_ShakeOffset.y; } Rectangle rect = { .x = drawPos.x - 5, .y = drawPos.y - 5, .width = (f32)(m_HSize * m_SquareSize + 10), .height = (f32)(m_VSize * m_SquareSize + 10), }; DrawRectangleLinesEx(rect, 5, LIGHTGRAY); // グリッド for (u8 j = 0; j < m_VSize; ++j) { for (u8 i = 0; i < m_HSize; ++i) { Color bCol = tetrominoColors[(board[j][i] - 1) % 7]; Rectangle rect = { .x = drawPos.x + i * (f32)m_SquareSize, .y = drawPos.y + j * (f32)m_SquareSize, .width = (f32)m_SquareSize, .height = (f32)m_SquareSize, }; if (board[j][i] > 0) { DrawTexturePro(blockTexture, { 0, 0, (f32)blockTexture.width, (f32)blockTexture.height }, rect, { 0, 0 }, 0.f, bCol); DrawRectangleLines(rect.x, rect.y, rect.width, rect.height, DARKGRAY); } else { DrawRectangleLines(rect.x, rect.y, rect.width, rect.height, Fade(GRAY, .3f)); } } } // VFX drawLandingEffects(); drawClearEffects(drawPos); // ゴースト drawGhostPiece(drawPos); // 持ち drawHoldAnimation(); // 現在のピース Color pieceCol = tetrominoColors[m_CurPiece]; if (m_HoldAnim == HoldAnimState::Beam) { f32 t = std::clamp(m_HoldAnimTimer / 0.15f, 0.0f, 1.0f); pieceCol.a = static_cast(255.0f * (1.0f - t)); } for (i8 i = 0; i < 4; ++i) { i8 px = m_CurX + tetrominoShapes[m_CurPiece][m_CurRot][i][0]; i8 py = m_CurY + tetrominoShapes[m_CurPiece][m_CurRot][i][1]; if (py >= 0 && px >= 0 && px < m_HSize && py < m_VSize) { Rectangle rect = { .x = drawPos.x + px * (f32)m_SquareSize, .y = drawPos.y + py * (f32)m_SquareSize, .width = (f32)m_SquareSize, .height = (f32)m_SquareSize, }; f32 scale = 1.0f; if (m_HoldAnim == HoldAnimState::Beam) { f32 t = std::clamp(m_HoldAnimTimer / 0.15f, 0.0f, 1.0f); scale = std::lerp(1.0f, 0.85f, t); } f32 cx = rect.x + rect.width * 0.5f; f32 cy = rect.y + rect.height * 0.5f; rect.width *= scale; rect.height *= scale; rect.x = cx - rect.width * 0.5f; rect.y = cy - rect.height * 0.5f; DrawTexturePro(blockTexture, { 0, 0, (f32)blockTexture.width, (f32)blockTexture.height }, rect, { 0, 0 }, 0.f, pieceCol); DrawRectangleLines(rect.x, rect.y, rect.width, rect.height, BLACK); } } // 次のピース Rectangle nextPanel = { .x = nextPos.x - 10, .y = nextPos.y - 10, .width = 4 * nextSquareSize + 20, .height = 4 * nextSquareSize + 20, }; DrawRectangleRec(nextPanel, DARKGRAY); DrawRectangleLinesEx(nextPanel, 3, LIGHTGRAY); DrawText("NEXT", nextPos.x + 10, nextPos.y - 30, 20, WHITE); drawNextPiece(); // 持ち Rectangle holdPanel = { .x = holdPos.x - 10, .y = holdPos.y - 10, .width = 4 * holdSquareSize + 20, .height = 4 * holdSquareSize + 20, }; DrawRectangleRec(holdPanel, DARKGRAY); DrawRectangleLinesEx(holdPanel, 3, LIGHTGRAY); DrawText("HOLD", holdPos.x + 10, holdPos.y - 45, 20, WHITE); drawHoldPiece(); f32 uiX = holdPos.x; f32 uiY = holdPos.y + 120; DrawText(TextFormat("SCORE"), uiX, uiY, 24, WHITE); DrawText(TextFormat("%08d", m_Score), uiX, uiY + 35, 28, YELLOW); DrawText(TextFormat("LEVEL"), uiX, uiY + 90, 24, WHITE); DrawText(TextFormat("%02d", m_Level), uiX, uiY + 125, 32, ORANGE); DrawText(TextFormat("LINES"), uiX, uiY + 180, 24, WHITE); DrawText(TextFormat("%04d", m_Lines), uiX, uiY + 215, 28, SKYBLUE); if (m_State == GameState::Countdown) { cstr text; switch (m_CountValue) { case 3: text = "3"; break; case 2: text = "2"; break; case 1: text = "1"; break; case 0: text = "GO!!"; break; default: text = ""; break; }; Font font = GetFontDefault(); Vector2 size = MeasureTextEx(font, text, 120.f * m_TextScale, 0.f); 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)); } if (m_State == GameState::Paused) { DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(BLACK, .55f)); m_PauseAlpha = std::min(m_PauseAlpha + GetFrameTime() * 6.f, 1.f); Font font = GetFontDefault(); cstr title = "PAUSE"; f32 titleSize = 60.f; Vector2 size = MeasureTextEx(font, title, titleSize, 0); 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); Vector2 resumePos = { GetScreenWidth() / 2.f - 120.f, GetScreenHeight() / 2.f + 20.f }; Vector2 quitPos = { GetScreenWidth() / 2.f + 120.f, GetScreenHeight() / 2.f + 20.f }; auto DrawButton = [](Font font, cstr text, Vector2 pos, bool selected) { f32 scale = selected ? 1.05f + 0.03f * sinf(GetTime() * 8.f) : 1.f; Color color = selected ? WHITE : Fade(WHITE, 0.5f); f32 sizePx = 40.f * scale; Vector2 textSize = MeasureTextEx(font, text, sizePx, 0); DrawTextPro(font, text, pos, { textSize.x / 2.f, textSize.y / 2.f }, 0.f, sizePx, 2.f, color); 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); }; DrawButton(font, "RESUME", resumePos, m_PauseSelect == PauseOption::Resume); DrawButton(font, "QUIT", quitPos, m_PauseSelect == PauseOption::Quit); } } void GameMain::Close() { sys::SndMan::Get().StopBgm(); sys::ImgMan::Get().UnloadAll(); m_State = GameState::Countdown; m_CurBg = 0; m_FallTimer = 0.f; m_FallSpeed = .8f; m_GameOver = false; m_CanHold = true; m_Gravity = 30; m_HSize = 10; m_VSize = 20; m_SquareSize = 20; m_Hold = -1; m_CurPiece = 0; m_CurRot = 0; m_CurX = 0; m_CurY = 0; m_ShakeTime = 0.f; m_HoldAnimTimer = 0.f; m_Countdown = 4.f; m_CountValue = 3; m_TextScale = 1.f; m_TextAlpha = 1.f; m_PauseAlpha = 0.f; m_DangerPitch = 1.f; m_Particles.clear(); m_ClearEffects.clear(); m_Sparkles.clear(); m_Smokes.clear(); } ////////// void GameMain::spawn() { m_CurPiece = nextPiece; nextPiece = GetRandomValue(0, 6); m_CurRot = 0; m_CurX = m_HSize / 2 - 2; m_CurY = 0; m_CanHold = true; if (!isValidPos(m_CurPiece, m_CurRot, m_CurX, m_CurY)) { m_GameOver = true; } } void GameMain::lock() { for (i8 i = 0; i < 4; ++i) { i8 px = m_CurX + tetrominoShapes[m_CurPiece][m_CurRot][i][0]; i8 py = m_CurY + tetrominoShapes[m_CurPiece][m_CurRot][i][1]; if (py >= 0) board[py][px] = m_CurPiece + 1; } clearLines(); spawn(); } void GameMain::updateBoardLayout() { i32 screenW = GetScreenWidth(); i32 screenH = GetScreenHeight(); f32 maxW = (screenW * .55f) / (f32)m_HSize; f32 maxH = (screenH * .85f) / (f32)m_VSize; m_SquareSize = std::min(maxW, maxH); m_SquareSize = std::clamp((f32)m_SquareSize, 20.f, 60.f); boardPos.x = (screenW * .5f - m_HSize * m_SquareSize) / 2.f - 40; boardPos.y = (screenH - m_VSize * m_SquareSize) / 2.f; nextSquareSize = m_SquareSize * .75f; nextPos = { .x = boardPos.x + m_HSize * m_SquareSize + 80, .y = boardPos.y + 60, }; holdSquareSize = m_SquareSize * .75f; holdPos = { .x = boardPos.x + m_HSize * m_SquareSize + 80, .y = boardPos.y + 220, }; } void GameMain::drawNextPiece() { Color col = tetrominoColors[nextPiece]; for (i8 i = 0; i < 4; ++i) { f32 x = nextPos.x + tetrominoShapes[nextPiece][0][i][0] * nextSquareSize; f32 y = nextPos.y + tetrominoShapes[nextPiece][0][i][1] * nextSquareSize; Rectangle rect = { x, y, nextSquareSize, nextSquareSize }; DrawTexturePro(blockTexture, { 0, 0, (f32)blockTexture.width, (f32)blockTexture.height }, rect, { 0, 0 }, 0.f, col); DrawRectangleLines(x, y, nextSquareSize, nextSquareSize, BLACK); } } void GameMain::drawHoldPiece() { if (m_Hold == -1) return; Color col = tetrominoColors[m_Hold]; for (i8 i = 0; i < 4; ++i) { f32 x = holdPos.x + tetrominoShapes[m_Hold][0][i][0] * holdSquareSize; f32 y = holdPos.y + tetrominoShapes[m_Hold][0][i][1] * holdSquareSize; Rectangle rect = { x, y, holdSquareSize, holdSquareSize }; DrawTexturePro(blockTexture, { 0, 0, (f32)blockTexture.width, (f32)blockTexture.height }, rect, { 0, 0 }, 0.f, col); DrawRectangleLines(x, y, holdSquareSize, holdSquareSize, BLACK); } } void GameMain::drawGhostPiece(Vector2 offset) { i8 ghostY = m_CurY; while (isValidPos(m_CurPiece, m_CurRot, m_CurX, ghostY + 1)) ghostY++; Color ghostCol = tetrominoColors[m_CurPiece]; ghostCol.a = 90; for (i8 i = 0; i < 4; ++i) { i8 px = m_CurX + tetrominoShapes[m_CurPiece][m_CurRot][i][0]; i8 py = ghostY + tetrominoShapes[m_CurPiece][m_CurRot][i][1]; if (py >= 0 && px >= 0 && px < m_HSize && py < m_VSize) { Rectangle rect = { offset.x + px * (f32)m_SquareSize, offset.y + py * (f32)m_SquareSize, (f32)m_SquareSize, (f32)m_SquareSize, }; DrawTexturePro(blockTexture, { 0, 0, (f32)blockTexture.width, (f32)blockTexture.height }, rect, { 0, 0 }, 0.f, ghostCol); DrawRectangleLines(rect.x, rect.y, rect.width, rect.height, Fade(BLACK, .3f)); } } } void GameMain::clearLines() { i32 clearLine = 0; for (i32 j = m_VSize - 1; j >= 0; --j) { bool isFull = true; for (i32 i = 0; i < m_HSize; ++i) { if (board[j][i] == 0) { isFull = false; break; } } if (isFull) { createClearEffect(j); sys::SndMan::Get().PlaySfx("sfx_line_clear"); clearLine++; for (i32 y = j; y > 0; --y) for (i32 x = 0; x < m_HSize; ++x) board[y][x] = board[y - 1][x]; for (i32 x = 0; x < m_HSize; ++x) board[0][x] = 0; j++; } } if (clearLine > 0) { m_Lines += clearLine; m_Score += clearLine * clearLine * 100; i8 oldLevel = m_Level; m_Level = 1 + (m_Lines / levelUpThreshold); if (m_Level > oldLevel) { m_CurBg = (m_Level - 1) % (i8)m_Bg.size(); sys::SndMan::Get().PlaySfx("sfx_level_up"); sys::ImgMan::Get().SetBackground(m_Bg[m_CurBg]); } m_FallSpeed = std::max(.15f, .8f - (m_Level - 1) * .07f); } } void GameMain::handleInput() { if (m_GameOver) return; if ((IsKeyPressed(KEY_LEFT) || IsKeyPressedRepeat(KEY_LEFT)) || (IsKeyPressed(KEY_A) || IsKeyPressedRepeat(KEY_A))) { if (isValidPos(m_CurPiece, m_CurRot, m_CurX - 1, m_CurY)) { m_CurX--; } } if ((IsKeyPressed(KEY_RIGHT) || IsKeyPressedRepeat(KEY_RIGHT)) || (IsKeyPressed(KEY_D) || IsKeyPressedRepeat(KEY_D))) { if (isValidPos(m_CurPiece, m_CurRot, m_CurX + 1, m_CurY)) { m_CurX++; } } if (IsKeyPressed(KEY_UP) || IsKeyPressed(KEY_W)) { i8 startY = m_CurY; while (isValidPos(m_CurPiece, m_CurRot, m_CurX, m_CurY + 1)) { m_CurY++; m_Score += 2; } if (m_CurY > startY) { createLandingEffect(); sys::SndMan::Get().PlaySfx("sfx_drop"); } lock(); m_FallTimer = 0.f; return; } if (IsKeyPressed(KEY_SPACE)) { u8 newRot = (m_CurRot + 1) % 4; if (isValidPos(m_CurPiece, newRot, m_CurX, m_CurY)) { m_CurRot = newRot; } else if (isValidPos(m_CurPiece, newRot, m_CurX - 1, m_CurY)) { m_CurX--; m_CurRot = newRot; } else if (isValidPos(m_CurPiece, newRot, m_CurX + 1, m_CurY)) { m_CurX++; m_CurRot = newRot; } } if ((IsKeyPressed(KEY_LEFT_CONTROL) || IsKeyPressed(KEY_RIGHT_CONTROL)) && m_CanHold) { startHoldAnimation(); sys::SndMan::Get().PlaySfx("sfx_hold_block"); } } bool GameMain::isValidPos(i8 piece, i8 rot, i8 x, i8 y) { for (i8 i = 0; i < 4; ++i) { i8 px = x + tetrominoShapes[piece][rot][i][0]; i8 py = y + tetrominoShapes[piece][rot][i][1]; if (px < 0 || px >= m_HSize || py >= m_VSize) return false; if (py >= 0 && board[py][px] != 0) return false; } return true; } i32 GameMain::stackHeight() const { i32 highestRow = m_VSize; for (i32 y = 0; y < m_VSize; ++y) { for (i32 x = 0; x < m_HSize; ++x) { if (board[y][x] != 0) { highestRow = y; goto found; } } } found: return m_VSize - highestRow; } f32 GameMain::targetDangerPitch() const { constexpr i32 beginDanger = 9; constexpr i32 maxDanger = 20; i32 h = stackHeight(); f32 t = std::clamp((h - beginDanger) / f32(maxDanger - beginDanger), 0.0f, 1.0f); t = t * t * (3.0f - 2.0f * t); return std::lerp(1.0f, 1.20f, t); } /////////// void GameMain::createLandingEffect() { Color baseCol = tetrominoColors[m_CurPiece]; for (i8 i = 0; i < 28; ++i) { Particle p; p.pos.x = boardPos.x + GetRandomValue(0, m_HSize * (i8)m_SquareSize); p.pos.y = boardPos.y + (m_CurY + 2) * m_SquareSize; p.vel.x = GetRandomValue(-180, 180); p.vel.y = GetRandomValue(-80, 40); p.color = baseCol; p.color.a = GetRandomValue(160, 255); p.life = GetRandomValue(25, 45) / 60.f; p.size = GetRandomValue(8, 18); if (!m_Smokes.empty()) { p.txIdx = GetRandomValue(0, (i8)m_Smokes.size() - 1); p.type = ParticleType::Smoke; } else { p.txIdx = -1; } m_Particles.push_back(p); } m_ShakeTime = .18f; } void GameMain::updateLandingEffects() { for (auto it = m_Particles.begin(); it != m_Particles.end();) { it->pos.x += it->vel.x * GetFrameTime(); it->pos.y += it->vel.y * GetFrameTime(); it->vel.y += 280.f * GetFrameTime(); it->life -= GetFrameTime(); if (it->life <= 0.f) it = m_Particles.erase(it); else ++it; } if (m_ShakeTime > 0.f) { m_ShakeTime -= GetFrameTime(); m_ShakeOffset.x = GetRandomValue(-6, 6); m_ShakeOffset.y = GetRandomValue(-4, 4); } else { m_ShakeOffset = { 0, 0 }; } } void GameMain::drawLandingEffects() { for (const auto &p : m_Particles) { f32 alpha = p.life * 2.f; if (alpha > 1.f) alpha = 1.f; Color c = p.color; c.a = (u8)(p.color.a * alpha); if (p.txIdx >= 0) { Texture2D *tx = (p.type == ParticleType::Sparkle) ? &m_Sparkles[p.txIdx] : &m_Smokes[p.txIdx]; if (tx && tx->id != 0) { Rectangle rect = { p.pos.x - p.size / 2, p.pos.y - p.size / 2, p.size, p.size }; DrawTexturePro(*tx, { 0, 0, (f32)tx->width, (f32)tx->height }, rect, { 0, 0 }, 0.f, c); } else { DrawRectangle((i8)(p.pos.x - p.size / 2), (i8)(p.pos.y - p.size / 2), (i8)p.size, (i8)p.size, c); } } } } void GameMain::createClearEffect(i8 row) { for (i8 i = 0; i < 42; ++i) { Particle p; p.pos.x = boardPos.x + GetRandomValue(0, m_HSize * (i8)m_SquareSize); p.pos.y = boardPos.y + row * m_SquareSize + GetRandomValue(0, (i8)m_SquareSize); p.vel.x = GetRandomValue(-120, 120); p.vel.y = GetRandomValue(-220, 60); p.color = GOLD; p.color.a = GetRandomValue(200, 255); p.life = GetRandomValue(35, 65) / 60.f; p.size = GetRandomValue(8, 18); if (!m_Sparkles.empty()) { p.txIdx = GetRandomValue(0, (i8)m_Sparkles.size() - 1); p.type = ParticleType::Sparkle; } else { p.txIdx = -1; } m_Particles.push_back(p); } ClearEffect vfx; vfx.row = row; vfx.timer = .18f; m_ClearEffects.push_back(vfx); } void GameMain::updateClearEffects() { for (auto it = m_ClearEffects.begin(); it != m_ClearEffects.end();) { it->timer -= GetFrameTime(); if (it->timer <= 0.f) it = m_ClearEffects.erase(it); else ++it; } } void GameMain::drawClearEffects(Vector2 offset) { for (const auto &e : m_ClearEffects) { f32 alpha = e.timer / .18f; Color flash = Fade(WHITE, alpha * .85f); Rectangle r = { .x = offset.x - 4, .y = offset.y + e.row * (f32)m_SquareSize - 4, .width = (f32)m_HSize * (f32)m_SquareSize + 8, .height = (f32)m_SquareSize + 8, }; DrawRectangleRec(r, flash); } } void GameMain::startHoldAnimation() { if (m_HoldAnim != HoldAnimState::None) return; m_HoldAnim = HoldAnimState::FadeOut; m_HoldAnimTimer = 0.f; f32 cx = 0.f; f32 cy = 0.f; for (int i = 0; i < 4; ++i) { cx += m_CurX + tetrominoShapes[m_CurPiece][m_CurRot][i][0] + 0.5f; cy += m_CurY + tetrominoShapes[m_CurPiece][m_CurRot][i][1] + 0.5f; } cx /= 4.f; cy /= 4.f; m_BeamStart = { boardPos.x + cx * m_SquareSize, boardPos.y + cy * m_SquareSize, }; m_BeamEnd = { holdPos.x + holdSquareSize * 2.f, holdPos.y + holdSquareSize * 2.f, }; } void GameMain::updateHoldAnimation() { if (m_HoldAnim == HoldAnimState::None) return; m_HoldAnimTimer += GetFrameTime(); switch (m_HoldAnim) { case HoldAnimState::FadeOut: if (m_HoldAnimTimer >= 0.08f) { m_HoldAnim = HoldAnimState::Beam; m_HoldAnimTimer = 0.f; } break; case HoldAnimState::Beam: if (m_HoldAnimTimer >= 0.15f) { if (m_Hold == -1) { m_Hold = m_CurPiece; spawn(); } else { std::swap(m_CurPiece, m_Hold); m_CurRot = 0; m_CurX = m_HSize / 2 - 2; m_CurY = 0; } m_CanHold = false; m_HoldAnim = HoldAnimState::FadeIn; m_HoldAnimTimer = 0.f; } break; case HoldAnimState::FadeIn: if (m_HoldAnimTimer >= 0.08f) m_HoldAnim = HoldAnimState::None; break; default: break; } } void GameMain::drawHoldAnimation() { if (m_HoldAnim == HoldAnimState::None) return; f32 t = m_HoldAnimTimer / 0.15f; t = std::clamp(t, 0.f, 1.f); t = t * t * (3.f - 2.f * t); Vector2 head = { std::lerp(m_BeamStart.x, m_BeamEnd.x, t), std::lerp(m_BeamStart.y, m_BeamEnd.y, t), }; DrawLineEx(m_BeamStart, head, 18.f, Fade(SKYBLUE, 0.20f)); DrawLineEx(m_BeamStart, head, 8.f, Fade(SKYBLUE, 0.80f)); DrawLineEx(m_BeamStart, head, 3.f, SKYBLUE); DrawCircleV(head, 12.f, SKYBLUE); DrawCircleV(head, 24.f, Fade(SKYBLUE, 0.25f)); if (m_HoldAnim != HoldAnimState::None) { f32 alpha = 0.0f; switch (m_HoldAnim) { case HoldAnimState::FadeOut: alpha = (m_HoldAnimTimer / 0.08f) * 0.75f; break; case HoldAnimState::Beam: alpha = 0.65f; break; case HoldAnimState::FadeIn: alpha = (1.0f - m_HoldAnimTimer / 0.08f) * 0.75f; break; default: break; } DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(BLACK, alpha)); } } void GameMain::countdown() { f32 dt = GetFrameTime() * 1.65f; m_Countdown -= dt; i32 val = (i32)ceilf(m_Countdown); if (val != m_CountValue) m_CountValue = val; if (m_Countdown <= -1.f) { m_State = GameState::Playing; return; } f32 t = 1.f - (m_Countdown - floorf(m_Countdown)); m_TextScale = 1.4f - t * .4f; m_TextAlpha = 1.f - t * .3f; } void GameMain::pause() { if (IsKeyPressed(KEY_LEFT) || IsKeyPressed(KEY_A) || IsKeyPressed(KEY_RIGHT) || IsKeyPressed(KEY_D)) { m_PauseSelect = m_PauseSelect == PauseOption::Resume ? PauseOption::Quit : PauseOption::Resume; sys::SndMan::Get().PlaySfx("sfx_select"); } if (IsKeyPressed(KEY_ENTER)) { switch (m_PauseSelect) { case PauseOption::Resume: m_State = GameState::Playing; m_PauseAlpha = 0.f; sys::SndMan::Get().PlaySfx("sfx_resume"); break; case PauseOption::Quit: m_Game->SetNextScene(Scene::Title); sys::SndMan::Get().PlaySfx("sfx_quit"); break; } } } } // namespace tetris::scene