-
Notifications
You must be signed in to change notification settings - Fork 0
/
characters_environment.js
630 lines (478 loc) · 15.6 KB
/
characters_environment.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
/*=================================
= Variables =
=================================*/
/* main character variabes */
var mario, bricks,clouds,mountains,enemyMushrooms,pipes,platforms,coins;
/* Control variabes */
var control={
up: "UP_ARROW", // 32=spaceBar
left: 'LEFT_ARROW',
right: 'RIGHT_ARROW',
revive: 32
}
//Inner game status, which might affect game balance or playability.
var gameConfig={
// start, playing, over
status: "start",
// initial lives of mario
initialLifes: 4,
// character moves speed
moveSpeed: 5,
enemyMoveSpeed: 1,
// gravity and jump speed for all the characters
gravity: 1,
gravityEnemy: 10,
jump:-15,
// character starting point
startingPointX: 500,
startingPointY: 0,
// default canvas size
screenX:1240,
screenY:336,
// scores
timeScores: 0,
scores: 0
}
/*===== End of Variables ======*/
/*====================================
= Game Status =
====================================*/
noseX = "";
noseY = "";
GameStatus = "";
function startGame(){
GameStatus = "start";
document.getElementById("status").innerHTML = "game is loading";
}
function game(){
console.log("x = " + noseX + " y = " + noseY);
instializeInDraw();
moveEnvironment(mario);
drawSprites();
if(gameConfig.status==='start'){
fill(0,0,0,150);
rect(0,0,gameConfig.screenX,gameConfig.screenY);
fill(255, 255, 255);
textSize(40);
textAlign(CENTER);
text("Press the play button ", gameConfig.screenX/2, gameConfig.screenY/2);
textSize(40);
stroke(255);
strokeWeight(7);
noFill();
changeGameStatud();
}
if(gameConfig.status==='play'){
positionOfCharacter(mario);
enemys(enemyMushrooms);
checkStatus(mario);
scores(mario);
manualControl(mario);
// optional control version of game
// autoControl(mario);
}
// if game is over
if(gameConfig.status==='gameover'){
fill(0,0,0,150);
rect(0,0,gameConfig.screenX,gameConfig.screenY);
fill(255, 255, 255);
textSize(40);
textAlign(CENTER);
text("GAME OVER", gameConfig.screenX/2, gameConfig.screenY/2+105);
textSize(15);
text("Press SPACE to Restart", gameConfig.screenX/2, gameConfig.screenY/2+135);
textSize(40);
text(round(gameConfig.scores),gameConfig.screenX/2,gameConfig.screenY/2-35);
text("points",gameConfig.screenX/2,gameConfig.screenY/2);
stroke(255);
strokeWeight(7);
noFill();
ellipse(gameConfig.screenX/2,gameConfig.screenY/2-30,160,160)
changeGameStatud(mario)
}
}
// change game status if any key is pressed
function changeGameStatud(character){
if(GameStatus=="start"&& noseX !="" && gameConfig.status==="start") {
world_start.play();
initializeCharacterStatus(mario);
gameConfig.status= "play";
}
if(gameConfig.status==="gameover" && keyDown(control.revive)) {
gameConfig.status= "start";
}
}
/*===== End of Game Status ======*/
/*=============================================
= Instialize =
=============================================*/
//initialize
function instializeInSetup(character){
frameRate(120);
character.scale=0.35;
initializeCharacterStatus(character)
bricks.displace(bricks);
platforms.displace(platforms);
coins.displace(coins);
coins.displace(platforms);
coins.collide(pipes);
coins.displace(bricks);
// change the scale of clouds
clouds.forEach(function(element){
element.scale=random(1,2);
})
}
function initializeCharacterStatus(character){
// set up the initial config of character
character.scale=0.35;
character["killing"]=0; //while is killing enemy
character["kills"]=0;
character["live"]=true;
character["liveNumber"]=gameConfig.initialLifes;
character["status"]='live';
character["coins"]=0;
character["dying"]=0;
character.position.x=gameConfig.startingPointX;
character.position.y=gameConfig.startingPointY;
}
function instializeInDraw(){
background(109,143,252);
//while killing
if(mario.killing>0){
mario.killing-=1;
}else{
mario.killing=0;
}
// make objects not overlap each other.
pipes.displace(pipes);
enemyMushrooms.displace(enemyMushrooms);
enemyMushrooms.collide(pipes);
clouds.displace(clouds);
// make character not overlap other objects
if(mario.live){
bricks.displace(mario);
pipes.displace(mario);
enemyMushrooms.displace(mario);
platforms.displace(mario);
}
// character config initialize
mario["standOnObj"]=false;
mario.velocity.x=0;
mario.maxSpeed=20;
}
/*===== End of Instialize ======*/
/*============================================
= Interactive Elements =
============================================*/
// Character get coins
function getCoins(coin,character){
if( character.overlap(coin) && character.live && coin.get==false){
character.coins+=1;
coin.get=true;
mario_coin.play();
};
}
// Reappear coin after goin is got.
function coinVanish(coin){
if(coin.get){
coin.position.x=random(50,gameConfig.screenX)+gameConfig.screenX;
coin.get=false;
};
}
/*===== End of Interactive Elements ======*/
/*=============================================
= Main character setting and control =
=============================================*/
/* Make main character standing on objs */
function positionOfCharacter(character){
// Not on the platform
if(character.live){
// See if standing on bricks
platforms.forEach(function(element){ standOnObjs(character,element); });
bricks.forEach(function(element){ standOnObjs(character,element); });
pipes.forEach(function(element){ standOnObjs(character,element); });
// Character affected by gravity
falling(character);
// If character can only jump if standing on the object
if(character.standOnObj) jumping(character);
}
// Coins interaction event
coins.forEach(function(element){
getCoins(element,mario);
coinVanish(element);
});
// EnemyMushrooms interaction event
enemyMushrooms.forEach(function(element){
StepOnEnemy(character,element);
if((element.touching.left||element.touching.right)&&character.live&&character.killing===0) die(mario);
})
// Make it stay in the screen
dontGetOutOfScreen(mario);
}
/* Auto moving character */
function autoControl(character){
character.velocity.x+=gameConfig.moveSpeed;
character.changeAnimation('move');
character.mirrorX(1);
}
/* Manual control character */
function manualControl(character){
if(character.live){
if(noseX < 300){
character.velocity.x-=gameConfig.moveSpeed;
character.changeAnimation('move');
character.mirrorX(-1);
}
if(noseX > 300){
character.velocity.x+=gameConfig.moveSpeed;
character.changeAnimation('move');
character.mirrorX(1);
}
if(!keyDown(control.left)&&!keyDown(control.right)&&!keyDown(control.up)){
character.changeAnimation('stand');
}
}
}
/* Movements of character */
function jumping(character){
if( (noseY < 200 && character.live) || (touchIsDown&&character.live) ){
character.velocity.y+=gameConfig.jump;
mario_jump.play();
}
}
/* Movements of character */
function falling(character){
character.velocity.y += gameConfig.gravity;
character.changeAnimation('jump');
}
/* See if obj1 stand on obj2, mainly for see if standing on the objcs*/
function standOnObjs(obj1,obj2){
var obj1_Left=leftSide(obj1);
var obj1_Right=rightSide(obj1);
var obj1_Up=upSide(obj1);
var obj1_Down=downSide(obj1);
var obj2_Left=leftSide(obj2);
var obj2_Right=rightSide(obj2);
var obj2_Up=upSide(obj2);
var obj2_Down=downSide(obj2);
if(obj1_Right>=obj2_Left&&obj1_Left<=obj2_Right && obj1_Down<=obj2_Up+7 && obj1_Down>=obj2_Up-7){
// println("YES");
obj1.velocity.y = 0;
obj1.position.y=obj2_Up-(obj1.height/2)-1;
obj1.standOnObj= true;
}
}
/* See if obj1 step on obj2 to kill it*/
function StepOnEnemy(obj1,obj2){
var obj1_Left=leftSide(obj1);
var obj1_Right=rightSide(obj1);
var obj1_Up=upSide(obj1);
var obj1_Down=downSide(obj1);
var obj2_Left=leftSide(obj2);
var obj2_Right=rightSide(obj2);
var obj2_Up=upSide(obj2);
var obj2_Down=downSide(obj2);
if(obj1_Right>=obj2_Left&&obj1_Left<=obj2_Right && obj1_Down<=obj2_Up+7 && obj1_Down>=obj2_Up-7 && obj2.live==true && obj2.touching.top){
obj2.live=false;
obj1.killing=30;
mario_kick.play();
obj1.kills++;
if(obj1.velocity.y>=gameConfig.jump*0.8){
obj1.velocity.y=gameConfig.jump*0.8;
}else{
obj1.velocity.y+=gameConfig.jump*0.8;
}
}
}
// make character die if he touched by enemy
function die(character){
character.live=false;
character.dying+=120;
character.liveNumber--;
character.status="dead";
character.changeAnimation('dead');
character.velocity.y-=2;
mario_allHeartsUsed.play();
}
// check character status and response to sprite and game status
function checkStatus(character){
if(character.live==false){
character.changeAnimation('dead');
character.dying-=1;
reviveAfterMusic(character);
}
if(character.live==false && character.liveNumber==0){
gameConfig.status="gameover"
mario_gameover.play();
}
}
// revive after dying music finished
function reviveAfterMusic(character){
if( character.live === false && mario.liveNumber !==0 && character.dying===0 ){
character.live=true;
character.status="live";
character.position.x=500;
character.position.y=40;
character.velocity.y=0;
}
}
/* Make character stay in screen */
function dontGetOutOfScreen(character){
//if mario drop in the holes
if(character.position.y>gameConfig.screenY&&character.live && character==mario){
die(mario);
}
if(character.position.x>gameConfig.screenX-(character.width*0.5)){
character.position.x=gameConfig.screenX-(character.width*0.5);
}else if(character.position.x<character.width*0.5){
if(character==mario){
character.position.x=character.width*0.5;
}else{
character.live=false;
}
}
}
/*===== End of main character setting and control ======*/
/*=============================================
= Enemy setting and control =
=============================================*/
function enemys(enemys){
enemys.forEach(function(enemy){
stateOfEnemy(enemy);
positionOfEnemy(enemy);
enemy.position.x-=gameConfig.enemyMoveSpeed;
});
}
// Check enemy status
function stateOfEnemy(enemy){
if (enemy.live==false||enemy.position.y>gameConfig.screenY+50){
enemy.position.x=random(gameConfig.screenX*1.5,2*gameConfig.screenX+50);
enemy.position.y=random(gameConfig.screenY*0.35,gameConfig.screenY*0.75);
enemy.live=true;
}
}
/* Make enemy standing on objs */
function positionOfEnemy(enemy){
platforms.forEach(function(element){ enemyStandOnObjs(enemy, element); });
bricks.forEach(function(element){ enemyStandOnObjs(enemy, element);});
pipes.forEach(function(element){ enemyStandOnObjs(enemy, element); })
enemy.position.y+=gameConfig.gravityEnemy;
dontGetOutOfScreen(enemy);
}
/* See if obj1 stand on obj2, mainly for see if standing on the objcs*/
function enemyStandOnObjs(obj1,obj2){
var obj1_Left=leftSide(obj1);
var obj1_Right=rightSide(obj1);
var obj1_Up=upSide(obj1);
var obj1_Down=downSide(obj1);
var obj2_Left=leftSide(obj2);
var obj2_Right=rightSide(obj2);
var obj2_Up=upSide(obj2);
var obj2_Down=downSide(obj2);
if(obj1_Right>=obj2_Left&&obj1_Left<=obj2_Right && obj1_Down<=obj2_Up+7 && obj1_Down>=obj2_Up-7){
// println("YES");
obj1.velocity.y = 0;
obj1.position.y=obj2_Up-(obj1.height);
}
}
/*===== End of enemy setting and control ======*/
/*===================================
= Environment =
===================================*/
// call all environment scroll functions
function moveEnvironment(character){
var environmentScrollingSpeed=gameConfig.moveSpeed*0.3;
if(gameConfig.status==='play'){
environmentScrolling(platforms,environmentScrollingSpeed);
environmentScrolling(bricks,environmentScrollingSpeed);
environmentScrolling(clouds,environmentScrollingSpeed*0.5);
environmentScrolling(mountains,environmentScrollingSpeed*0.3);
environmentScrolling(pipes,environmentScrollingSpeed);
environmentScrolling(coins,environmentScrollingSpeed);
environmentScrolling(enemyMushrooms,environmentScrollingSpeed);
character.position.x-=environmentScrollingSpeed;
}
}
// scroll different element in the screen
function environmentScrolling(group,environmentScrollingSpeed){
group.forEach(function(element){
if(element.position.x>-50){
element.position.x-=environmentScrollingSpeed;
}else{
element.position.x=gameConfig.screenX+50;
//if group is bricks, randomize its y position
if(group===bricks){
element.position.y=random(gameConfig.screenY*0.35,gameConfig.screenY*0.75);
}
//if group is bricks or mountains, randomize its x position
if(group===pipes||group===mountains){
element.position.x=random(50,gameConfig.screenX)+gameConfig.screenX;
}
//if group is clouds, randomize its x & y position
if(group===clouds){
element.position.x=random(50,gameConfig.screenX)+gameConfig.screenX;
element.position.y=random(0,gameConfig.screenY*0.5);
element.scale=random(0.3,1.5);
}
if(group===coins){
element.position.x=random(0,gameConfig.screenX)+gameConfig.screenX;
element.position.y=random(gameConfig.screenY*0.2,gameConfig.screenY*0.8);
}
}
})
}
/*===== End of Environment ======*/
/*=====================================
= For Debugging =
=====================================*/
/* for position state of character */
function debugging(character){
strokeWeight(1);
fill(255);
textSize(12);
text(character.dying, 20,20);
text(gameConfig.status, 20,80);
// text("v: "+character.velocity.y,150,20);
noFill();
// outline(tube01);
stroke(251);
strokeWeight(2);
outline(character);
pipes.forEach(function(element){ outline(element); });
enemyMushrooms.forEach(function(element){ outline(element); });
}
// calculate scores of every game
function scores(character){
strokeWeight(0);
fill(255, 255, 255, 71);
textSize(40);
gameConfig.scores=character.coins+character.kills+gameConfig.timeScores;
if(character.live&&gameConfig.status==='play') gameConfig.timeScores+=0.05;
text("scores: "+round(gameConfig.scores),20,40);
text("lives: "+character.liveNumber,20,80);
if(mario.live==false && mario.liveNumber!=0){
fill(0,0,0,150);
rect(0,0,gameConfig.screenX,gameConfig.screenY);
strokeWeight(7);
noFill();
stroke(255);
ellipse(gameConfig.screenX/2,gameConfig.screenY/2-30,150,150)
stroke("red");
var ratio=(character.liveNumber/gameConfig.initialLifes);
arc(gameConfig.screenX/2,gameConfig.screenY/2-30,150,150, PI+HALF_PI,(PI+HALF_PI)+(TWO_PI*ratio));
fill(255, 255, 255);
noStroke();
textAlign(CENTER);
textSize(40);
text(round(character.liveNumber),gameConfig.screenX/2,gameConfig.screenY/2-35);
text("lives",gameConfig.screenX/2,gameConfig.screenY/2);
}
}
/* make outline of obj*/
function outline(obj){ rect(leftSide(obj),upSide(obj),rightSide(obj)-leftSide(obj),downSide(obj)-upSide(obj));}
/* get each side position of obj*/
function leftSide(obj){ return obj.position.x-(obj.width/2);}
function rightSide(obj){ return obj.position.x+(obj.width/2);}
function upSide(obj){ return obj.position.y-(obj.height/2);}
function downSide(obj){ return obj.position.y+(obj.height/2);}
/*===== End of For Debugging ======*/