持ちブロックのアニメーション

守矢諏訪子 <suwako@076.moe>、2026-07-16 00:32
コミット a754b973c20c5bdf4440da34b4f89f077f9cbab8 ・親 e3d12b8 ・ファイルを見る

2個のファイル、+155 -18

@@ -63,6 +63,9 @@
6363 void createClearEffect(i8 row);
6464 void updateClearEffects();
6565 void drawClearEffects(Vector2 offset);
66+ void updateHoldAnimation();
67+ void startHoldAnimation();
68+ void drawHoldAnimation();
6669
6770 private:
6871 u8 m_Score = 0;
@@ -134,6 +137,18 @@
134137 Resume,
135138 Quit,
136139 };
140+
141+ enum class HoldAnimState {
142+ None,
143+ FadeOut,
144+ Beam,
145+ FadeIn,
146+ };
147+
148+ HoldAnimState m_HoldAnim = HoldAnimState::None;
149+ f32 m_HoldAnimTimer = 0.f;
150+ Vector2 m_BeamStart{};
151+ Vector2 m_BeamEnd{};
137152
138153 GameState m_State = GameState::Countdown;
139154
@@ -200,6 +200,7 @@
200200 handleInput();
201201 updateLandingEffects();
202202 updateClearEffects();
203+ updateHoldAnimation();
203204 sys::SndMan::Get().Update(GetFrameTime());
204205
205206 m_FallTimer += GetFrameTime();
@@ -281,8 +282,17 @@
281282 // ゴースト
282283 drawGhostPiece(drawPos);
283284
285+ // 持ち
286+ drawHoldAnimation();
287+
284288 // 現在のピース
285289 Color pieceCol = tetrominoColors[m_CurPiece];
290+
291+ if (m_HoldAnim == HoldAnimState::Beam) {
292+ f32 t = std::clamp(m_HoldAnimTimer / 0.15f, 0.0f, 1.0f);
293+ pieceCol.a = static_cast<u8>(255.0f * (1.0f - t));
294+ }
295+
286296 for (i8 i = 0; i < 4; ++i) {
287297 i8 px = m_CurX + tetrominoShapes[m_CurPiece][m_CurRot][i][0];
288298 i8 py = m_CurY + tetrominoShapes[m_CurPiece][m_CurRot][i][1];
@@ -295,6 +305,22 @@
295305 .height = (f32)m_SquareSize,
296306 };
297307
308+ f32 scale = 1.0f;
309+
310+ if (m_HoldAnim == HoldAnimState::Beam) {
311+ f32 t = std::clamp(m_HoldAnimTimer / 0.15f, 0.0f, 1.0f);
312+ scale = std::lerp(1.0f, 0.85f, t);
313+ }
314+
315+ f32 cx = rect.x + rect.width * 0.5f;
316+ f32 cy = rect.y + rect.height * 0.5f;
317+
318+ rect.width *= scale;
319+ rect.height *= scale;
320+
321+ rect.x = cx - rect.width * 0.5f;
322+ rect.y = cy - rect.height * 0.5f;
323+
298324 DrawTexturePro(blockTexture, { 0, 0, (f32)blockTexture.width, (f32)blockTexture.height }, rect, { 0, 0 }, 0.f, pieceCol);
299325 DrawRectangleLines(rect.x, rect.y, rect.width, rect.height, BLACK);
300326 }
@@ -341,7 +367,7 @@
341367 DrawText(TextFormat("%04d", m_Lines), uiX, uiY + 215, 28, SKYBLUE);
342368
343369 if (m_State == GameState::Countdown) {
344- const char *text;
370+ cstr text;
345371
346372 switch (m_CountValue) {
347373 case 3: text = "3"; break;
@@ -361,8 +387,8 @@
361387 m_PauseAlpha = std::min(m_PauseAlpha + GetFrameTime() * 6.f, 1.f);
362388
363389 Font font = GetFontDefault();
364- const char *title = "PAUSE";
365- float titleSize = 60.f;
390+ cstr title = "PAUSE";
391+ f32 titleSize = 60.f;
366392 Vector2 size = MeasureTextEx(font, title, titleSize, 0);
367393
368394 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);
@@ -376,10 +402,10 @@
376402 GetScreenHeight() / 2.f + 20.f
377403 };
378404
379- auto DrawButton = [](Font font, const char *text, Vector2 pos, bool selected) {
380- float scale = selected ? 1.05f + 0.03f * sinf(GetTime() * 8.f) : 1.f;
405+ auto DrawButton = [](Font font, cstr text, Vector2 pos, bool selected) {
406+ f32 scale = selected ? 1.05f + 0.03f * sinf(GetTime() * 8.f) : 1.f;
381407 Color color = selected ? WHITE : Fade(WHITE, 0.5f);
382- float sizePx = 40.f * scale;
408+ f32 sizePx = 40.f * scale;
383409 Vector2 textSize = MeasureTextEx(font, text, sizePx, 0);
384410
385411 DrawTextPro(font, text, pos, { textSize.x / 2.f, textSize.y / 2.f }, 0.f, sizePx, 2.f, color);
@@ -592,19 +618,8 @@
592618 }
593619
594620 if ((IsKeyPressed(KEY_LEFT_CONTROL) || IsKeyPressed(KEY_RIGHT_CONTROL)) && m_CanHold) {
621+ startHoldAnimation();
595622 sys::SndMan::Get().PlaySfx("sfx_hold_block");
596-
597- if (m_Hold == -1) {
598- m_Hold = m_CurPiece;
599- spawn();
600- } else {
601- std::swap(m_CurPiece, m_Hold);
602- m_CurRot = 0;
603- m_CurX = m_HSize / 2 - 2;
604- m_CurY = 0;
605- }
606-
607- m_CanHold = false;
608623 }
609624 }
610625
@@ -746,6 +761,113 @@
746761 };
747762
748763 DrawRectangleRec(r, flash);
764+ }
765+ }
766+
767+ void GameMain::startHoldAnimation() {
768+ if (m_HoldAnim != HoldAnimState::None) return;
769+
770+ m_HoldAnim = HoldAnimState::FadeOut;
771+ m_HoldAnimTimer = 0.f;
772+
773+ f32 cx = 0.f;
774+ f32 cy = 0.f;
775+
776+ for (int i = 0; i < 4; ++i) {
777+ cx += m_CurX + tetrominoShapes[m_CurPiece][m_CurRot][i][0] + 0.5f;
778+ cy += m_CurY + tetrominoShapes[m_CurPiece][m_CurRot][i][1] + 0.5f;
779+ }
780+
781+ cx /= 4.f;
782+ cy /= 4.f;
783+
784+ m_BeamStart = {
785+ boardPos.x + cx * m_SquareSize,
786+ boardPos.y + cy * m_SquareSize,
787+ };
788+
789+ m_BeamEnd = {
790+ holdPos.x + holdSquareSize * 2.f,
791+ holdPos.y + holdSquareSize * 2.f,
792+ };
793+ }
794+
795+ void GameMain::updateHoldAnimation() {
796+ if (m_HoldAnim == HoldAnimState::None) return;
797+ m_HoldAnimTimer += GetFrameTime();
798+
799+ switch (m_HoldAnim) {
800+ case HoldAnimState::FadeOut:
801+ if (m_HoldAnimTimer >= 0.08f) {
802+ m_HoldAnim = HoldAnimState::Beam;
803+ m_HoldAnimTimer = 0.f;
804+ }
805+ break;
806+ case HoldAnimState::Beam:
807+ if (m_HoldAnimTimer >= 0.15f) {
808+ if (m_Hold == -1) {
809+ m_Hold = m_CurPiece;
810+ spawn();
811+ } else {
812+ std::swap(m_CurPiece, m_Hold);
813+ m_CurRot = 0;
814+ m_CurX = m_HSize / 2 - 2;
815+ m_CurY = 0;
816+ }
817+
818+ m_CanHold = false;
819+
820+ m_HoldAnim = HoldAnimState::FadeIn;
821+ m_HoldAnimTimer = 0.f;
822+ }
823+ break;
824+ case HoldAnimState::FadeIn:
825+ if (m_HoldAnimTimer >= 0.08f)
826+ m_HoldAnim = HoldAnimState::None;
827+ break;
828+ default:
829+ break;
830+ }
831+ }
832+
833+ void GameMain::drawHoldAnimation() {
834+ if (m_HoldAnim == HoldAnimState::None) return;
835+ f32 t = m_HoldAnimTimer / 0.15f;
836+ t = std::clamp(t, 0.f, 1.f);
837+ t = t * t * (3.f - 2.f * t);
838+
839+ Vector2 head = {
840+ std::lerp(m_BeamStart.x, m_BeamEnd.x, t),
841+ std::lerp(m_BeamStart.y, m_BeamEnd.y, t),
842+ };
843+
844+ DrawLineEx(m_BeamStart, head, 18.f, Fade(SKYBLUE, 0.20f));
845+ DrawLineEx(m_BeamStart, head, 8.f, Fade(SKYBLUE, 0.80f));
846+ DrawLineEx(m_BeamStart, head, 3.f, SKYBLUE);
847+
848+ DrawCircleV(head, 12.f, SKYBLUE);
849+ DrawCircleV(head, 24.f, Fade(SKYBLUE, 0.25f));
850+
851+
852+
853+ if (m_HoldAnim != HoldAnimState::None) {
854+ f32 alpha = 0.0f;
855+
856+ switch (m_HoldAnim) {
857+ case HoldAnimState::FadeOut:
858+ alpha = (m_HoldAnimTimer / 0.08f) * 0.75f;
859+ break;
860+ case HoldAnimState::Beam:
861+ alpha = 0.65f;
862+ break;
863+ case HoldAnimState::FadeIn:
864+ alpha = (1.0f - m_HoldAnimTimer / 0.08f) * 0.75f;
865+ break;
866+ default:
867+ break;
868+ }
869+
870+ DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), Fade(BLACK, alpha));
749871 }
750872 }
751873