| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | #pragma once |
| 36 | |
| 37 | #include <util/types.hh> |
| 38 | #include <raylib.h> |
| 39 | #include <tetris/tetris.hh> |
| 40 | |
| 41 | namespace tetris::scene { |
| 42 | struct GameMain { |
| 43 | public: |
| 44 | void Init(Game *game); |
| 45 | void FuncUpdate(); |
| 46 | void DrawUpdate(); |
| 47 | void Close(); |
| 48 | |
| 49 | public: |
| 50 | u32 GetScore() const { return m_Score; } |
| 51 | u16 GetLevel() const { return m_Level; } |
| 52 | u16 GetLines() const { return m_Lines; } |
| 53 | |
| 54 | private: |
| 55 | void spawn(); |
| 56 | void lock(); |
| 57 | void updateBoardLayout(); |
| 58 | void drawNextPiece(); |
| 59 | void drawHoldPiece(); |
| 60 | void drawGhostPiece(Vector2 offset); |
| 61 | void clearLines(); |
| 62 | void handleInput(); |
| 63 | bool isValidPos(i8 piece, i8 rot, i8 x, i8 y); |
| 64 | i32 stackHeight() const; |
| 65 | f32 targetDangerPitch() const; |
| 66 | |
| 67 | private: |
| 68 | void createLandingEffect(); |
| 69 | void updateLandingEffects(); |
| 70 | void drawLandingEffects(); |
| 71 | void createClearEffect(i8 row); |
| 72 | void updateClearEffects(); |
| 73 | void drawClearEffects(Vector2 offset); |
| 74 | void updateHoldAnimation(); |
| 75 | void startHoldAnimation(); |
| 76 | void drawHoldAnimation(); |
| 77 | |
| 78 | private: |
| 79 | Game *m_Game = nullptr; |
| 80 | |
| 81 | u32 m_Score = 0; |
| 82 | u16 m_Level = 1; |
| 83 | u16 m_Lines = 0; |
| 84 | u8 m_Gravity = 30; |
| 85 | |
| 86 | u8 m_HSize = 10; |
| 87 | u8 m_VSize = 20; |
| 88 | u8 m_SquareSize = 20; |
| 89 | |
| 90 | i8 m_Hold = -1; |
| 91 | |
| 92 | i8 m_CurPiece = 0; |
| 93 | i8 m_CurRot = 0; |
| 94 | i8 m_CurX = 0; |
| 95 | i8 m_CurY = 0; |
| 96 | |
| 97 | f32 m_FallTimer = 0.f; |
| 98 | f32 m_FallSpeed = .8f; |
| 99 | |
| 100 | bool m_GameOver = false; |
| 101 | bool m_CanHold = true; |
| 102 | |
| 103 | vector<string> m_Bg = { "bg_beach", "bg_city", "bg_mount", "bg_road", "bg_sunset", "bg_waterfall" }; |
| 104 | i8 m_CurBg = 0; |
| 105 | |
| 106 | private: |
| 107 | enum class ParticleType { |
| 108 | Sparkle, |
| 109 | Smoke, |
| 110 | }; |
| 111 | |
| 112 | struct Particle { |
| 113 | Vector2 pos; |
| 114 | Vector2 vel; |
| 115 | Color color; |
| 116 | f32 life; |
| 117 | f32 size; |
| 118 | i8 txIdx; |
| 119 | ParticleType type; |
| 120 | }; |
| 121 | |
| 122 | vector<Particle> m_Particles; |
| 123 | |
| 124 | struct ClearEffect { |
| 125 | i8 row; |
| 126 | f32 timer; |
| 127 | }; |
| 128 | |
| 129 | vector<ClearEffect> m_ClearEffects; |
| 130 | |
| 131 | f32 m_ShakeTime = 0.f; |
| 132 | Vector2 m_ShakeOffset = { 0, 0 }; |
| 133 | |
| 134 | |
| 135 | vector<Texture2D> m_Sparkles; |
| 136 | vector<Texture2D> m_Smokes; |
| 137 | |
| 138 | private: |
| 139 | enum class GameState { |
| 140 | Countdown, |
| 141 | Playing, |
| 142 | Paused, |
| 143 | GameOver, |
| 144 | }; |
| 145 | |
| 146 | enum class PauseOption { |
| 147 | Resume, |
| 148 | Quit, |
| 149 | }; |
| 150 | |
| 151 | enum class HoldAnimState { |
| 152 | None, |
| 153 | FadeOut, |
| 154 | Beam, |
| 155 | FadeIn, |
| 156 | }; |
| 157 | |
| 158 | HoldAnimState m_HoldAnim = HoldAnimState::None; |
| 159 | f32 m_HoldAnimTimer = 0.f; |
| 160 | Vector2 m_BeamStart{}; |
| 161 | Vector2 m_BeamEnd{}; |
| 162 | |
| 163 | GameState m_State = GameState::Countdown; |
| 164 | |
| 165 | f32 m_Countdown = 4.f; |
| 166 | i8 m_CountValue = 3; |
| 167 | f32 m_TextScale = 1.f; |
| 168 | f32 m_TextAlpha = 1.f; |
| 169 | |
| 170 | PauseOption m_PauseSelect = PauseOption::Resume; |
| 171 | f32 m_PauseAlpha = 0.f; |
| 172 | |
| 173 | f32 m_DangerPitch = 1.f; |
| 174 | |
| 175 | private: |
| 176 | void countdown(); |
| 177 | void pause(); |
| 178 | }; |
| 179 | } |