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