ブロック落ちるVFX

守矢諏訪子 <suwako@076.moe>、2026-07-11 21:21
コミット 50be656ad6f72681ff9124f9f7e33237945f3f25 ・親 1fcaf1f ・ファイルを見る

2個のファイル、+102 -11

@@ -1,6 +1,7 @@
11 #pragma once
22
33 #include <util/types.hh>
4+#include <raylib.h>
45
56 namespace tetris::scene {
67 struct GameMain {
@@ -16,12 +17,17 @@
1617 void updateBoardLayout();
1718 void drawNextPiece();
1819 void drawHoldPiece();
19- void drawGhostPiece();
20+ void drawGhostPiece(Vector2 offset);
2021 void clearLines();
2122 void handleInput();
2223 bool isValidPos(i8 piece, i8 rot, i8 x, i8 y);
2324
2425 private:
26+ void createLandingEffect();
27+ void updateEffects();
28+ void drawEffects();
29+
30+ private:
2531 u8 m_Score = 0;
2632 u8 m_Level = 1;
2733 u8 m_Lines = 0;
@@ -46,5 +52,17 @@
4652
4753 vector<string> m_Bg = { "bg_beach", "bg_city", "bg_mount", "bg_road", "bg_sunset", "bg_waterfall" };
4854 i8 m_CurBg = 0;
55+
56+ private:
57+ struct Particle {
58+ Vector2 pos;
59+ Vector2 vel;
60+ Color color;
61+ f32 life;
62+ };
63+
64+ vector<Particle> m_Particles;
65+ f32 m_ShakeTime = 0.f;
66+ Vector2 m_ShakeOffset = { 0, 0 };
4967 }; // struct GameMain
5068 } // namespace tetris::scene
@@ -75,6 +75,7 @@
7575 }
7676 updateBoardLayout();
7777 handleInput();
78+ updateEffects();
7879
7980 m_FallTimer += GetFrameTime();
8081
@@ -107,9 +108,19 @@
107108 i32 titleSize = std::max(30, (i32)(m_SquareSize * 1.2f));
108109 DrawText("TETRIS", GetScreenWidth() / 2 - titleSize * 2, 30, titleSize, WHITE);
109110
111+ if (m_ShakeTime > 0.f) {
112+ DrawText(TextFormat("SHAKE: %.2f", m_ShakeTime), 20, 20, 20, RED);
113+ }
114+
115+ Vector2 drawPos = boardPos;
116+ if (m_ShakeTime > 0.f) {
117+ drawPos.x += m_ShakeOffset.x;
118+ drawPos.y += m_ShakeOffset.y;
119+ }
120+
110121 Rectangle rect = {
111- .x = boardPos.x - 5,
112- .y = boardPos.y - 5,
122+ .x = drawPos.x - 5,
123+ .y = drawPos.y - 5,
113124 .width = (f32)(m_HSize * m_SquareSize + 10),
114125 .height = (f32)(m_VSize * m_SquareSize + 10),
115126 };
@@ -120,8 +131,8 @@
120131 for (u8 i = 0; i < m_HSize; ++i) {
121132 Color bCol = tetrominoColors[(board[j][i] - 1) % 7];
122133 Rectangle rect = {
123- .x = boardPos.x + i * (f32)m_SquareSize,
124- .y = boardPos.y + j * (f32)m_SquareSize,
134+ .x = drawPos.x + i * (f32)m_SquareSize,
135+ .y = drawPos.y + j * (f32)m_SquareSize,
125136 .width = (f32)m_SquareSize,
126137 .height = (f32)m_SquareSize,
127138 };
@@ -135,8 +146,11 @@
135146 }
136147 }
137148
149+ // VFX
150+ drawEffects();
151+
138152 // ゴースト
139- drawGhostPiece();
153+ drawGhostPiece(drawPos);
140154
141155 // 現在のピース
142156 Color pieceCol = tetrominoColors[m_CurPiece];
@@ -146,8 +160,8 @@
146160
147161 if (py >= 0 && px >= 0 && px < m_HSize && py < m_VSize) {
148162 Rectangle rect = {
149- .x = boardPos.x + px * (f32)m_SquareSize,
150- .y = boardPos.y + py * (f32)m_SquareSize,
163+ .x = drawPos.x + px * (f32)m_SquareSize,
164+ .y = drawPos.y + py * (f32)m_SquareSize,
151165 .width = (f32)m_SquareSize,
152166 .height = (f32)m_SquareSize,
153167 };
@@ -283,7 +297,7 @@
283297 }
284298 }
285299
286- void GameMain::drawGhostPiece() {
300+ void GameMain::drawGhostPiece(Vector2 offset) {
287301 i8 ghostY = m_CurY;
288302 while (isValidPos(m_CurPiece, m_CurRot, m_CurX, ghostY + 1)) ghostY++;
289303
@@ -296,8 +310,8 @@
296310
297311 if (py >= 0 && px >= 0 && px < m_HSize && py < m_VSize) {
298312 Rectangle rect = {
299- boardPos.x + px * (f32)m_SquareSize,
300- boardPos.y + py * (f32)m_SquareSize,
313+ offset.x + px * (f32)m_SquareSize,
314+ offset.y + py * (f32)m_SquareSize,
301315 (f32)m_SquareSize,
302316 (f32)m_SquareSize,
303317 };
@@ -366,10 +380,14 @@
366380 }
367381
368382 if (IsKeyPressed(KEY_UP) || IsKeyPressed(KEY_W)) {
383+ i8 startY = m_CurY;
369384 while (isValidPos(m_CurPiece, m_CurRot, m_CurX, m_CurY + 1)) {
370385 m_CurY++;
371386 m_Score += 2;
372387 }
388+
389+ if (m_CurY > startY) createLandingEffect();
390+
373391 lock();
374392 m_FallTimer = 0.f;
375393 return;
@@ -412,5 +430,60 @@
412430 if (py >= 0 && board[py][px] != 0) return false;
413431 }
414432 return true;
433+ }
434+
435+ ///////////
436+
437+ void GameMain::createLandingEffect() {
438+ Color baseCol = tetrominoColors[m_CurPiece];
439+
440+ for (i8 i = 0; i < 28; ++i) {
441+ Particle p;
442+ p.pos.x = boardPos.x + GetRandomValue(0, m_HSize * (i8)m_SquareSize);
443+ p.pos.y = boardPos.y + (m_CurY + 2) * m_SquareSize;
444+
445+ p.vel.x = GetRandomValue(-180, 180);
446+ p.vel.y = GetRandomValue(-80, 40);
447+
448+ p.color = baseCol;
449+ p.color.a = GetRandomValue(160, 255);
450+ p.life = GetRandomValue(25, 45) / 60.f;
451+
452+ m_Particles.push_back(p);
453+ }
454+
455+ m_ShakeTime = .18f;
456+ }
457+
458+ void GameMain::updateEffects() {
459+ for (auto it = m_Particles.begin(); it != m_Particles.end();) {
460+ it->pos.x += it->vel.x * GetFrameTime();
461+ it->pos.y += it->vel.y * GetFrameTime();
462+ it->vel.y += 280.f * GetFrameTime();
463+ it->life -= GetFrameTime();
464+
465+ if (it->life <= 0.f) it = m_Particles.erase(it);
466+ else ++it;
467+ }
468+
469+ if (m_ShakeTime > 0.f) {
470+ m_ShakeTime -= GetFrameTime();
471+ m_ShakeOffset.x = GetRandomValue(-6, 6);
472+ m_ShakeOffset.y = GetRandomValue(-4, 4);
473+ } else {
474+ m_ShakeOffset = { 0, 0 };
475+ }
476+ }
477+
478+ void GameMain::drawEffects() {
479+ for (const auto &p : m_Particles) {
480+ f32 alpha = p.life * 2.f;
481+ if (alpha > 1.f) alpha = 1.f;
482+
483+ Color c = p.color;
484+ c.a = (u8)(p.color.a * alpha);
485+
486+ DrawRectangle(p.pos.x - 6, p.pos.y - 6, 13, 13, c);
487+ }
415488 }
416489 } // namespace tetris::scene