-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.h
402 lines (375 loc) · 9.48 KB
/
game.h
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#ifndef game_h
#define game_h
#include "time.h"
#include "input.h"
#include "video.h"
#include "level.h"
#include "commonvars.h"
#include "helperfuncs.h"
#include "sound.h"
#include "printfuncs.h"
#include "cursor.h"
#include "savestate.h"
void updateBackgroundGame()
{
//background
clear_screen(backgroundColor);
//LEVEL:
printMessage(maxBoardBgWidth, 0, "LEVEL:");
//[LEVEL NR] 2 ints
printNumber(maxBoardBgWidth + 4, 1, selectedLevel, 2);
//MOVES:
printMessage(maxBoardBgWidth, 2, "MOVES:");
printNumber(maxBoardBgWidth + 1, 3, moves, 5);
//A:XXXXXX (XXXXXX="ROTATE" or XXXXXX="SLIDE " or XXXXXX="ROSLID")
if(gameMode == gmRotate)
{
printMessage(maxBoardBgWidth, 4, "a:");
printMessage(maxBoardBgWidth, 5, "ROTATE");
}
else
{
if(gameMode == gmSlide)
{
printMessage(maxBoardBgWidth, 4, "a:");
printMessage(maxBoardBgWidth, 5, "SLIDE");
}
else
{
if(gameMode == gmRotateSlide)
{
printMessage(maxBoardBgWidth, 4, "a:");
printMessage(maxBoardBgWidth, 5, "ROSLID");
}
}
}
//B:BACK
printMessage(maxBoardBgWidth, 6, "b:");
printMessage(maxBoardBgWidth, 7, "BACK");
drawLevel();
if (paused)
{
printMessage(0, (maxBoardBgHeight >> 1) - 3, "[**************]");
printMessage(0, (maxBoardBgHeight >> 1) - 2, "|PLEASE CONFIRM+");
printMessage(0, (maxBoardBgHeight >> 1) - 1, "| +");
printMessage(0, (maxBoardBgHeight >> 1) + 0, "| a PLAY +");
printMessage(0, (maxBoardBgHeight >> 1) + 1, "| b TO QUIT +");
printMessage(0, (maxBoardBgHeight >> 1) + 2, "<##############>");
}
if (levelDoneBit && (fpsStep == 0))
{
printMessage(((16 - 13) >> 1), (maxBoardBgHeight >> 1) - 3, "[************]");
printMessage(((16 - 13) >> 1), (maxBoardBgHeight >> 1) - 2, "| LEVEL DONE +");
printMessage(((16 - 13) >> 1), (maxBoardBgHeight >> 1) - 1, "| +");
printMessage(((16 - 13) >> 1), (maxBoardBgHeight >> 1) + 0, "| a CONTINUE +");
printMessage(((16 - 13) >> 1), (maxBoardBgHeight >> 1) + 1, "| b SOLUTION +");
printMessage(((16 - 13) >> 1), (maxBoardBgHeight >> 1) + 2, "<############>");
}
if ((!paused) && (!levelDoneBit))
drawCursors();
}
void initGame()
{
SelectMusic(musGame);
//set background tiles
setBlockTilesAsBackground();
//set sprite for selector / cursor
initCursors();
setCursorPos(0, boardX + selectionX, boardY + selectionY);
showCursors();
levelDoneBit = 0;
paused = 0;
fpsStep = 0;
updateBackgroundGame();
}
void doPause()
{
paused = 1;
setMusicOn(0);
setSoundOn(0);
hideCursors();
needRedraw = 1;
}
void doUnPause()
{
paused = 0;
//music could have been disabled by menu options during pause screen so we use the savestate value
setMusicOn(isMusicOnSaveState());
setSoundOn(isSoundOnSaveState());
setCursorPos(0, boardX + selectionX, boardY + selectionY);
showCursors();
playMenuAcknowlege();
needRedraw = 1;
}
void game()
{
int needRedraw = updateCursorFrame();
if (gameState == gsInitGame)
{
initGame();
gameState -= gsInitDiff;
}
if (fpsStep > 0)
{
fpsStep--;
if (fpsStep == 0)
{
needRedraw = 1;
}
}
if (gamepad_down() == 1)
{
if(!levelDone && !paused)
{
playGameMoveSound();
//if not touching border on bottom
if (selectionY + 1 < boardHeight + posAdd)
{
selectionY += 1;
needRedraw = 1;
}
else
//set to border on top
{
selectionY = -posAdd;
needRedraw = 1;
}
setCursorPos(0, boardX + selectionX, boardY + selectionY);
}
}
if (gamepad_up() == 1)
{
if (!levelDone && !paused)
{
//if not touching border on top
playGameMoveSound();
if (selectionY -1 >= -posAdd)
{
selectionY -= 1;
needRedraw = 1;
}
else
//set to border on bottom
{
selectionY = boardHeight -1 +posAdd;
needRedraw = 1;
}
setCursorPos(0, boardX + selectionX, boardY + selectionY);
}
}
if (gamepad_right() == 1)
{
if (!levelDone && !paused)
{
playGameMoveSound();
//if not touching border on right
if(selectionX + 1 < boardWidth + posAdd)
{
selectionX += 1;
needRedraw = 1;
}
else
//set to border on left
{
selectionX = -posAdd;
needRedraw = 1;
}
setCursorPos(0, boardX + selectionX, boardY + selectionY);
}
}
if (gamepad_left() == 1)
{
if(!levelDone && !paused)
{
playGameMoveSound();
//if not touching border on left
if( selectionX -1 >= -posAdd)
{
selectionX -= 1;
needRedraw = 1;
}
//set to border on right
else
{
selectionX = boardWidth -1 + posAdd;
needRedraw = 1;
}
setCursorPos(0, boardX + selectionX, boardY + selectionY);
}
}
if (gamepad_button_a() == 1)
{
if(paused)
{
doUnPause();
}
else
{
if(!levelDone)
{
if ((selectionX > -1) && (selectionX < boardWidth) &&
(selectionY > -1) && (selectionY < boardHeight))
{
if (gameMode != gmSlide)
{
rotateBlock((int)selectionX + ((int)selectionY * boardWidth));
moves++;
playGameAction();
needRedraw = 1;
}
else
{
playErrorSound();
}
}
else
{
if ((selectionX > -1) && (selectionX < boardWidth))
{
if (selectionY == -1)
{
moveBlockDown((int)selectionX + ((int)(selectionY+1) * boardWidth));
moves++;
playGameAction();
needRedraw = 1;
}
else
{
if (selectionY == boardHeight)
{
moveBlockUp((int)selectionX + ((int)(selectionY-1) * boardWidth));
moves++;
playGameAction();
needRedraw = 1;
}
}
}
else
{
if ((selectionY > -1) && (selectionY < boardHeight))
{
if (selectionX == -1)
{
moveBlockRight((int)(selectionX + 1) + ((int)selectionY * boardWidth));
moves++;
playGameAction();
needRedraw = 1;
}
else
{
if (selectionX == boardWidth)
{
moveBlockLeft( (int)(selectionX - 1) + ((int)selectionY * boardWidth));
moves++;
playGameAction();
needRedraw = 1;
}
}
}
else
{
playErrorSound();
}
}
}
updateConnected();
levelDone = isLevelDone();
if(levelDone)
{
fpsStep = 11; //to display solution for brief moment
SelectMusic(musLevelClear);
//hide cursor it's only sprite we use
hideCursors();
levelDoneBit = 1;
needRedraw = 1;
}
}
else
{
levelDoneBit = 0;
//could have been enabled / disabled using menu's so need to replay as it's blocked from renabling
//during pause leveldonebit = 1
setMusicOn(isMusicOnSaveState());
setSoundOn(isSoundOnSaveState());
//goto next level
if (difficulty == diffRandom)
{
//ned new seed based on time
randomSeedGame = get_date() + get_frame_counter();
initLevel(randomSeedGame);
SelectMusic(musGame);
//show cursor again (it's actually to early but i'm not fixing that)
setCursorPos(0, boardX + selectionX, boardY + selectionY);
showCursors();
needRedraw = 1;
}
else
{
//goto next level if any
if (selectedLevel < maxLevel)
{
selectedLevel++;
unlockLevel(gameMode, difficulty, selectedLevel-1);
initLevel(randomSeedGame);
SelectMusic(musGame);
//show cursor again (it's actually to early but i'm not fixing that)
setCursorPos(0, boardX + selectionX, boardY + selectionY);
showCursors();
needRedraw = 1;
}
else //Goto some congrats screen
{
gameState = gsInitLevelsCleared;
}
}
}
}
}
if (gamepad_button_b() > 0)
{
if (gamepad_button_b() > 1)
{
if (levelDone)
{
fpsStep = 1;
needRedraw = 1;
}
}
if (gamepad_button_b() == 1)
{
if (!levelDone)
{
if (!paused)
{
playMenuBackSound();
doPause();
needRedraw = 1;
}
else
{
//need to reanble early
setSoundOn(isSoundOnSaveState());
hideCursors();
playMenuBackSound();
gameState = gsInitLevelSelect;
doUnPause();
//unpause sets cursor visible !
hideCursors();
//need to reset the level to initial state when going back to level selector
//could not find a better way unfortunatly
//also we do not want to reset the randomseed used for random level generating
//or a new level would have been created when going back we only want the level
//with random to change when pressing left and right in the level selector
//this way it stays consistent with the normal levels
//and the player can replay the level if he wants to
initLevel(randomSeedGame);
}
}
}
}
if (needRedraw)
{
updateBackgroundGame();
}
}
#endif