forked from DKU-EmbeddedSystem-Lab/2025_DKU_OpenSourceBasic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameState.hpp
More file actions
142 lines (127 loc) · 4.57 KB
/
GameState.hpp
File metadata and controls
142 lines (127 loc) · 4.57 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// GameState.cpp
#include "GameState.hpp"
#include "game.hpp"
#include <SDL.h>
#include <cmath>
GameState::GameState(InputManager* manager)
: State(manager), currentPhase(GAME_STARTED), board(nullptr),
hold_block_first_time(true), hold_block_used(false),
game_just_started(true), isExited(false),
logic(nullptr), renderer(nullptr) {}
GameState::~GameState() {
if (!isExited) {
exit();
}
}
void GameState::initialize() {
currentPhase = GAME_STARTED;
isExited = false;
cleanup();
board = new Board;
logic = new GameLogic(board);
renderer = new GameRenderer(board);
srand(static_cast<unsigned>(time(0)));
nextPiece.piece_type = logic->getRandom(0, 6);
nextPiece.rotation = 0;
nextPiece.r = config::next_box_y;
nextPiece.c = config::next_box_x;
logic->createNewPiece(currentPiece, nextPiece);
}
void GameState::exit() {
if (isExited) return;
isExited = true;
cleanup();
}
void GameState::cleanup() {
delete board; board = nullptr;
delete logic; logic = nullptr;
delete renderer; renderer = nullptr;
}
void GameState::run() {
switch (currentPhase) {
case GAME_STARTED: {
if (game_just_started) {
time_snap1 = SDL_GetTicks();
game_just_started = false;
}
unsigned long long ms_passed = SDL_GetTicks() - time_snap1;
if (ms_passed < 3000) {
while (mInputManager->pollAction()) {
if (mInputManager->isGameExiting()) {
nextStateID = STATE_EXIT;
break;
}
if (mInputManager->getAction() == Action::back) {
Game::getInstance()->popState();
return;
}
}
Game::getInstance()->mRenderer->clearScreen();
draw();
int countdown_time = std::ceil((3000 - ms_passed) / 1000.0);
renderer->drawCountdown(countdown_time);
Game::getInstance()->mRenderer->updateScreen();
} else {
currentPhase = GAME_PLAYING;
time_snap1 = SDL_GetTicks();
}
break;
}
case GAME_PLAYING: {
if (mInputManager->isGameExiting()) {
nextStateID = STATE_EXIT;
} else if (!logic->isGameOver()) {
bool shouldReturn = false;
while (mInputManager->pollAction()) {
if (mInputManager->getAction() == Action::back) {
Game::getInstance()->popState();
shouldReturn = true;
break;
} else {
logic->handleEvent(mInputManager->getAction(), currentPiece, nextPiece, holdPiece,
hold_block_first_time, hold_block_used, game_just_started);
}
}
if (shouldReturn) return;
time_snap2 = SDL_GetTicks();
if (time_snap2 - time_snap1 >= static_cast<unsigned long long>(logic->getDropInterval() * 1000)) {
logic->movePieceDown(currentPiece, nextPiece, holdPiece, hold_block_used, game_just_started);
time_snap1 = SDL_GetTicks();
}
Game::getInstance()->mRenderer->clearScreen();
draw();
Game::getInstance()->mRenderer->updateScreen();
} else {
currentPhase = GAME_FINISHED;
}
break;
}
case GAME_FINISHED: {
if (!mInputManager->isGameExiting()) {
while (mInputManager->pollAction() != 0) {
if (mInputManager->getAction() == Action::back) {
Game::getInstance()->popState();
return;
}
}
Game::getInstance()->mRenderer->clearScreen();
draw();
renderer->drawGameOver();
Game::getInstance()->mRenderer->updateScreen();
} else {
nextStateID = STATE_EXIT;
}
break;
}
}
}
void GameState::update() {
// No-op
}
void GameState::draw() {
ghostPiece = currentPiece;
while (board->isPositionLegal(ghostPiece)) ghostPiece.r++;
ghostPiece.r--;
renderer->drawAll(currentPiece, nextPiece, holdPiece, ghostPiece,
hold_block_first_time, logic->getStage(), logic->getScore());
}