Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
AroriaNetwork
GitHub Repository: AroriaNetwork/3kho-backup
Path: blob/main/projects/polybranch/js/script.js
1834 views
1
var firstPlaythrough = true;
2
var playing = false;
3
4
var bells = new Array();
5
var bellsIndex = randomXToY(0,4);
6
var endSound = ((new Audio()).canPlayType("audio/ogg; codecs=vorbis") != "") ? new Audio("sound/end.ogg") : new Audio("sound/end.mp3");
7
8
for(var i = 0; i < 10; i++){
9
var index = (i < 5) ? i : i - 5;
10
if((new Audio()).canPlayType("audio/ogg; codecs=vorbis") != ""){
11
bells[i] = new Audio("sound/_bell"+index+".ogg");
12
}else{
13
bells[i] = new Audio("sound/_bell"+index+".mp3");
14
}
15
}
16
17
18
$(document).ready(function(){
19
$("#main-menu #start").click(function(){
20
if(!playing){
21
jsStartGame(false);
22
playing = true;
23
}
24
});
25
26
$("#gameover-menu #retry").click(function(){
27
if(!playing){
28
jsNewGame();
29
playing = true;
30
}
31
});
32
33
$(document).bind("keydown",function(){
34
if($("#arrowkeys:visible").length > 0){
35
$("#arrowkeys").fadeOut(300);
36
}
37
});
38
});
39
40
41
42
// function start(){
43
// if(pjs!=null) {
44
// pjs.pause();
45
// }
46
// }
47
function processingIsReady(){
48
$("#loading").fadeOut(300);
49
}
50
51
function jsStartGame(fromProcessing){
52
if(!fromProcessing){
53
pjs.pause();
54
}
55
$("#main-menu .content").fadeOut(300,function(){
56
if(firstPlaythrough){
57
firstPlaythrough = false;
58
$("#arrowkeys").fadeIn(300);
59
}
60
$("#hud").fadeIn(300);
61
$("#main-menu").fadeOut(300,function(){
62
63
});
64
});
65
}
66
67
function jsNewGame(){
68
pjs.newGame();
69
$("#hud #score").html("0");
70
$("#hud #level span").html("1");
71
$("#gameover-menu .content").animate({"opacity":"0"},300,function(){
72
$("#gameover-menu .content").hide();
73
$("#gameover-menu").animate({"opacity":"0"},300,function(){
74
$("#gameover-menu").hide();
75
$("#hud").fadeIn(300);
76
pjs.pause();
77
});
78
});
79
}
80
81
function jsTriggerBell(){
82
var newIndex = randomXToY(0,4);
83
if(newIndex == bellsIndex){
84
jsTriggerBell();
85
}else{
86
bellsIndex = newIndex;
87
if(bells[bellsIndex].paused){
88
bells[bellsIndex].currentTime=0;
89
bells[bellsIndex].play();
90
}else{
91
bells[bellsIndex+5].currentTime=0;
92
bells[bellsIndex+5].play();
93
}
94
}
95
}
96
97
function jsUpdateScore(score){
98
$("#hud #score").html(addCommas(score));
99
}
100
101
function jsIncrementLevel(){
102
$("#hud #level span").html((parseInt($("#hud #level span").html())+1));
103
}
104
105
function jsGameOver(score){
106
playing = false;
107
$("#flash").show();
108
endSound.currentTime=0;
109
endSound.play();
110
$("#hud").hide();
111
$("#flash").delay(1000).animate({"opacity":0}, 1000,function(){
112
$(this).hide();
113
$(this).css("opacity","1");
114
$("#gameover-menu #score").html(addCommas(score)+"<span id='L'>L"+$("#hud #level span").html()+"</span>");
115
if(localStorage["highScore"] == undefined || score > parseInt(localStorage["highScore"])){
116
$("#gameover-menu #highscore").html("NEW RECORD!");
117
localStorage["highScore"] = score;
118
localStorage["highLevel"] = $("#hud #level span").html();
119
}else{
120
$("#gameover-menu #highscore").html("PERSONAL BEST: "+addCommas(parseInt(localStorage["highScore"]))+"<span id='L'>L"+localStorage["highLevel"]+"</span>");
121
}
122
$("#gameover-menu #nextlevel span").html(addCommas((pjs.getNextScore(parseInt($("#hud #level span").html())))+1000));
123
$("#gameover-menu").show();
124
$("#gameover-menu").animate({"opacity":"1"},300,function(){
125
$("#gameover-menu .content").show();
126
$("#gameover-menu .content").animate({"opacity":"1"},300);
127
});
128
});
129
}
130
131
//function to get random number upto m
132
function randomXToY(minVal,maxVal,floatVal)
133
{
134
var randVal = minVal+(Math.random()*(maxVal-minVal));
135
return typeof floatVal=='undefined'?Math.round(randVal):randVal.toFixed(floatVal);
136
}
137
138
function addCommas(nStr){
139
nStr += '';
140
x = nStr.split('.');
141
x1 = x[0];
142
x2 = x.length > 1 ? '.' + x[1] : '';
143
var rgx = /(\d+)(\d{3})/;
144
while (rgx.test(x1)) {
145
x1 = x1.replace(rgx, '$1' + ',' + '$2');
146
}
147
return x1 + x2;
148
}
149