-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
618 lines (447 loc) · 17.5 KB
/
Copy pathmain.cpp
File metadata and controls
618 lines (447 loc) · 17.5 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
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
#include "raylib.h"
#include <bits/stdc++.h>
enum Level {
MAIN_MENU,
SAVE_POPUP,
GAME,
PAUSED_GAME,
SETTINGS,
EXIT_CONFIRMATION,
TEST // for beta, remove in release build
};
Level lastFrameLevel = MAIN_MENU;
Level currentLevel = MAIN_MENU;
enum TimeOfDay {
SUNRISE,
DAYTIME,
SUNSET,
NIGHTTIME
};
enum Season {
SUMMER,
AUTUMN,
WINTER,
SPRING
};
enum TextureID {
CONKER,
SHADOW,
GROUND,
SUN,
TREE,
SKY_DAY,
SKY_NIGHT,
CONKER_BREAK_SPRITES,
MENU_BG,
LOGO,
SETTINGS_BG,
STUMP,
PLAY_BUTTON,
SETTINGS_BUTTON,
EXIT_BUTTON,
CLOUD1,
TEXTURE_COUNT
};
Texture2D textures[TEXTURE_COUNT];
void load()
{
textures[CONKER] = LoadTexture("assets/conker/conker.png");
textures[SHADOW] = LoadTexture("assets/conker/shadow.png");
textures[GROUND] = LoadTexture("assets/environment/ground.png");
textures[SUN] = LoadTexture("assets/environment/sun.png");
textures[TREE] = LoadTexture("assets/environment/tree.png");
textures[SKY_DAY] = LoadTexture("assets/environment/sky/background_day.png");
textures[SKY_NIGHT] = LoadTexture("assets/environment/sky/background_night.png");
textures[CONKER_BREAK_SPRITES] = LoadTexture("assets/conker/break_spritesheet.png");
textures[MENU_BG] = LoadTexture("assets/gui/menu_background.png");
textures[LOGO] = LoadTexture("assets/gui/sconker_logo_no_bg.png");
textures[SETTINGS_BG] = LoadTexture("assets/gui/settings_background.png");
textures[PLAY_BUTTON] = LoadTexture("assets/gui/buttons/play_button.png");
textures[EXIT_BUTTON] = LoadTexture("assets/gui/buttons/exit_button.png");
textures[SETTINGS_BUTTON] = LoadTexture("assets/gui/buttons/settings_button.png");
textures[CLOUD1] = LoadTexture("assets/environment/clouds/cloud1.png");
textures[STUMP] = LoadTexture("assets/gui/stump.png");
}
// TODO - redraw following textures:
// GROUND, TREE, SUN, SKY_DAY, SKY_NIGHT
// TODO - draw following textures:
// FARMER_CHARACTER, DAY_SKY_OVERLAY, NIGHT_SKY_OVERLAY, DNA
void unload()
{
for (int i = 0; i < TEXTURE_COUNT; i++) {
UnloadTexture(textures[i]);
}
}
// helper functions
std::string toLower(std::string s) {
std::transform(s.begin(), s.end(), s.begin(), ::tolower);
return s;
}
bool hoveringTexture(Texture2D tex, Vector2 pos, float scale) {
Vector2 mouse = GetMousePosition();
return !(mouse.x < pos.x || mouse.y < pos.y ||
mouse.x > pos.x + tex.width * scale ||
mouse.y > pos.y + tex.height * scale);
}
float clamp(float num, float limitA, float limitB) {
if (num >= limitA && num <= limitB) {
return num;
} else if (num < limitA) {
return limitA;
} else if (num > limitB) {
return limitB;
} else {
return num;
}
}
// animation classes
class AnimObject {
public:
bool isBasic = true;
// basic anim
float current;
float start;
float end;
// spritesheet anim
int frames;
int frameWidth;
int frameHeight;
bool loop;
// all anims
double startTime;
float length;
double timePassed;
AnimObject(float start, float end, float length, float offset)
{
this->start = start;
this->end = end;
this->length = length;
startTime = GetTime() - offset;
timePassed = 0;
}
AnimObject(int frames, int frameWidth, int frameHeight, float length, bool loop, float offset)
{
isBasic = false;
this->frames = frames;
this->frameWidth = frameWidth;
this->frameHeight = frameHeight;
this->length = length;
this->loop = loop;
startTime = GetTime() - offset;
timePassed = 0;
}
};
class AnimHandler {
private:
std::map<int, AnimObject> playingAnims;
public:
float quadraticInOut(int id) {
AnimObject &anim = playingAnims.at(id);
if (!anim.isBasic) return 0.0f;
float t = (GetTime() - anim.startTime) / anim.length;
float x;
if (t >= 1) {
x = 1;
} else {
x = (t < 0.5) ? t*t*2 : -1+(4-2*t)*t;
}
anim.timePassed = GetTime() - anim.startTime;
anim.current = anim.start + (anim.end - anim.start) * x;
return anim.start + (anim.end - anim.start) * x;
}
float quadraticIn(int id) {
AnimObject &anim = playingAnims.at(id);
if (!anim.isBasic) return 0.0f;
float t = (GetTime() - anim.startTime) / anim.length;
float x;
if (t >= 1) {
x = 1;
} else {
x = t*t;
}
anim.timePassed = GetTime() - anim.startTime;
anim.current = anim.start + (anim.end - anim.start) * x;
return anim.start + (anim.end - anim.start) * x;
}
float quadraticOut(int id) {
AnimObject &anim = playingAnims.at(id);
if (!anim.isBasic) return 0.0f;
float t = (GetTime() - anim.startTime) / anim.length;
float x;
if (t >= 1) {
x = 1;
} else {
x = t * (2 - t);
}
anim.timePassed = GetTime() - anim.startTime;
anim.current = anim.start + (anim.end - anim.start) * x;
return anim.start + (anim.end - anim.start) * x;
}
float linear(int id) {
AnimObject &anim = playingAnims.at(id);
if (!anim.isBasic) return 0.0f;
float t = (GetTime() - anim.startTime) / anim.length;
float x;
if (t >= 1) {
x = 1;
} else {
x = t;
}
anim.timePassed = GetTime() - anim.startTime;
anim.current = anim.start + (anim.end - anim.start) * x;
return anim.start + (anim.end - anim.start) * x;
}
Rectangle spriteAnim(int id) {
AnimObject &anim = playingAnims.at(id);
if (!anim.isBasic) {
float t = (GetTime() - anim.startTime) / anim.length;
int i = t*anim.frames;
if (t >= 1) {
if (anim.loop) {
t = std::fmod(t, 1);
} else {
i = anim.frames - 1;
}
}
anim.timePassed = GetTime() - anim.startTime;
return Rectangle{(float)i*anim.frameWidth, 0, (float)anim.frameWidth, (float)anim.frameHeight};
}
}
void createAnim(int id, float startV, float endV, float length, float offset = 0.0f) { // create a basic anim | quadratic, linear
if (playingAnims.count(id) != 1) {
playingAnims.insert({id, AnimObject(startV, endV, length, offset)});
}
}
void createSpriteAnim(int id, int frameAmount, int frameW, int frameH, float length, bool loop = false, float offset = 0.0f) { // create a spritesheet anim | spriteAnim()
if (playingAnims.count(id) != 1) {
playingAnims.insert({id, AnimObject(frameAmount, frameW, frameH, length, loop, offset)});
}
}
void overrideAnim(int id, float startV, float endV, float length, float offset = 0.0f) {
playingAnims.at(id) = AnimObject(startV, endV, length, offset);
}
AnimObject getAnim(int id) {
return playingAnims.at(id);
}
bool animFinished(int id) {
return playingAnims.at(id).timePassed >= playingAnims.at(id).length;
}
void stopAnim(int id) {
if (playingAnims.count(id) > 0) playingAnims.erase(id);
}
void stopAnims(std::vector<int> ids) {
for (int id : ids) {
stopAnim(id);
}
}
};
AnimHandler animHandler;
void drawButton(int id, Texture2D texture, Vector2 buttonPos, float startScale, float endScale, float length)
{
bool isHovering = hoveringTexture(texture, buttonPos, startScale);
static std::unordered_map<int, bool> wasHovering;
animHandler.createAnim(id, startScale, startScale, length);
float animLen = clamp(length - animHandler.getAnim(id).timePassed, 0, length);
animLen = length - animLen;
if (isHovering && !wasHovering[id]) {
animHandler.overrideAnim(
id,
animHandler.getAnim(id).current,
endScale,
animLen
);
}
if (!isHovering && wasHovering[id]) {
animHandler.overrideAnim(
id,
animHandler.getAnim(id).current,
startScale,
animLen
);
}
float scale = animHandler.quadraticInOut(id);
wasHovering[id] = isHovering;
buttonPos.x -= (scale * texture.width - startScale * texture.width) / 2;
buttonPos.y -= (scale * texture.height - startScale * texture.height) / 2;
DrawTextureEx(
texture,
buttonPos,
0.0f,
scale,
WHITE
);
}
// draw levels
void handleMenu() {
Vector2 scrDimensions = {GetScreenWidth(), GetScreenHeight()};
// bg
DrawTextureRec(textures[SKY_DAY], Rectangle{0, 0, scrDimensions.x, scrDimensions.y}, Vector2{0, 0}, WHITE);
// clouds
animHandler.createAnim(4, scrDimensions.x*0.25f, scrDimensions.x*0.4f, 27);
if (animHandler.animFinished(4)) {
animHandler.overrideAnim(4, animHandler.getAnim(4).end, animHandler.getAnim(4).start, 27);
}
DrawTextureEx(textures[CLOUD1], Vector2{animHandler.quadraticInOut(4), scrDimensions.y*0.2f}, 0, scrDimensions.x/1400.0f, WHITE);
animHandler.createAnim(5, scrDimensions.x*0.45f, scrDimensions.x*0.35f, 21, 3);
if (animHandler.animFinished(5)) {
animHandler.overrideAnim(5, animHandler.getAnim(5).end, animHandler.getAnim(5).start, 21);
}
DrawTextureEx(textures[CLOUD1], Vector2{animHandler.quadraticInOut(5), scrDimensions.y*0.6f}, 0, scrDimensions.x/1400.0f, WHITE); // TODO - draw new cloud texture, make it unique
// static things
DrawTextureEx(textures[LOGO], Vector2{scrDimensions.x*0.4f, scrDimensions.y*0.15f}, 0.0f, scrDimensions.x/10000*4, WHITE); // logo
DrawTextureEx(textures[CONKER], Vector2{scrDimensions.x*0.6f, scrDimensions.y*0.69f}, 0.0f, scrDimensions.x/10000*7.1, WHITE); // conker
DrawTextureEx(textures[STUMP], Vector2{scrDimensions.x*0.1f, 0}, 0.0f, scrDimensions.y/1958.0f, WHITE); // stump
char *verText = "beta 0.1";
int size = scrDimensions.y*0.03;
DrawText(verText, scrDimensions.x-MeasureText(verText, size)-10, scrDimensions.y-size, size, WHITE);
// buttons
drawButton(1, textures[PLAY_BUTTON], Vector2{scrDimensions.x*0.073f, scrDimensions.y*0.01f}, scrDimensions.x/1300.0f, scrDimensions.x/1200.0f, 0.2f); // play
drawButton(2, textures[SETTINGS_BUTTON], Vector2{scrDimensions.x*0.045f, scrDimensions.y*0.36f}, scrDimensions.x/1400.0f, scrDimensions.x/1300.0f, 0.2f); // settings
drawButton(3, textures[EXIT_BUTTON], Vector2{scrDimensions.x*0.083f, scrDimensions.y*0.67f}, scrDimensions.x/1250.0f, scrDimensions.x/1150.0f, 0.2f); // exit
// handle button presses
if (hoveringTexture(textures[PLAY_BUTTON], Vector2{scrDimensions.x*0.073f, scrDimensions.y*0.01f}, scrDimensions.x/1200.0f) && IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
currentLevel = GAME;
}
if (hoveringTexture(textures[SETTINGS_BUTTON], Vector2{scrDimensions.x*0.045f, scrDimensions.y*0.36f}, scrDimensions.x/1400.0f) && IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
currentLevel = SETTINGS;
}
if (hoveringTexture(textures[EXIT_BUTTON], Vector2{scrDimensions.x*0.083f, scrDimensions.y*0.67f}, scrDimensions.x/1150.0f) && IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
currentLevel = EXIT_CONFIRMATION;
}
}
void handleSettings() { // placeholder for now
Vector2 scrDimensions = {GetScreenWidth(), GetScreenHeight()};
DrawTextureRec(textures[SETTINGS_BG], Rectangle{0, 0, scrDimensions.x, scrDimensions.y}, Vector2{0, 0}, WHITE);
}
void handleTest() { // remove in release
Vector2 scrDimensions = {GetScreenWidth(), GetScreenHeight()};
DrawTextureRec(textures[SKY_DAY], Rectangle{0, 0, scrDimensions.x, scrDimensions.y}, Vector2{0, 0}, WHITE);
animHandler.createSpriteAnim(6, 12, 500, 500, 1.5, true);
Rectangle rec = animHandler.spriteAnim(6);
DrawTextureRec(textures[CONKER_BREAK_SPRITES], rec, Vector2{0, 0}, WHITE);
}
void handleGame() {
/*
game updates happen 20 times every seconds, so ups = 20 (updates per second)
visual, input updates happen every frame
*/
static double accumulator;
static int totalUpdates; // get from save file
const float tickTime = 0.05f; // 1/20 = 0.05
float frameTime = GetFrameTime();
static Season currentSeason = SUMMER; // based on the time gotten from the save file
static TimeOfDay currentTimeOfDay = DAYTIME;
Vector2 scrDimensions = {GetScreenWidth(), GetScreenHeight()};
accumulator += frameTime;
if (accumulator >= tickTime) {
// ------------------------------
// | GAME UPDATES |
// ------------------------------
// 10 minutes = 1 day ( 1 - sunrise / 4 - day / 1 - sunset / 4 - night )
// 0-1 = sunrise | 1-5 = day | 5-6 = sunset | 6-10 = night
// 1 season = 7 days
// 1 year = 28 days
int yearTime = totalUpdates % (28*10*60*20); // 28 days * 10 minutes * 60 seconds * 20 updates | num range = [ 0, 336000 )
int dayTime = totalUpdates % (10*60*20); // 1 day * 10 minutes * 60 seconds * 20 updates | num range = [ 0, 12000 )
if (dayTime <= 60*20) {
if (currentTimeOfDay != SUNRISE) {
currentTimeOfDay = SUNRISE;
}
} else if (dayTime <= 5*60*20) {
if (currentTimeOfDay != DAYTIME) {
currentTimeOfDay = DAYTIME;
}
} else if (dayTime <= 6*60*20) {
if (currentTimeOfDay != SUNSET) {
currentTimeOfDay = SUNSET;
}
} else {
if (currentTimeOfDay != NIGHTTIME) {
currentTimeOfDay = NIGHTTIME;
}
}
if (yearTime <= 7*10*60*20) { // summer [0, 84000]
if (currentSeason != SUMMER) {
currentSeason = SUMMER;
}
} else if (yearTime <= 14*10*60*20) { // autumn
if (currentSeason != AUTUMN) {
currentSeason = AUTUMN;
}
} else if (yearTime <= 21*10*60*20) { // winter
if (currentSeason != WINTER) {
currentSeason = WINTER;
}
} else { // spring
if (currentSeason != SPRING) {
currentSeason = SPRING;
}
}
TraceLog(LOG_DEBUG, std::to_string(dayTime).c_str());
totalUpdates += 1; // 2x, 3x speed mode/upgrade ?
accumulator -= tickTime;
}
// ------------------------------
// | INPUT UPDATE |
// ------------------------------
// ------------------------------
// | VISUAL UPDATE |
// ------------------------------
switch (currentTimeOfDay) {
case DAYTIME:
DrawTextureRec(textures[SKY_DAY], Rectangle{0, 0, scrDimensions.x, scrDimensions.y}, Vector2{0, 0}, WHITE);
break;
case NIGHTTIME:
DrawTextureRec(textures[SKY_NIGHT], Rectangle{0, 0, scrDimensions.x, scrDimensions.y}, Vector2{0, 0}, WHITE);
break;
case SUNSET:
DrawTextureRec(textures[SKY_NIGHT], Rectangle{0, 0, scrDimensions.x, scrDimensions.y}, Vector2{0, 0}, WHITE);
DrawTexture(textures[CONKER], 0, 0, WHITE); // placeholder to identify it changed
break;
case SUNRISE:
DrawTextureRec(textures[SKY_NIGHT], Rectangle{0, 0, scrDimensions.x, scrDimensions.y}, Vector2{0, 0}, WHITE);
DrawTexture(textures[CLOUD1], 0, 0, WHITE); // placeholder to identify it changed
break;
default:
break;
}
}
int main()
{
const int screenWidth = 1920;
const int screenHeight = 1080;
bool running = true;
SetTargetFPS(0); // uncap fps
SetTraceLogLevel(LOG_ALL);
SetConfigFlags(FLAG_VSYNC_HINT); // enables vsync
InitWindow(screenWidth, screenHeight, "Sconker");
load();
SetExitKey(KEY_NULL);
while (!WindowShouldClose() && running) {
BeginDrawing();
switch (currentLevel) {
case MAIN_MENU:
handleMenu();
break;
case GAME:
handleGame();
break;
case SETTINGS:
handleSettings();
break;
case EXIT_CONFIRMATION:
running = false; // TODO - make this an actual exit confirmation
case TEST:
handleTest();
break;
default:
currentLevel = MAIN_MENU;
handleMenu();
}
EndDrawing();
lastFrameLevel = currentLevel;
}
unload();
CloseWindow();
return 0;
}