forked from lfeng1420/BrickGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelectGame.cpp
226 lines (183 loc) · 3.65 KB
/
SelectGame.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
#include "stdafx.h"
#include "SelectGame.h"
static const int GAME_LIFE[GAMEID_MAX] =
{
0, // Over
0, // Select
4, // Tank,
4, // Race,
4, // Snake,
4, // Match,
4, // Frogger,
4, // Pinball,
0, // Tetris,
4, // Flappybird,
0, // TetrisEx,
4, // PinballEx,
4, // HitBrick,
4, // AddBrick,
};
void CSelectGame::Start()
{
CGameBase::Start();
// Init update game interval
m_nUpdateGameInterval = -1;
m_nCurInterval = 0;
// Get game id record
m_nGameID = GET_INTVALUE("GAME", GAMEID_IDMIN);
if (m_nGameID < GAMEID_IDMIN
|| m_nGameID >= GAMEID_MAX)
{
m_nGameID = GAMEID_IDMIN;
}
// Show high score
UpdateGameData(m_nGameID);
// Update immediately
m_nFrameIdx = 0;
Update(PLAY_ANIM_INTERVAL);
// Play BGM
PlayBGM();
}
void CSelectGame::Update(float dt)
{
bool bUpdateFlag = false;
__UpdateGameID(dt, bUpdateFlag);
__UpdateGameAnim(dt, bUpdateFlag);
if (bUpdateFlag)
{
// Update all bricks
UpdateAllBrickState();
}
}
EnGameID CSelectGame::GetGameID()
{
return GAMEID_SELECT;
}
void CSelectGame::OnButtonEvent(const SEventContextButton* pButtonEvent)
{
if (pButtonEvent->nButtonID == BTNID_FIRE)
{
m_nUpdateGameInterval = (pButtonEvent->bPressedFlag ? 0 : -1);
if (pButtonEvent->bPressedFlag)
{
m_nUpdateGameInterval = UPDATE_GAME_INTERVAL;
Update(0);
}
return;
}
if (!pButtonEvent->bPressedFlag)
{
return;
}
switch (pButtonEvent->nButtonID)
{
case BTNID_RIGHT:
{
if(++m_nLevel > LEVEL_MAX)
{
m_nLevel = 0;
}
UpdateGameData();
}
break;
case BTNID_DOWN:
{
if (--m_nSpeed < 0)
{
m_nSpeed = SPEED_MAX;
}
UpdateGameData();
}
break;
case BTNID_LEFT:
{
if (--m_nLevel < 0)
{
m_nLevel = LEVEL_MAX;
}
UpdateGameData();
}
break;
case BTNID_UP:
{
if (++m_nSpeed > SPEED_MAX)
{
m_nSpeed = 0;
}
UpdateGameData();
}
break;
case BTNID_START:
__StartGame();
break;
default:
break;
}
}
void CSelectGame::__StartGame()
{
if (m_nGameID > GAMEID_MAX || m_nGameID < GAMEID_IDMIN)
{
return;
}
// Temp record
SET_INTVALUE("GAME", m_nGameID);
// Stop BGM
PlayBGM(false, false);
// Play start effect
PlayEffect(EFFECT_START);
// Start game
SEventContextGameStart stContext;
stContext.nGameID = m_nGameID;
stContext.nLevel = m_nLevel;
stContext.nSpeed = m_nSpeed;
stContext.nScore = 0;
stContext.nLife = GAME_LIFE[m_nGameID];
g_oEventEngine.FireEvent(EVENT_GAME_START, (const char*)&stContext, sizeof(stContext));
}
void CSelectGame::__UpdateGameAnim(float dt, bool& bUpdateFlag)
{
m_nCurInterval += dt;
if (m_nCurInterval < PLAY_ANIM_INTERVAL)
{
return;
}
// Reset
m_nCurInterval = 0;
// Get anim data
if (!CGameLogic::GetInstance()->GetDataManager()->GetGameAnimData(m_nGameID, m_arrBrickState, m_nFrameIdx))
{
return;
}
// Update frame index
if (++m_nFrameIdx >= GAMEID_ANIM_COUNT)
{
m_nFrameIdx = 0;
}
// Need update
bUpdateFlag = true;
}
void CSelectGame::__UpdateGameID(float dt, bool& bUpdateFlag)
{
if (m_nUpdateGameInterval < 0)
{
return;
}
m_nUpdateGameInterval += dt;
if (m_nUpdateGameInterval < UPDATE_GAME_INTERVAL)
{
return;
}
m_nUpdateGameInterval = 0;
bUpdateFlag = true;
// Next game
if (++m_nGameID >= GAMEID_MAX)
{
m_nGameID = GAMEID_IDMIN;
}
// Show high score
UpdateGameData(m_nGameID);
// Reset frame index
m_nFrameIdx = 0;
m_nCurInterval = PLAY_ANIM_INTERVAL;
}