| | @@ -15,12 +15,33 @@ |
| 15 | 15 | SKYBLUE, |
| 16 | 16 | }; |
| 17 | 17 | |
| 18 | + // [ピース][回転][4ブロック][x,y] |
| 19 | + static const i32 tetrominoShapes[7][4][4][2] = { |
| 20 | + // I |
| 21 | + {{{0,0},{1,0},{2,0},{3,0}}, {{0,0},{0,1},{0,2},{0,3}}, {{0,0},{1,0},{2,0},{3,0}}, {{0,0},{0,1},{0,2},{0,3}}}, |
| 22 | + // O |
| 23 | + {{{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}}}, |
| 24 | + // S |
| 25 | + {{{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}}}, |
| 26 | + // Z |
| 27 | + {{{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}}}, |
| 28 | + // T |
| 29 | + {{{1,0},{0,1},{1,1},{2,1}}, {{1,0},{1,1},{1,2},{0,1}}, {{0,1},{1,1},{2,1},{1,0}}, {{1,0},{1,1},{1,2},{2,1}}}, |
| 30 | + // J |
| 31 | + {{{0,0},{0,1},{1,1},{2,1}}, {{0,0},{1,0},{1,1},{1,2}}, {{0,1},{1,1},{2,1},{2,0}}, {{1,0},{1,1},{1,2},{0,2}}}, |
| 32 | + // L |
| 33 | + {{{2,0},{0,1},{1,1},{2,1}}, {{0,0},{0,1},{0,2},{1,2}}, {{0,0},{1,0},{2,0},{0,1}}, {{1,0},{1,1},{1,2},{2,0}}} |
| 34 | + }; |
| 35 | + |
| 18 | 36 | Vector2 boardPos = { 0, 0 }; |
| 19 | 37 | |
| 20 | 38 | i8 nextPiece = 0; |
| 21 | | - Vector2 nextPos = {}; |
| 39 | + Vector2 nextPos = { 0, 0 }; |
| 22 | 40 | f32 nextSquareSize = 0.f; |
| 23 | 41 | |
| 42 | + Vector2 holdPos = { 0, 0 }; |
| 43 | + f32 holdSquareSize; |
| 44 | + |
| 24 | 45 | std::vector<std::vector<i32>> board = {}; |
| 25 | 46 | |
| 26 | 47 | void GameMain::Init() { |
| | @@ -31,18 +52,40 @@ |
| 31 | 52 | |
| 32 | 53 | board.assign(m_VSize, std::vector<i32>(m_HSize, 0)); |
| 33 | 54 | nextPiece = GetRandomValue(0, 6); |
| 55 | + spawn(); |
| 34 | 56 | updateBoardLayout(); |
| 35 | 57 | } |
| 36 | 58 | |
| 37 | 59 | void GameMain::FuncUpdate() { |
| 60 | + if (m_GameOver) { |
| 61 | + // TODO: ゲームオーバー |
| 62 | + return; |
| 63 | + } |
| 38 | 64 | updateBoardLayout(); |
| 65 | + handleInput(); |
| 66 | + |
| 67 | + m_FallTimer += GetFrameTime(); |
| 68 | + |
| 69 | + f32 curFallSpeed = m_FallSpeed; |
| 70 | + if (IsKeyDown(KEY_DOWN) || IsKeyDown(KEY_S)) { |
| 71 | + curFallSpeed = .05f; |
| 72 | + } |
| 73 | + |
| 74 | + if (m_FallTimer >= curFallSpeed) { |
| 75 | + if (isValidPos(m_CurPiece, m_CurRot, m_CurX, m_CurY + 1)) { |
| 76 | + m_CurY++; |
| 77 | + } else { |
| 78 | + lock(); |
| 79 | + } |
| 80 | + |
| 81 | + m_FallTimer = 0.f; |
| 82 | + } |
| 39 | 83 | } |
| 40 | 84 | |
| 41 | 85 | void GameMain::DrawUpdate() { |
| 42 | 86 | i32 titleSize = std::max(30, (i32)(m_SquareSize * 1.2f)); |
| 43 | 87 | DrawText("TETRIS", GetScreenWidth() / 2 - titleSize * 2, 30, titleSize, WHITE); |
| 44 | 88 | |
| 45 | | - // Draw the game board border |
| 46 | 89 | Rectangle rect = { |
| 47 | 90 | .x = boardPos.x - 5, |
| 48 | 91 | .y = boardPos.y - 5, |
| | @@ -64,6 +107,22 @@ |
| 64 | 107 | } |
| 65 | 108 | } |
| 66 | 109 | |
| 110 | + // 現在のピース |
| 111 | + Color pieceCol = tetrominoColors[m_CurPiece]; |
| 112 | + for (i8 i = 0; i < 4; ++i) { |
| 113 | + i8 px = m_CurX + tetrominoShapes[m_CurPiece][m_CurRot][i][0]; |
| 114 | + i8 py = m_CurY + tetrominoShapes[m_CurPiece][m_CurRot][i][1]; |
| 115 | + |
| 116 | + if (py >= 0 && px >= 0 && px < m_HSize && py < m_VSize) { |
| 117 | + f32 x = boardPos.x + px * m_SquareSize; |
| 118 | + f32 y = boardPos.y + py * m_SquareSize; |
| 119 | + |
| 120 | + DrawRectangle(x, y, m_SquareSize, m_SquareSize, pieceCol); |
| 121 | + DrawRectangleLines(x, y, m_SquareSize, m_SquareSize, BLACK); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + // 次のピース |
| 67 | 126 | Rectangle nextPanel = { |
| 68 | 127 | .x = nextPos.x - 10, |
| 69 | 128 | .y = nextPos.y - 10, |
| | @@ -71,16 +130,29 @@ |
| 71 | 130 | .height = 4 * nextSquareSize + 20, |
| 72 | 131 | }; |
| 73 | 132 | |
| 74 | | - // 次のピース |
| 75 | 133 | DrawRectangleRec(nextPanel, DARKGRAY); |
| 76 | 134 | DrawRectangleLinesEx(nextPanel, 3, LIGHTGRAY); |
| 77 | 135 | DrawText("NEXT", nextPos.x + 10, nextPos.y - 30, 20, WHITE); |
| 78 | 136 | |
| 79 | 137 | drawNextPiece(); |
| 80 | 138 | |
| 81 | | - f32 uiX = nextPos.x; |
| 82 | | - f32 uiY = nextPos.y + 180; |
| 139 | + // 持ち |
| 140 | + Rectangle holdPanel = { |
| 141 | + .x = holdPos.x - 10, |
| 142 | + .y = holdPos.y - 10, |
| 143 | + .width = 4 * holdSquareSize + 20, |
| 144 | + .height = 4 * holdSquareSize + 20, |
| 145 | + }; |
| 83 | 146 | |
| 147 | + DrawRectangleRec(holdPanel, DARKGRAY); |
| 148 | + DrawRectangleLinesEx(holdPanel, 3, LIGHTGRAY); |
| 149 | + DrawText("HOLD", holdPos.x + 10, holdPos.y - 45, 20, WHITE); |
| 150 | + |
| 151 | + drawHoldPiece(); |
| 152 | + |
| 153 | + f32 uiX = holdPos.x; |
| 154 | + f32 uiY = holdPos.y + 120; |
| 155 | + |
| 84 | 156 | DrawText(TextFormat("SCORE"), uiX, uiY, 24, WHITE); |
| 85 | 157 | DrawText(TextFormat("%08d", m_Score), uiX, uiY + 35, 28, YELLOW); |
| 86 | 158 | |
| | @@ -97,6 +169,31 @@ |
| 97 | 169 | |
| 98 | 170 | ////////// |
| 99 | 171 | |
| 172 | + void GameMain::spawn() { |
| 173 | + m_CurPiece = nextPiece; |
| 174 | + nextPiece = GetRandomValue(0, 6); |
| 175 | + |
| 176 | + m_CurRot = 0; |
| 177 | + m_CurX = m_HSize / 2 - 2; |
| 178 | + m_CurY = 0; |
| 179 | + m_CanHold = true; |
| 180 | + |
| 181 | + if (!isValidPos(m_CurPiece, m_CurRot, m_CurX, m_CurY)) { |
| 182 | + m_GameOver = true; |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + void GameMain::lock() { |
| 187 | + for (i8 i = 0; i < 4; ++i) { |
| 188 | + i8 px = m_CurX + tetrominoShapes[m_CurPiece][m_CurRot][i][0]; |
| 189 | + i8 py = m_CurY + tetrominoShapes[m_CurPiece][m_CurRot][i][1]; |
| 190 | + if (py >= 0) board[py][px] = m_CurPiece + 1; |
| 191 | + } |
| 192 | + |
| 193 | + // TODO: クリアでチェック |
| 194 | + spawn(); |
| 195 | + } |
| 196 | + |
| 100 | 197 | void GameMain::updateBoardLayout() { |
| 101 | 198 | i32 screenW = GetScreenWidth(); |
| 102 | 199 | i32 screenH = GetScreenHeight(); |
| | @@ -115,26 +212,99 @@ |
| 115 | 212 | .x = boardPos.x + m_HSize * m_SquareSize + 80, |
| 116 | 213 | .y = boardPos.y + 60, |
| 117 | 214 | }; |
| 118 | | - } |
| 119 | 215 | |
| 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 |
| 216 | + holdSquareSize = m_SquareSize * .75f; |
| 217 | + holdPos = { |
| 218 | + .x = boardPos.x + m_HSize * m_SquareSize + 80, |
| 219 | + .y = boardPos.y + 220, |
| 129 | 220 | }; |
| 221 | + } |
| 130 | 222 | |
| 223 | + void GameMain::drawNextPiece() { |
| 131 | 224 | Color col = tetrominoColors[nextPiece]; |
| 132 | 225 | |
| 133 | 226 | 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; |
| 227 | + float x = nextPos.x + tetrominoShapes[nextPiece][0][i][0] * nextSquareSize; |
| 228 | + float y = nextPos.y + tetrominoShapes[nextPiece][0][i][1] * nextSquareSize; |
| 136 | 229 | DrawRectangle(x, y, nextSquareSize, nextSquareSize, col); |
| 137 | 230 | DrawRectangleLines(x, y, nextSquareSize, nextSquareSize, BLACK); |
| 138 | 231 | } |
| 232 | + } |
| 233 | + |
| 234 | + void GameMain::drawHoldPiece() { |
| 235 | + if (m_Hold == -1) return; |
| 236 | + |
| 237 | + Color col = tetrominoColors[m_Hold]; |
| 238 | + |
| 239 | + for (int i = 0; i < 4; ++i) { |
| 240 | + float x = holdPos.x + tetrominoShapes[m_Hold][0][i][0] * holdSquareSize; |
| 241 | + float y = holdPos.y + tetrominoShapes[m_Hold][0][i][1] * holdSquareSize; |
| 242 | + DrawRectangle(x, y, holdSquareSize, holdSquareSize, col); |
| 243 | + DrawRectangleLines(x, y, holdSquareSize, holdSquareSize, BLACK); |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + void GameMain::handleInput() { |
| 248 | + if (m_GameOver) return; |
| 249 | + |
| 250 | + if ((IsKeyPressed(KEY_LEFT) || IsKeyPressedRepeat(KEY_LEFT)) || (IsKeyPressed(KEY_A) || IsKeyPressedRepeat(KEY_A))) { |
| 251 | + if (isValidPos(m_CurPiece, m_CurRot, m_CurX - 1, m_CurY)) { |
| 252 | + m_CurX--; |
| 253 | + } |
| 254 | + } |
| 255 | + |
| 256 | + if ((IsKeyPressed(KEY_RIGHT) || IsKeyPressedRepeat(KEY_RIGHT)) || (IsKeyPressed(KEY_D) || IsKeyPressedRepeat(KEY_D))) { |
| 257 | + if (isValidPos(m_CurPiece, m_CurRot, m_CurX + 1, m_CurY)) { |
| 258 | + m_CurX++; |
| 259 | + } |
| 260 | + } |
| 261 | + |
| 262 | + if (IsKeyPressed(KEY_UP) || IsKeyPressed(KEY_W)) { |
| 263 | + while (isValidPos(m_CurPiece, m_CurRot, m_CurX, m_CurY + 1)) { |
| 264 | + m_CurY++; |
| 265 | + m_Score += 2; |
| 266 | + } |
| 267 | + lock(); |
| 268 | + m_FallTimer = 0.f; |
| 269 | + return; |
| 270 | + } |
| 271 | + |
| 272 | + if (IsKeyPressed(KEY_SPACE)) { |
| 273 | + u8 newRot = (m_CurRot + 1) % 4; |
| 274 | + if (isValidPos(m_CurPiece, newRot, m_CurX, m_CurY)) { |
| 275 | + m_CurRot = newRot; |
| 276 | + } else if (isValidPos(m_CurPiece, newRot, m_CurX - 1, m_CurY)) { |
| 277 | + m_CurX--; |
| 278 | + m_CurRot = newRot; |
| 279 | + } else if (isValidPos(m_CurPiece, newRot, m_CurX + 1, m_CurY)) { |
| 280 | + m_CurX++; |
| 281 | + m_CurRot = newRot; |
| 282 | + } |
| 283 | + } |
| 284 | + |
| 285 | + if ((IsKeyPressed(KEY_LEFT_CONTROL) || IsKeyPressed(KEY_RIGHT_CONTROL)) && m_CanHold) { |
| 286 | + if (m_Hold == -1) { |
| 287 | + m_Hold = m_CurPiece; |
| 288 | + spawn(); |
| 289 | + } else { |
| 290 | + std::swap(m_CurPiece, m_Hold); |
| 291 | + m_CurRot = 0; |
| 292 | + m_CurX = m_HSize / 2 - 2; |
| 293 | + m_CurY = 0; |
| 294 | + } |
| 295 | + |
| 296 | + m_CanHold = false; |
| 297 | + } |
| 298 | + } |
| 299 | + |
| 300 | + bool GameMain::isValidPos(i8 piece, i8 rot, i8 x, i8 y) { |
| 301 | + for (i8 i = 0; i < 4; ++i) { |
| 302 | + i8 px = x + tetrominoShapes[piece][rot][i][0]; |
| 303 | + i8 py = y + tetrominoShapes[piece][rot][i][1]; |
| 304 | + |
| 305 | + if (px < 0 || px >= m_HSize || py >= m_VSize) return false; |
| 306 | + if (py >= 0 && board[py][px] != 0) return false; |
| 307 | + } |
| 308 | + return true; |
| 139 | 309 | } |
| 140 | 310 | } // namespace tetris::scene |