-
Notifications
You must be signed in to change notification settings - Fork 1
/
catch_the_egg.cpp
625 lines (591 loc) · 21.3 KB
/
catch_the_egg.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
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
#include "ui.h"
#include "gamestate.h"
#include "leaderboard.h"
#include "sound.h"
int width = 1280, height = 720;
typedef enum { NULL_UI, MAIN_UI, IN_GAME_UI, ENTER_NAME_UI, LEADERBOARD_UI, HELP_UI } UIScreen;
Layout* ui;
UIScreen currentUI = NULL_UI;
UIScreen activeUI = MAIN_UI;
UIStyle buttonStyle;
UIStyle buttonLabelStyle;
UIStyle iconButtonStyle;
UIStyle helpLabelStyle;
SVGObject *bg, *helpBg;
SVGObject* closeIcon;
SVGObject* fullscreenIcon;
SVGObject* pauseIcon;
SVGObject *oneChicken, *twoChicken;
Div* gameFormatMenu;
GameState* game = NULL;
GameState* saveGame = NULL;
GameFormat lastFormat;
int lastScore;
Label* scoreLabel;
Label* timeLabel;
int isFullScreen = 0;
void addHelp();
void addMainButtons();
void addGameSelectionButtons(Button* ngButton);
void addLeaderBoardSelectionButtons(Button* lbButton);
void mouseOverButton(UIStyle* style, int enter);
void mouseOverIconButton(UIStyle* style, int enter);
void handleNgButton(Button* b, int button, int state);
void handleLbButton(Button* b, int button, int state);
void handleHelpButton(Button* b, int button, int state);
void handleGameSelection(Button* b, int button, int state);
void handleLeaderBoardSelection(Button* b, int button, int state);
void handleClose(Button* b, int button, int state);
void handleFullScreen(Button* b, int button, int state);
void handlePause(Button* b, int button, int state);
void addTable();
void addEditText();
void addGameOverlay();
void loadUIAssets();
void loadStyles();
void loadUI();
void updateGameUI();
void toggleFullScreen();
void iDraw()
{
if (game != NULL && game == saveGame) {
resumeGame(game);
saveGame = NULL;
}
if (!game || !game->paused) {
iClear();
if (game) drawFrame(game);
if (activeUI != currentUI) {
if (ui) {
clearLayout(ui);
if (currentUI == MAIN_UI && gameFormatMenu) gameFormatMenu = NULL;
}
loadUI();
currentUI = activeUI;
}
if (currentUI == IN_GAME_UI && game) updateGameUI();
renderLayout(ui);
if (game) {
if (isFinished(game)) {
lastFormat = game->format;
lastScore = game->score;
freeGameState(game);
game = NULL;
activeUI = ENTER_NAME_UI;
}
}
}
}
void iMouseMove(int mx, int my) {}
void iMouse(int button, int state, int mx, int my)
{
if (ui) handleMouseClick(ui, button, state, mx, my);
}
void iPassiveMouseMove(int mx, int my)
{
static int mx0 = mx, my0 = my;
if (ui) handlePassiveMouse(ui, mx, my);
}
void iResize(int w, int h) {}
void iKeyboard(unsigned char key)
{
if (ENTER_NAME_UI != activeUI && tolower(key) == 'f') toggleFullScreen();
if (game) keyDown(game, key, 0);
if (ui) handleKeyInput(ui, key);
}
void iSpecialKeyboard(unsigned char key)
{
if (game) keyDown(game, key, 1);
}
void iKeyboardUp(unsigned char key)
{
if (game) keyUp(game, key);
}
void iSpecialKeyboardUp(unsigned char key)
{
if (game) keyUp(game, key);
}
void iExit()
{
if (game) saveGameState(game);
freeSounds();
}
int main(int argc, char* argsv[])
{
loadSounds();
loadStyles();
loadUIAssets();
loadLeaderBoards();
iSetTransparency(1);
iInitializeEx(width, height, 0, "Catch the Egg");
return 0;
}
void loadUI()
{
ui = createLayout(0, 0, 1280, 720);
switch (activeUI) {
case MAIN_UI:
addBackground(ui, bg);
addMainButtons();
break;
case IN_GAME_UI: addGameOverlay(); break;
case ENTER_NAME_UI:
addBackground(ui, bg);
addEditText();
break;
case LEADERBOARD_UI:
addBackground(ui, bg);
addTable();
break;
case HELP_UI:
addBackground(ui, helpBg);
addHelp();
break;
}
}
void loadUIAssets()
{
bg = SVGParse("assets/scene/background.svg");
helpBg = SVGParse("assets/scene/help.svg");
closeIcon = SVGParse("assets/icons/close.svg");
fullscreenIcon = SVGParse("assets/icons/fullscreen.svg");
pauseIcon = SVGParse("assets/icons/pause.svg");
oneChicken = SVGParse("assets/icons/one_chicken.svg");
twoChicken = SVGParse("assets/icons/two_chicken.svg");
}
void loadStyles()
{
buttonStyle = defaultStyle();
buttonLabelStyle = defaultStyle();
buttonLabelStyle.textFill.color = {255, 255, 255};
buttonLabelStyle.fontSize = FONT_MEDIUM;
buttonStyle.flowDirection = FLOW_ROW;
buttonStyle.alignment.h = ALIGN_HCENTER;
buttonStyle.stroke.color = {255, 255, 255};
buttonStyle.margin.t = 20;
buttonStyle.padding = {10, 12, 15, 15};
buttonStyle.stroke.width = 3;
buttonStyle.fill.fill = 1;
buttonStyle.fill.color = {240, 148, 93};
buttonStyle.fill.opacity = 1;
buttonStyle.onMouse = mouseOverButton;
iconButtonStyle = defaultStyle();
iconButtonStyle.fill.fill = 1;
iconButtonStyle.fill.opacity = 0.1;
iconButtonStyle.margin = {10, 10, 10, 10};
iconButtonStyle.padding = {5, 5, 5, 5};
iconButtonStyle.onMouse = mouseOverIconButton;
helpLabelStyle = defaultStyle();
helpLabelStyle.textFill.color = {255, 255, 255};
helpLabelStyle.margin = {5, 5, 10, 10};
}
void toggleFullScreen()
{
isFullScreen = !isFullScreen;
if (isFullScreen) {
glutDestroyWindow(glutGetWindow());
glutGameModeString("1280x720");
glutEnterGameMode();
iInit();
}
else {
glutLeaveGameMode();
glutCreateWindow("Catch The Egg");
iInit();
};
}
void handleResumeSaveGame(Button* b, int button, int state)
{
if (state == GLUT_DOWN) {
assert(saveGame);
game = saveGame;
activeUI = IN_GAME_UI;
}
}
void addMainButtons()
{
saveGame = loadGameState();
Button* button[5];
button[0] = createButton("NEW GAME", NULL);
button[0]->onClick = handleNgButton;
if (saveGame) {
button[1] = createButton("RESUME GAME", NULL);
button[1]->onClick = handleResumeSaveGame;
}
button[2] = createButton("LEADERBOARD", NULL);
button[2]->onClick = handleLbButton;
button[3] = createButton("HELP", NULL);
button[3]->onClick = handleHelpButton;
button[4] = createButton("EXIT", NULL);
button[4]->onClick = handleClose;
for (int i = 0; i < 5; i++) {
if (button[i]) {
button[i]->base.style = buttonStyle;
button[i]->label->base.style = buttonLabelStyle;
if (i == 0) button[i]->base.style.margin.t = 200;
addToLayout(ui, (Element*)button[i]);
}
}
}
void handleGameSelection(Button* b, int button, int state)
{
if (state == GLUT_DOWN) {
assert(!game);
game = createGame((GameFormat)b->base.data.iData);
activeUI = IN_GAME_UI;
}
}
void handleLeaderBoardSelection(Button* b, int button, int state)
{
if (state == GLUT_DOWN) {
lastFormat = (GameFormat)b->base.data.iData;
activeUI = LEADERBOARD_UI;
}
}
void addSelectionButtons(Button* bfButton, int ng)
{
UIStyle style = buttonStyle;
UIStyle labelStyle = buttonLabelStyle;
style.margin.l = 2;
style.alignment.h = ALIGN_LEFT;
labelStyle.alignment.v = ALIGN_VCENTER;
Div* div = createDiv();
div->base.style = defaultStyle();
div->base.style.alignment.h = ALIGN_HCENTER;
div->base.style.width = 0;
div->base.style.height = 0;
div->base.style.flowDirection = FLOW_ROW;
Button* buttons[6];
buttons[0] = createButton("1:30", oneChicken);
buttons[0]->base.data.iData = ONE_THIRTY_X1;
buttons[1] = createButton("1:30", twoChicken);
buttons[1]->base.data.iData = ONE_THIRTY_X2;
buttons[2] = createButton("2:00", oneChicken);
buttons[2]->base.data.iData = TWO_ZERO_X1;
buttons[3] = createButton("2:00", twoChicken);
buttons[3]->base.data.iData = TWO_ZERO_X2;
buttons[4] = createButton("2:30", oneChicken);
buttons[4]->base.data.iData = TWO_THIRTY_X1;
buttons[5] = createButton("2:30", twoChicken);
buttons[5]->base.data.iData = TWO_THIRTY_X2;
for (int i = 0; i < 6; i++) {
buttons[i]->icon->base.style.width = 15 + (i % 2) * 15;
buttons[i]->base.style = style;
buttons[i]->base.style.height = 50;
buttons[i]->label->base.style = labelStyle;
if (ng)
buttons[i]->onClick = handleGameSelection;
else
buttons[i]->onClick = handleLeaderBoardSelection;
addChild((Element*)div, (Element*)buttons[i]);
}
addAfter((Element*)bfButton, (Element*)div);
gameFormatMenu = div;
}
void addGameSelectionButtons(Button* bfButton) { addSelectionButtons(bfButton, 1); }
void addLeaderBoardSelectionButtons(Button* bfButton) { addSelectionButtons(bfButton, 0); }
void mouseOverButton(UIStyle* style, int enter)
{
if (enter) {
style->fill.opacity = 0.75;
style->stroke.color = {255, 220, 212};
}
else {
style->fill.opacity = 1;
style->stroke.color = {255, 255, 255};
}
}
void mouseOverTableRow(UIStyle* style, int enter)
{
if (enter) {
style->fill.fill = 1;
style->fill.opacity = 0.25;
style->fill.color = {255, 255, 255};
style->stroke.color = {255, 255, 255};
}
else {
style->fill.fill = 0;
}
}
void handleNgButton(Button* b, int button, int state)
{
if (state == GLUT_DOWN) {
if (!gameFormatMenu) { addGameSelectionButtons(b); }
else {
removeElement((Element*)gameFormatMenu);
gameFormatMenu = NULL;
}
}
}
void handleLbButton(Button* b, int button, int state)
{
if (state == GLUT_DOWN) {
if (!gameFormatMenu) { addLeaderBoardSelectionButtons(b); }
else {
removeElement((Element*)gameFormatMenu);
gameFormatMenu = NULL;
}
}
}
void handleHelpButton(Button* b, int button, int state)
{
if (state == GLUT_DOWN) { activeUI = HELP_UI; }
}
void mouseOverIconButton(UIStyle* style, int enter)
{
if (enter) { style->fill.opacity = 0.25; }
else {
style->fill.opacity = 0.1;
}
}
void handleClose(Button* b, int button, int state)
{
if (state == GLUT_DOWN) {
switch (activeUI) {
case MAIN_UI: exit(0); break;
case IN_GAME_UI:
if (game) {
saveGameState(game);
freeGameState(game);
game = NULL;
}
activeUI = MAIN_UI;
break;
case HELP_UI:
case LEADERBOARD_UI: activeUI = MAIN_UI; break;
}
}
}
void handleFullScreen(Button* b, int button, int state)
{
if (state == GLUT_DOWN) { toggleFullScreen(); }
}
void handlePause(Button* b, int button, int state)
{
if (state == GLUT_DOWN) {
assert(game && activeUI == IN_GAME_UI);
if (game->paused)
resumeGame(game);
else
pauseGame(game);
}
}
Button* createCloseButton()
{
Button* closeButton = createButton(NULL, closeIcon);
closeButton->base.style = iconButtonStyle;
closeButton->base.style.alignment.h = ALIGN_RIGHT;
closeButton->onClick = handleClose;
closeButton->icon->base.style.height = 30;
closeButton->icon->base.style.width = 30;
closeButton->icon->opacity = 0.5;
return closeButton;
}
Button* createFullScreenButton()
{
Button* fullscreenButton = createButton(NULL, fullscreenIcon);
fullscreenButton->base.style = iconButtonStyle;
fullscreenButton->base.style.alignment.h = ALIGN_RIGHT;
fullscreenButton->base.style.alignment.v = ALIGN_BOTTOM;
fullscreenButton->onClick = handleFullScreen;
fullscreenButton->icon->base.style.height = 30;
fullscreenButton->icon->base.style.width = 30;
fullscreenButton->icon->opacity = 0.5;
return fullscreenButton;
}
Button* createPauseButton()
{
Button* pauseButton = createButton(NULL, pauseIcon);
pauseButton->base.style = iconButtonStyle;
pauseButton->base.style.alignment.h = ALIGN_LEFT;
pauseButton->base.style.alignment.v = ALIGN_BOTTOM;
pauseButton->base.style.padding = {5, 5, 10, 10};
pauseButton->onClick = handlePause;
pauseButton->icon->base.style.height = 30;
pauseButton->icon->base.style.width = 20;
pauseButton->icon->opacity = 0.5;
return pauseButton;
}
void addTable()
{
Table* table = createTable();
Div* title = createDiv();
Label* label = createLabel("LEADERBOARD");
Button* closeButton = createCloseButton();
label->base.style.fontSize = FONT_LARGE;
label->base.style.textFill.color = {255, 255, 255};
title->base.style.alignment.h = ALIGN_HCENTER;
title->base.style.margin.t = 150;
title->base.style.fill.fill = 1;
title->base.style.fill.color = {92, 158, 196};
title->base.style.fill.opacity = 0.5;
title->base.style.padding = {10, 10, 30, 30};
table->base.style.alignment.h = ALIGN_HCENTER;
table->base.style.margin.t = 30;
table->base.style.fill.fill = 1;
table->base.style.fill.color = {191, 214, 217};
table->base.style.fill.opacity = 0.5;
for (int i = 0; i < 11; i++) {
TableRow* row = addTableRow(table);
if (i > 0)
row->base.style.onMouse = mouseOverTableRow;
else {
row->base.style.fill.fill = 1;
row->base.style.fill.color = {92, 158, 196};
table->base.style.fill.opacity = 0.25;
}
for (int j = 0; j < 3; j++) {
char num[16];
TableData* data;
if (i == 0) {
switch (j) {
case 0: data = addTableData(row, "Rank"); break;
case 1: data = addTableData(row, "Name"); break;
case 2: data = addTableData(row, "Score"); break;
}
}
else {
switch (j) {
case 0:
itoa(i, num, 10);
data = addTableData(row, num);
break;
case 1: data = addTableData(row, leaderBoards[lastFormat].entries[i - 1].name); break;
case 2:
itoa(leaderBoards[lastFormat].entries[i - 1].score, num, 10);
data = addTableData(row, num);
break;
}
}
data->base.style = defaultStyle();
data->base.style.padding = {5, 5, 20, 20};
data->text->base.style = defaultStyle();
data->text->base.style.textFill.color = {255, 255, 255};
}
}
addChild((Element*)title, (Element*)label);
addToLayout(ui, (Element*)title);
addToLayout(ui, (Element*)table);
addToLayout(ui, (Element*)closeButton);
}
void enterPlayerName(const char* name)
{
addToLeaderBoard(lastFormat, name, lastScore);
activeUI = LEADERBOARD_UI;
}
void onKeyPlayerName(EditText* editText, unsigned char key)
{
if (key == '\b')
subChar(editText->text);
else if (key == '\r') {
if (editText->text->pos > 0) { enterPlayerName(editText->text->buffer); }
}
else if (editText->text->pos < 32)
addChar(editText->text, key);
}
void addEditText()
{
Div* cont = createDiv();
Div* hLine = createDiv();
EditText* editText = createEditText();
Label* label = createLabel("Enter your name:");
cont->base.style.alignment.h = ALIGN_HCENTER;
cont->base.style.alignment.v = ALIGN_VCENTER;
cont->base.style.padding = {40, 40, 40, 40};
cont->base.style.stroke.color = {255, 255, 255};
cont->base.style.stroke.width = 4;
label->base.style.fontSize = FONT_LARGE;
label->base.style.margin.b = 20;
label->base.style.alignment.h = ALIGN_HCENTER;
label->base.style.textFill.color = {255, 255, 255};
editText->base.style.alignment.h = ALIGN_HCENTER;
editText->base.style.padding = {10, 10, 10, 10};
editText->text->base.style.textFill.color = {255, 255, 255};
editText->onKey = onKeyPlayerName;
hLine->base.style.width = -1;
hLine->base.style.height = 1;
hLine->base.style.stroke.width = 2;
hLine->base.style.stroke.color = {255, 255, 255};
hLine->base.style.alignment.h = ALIGN_HCENTER;
addChild((Element*)cont, (Element*)label);
addChild((Element*)cont, (Element*)editText);
addChild((Element*)cont, (Element*)hLine);
addToLayout(ui, (Element*)cont);
}
void addGameOverlay()
{
UIStyle labelStyle = defaultStyle();
labelStyle.fontSize = FONT_LARGE;
labelStyle.textFill.color = {255, 255, 255};
labelStyle.margin.t = 10;
Button* closeButton = createCloseButton();
Button* fullscreenButton = createFullScreenButton();
Button* pauseButton = createPauseButton();
timeLabel = createLabel("Time: 1:30");
scoreLabel = createLabel("Score: 100");
scoreLabel->base.style = timeLabel->base.style = labelStyle;
scoreLabel->base.style.margin.l = 20;
scoreLabel->base.style.alignment.h = ALIGN_LEFT;
timeLabel->base.style.alignment.h = ALIGN_HCENTER;
addToLayout(ui, (Element*)scoreLabel);
addToLayout(ui, (Element*)timeLabel);
addToLayout(ui, (Element*)closeButton);
addToLayout(ui, (Element*)fullscreenButton);
addToLayout(ui, (Element*)pauseButton);
}
void updateGameUI()
{
assert(game);
char str[32];
sprintf(str, "Score: %d", game->score);
updateText(scoreLabel, str);
int rem = game->duration - floor(game->t - game->start_t);
sprintf(str, "Time: %02d:%02d", rem / 60, rem % 60);
updateText(timeLabel, str);
}
const char* help[] = {"Catch the Egg is a game where a chicken is sitting on a rope and laying eggs.",
"Your task is to catch the egg with the basket. Basket can be moved with the ",
"keyboard left/right arrow or A/D keys.",
"You get 1 points for a egg you catch. But if you are lucky and the chicken",
"lays a blue egg, you get 5 points. The even rarer golden egg will give you",
"10 points. But more often, the chicken might poop. Avoid the poop, you lose",
"10 points if it ends up in your basket.",
"Your goal is to collect as many points as you can in the game time limit.",
"Aside from eggs, the sky can bestow perks on you in different shapes and forms.",
"The thunder perk, makes your basket much faster.",
"Everything falls in much slower velocity if you catch the parachute perk.",
"The basket sizeup, perk, makes your basket much larger.",
"You might never run out of time if you keep catching clock perks.",
"But if you want longer or shorter games anyway, you can just select your desired",
"format or even go crazy with multiple chickens and ropes.",
"Have fun!",
NULL};
void addHelp()
{
Button* closeButton = createCloseButton();
Div* cont = createDiv();
cont->base.style.margin = {20, 20, 20, 20};
cont->base.style.alignment.v = ALIGN_VCENTER;
cont->base.style.height = 0;
cont->base.style.width = 0;
cont->base.style.fill.fill = 1;
cont->base.style.fill.color = {12, 124, 168};
cont->base.style.stroke.color = {12, 124, 168};
cont->base.style.stroke.width = 4;
cont->base.style.fill.opacity = 0.7;
cont->base.style.padding = {10, 10, 10, 10};
Label* title = createLabel("Catch the Egg");
title->base.style.margin = {20, 20, 10, 10};
title->base.style.textFill.color = {255, 255, 255};
title->base.style.fontSize = FONT_LARGE;
title->base.style.alignment.h = ALIGN_HCENTER;
addChild((Element*)cont, (Element*)title);
for (int i = 0; help[i]; i++) {
Label* text = createLabel(help[i]);
text->base.style = helpLabelStyle;
if (i == 0) text->base.style.margin.t = 60;
addChild((Element*)cont, (Element*)text);
}
addToLayout(ui, (Element*)cont);
addToLayout(ui, (Element*)closeButton);
}