forked from lfeng1420/BrickGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTetrisGameEx.cpp
295 lines (242 loc) · 6.75 KB
/
TetrisGameEx.cpp
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include "stdafx.h"
#include "TetrisGameEx.h"
static const char* TETRISEX_DATAPATH = "./tetrisex.json";
void CTetrisGameEx::Start()
{
CTetrisGame::Start();
m_bVisibleFlag = true;
m_nFlashInterval = 0;
}
int CTetrisGameEx::GetShapeCount()
{
return TETRIS_SHAPE_COUNT;
}
void CTetrisGameEx::OnTetrisMoveFinish()
{
if (m_stTetrisData.nShapeID == SHAPEID_BOOM)
{
__OnBoomShapeMoveFinish();
}
else if (m_stTetrisData.nShapeID == SHAPEID_ADD_BRICK
|| m_stTetrisData.nShapeID == SHAPEID_REMOVE_BRICK)
{
// Special shape can not save
m_bVisibleFlag = false;
UpdateShape(m_stTetrisData.nShapeID, m_stTetrisData.stPos);
m_bVisibleFlag = true;
}
else
{
// Sync current shape
UpdateShape(m_stTetrisData.nShapeID, m_stTetrisData.stPos, true);
}
// Check game over
if (m_stTetrisData.stPos.x == 0)
{
// Vibrate
Vibrate();
m_enGameStage = STAGE_FAIL;
return;
}
// Remove rows
RemoveRows();
// Generate new shape
RandomShape();
}
bool CTetrisGameEx::GetShapeBrickState(int nShapeID, int nRowIdx, int nColIdx)
{
bool bVisibleFlag = GetShapeBrickOriginState(nShapeID, nRowIdx, nColIdx);
if (nShapeID >= TETRIS_NORMAL_SHAPE_COUNT)
{
bVisibleFlag = (bVisibleFlag && m_bVisibleFlag);
}
return bVisibleFlag;
}
bool CTetrisGameEx::CheckShapePos(const POSITION& stNextPos, int nShapeID)
{
if (nShapeID == SHAPEID_DOT)
{
if (stNextPos.x < 0 || stNextPos.x >= ROW_COUNT
|| stNextPos.y < 0 || stNextPos.y >= COLUMN_COUNT)
{
return false;
}
if (__GetCurFillBrickRowIdx() == m_stTetrisData.stPos.x)
{
return false;
}
return true;
}
if (nShapeID == SHAPEID_BOOM)
{
int nTetrisColCount = TETRIS_SHAPE_COL_COUNT[nShapeID];
int nTetrisRowCount = TETRIS_SHAPE_ROW_COUNT[nShapeID];
int nLowRowIdx = stNextPos.x - GetShapeRowOffset(nShapeID);
int nHighRowIdx = nLowRowIdx + nTetrisRowCount - 1;
int nLowColIdx = stNextPos.y - GetShapeColOffset(nShapeID);
int nHighColIdx = nLowColIdx + nTetrisColCount - 1;
if (nLowRowIdx < 0 || nLowRowIdx >= ROW_COUNT
|| nHighRowIdx < 0 || nHighRowIdx >= ROW_COUNT
|| nLowColIdx < 0 || nLowColIdx >= COLUMN_COUNT
|| nHighColIdx < 0 || nHighColIdx >= COLUMN_COUNT)
{
return false;
}
for (int nColIdx = nLowColIdx; nColIdx <= nHighColIdx; ++nColIdx)
{
if (GetBrickState(GET_BRICKID(nHighRowIdx, nColIdx)))
{
return false;
}
}
return true;
}
return CTetrisGame::CheckShapePos(stNextPos, nShapeID);
}
void CTetrisGameEx::OnButtonEvent(const SEventContextButton* pButtonEvent)
{
if (m_enGameStage != STAGE_NORMAL)
{
return;
}
if (pButtonEvent->nButtonID == BTNID_FIRE
&& pButtonEvent->bPressedFlag)
{
if (m_stTetrisData.nShapeID == SHAPEID_ADD_BRICK
|| m_stTetrisData.nShapeID == SHAPEID_REMOVE_BRICK)
{
bool bVisibleFlag = false;
int nRowIdx = __GetOperateRowIdx();
if (m_stTetrisData.nShapeID == SHAPEID_ADD_BRICK)
{
--nRowIdx;
bVisibleFlag = true;
}
if (nRowIdx < 0 || nRowIdx >= ROW_COUNT)
{
return;
}
UpdateBrickState(GET_BRICKID(nRowIdx, m_stTetrisData.stPos.y), bVisibleFlag);
RemoveRows();
UpdateValidBricksState();
return;
}
else if (m_stTetrisData.nShapeID == SHAPEID_BOOM)
{
POSITION stNextPos = { m_stTetrisData.stPos.x + 1, m_stTetrisData.stPos.y};
bool bCanMoveFlag = (CheckShapePos(m_stTetrisData.stPos, m_stTetrisData.nShapeID)
&& CheckShapePos(stNextPos, m_stTetrisData.nShapeID));
__OnBoomShapeMoveFinish(!bCanMoveFlag);
// Generate new shape
RandomShape();
UpdateValidBricksState();
return;
}
}
CTetrisGame::OnButtonEvent(pButtonEvent);
}
void CTetrisGameEx::UpdateOther(float dt, bool& bUpdateFlag)
{
if (m_stTetrisData.nShapeID < TETRIS_NORMAL_SHAPE_COUNT)
{
return;
}
m_nFlashInterval += dt;
if (m_nFlashInterval < SHAPE_FLASH_INTERVAL)
{
return;
}
m_nFlashInterval = 0;
m_bVisibleFlag = !m_bVisibleFlag;
UpdateShape(m_stTetrisData.nShapeID, m_stTetrisData.stPos);
bUpdateFlag = true;
}
void CTetrisGameEx::UpdateValidBricksState()
{
for (int nIndex = 0; nIndex < ROW_COUNT * COLUMN_COUNT; ++nIndex)
{
if (m_stTetrisData.nShapeID == SHAPEID_DOT
&& nIndex == GET_BRICKIDBYPOS(m_stTetrisData.stPos))
{
continue;
}
if (GetBrickState(nIndex))
{
m_mapUpdateBricks[nIndex] = true;
}
}
UpdateBrickState(0, GetBrickState(0), true);
}
EnGameID CTetrisGameEx::GetGameID()
{
return GAMEID_TETRISEX;
}
bool CTetrisGameEx::LoadTetrisData()
{
if (!GET_BOOLVALUE("TETRIS_RECORD_VALID", false))
{
return false;
}
return CGameLogic::GetInstance()->GetDataManager()->LoadTetrisData(TETRISEX_DATAPATH,
m_arrBrickState, m_stTetrisData.nShapeID, m_nNextShapeID, m_nCurScore);
}
bool CTetrisGameEx::SaveTetrisData(bool bForceFlag /*= false*/)
{
if (m_enGameStage != STAGE_NORMAL
|| (!bForceFlag && !GET_BOOLVALUE("TETRIS_RECORD_VALID", false)))
{
return false;
}
return CGameLogic::GetInstance()->GetDataManager()->SaveTetrisData(TETRISEX_DATAPATH,
m_arrBrickState, m_stTetrisData.nShapeID, m_nNextShapeID, m_nCurScore);
}
int CTetrisGameEx::__GetCurFillBrickRowIdx()
{
int nTetrisRowCount = TETRIS_SHAPE_ROW_COUNT[m_stTetrisData.nShapeID];
list<int> lsRowIdx;
for (int nRowIdx = ROW_COUNT - 1; nRowIdx >= m_stTetrisData.stPos.x - GetShapeRowOffset(m_stTetrisData.nShapeID) + nTetrisRowCount; --nRowIdx)
{
if (!GetBrickState(GET_BRICKID(nRowIdx, m_stTetrisData.stPos.y)))
{
lsRowIdx.push_back(nRowIdx);
}
}
return lsRowIdx.empty() ? m_stTetrisData.stPos.x : lsRowIdx.back();
}
int CTetrisGameEx::__GetOperateRowIdx()
{
int nTetrisRowCount = TETRIS_SHAPE_ROW_COUNT[m_stTetrisData.nShapeID];
for (int nRowIdx = m_stTetrisData.stPos.x - GetShapeRowOffset(m_stTetrisData.nShapeID) + nTetrisRowCount; nRowIdx < ROW_COUNT; ++nRowIdx)
{
if (GetBrickState(GET_BRICKID(nRowIdx, m_stTetrisData.stPos.y)))
{
return nRowIdx;
}
}
return ROW_COUNT;
}
void CTetrisGameEx::__OnBoomShapeMoveFinish(bool bClearBricksFlag /*= true*/)
{
// Special shape can not save
m_bVisibleFlag = false;
UpdateShape(m_stTetrisData.nShapeID, m_stTetrisData.stPos);
m_bVisibleFlag = true;
// Play effect
PlayEffect(EFFECT_BOOM);
if (!bClearBricksFlag)
{
return;
}
int nTetrisRowCount = TETRIS_SHAPE_ROW_COUNT[m_stTetrisData.nShapeID];
int nTetrisColCount = TETRIS_SHAPE_COL_COUNT[m_stTetrisData.nShapeID];
int nStartRowIdx = m_stTetrisData.stPos.x - GetShapeRowOffset(m_stTetrisData.nShapeID) + nTetrisRowCount;
int nDelRowCount = (BOOM_REMOVE_ROW_COUNT + (m_stTetrisData.bSpeedUpFlag ? (m_nSpeed / 4 + 1) : 0));
for (int nRowIdx = nStartRowIdx; nRowIdx < nStartRowIdx + nDelRowCount && nRowIdx < ROW_COUNT; ++nRowIdx)
{
for (int nColIdx = 0; nColIdx < nTetrisColCount; ++nColIdx)
{
int nAcutalColIdx = m_stTetrisData.stPos.y - GetShapeColOffset(m_stTetrisData.nShapeID) + nColIdx;
UpdateBrickState(GET_BRICKID(nRowIdx, nAcutalColIdx), false);
}
}
}