-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.js
692 lines (538 loc) · 16.3 KB
/
sketch.js
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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
/*
(Project Name: Egg Basket)
*/
////////// Initial Variables //////////////
/* initialization of character, wall positions and item drop array, the key game actors */
let character, leftWallX, rightWallX;
let drops = []; // Array to hold falling items
/* Core Variable Definitions
- eggImage = regular green egg when caught, adds 1+ point excluding any additional bonuses, and when missed deducts health slightly
- supereggImage = larger purple egg when caught, adds 50+ points and deducts health moderately when missed
- carrierImage = main character or basket to catch any drops
- healPUP = healing power up drop image, which recovers health when caught
- bgSound = background sound during gameplay
- crackedSFX = when regular egg misses
- supercrackedSFX = when super egg misses
- levelUpSFX = when level is passed
- pupSFX = when power up is acquired
*/
let eggImage, supereggImage, carrierImage,
healPUP,
lMove, rMove;
let bgSound, crackedSFX, supercrackedSFX, levelUpSFX,
pupSFX;
// initial states and values
let gameState = 'start';
let score = 0;
let currentLevel = 1;
let fallSpeed = 2; // rate of fall speed
let laneWidth = 200; // region of fall space
let scoreIncrement = 1; // bonus increments
let lastBonusUpdateTime = 0; // check for extra points for being in higher health
let levelBonus = 0; // extra points for being in higher level
let pointsRequired = 500; // points to level up
// Highest score earned after a game over
let highScore = 0;
// Key Press States
let keyStates = {
LEFT: false,
RIGHT: false
};
let leftButton, rightButton;
const levelThresholds = [0, 10, 20, 40, 80, 160, 320, 640, 1280, 2560]; // thresholds for levels 1-10, const used as this isn't meant to change in future
/////// General P5 Functions //////////
function preload() {
//Preloading images and sounds
carrierImage = loadImage('images/basket.png');
eggImage = loadImage('images/egg_n.png');
supereggImage = loadImage('images/egg_s.png');
healPUP = loadImage('images/heal.png');
lMove = loadImage('images/larrow.png');
rMove = loadImage('images/rarrow.png');
bgSound = loadSound('sounds/horror_bgm.mp3');
bgSound.setVolume(0.4);
crackedSFX = loadSound('sounds/cracked.mp3');
supercrackedSFX = loadSound('sounds/supercracked.mp3');
levelUpSFX = loadSound('sounds/levelup.mp3');
pupSFX = loadSound('sounds/pup.mp3');
pupSFX.setVolume(0.3);
}
function setup() {
createCanvas(800, 600);
character = new Character();
//bgSound.loop();
leftButton = { x: 110,
y: height - 200,
width: 80,
height: 70 };
rightButton = { x: width - 180,
y: height - 200,
width: 80,
height: 70 };
}
// State Based Programming where sketch loops to different states of the canvas such as start menu, paused menu, gameOver menu and playing menu
function draw() {
background(0);
if (gameState === 'start') {
drawStartButton();
}
else if (gameState === 'playing' || gameState === 'paused') {
// The main game loop logic
if (gameState === 'playing') {
drawWalls(); // walls are barriers that determine the space where the eggs or items drop
//image(lMove,
// leftButton.x,
// leftButton.y,
// leftButton.width,
// leftButton.height);
//image(rMove,
// rightButton.x,
// rightButton.y,
// rightButton.width,
// rightButton.height);
updateLevel();
// Creation of new egg objs
if (frameCount % 60 === 0) {
if (currentLevel > 5 && random() < 0.2) {
drops.push(new SuperEgg());
} else {
drops.push(new Egg());
}
if (currentLevel > 6 && random() < 0.1) {
drops.push(new HealPUP());
}
}
// To check for egg height as its falling
for (let i = drops.length - 1; i >= 0; i--) {
drops[i].update();
drops[i].display();
if (drops[i].y > height) {
if (drops[i].powerUp === 'none') {
if (drops[i].isSuperEgg === true) {
character.missSuperEgg();
}
else {
character.missEgg();
}
}
drops.splice(i, 1); // Egg missed
}
else if (character.catchEgg(drops[i])) {
if (drops[i].powerUp === 'none') {
if (drops[i].isSuperEgg === true) {
score += 50;
}
else {
score += scoreIncrement;
}
}
else
{
if (drops[i].powerUp === 'heal') {
drops[i].power(character);
}
}
drops.splice(i, 1); // Remove egg if caught
}
}
character.move();
}
character.display();
displayLevel(); // Show current level
displayScore(); // Show current score
updateBonusScore();
if (gameState === 'paused') {
displayPaused(); // Display the "Paused" message
}
// Check for game over condition
if (character.healthWidth <= 0) {
updateHighScore();
gameState = 'gameOver';
displayGameOver();
}
} else if (gameState === 'gameOver') {
displayGameOver();
}
}
/////////// Classes ////////////
class Character {
constructor() {
//character init
this.x = width / 2;
this.y = height - 100;
this.size = 70;
this.speed = 5.1; // movement speed, will manipulate in future for power ups
this.healthWidth = width / 2; // Health starts at 50%
this.maxHealthWidth = width; // Max health = Canvas Width
this.healthDecreaseRate = width * 0.05; // when egg cracks
this.healthIncreaseRate = width * 0.02; // when egg !crack
}
display() {
image(carrierImage, this.x, this.y, this.size, this.size);
this.displayHealth();
}
// Keyboard Pressed Movement
move() {
if (keyStates.LEFT) {
this.x -= this.speed;
}
else if (keyStates.RIGHT) {
this.x += this.speed;
}
this.x = constrain(this.x, 0, width - this.size);
}
// Logic for when egg is caught
catchEgg(egg) {
let d = dist(this.x + this.size / 2,
this.y + this.size / 2,
egg.x,
egg.y);
if (d < this.size / 2 + egg.size / 2) {
score += scoreIncrement;
this.healthWidth += this.healthIncreaseRate;
// Ensure health doesn't exceed max width
this.healthWidth = min(this.healthWidth,
this.maxHealthWidth);
return true; // Egg was caught
}
return false; // Egg wasn't caught
}
// Logic for when egg is missed
missEgg() {
crackedSFX.play();
this.healthWidth -= this.healthDecreaseRate;
this.healthWidth = max(this.healthWidth, 0);
}
// Logic for when super egg is missed
missSuperEgg() {
supercrackedSFX.play();
this.healthWidth -= this.healthDecreaseRate * 4;
this.healthWidth = max(this.healthWidth, 0);
}
// Breakpoints for the different colors based on health width
displayHealth() {
let redLimit = this.maxHealthWidth * 0.2; // 20%
let orangeLimit = this.maxHealthWidth * 0.5; // 50%
let yellowLimit = this.maxHealthWidth * 0.8; // 80%
// Red portion
fill(255, 0, 0);
let redWidth = min(this.healthWidth, redLimit);
rect(0, height - 30, redWidth, 20);
// Orange portion
if (this.healthWidth > redLimit) {
fill(255, 165, 0);
let orangeWidth = min(this.healthWidth - redLimit, orangeLimit - redLimit);
rect(redLimit, height - 30, orangeWidth, 20);
}
// Yellow portion
if (this.healthWidth > orangeLimit) {
fill(255, 255, 0);
let yellowWidth = min(this.healthWidth - orangeLimit, yellowLimit - orangeLimit);
rect(orangeLimit, height - 30, yellowWidth, 20);
}
// Green portion
if (this.healthWidth > yellowLimit) {
fill(0, 255, 0);
let greenWidth = this.healthWidth - yellowLimit;
rect(yellowLimit, height - 30, greenWidth, 20);
}
}
}
class Egg {
constructor() {
//Egg Init
this.x = random(leftWallX + 20,
rightWallX - 10); // fall range
this.y = 0; //falls steadily down
this.size = 30; // size of regular egg
this.speed = fallSpeed; //
this.isSuperEgg = false; // super egg or not super egg
this.powerUp = 'none'; // power up type
}
update() {
this.y += this.speed; // speed manipulation
}
display() {
image(eggImage,
this.x,
this.y,
this.size,
this.size);
}
}
//Extended Super Egg class from Egg with now displaying a larger sized purple egg
class SuperEgg extends Egg {
constructor() {
super();
this.size = 50;
this.speed = fallSpeed - 1;
this.isSuperEgg = true
}
// Override the display method to use the Super Egg image
display() {
image(supereggImage,
this.x,
this.y,
this.size,
this.size);
}
}
class HealPUP extends Egg {
constructor() {
super();
this.size = 35;
this.speed = fallSpeed - 0.5;
this.powerUp = 'heal'
}
// Override the display method to use Heal Power Up Image
display() {
image(healPUP,
this.x,
this.y,
this.size,
this.size);
}
power(char) {
char.healthWidth += 50;
char.healthWidth = min(char.healthWidth,
char.maxHealthWidth);
pupSFX.play();
}
}
////// Built-in Event Handlers or User Interactables //////
// When Left or Right Arrow Keys are pressed
function keyPressed() {
if (keyCode === LEFT_ARROW) {
keyStates.LEFT = true;
}
else if (keyCode === RIGHT_ARROW) {
keyStates.RIGHT = true;
}
else if (keyCode === 32) { // 32 is the keyCode for SPACEBAR
if (gameState === 'playing') {
gameState = 'paused';
noLoop(); // Pauses the draw loop
} else if (gameState === 'paused') {
gameState = 'playing';
loop(); // Resumes the draw loop
}
}
}
// When Left or Right Arrow Keys are held / let go
function keyReleased() {
if (keyCode === LEFT_ARROW) {
keyStates.LEFT = false;
}
else if (keyCode === RIGHT_ARROW) {
keyStates.RIGHT = false;
}
}
function mouseClicked() {
if ((gameState === 'start' || gameState === 'gameOver')) {
startGame();
}
else if (gameState === 'paused') {
// Resume the game
gameState = 'playing';
loop(); // Resume the draw loop
}
}
function mousePressed() {
if (overButton(leftButton)) {
keyStates.LEFT = true;
}
else if (overButton(rightButton)) {
keyStates.RIGHT = true;
}
}
function mouseReleased() {
keyStates.LEFT = false;
keyStates.RIGHT = false;
/*
if (gameState === 'playing') {
if (!overButton(leftButton) && !overButton(rightButton)) {
gameState = 'paused';
noLoop(); // Stop the draw loop to pause the game
}
}
*/
}
// !!! Replace with native button classes later !!!
function overButton(button) {
// Check if the mouse is over the given button area
return mouseX > button.x && mouseX < button.x + button.width &&
mouseY > button.y && mouseY < button.y + button.height;
}
// Starts/Restarts the Game
function startGame() {
// Re-initialize variables or start playing state loop
gameState = 'playing';
character = new Character();
drops = []; // Clear any existing drops
currentLevel = 1; // Reset level to 1
fallSpeed = 2;
leftWallX = width / 2 - laneWidth / 2;
rightWallX = width / 2 + laneWidth / 2;
score = 0; // Reset score
// Reset lane width and wall positions
laneWidth = 150;
leftWallX = width / 2 - laneWidth / 2;
rightWallX = width / 2 + laneWidth / 2;
lastScoreUpdateTime = millis(); // Reset bonus score
loop(); // Start the draw loop again
}
////////// Visuals ////////////
function drawStartButton() {
fill(255);
rect(width / 2 - 50, height / 2 - 25, 100, 50);
fill(0);
textAlign(CENTER, CENTER);
textSize(20);
text('Play', width / 2, height / 2);
}
function displayScore() {
fill(255);
textSize(32);
textAlign(RIGHT, TOP);
text(`Score: ${score}`, width - 20, 20);
text(`High Score: ${highScore}`, width - 20, 60);
}
function displayGameOver() {
displayScore();
textAlign(CENTER);
if (currentLevel == 100)
{
textSize(35);
text("Congratulations", width / 2, height / 2 - 10);
}
textSize(32);
fill(255);
text("Game Over", width / 2, height / 2 - 20);
textSize(20);
text('Click to Restart', width / 2, height / 2 + 25);
}
function displayLevel() {
fill(255);
textSize(32);
textAlign(LEFT, TOP);
text(`Level: ${currentLevel}`, 20, 20);
}
function displayPaused() {
fill(255);
textSize(48);
textAlign(CENTER, CENTER);
text("Paused", width / 2, height / 2);
}
function drawWalls() {
fill(204,82,122); // Example wall color
rect(leftWallX, 0, 10, height - 100); // Left wall
rect(rightWallX - 10, 0, 10, height - 100); // Right wall
}
/////////// Gameplay/Level Logic //////////
// New scenarios for new levels, can get really creative here..
function updateLevel() {
if (currentLevel < 10) {
for (let i = 0; i < levelThresholds.length; i++) {
if (score >= levelThresholds[i] && currentLevel === i) {
currentLevel++;
levelUpSFX.play();
break;
}
}
if (currentLevel == 2) {
fallSpeed = 2.5;
}
if (currentLevel == 3) {
fallSpeed = 2.8;
leftWallX = max(0,
width / 2 - laneWidth / 2) + 200;
rightWallX = min(width,
width / 2 + laneWidth / 2) - 200;
}
if (currentLevel == 4) {
fallSpeed = 3.2;
levelBonus = 10;
}
if (currentLevel == 5) {
fallSpeed = 3.5;
leftWallX = max(0,
width / 2 - laneWidth / 2) + 350;
rightWallX = min(width,
width / 2 + laneWidth / 2) - 350;
}
if (currentLevel == 6) {
fallSpeed = 3.7;
}
if (currentLevel == 7) {
fallSpeed = 4.2;
leftWallX = max(0,
width / 2 - laneWidth / 2) + 400;
rightWallX = min(width,
width / 2 + laneWidth / 2) - 400;
}
if (currentLevel == 8) {
fallSpeed = 4.9;
levelBonus = 100;
}
if (currentLevel == 9) {
fallSpeed = 5.8;
character.speed = 5.5;
}
if (currentLevel == 10) {
fallSpeed = 6.9;
leftWallX = max(0,
width / 2 - laneWidth / 2) + 420;
rightWallX = min(width,
width / 2 + laneWidth / 2) - 420;
}
}
else if (currentLevel >= 10 && currentLevel < 100) {
// After level 10
if (score >= levelThresholds[9] + pointsRequired * (currentLevel - 9)) {
currentLevel++;
fallSpeed += 0.5;
levelBonus += 50;
if (character.speed < 8){ // ensures a speed cap
character.speed += 0.1;
}
levelUpSFX.play();
pointsRequired += 1.1 * (levelBonus/2);
}
}
else if (currentLevel === 100) {
// Handle the game over or victory condition
gameState = 'gameOver';
}
}
// Update high score if higher than previous one
function updateHighScore() {
if (score > highScore) {
highScore = score;
}
}
// High Health Score Bonus
function updateBonusScore() {
let healthPercentage = (character.healthWidth / character.maxHealthWidth) * 100;
// Determine the bonus increment based on health percentage
let bonusIncrement = 0;
if (healthPercentage > 80) {
bonusIncrement = 3 + levelBonus; // Green zone
} else if (healthPercentage > 50) {
bonusIncrement = 1 + levelBonus / 2; // Yellow zone
} // Red/Orange zone has no bonus increment
// Apply bonus increment per second
if (millis() - lastBonusUpdateTime >= 1000) {
score += bonusIncrement;
lastBonusUpdateTime = millis();
}
}
////// Extra Stuff or WIP ///////
// Gamepad Implementation
let connectedPads = [];
// Touch Screen Controls
// Bigger Basket Power Up
// Faster Basket Power Up
// Explosive Bomb (Takes Out Half Life)
// Poisonous Bad Egg (Causes Slow Down)
// Instructions/Controls Page
// Proper Button Implementations