| | @@ -0,0 +1,140 @@ |
| 1 | +#include <tetris/scene/gamemain.hh> |
| 2 | +#include <raylib.h> |
| 3 | +#include <vector> |
| 4 | +#include <cmath> |
| 5 | +#include <algorithm> |
| 6 | + |
| 7 | +namespace tetris::scene { |
| 8 | + Color tetrominoColors[7] = { |
| 9 | + BLUE, |
| 10 | + YELLOW, |
| 11 | + GREEN, |
| 12 | + RED, |
| 13 | + PURPLE, |
| 14 | + ORANGE, |
| 15 | + SKYBLUE, |
| 16 | + }; |
| 17 | + |
| 18 | + Vector2 boardPos = { 0, 0 }; |
| 19 | + |
| 20 | + i8 nextPiece = 0; |
| 21 | + Vector2 nextPos = {}; |
| 22 | + f32 nextSquareSize = 0.f; |
| 23 | + |
| 24 | + std::vector<std::vector<i32>> board = {}; |
| 25 | + |
| 26 | + void GameMain::Init() { |
| 27 | + boardPos = { |
| 28 | + .x = (GetScreenWidth() - m_HSize * m_SquareSize) / 2.f - 150, |
| 29 | + .y = (GetScreenHeight() - m_VSize * m_SquareSize) / 2.f, |
| 30 | + }; |
| 31 | + |
| 32 | + board.assign(m_VSize, std::vector<i32>(m_HSize, 0)); |
| 33 | + nextPiece = GetRandomValue(0, 6); |
| 34 | + updateBoardLayout(); |
| 35 | + } |
| 36 | + |
| 37 | + void GameMain::FuncUpdate() { |
| 38 | + updateBoardLayout(); |
| 39 | + } |
| 40 | + |
| 41 | + void GameMain::DrawUpdate() { |
| 42 | + i32 titleSize = std::max(30, (i32)(m_SquareSize * 1.2f)); |
| 43 | + DrawText("TETRIS", GetScreenWidth() / 2 - titleSize * 2, 30, titleSize, WHITE); |
| 44 | + |
| 45 | + // Draw the game board border |
| 46 | + Rectangle rect = { |
| 47 | + .x = boardPos.x - 5, |
| 48 | + .y = boardPos.y - 5, |
| 49 | + .width = (f32)(m_HSize * m_SquareSize + 10), |
| 50 | + .height = (f32)(m_VSize * m_SquareSize + 10), |
| 51 | + }; |
| 52 | + DrawRectangleLinesEx(rect, 5, LIGHTGRAY); |
| 53 | + |
| 54 | + // グリッド |
| 55 | + for (u8 j = 0; j < m_VSize; ++j) { |
| 56 | + for (u8 i = 0; i < m_HSize; ++i) { |
| 57 | + if (board[j][i] > 0) { |
| 58 | + Color bCol = tetrominoColors[(board[j][i] - 1) % 7]; |
| 59 | + DrawRectangle(boardPos.x + i * m_SquareSize, boardPos.y + j * m_SquareSize, m_SquareSize, m_SquareSize, bCol); |
| 60 | + DrawRectangleLines(boardPos.x + i * m_SquareSize, boardPos.y + j * m_SquareSize, m_SquareSize, m_SquareSize, DARKGRAY); |
| 61 | + } else { |
| 62 | + DrawRectangleLines(boardPos.x + i * m_SquareSize, boardPos.y + j * m_SquareSize, m_SquareSize, m_SquareSize, Fade(GRAY, .3f)); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + Rectangle nextPanel = { |
| 68 | + .x = nextPos.x - 10, |
| 69 | + .y = nextPos.y - 10, |
| 70 | + .width = 4 * nextSquareSize + 20, |
| 71 | + .height = 4 * nextSquareSize + 20, |
| 72 | + }; |
| 73 | + |
| 74 | + // 次のピース |
| 75 | + DrawRectangleRec(nextPanel, DARKGRAY); |
| 76 | + DrawRectangleLinesEx(nextPanel, 3, LIGHTGRAY); |
| 77 | + DrawText("NEXT", nextPos.x + 10, nextPos.y - 30, 20, WHITE); |
| 78 | + |
| 79 | + drawNextPiece(); |
| 80 | + |
| 81 | + f32 uiX = nextPos.x; |
| 82 | + f32 uiY = nextPos.y + 180; |
| 83 | + |
| 84 | + DrawText(TextFormat("SCORE"), uiX, uiY, 24, WHITE); |
| 85 | + DrawText(TextFormat("%08d", m_Score), uiX, uiY + 35, 28, YELLOW); |
| 86 | + |
| 87 | + DrawText(TextFormat("LEVEL"), uiX, uiY + 90, 24, WHITE); |
| 88 | + DrawText(TextFormat("%02d", m_Level), uiX, uiY + 125, 32, ORANGE); |
| 89 | + |
| 90 | + DrawText(TextFormat("LINES"), uiX, uiY + 180, 24, WHITE); |
| 91 | + DrawText(TextFormat("%04d", m_Lines), uiX, uiY + 215, 28, SKYBLUE); |
| 92 | + } |
| 93 | + |
| 94 | + void GameMain::Close() { |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | + ////////// |
| 99 | + |
| 100 | + void GameMain::updateBoardLayout() { |
| 101 | + i32 screenW = GetScreenWidth(); |
| 102 | + i32 screenH = GetScreenHeight(); |
| 103 | + |
| 104 | + f32 maxW = (screenW * .55f) / (f32)m_HSize; |
| 105 | + f32 maxH = (screenH * .85f) / (f32)m_VSize; |
| 106 | + |
| 107 | + m_SquareSize = std::min(maxW, maxH); |
| 108 | + m_SquareSize = std::clamp((f32)m_SquareSize, 20.f, 60.f); |
| 109 | + |
| 110 | + boardPos.x = (screenW * .5f - m_HSize * m_SquareSize) / 2.f - 40; |
| 111 | + boardPos.y = (screenH - m_VSize * m_SquareSize) / 2.f; |
| 112 | + |
| 113 | + nextSquareSize = m_SquareSize * .75f; |
| 114 | + nextPos = { |
| 115 | + .x = boardPos.x + m_HSize * m_SquareSize + 80, |
| 116 | + .y = boardPos.y + 60, |
| 117 | + }; |
| 118 | + } |
| 119 | + |
| 120 | + void GameMain::drawNextPiece() { |
| 121 | + const u32 shapes[7][4][2] = { |
| 122 | + {{0,1},{1,1},{2,1},{3,1}}, // I |
| 123 | + {{0,0},{0,1},{1,0},{1,1}}, // O |
| 124 | + {{0,1},{1,1},{2,1},{2,0}}, // S |
| 125 | + {{0,0},{1,0},{1,1},{2,1}}, // Z |
| 126 | + {{1,0},{0,1},{1,1},{2,1}}, // T |
| 127 | + {{0,1},{1,1},{2,1},{2,2}}, // J |
| 128 | + {{2,1},{1,1},{0,1},{0,2}} // L |
| 129 | + }; |
| 130 | + |
| 131 | + Color col = tetrominoColors[nextPiece]; |
| 132 | + |
| 133 | + for (int i = 0; i < 4; ++i) { |
| 134 | + float x = nextPos.x + shapes[nextPiece][i][0] * nextSquareSize; |
| 135 | + float y = nextPos.y + shapes[nextPiece][i][1] * nextSquareSize; |
| 136 | + DrawRectangle(x, y, nextSquareSize, nextSquareSize, col); |
| 137 | + DrawRectangleLines(x, y, nextSquareSize, nextSquareSize, BLACK); |
| 138 | + } |
| 139 | + } |
| 140 | +} // namespace tetris::scene |