-
Notifications
You must be signed in to change notification settings - Fork 1
/
full.js
559 lines (461 loc) · 13.3 KB
/
full.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
// ********** Start of Chroma.js **********
var background; // Background Image
var pc; // The player obj
var platforms; //Array holding all the platform obj.s
var startTime;
var screenWidth = 1000;
var screenHeight = 600;
var clearPlatforms; // A boolean true when a platform is off the screen to the left
var music = new Audio("ChoazFantasy.mp3");
var prevLevel;
var level;
var timeIncrement = 10; // Number of seconds between level
var initialGameSpeed = 1;
var gameSpeed;
var gameSpeedIncrement = 0.2; // How much the game speeds up each time
var platformSpeed = 1.7;
var timePassed;
var secondsPassed; // The number of seconds since the game started
var gameOn = false;
var paused = false;
var backgroundLayer, mainLayer, hudLayer;
var highscore = 0;
function init(){
backgroundLayer = project.activeLayer;
mainLayer = new Layer();
hudLayer = new Layer();
musicControlLayer = new Layer();
mainLayer.activate();
initBackground();
initMusic();
initWelcome();
}
function newGame() {
mainLayer.removeChildren();
level = 1;
gameSpeed = initialGameSpeed;
timePassed = 0;
secondsPassed = 0;
pc = new PC(250,50);
platforms = []
platforms.push(new Platform(new Rectangle(200,275, 350,30), platformSpeed,0));
for(var i=0; i<8; i++)
newPlatform();
gameOn = true;
newHud();
startTime = Date.now();
}
var soundButton; //A path, clicking it toggles the music
function initMusic() {
musicControlLayer.activate();
soundButton = new Raster("volumeMed");
soundButton.position = new Point(950, 550);
soundButton.box = new Path.Rectangle(925,525, 50,50);
soundButton.box.fillColor = new Color(1,1,1, 0.00001); //Needs to be filled in for onMouseDown to work
soundButton.box.onMouseDown = function(event){
switch (music.volume) {
case 0.5:
music.volume = 1;
soundButton.image = document.getElementById("volumeHigh")
break;
case 1:
music.volume = 0;
soundButton.image = document.getElementById("volumeOff")
break;
case 0:
music.volume = 0.25;
soundButton.image = document.getElementById("volumeLow")
break;
case 0.25:
music.volume = 0.5;
soundButton.image = document.getElementById("volumeMed")
break;
default:
music.volume = 1;
soundButton.image = document.getElementById("volumeHigh")
break;
}
};
mainLayer.activate();
music.play();
music.loop = true;
music.volume = 1;
}
// The GAME LOOP
function onFrame(event) {
onFrameBackground();
if(gameOn && !paused) {
pc.move();
updateTime(event);
}
if(!paused)
for (var i=0; i<platforms.length; i++) {
platforms[i].move();
}
if(clearPlatforms) {
if (platforms[0].offScreen) {
platforms.shift();
newPlatform();
}
}
}
function updateTime(event) {
if(event.delta > 0.05){
pause();
return;
}
timePassed += event.delta;
secondsPassed = Math.floor(timePassed);
if(secondsPassed % timeIncrement == 0)
if (level < 1 + secondsPassed / timeIncrement)
nextLevel();
updateHud();
}
function nextLevel() {
level++;
gameSpeed = initialGameSpeed + gameSpeedIncrement * (level - 1);
updateHudNewLevel();
}
function newPlatform() {
var lastPlat = platforms[platforms.length-1];
var newOriginPoint;
do {
newOriginPoint = lastPlat.box.topRight+new Point(randomInt(-100, Math.min(100+10*level, 500)), randomInt(-250,250));
} while (newOriginPoint.y > 550 || newOriginPoint.y < 120);
platforms.push(new Platform(new Rectangle(newOriginPoint, new Size(randomInt(100,350), 30)),platformSpeed, randomInt(0,3)));
}
function randomInt(min, max) {
return Math.floor(Math.random()*(max-min)+min);
}
function pause() {
paused = true;
pausedHud();
}
function unpause(){
paused = false;
unpausedHud();
}
function pauseToggle() {
if(!paused)
pause();
else
unpause();
}
function gameOver() {
gameOn = false;
if(navigator.cookieEnabled)
highscore = getScoreCookie();
if(secondsPassed > highscore){
setScoreCookie(secondsPassed);
highscore = secondsPassed;
}
gameOverHud();
}
function setScoreCookie(score) {
if(!navigator.cookieEnabled)
return;
var d = new Date();
d.setTime(d.getTime() + (10*24*60*60*1000)); // 10 days to expiry
var expires = "expires="+ d.toUTCString();
document.cookie = "score=" + score + ";" + expires + ";path=/";
}
function getScoreCookie(){
var decodedCookie = decodeURIComponent(document.cookie);
var splitted = decodedCookie.split("=");
var cookieScore = splitted[splitted.length - 1]
return cookieScore;
}
function onKeyDown(event){
switch (event.key) {
case 'f':
pc.nextC();
break;
case 'd':
pc.prevC();
break;
case 'space':
if(!gameOn)
newGame();
break;
default:
;
}
}
function onKeyUp(event) {
if(event.key == 'space')
pc.peaked = true;
if(event.key == 'p' && gameOn)
pauseToggle();
}
// ********** End of Chroma.js **********
// ********** Start of BACKGROUND.js **********
var backCirc;
function initBackground() {
backgroundLayer.activate();
backCirc = new Path.Circle({
center: view.center,
radius: view.bounds.height * 0.45,
opacity: 0.8
});
backCirc.fillColor = {
gradient: {
stops: [new Color(1,1,0), new Color(1,1,1)],
radial: true
},
origin: backCirc.position,
destination: backCirc.bounds.rightCenter
};
mainLayer.activate();
}
function onFrameBackground(event){
backCirc.fillColor.gradient.stops[0].color.hue+= 0.3;
}
// ********** End of BACKGROUND.js **********
function initWelcome() {
platforms = []
platforms.push(new Platform(new Rectangle(screenWidth+100,275, 150,30), platformSpeed,0));
level = 8; // newPlatform needs a defined level number
for(var i=0; i<8; i++)
newPlatform();
gameSpeed = initialGameSpeed*2;
hudLayer.activate();
var title = new Raster("title");
title.position = view.center;
var spaceText = makeText(view.center+new Point(0, 95), largeTextStyle, 35, "Press SPACE to start");
var controlsText = makeText(view.center+new Point(0, 130), largeTextStyle, 20, "SPACE to jump. F and D to switch colors.");
var authorText = makeText(new Point(20, screenHeight - 55), smallTextStyle, 16, "Made by Jay Karon");
makeLink(authorText, "https://github.com/jaykaron");
var originalText = makeText(new Point(20, screenHeight - 38), smallTextStyle, 12, "Game design by Ari Karon");
var musicText = makeText(new Point(20, screenHeight - 20), smallTextStyle, 12, "Music: Chaoz Fantasy by ParagonX9");
makeLink(musicText, "https://soundcloud.com/paragonx9");
mainLayer.activate();
}
// ********** Start of PC.js **********
function PC(x, y) {
this.w = 35; this.h = 35;
this.v = 0; this.a = 0.5; // Velocity, fallingAcceleration
this.MAX_V = 25;
this.MIN_V = -12;
this.c = 0; // Color
this.peaked = true;
this.onPlatform = false;
this.box = new Rectangle(x,y, this.w,this.h);
this.path = new Path.Rectangle(this.box);
this.path.fillColor = "red";
this.path.strokeColor = "black";
this.move = function() {
this.updateV();
this.shift(this.v);
var currentPlatform = this.checkPlatforms();
if(currentPlatform) {
this.shift(-this.box.intersect(currentPlatform.box).height);
}
}
this.shift = function(deltaY) {
this.box.y += deltaY;
var vector = new Point(0, deltaY);
this.path.position += vector;
if(this.box.topRight.y > screenHeight + 2) {
gameOver();
}
}
this.nextC = function() {
/* Switches to the next color */
this.c = (this.c + 1) % 3;
this.updateC();
}
this.prevC = function() {
/* Switches to the previous color */
this.c = (this.c + 2) % 3;
this.updateC();
}
this.updateC = function() {
switch (this.c) {
case 0:
this.path.fillColor = "red";
break;
case 1:
this.path.fillColor = "blue";
break;
case 2:
this.path.fillColor = "green";
break;
default:
this.path.fillColor = "red";
}
}
this.checkPlatforms = function() {
if(this.v < 0) {
this.onPlatform = false;
return null;
}
for (var i=0; i<platforms.length; i++) {
if (platforms[i].c == this.c) {
if(platforms[i].box.contains(this.box.bottomRight) || platforms[i].box.contains(this.box.bottomLeft)) {
this.onPlatform = true;
this.peaked = false;
return platforms[i];
}
}
}
this.onPlatform = false;
return null;
}
this.updateV = function() {
if(Key.isDown('space') && !this.peaked) {
if(this.onPlatform && this.v >= 0) { //Landing or on a platform
this.v = -5;
}
else if (this.v < 0) {
this.v -= 0.5;
}
if(this.v == 0)
this.peaked = true;
if(this.v < this.MIN_V || this.v > 0) {
this.peaked = true;
}
}
else if(!this.onPlatform) {
this.v += this.a;
}
else if (this.v > 0) { // on a platform
this.v = 0;
}
if(this.v > this.MAX_V)
this.v = this.MAX_V;
}
}
// ********** End of PC.js **********
// ********** Start of Platform.js **********
function Platform(rect, v, c) {
this.move = function() {
this.box.x -= v * gameSpeed;
if(this.box.topRight.x < 0) {
clearPlatforms = true;
this.offScreen = true;
}
var vector = new Point(-v*gameSpeed, 0);
this.path.position += vector;
}
this.updateC = function() {
switch (this.c) {
case 0:
this.path.fillColor = "red";
break;
case 1:
this.path.fillColor = "blue";
break;
case 2:
this.path.fillColor = "green";
break;
default:
this.path.fillColor = "red";
}
}
this.box = rect;
this.path = new Path.Rectangle(this.box);
this.path.strokeColor = "black";
this.v = v; this.c = c;
this.offScreen = false;
this.updateC();
}
// ********** End of Platform.js **********
// ********** Start of HUD.js **********
var timeText;
var showedTime = 0;
var speedText;
var countDown;
var speedingUpText;
var pausedText, pausedSmallText;
var largeTextStyle = new Style({
fontFamily: 'Impact',
fontWeight: 'bold',
justification: 'center'
});
var smallTextStyle = new Style({
justification: "left",
fontWeight: "normal",
fontFamily: "arial black"
});
function newHud() {
hudLayer.activate();
hudLayer.removeChildren();
timeText = makeText(new Point(10,35), smallTextStyle, 26, "");
speedText = makeText(new Point(10,60), smallTextStyle, 15, "");
countDown = makeText(new Point(view.center+new Point(0,75)), largeTextStyle, 150, "");
pausedText = makeText(new Point(view.center), largeTextStyle, 70, "");
pausedSmallText = makeText(new Point(view.center+new Point(0,55)), largeTextStyle, 35, "");
updateHudNewLevel();
updateHudNewSecond();
mainLayer.activate();
}
function updateHud(){
if(secondsPassed > showedTime)
updateHudNewSecond();
if(countDown.opacity < 1)
countDown.opacity+=0.05;
if(countDown.content){
countDown.style.fontSize-= 0.8;
countDown.position.y -= 0.4;
}
}
function updateHudNewSecond() {
if(timeIncrement - secondsPassed%timeIncrement <= 3) {
countDown.content = timeIncrement - secondsPassed%timeIncrement;
countDown.opacity = 0;
countDown.style.fontSize = 150;
countDown.position = view.center+new Point(0,20)
}
else
countDown.content = "";
showedTime = secondsPassed;
updateTimeText();
}
function updateHudNewLevel() {
speedText.content = gameSpeed.toPrecision(2) + " x";
}
function updateTimeText() {
timeText.content = showedTime;
}
function gameOverHud() {
hudLayer.activate();
var gameOverText = makeText(new Point(view.center), largeTextStyle, 70, "GAME OVER");
var restartText = makeText(new Point(view.center+new Point(0,55)), largeTextStyle, 35, "Press SPACE to restart");
var scoreText = makeText(new Point(view.center+new Point(0,120)), largeTextStyle, 28, "Longest Run: "+highscore);
countDown.content = "";
mainLayer.activate();
}
function pausedHud(){
pausedText.content = "PAUSED"
pausedSmallText.content = "Press P to continue";
}
function unpausedHud(){
pausedText.content = "";
pausedSmallText.content = "";
}
// ******* TEXT HELPER FUNCTIONS *******
function makeText(point, style, size, content) {
var text = new PointText(point);
text.style = style;
text.style.fontSize = size;
text.content = content;
return text;
}
function makeLink(pointText, url){
pointText.onMouseDown = function() {
window.open(url);
};
pointText.onMouseEnter = function(){
this.style.fontSize += 3;
document.body.style.cursor = 'pointer';
};
pointText.onMouseLeave = function(){
this.style.fontSize -= 3;
document.body.style.cursor = "";
};
}
// ********** End of HUD.js **********
// ********** Start of INIT.js **********
// The code to run after the rest of the code is already loaded
init();
// ********** End of INIT.js **********