forked from RobeeeJay/Forest-Racer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fr_main.js
1 lines (1 loc) · 14.8 KB
/
fr_main.js
1
function ForestRacer(e){this.canMainCanvas=e,this.width=this.canMainCanvas.width,this.height=this.canMainCanvas.height,this.conCanvas=this.canMainCanvas.getContext("2d"),this.canBackground=document.createElement("canvas"),this.canBackground.width=this.width,this.canBackground.height=this.height,this.conBackground=this.canBackground.getContext("2d"),this.timeout=null,this.keys={left:78,right:77,up:65,down:90,start:83,debug:89},this.keysdown={},this.score=0,this.level=1,this.topscore=1e4,this.speed=.1,this.horizon=0,this.turnspeed=.3,this.treespeed=1,this.accelcounter=0,this.audcounter=0,this.linepos=0,this.gamemode=this.gamePreload,this.fr_target=30,this.fr_framems=1e3/this.fr_target,this.fr_count=0,this.fr_fps=0,this.fr_counter=0,this.fr_timer=0,this.confTreeWidth=8,this.confTreeHeight=50,this.confTreeGap=Math.floor(this.confTreeWidth/3),this.confTreeArea=this.confTreeWidth+this.confTreeGap,this.confLimit=15,this.confForestWidth=Math.floor(this.width/this.confTreeArea)+this.confLimit,this.confFramesPerLevel=1800,this.confMaxSpeed=.5,this.confMinSpeed=.05,this.confMaxLevel=13,this.confTreeCap=.1,this.confSize=8,this.confCrashSteps=5,this.confMountainHeight=7,this.confOverTime=5e3,this.confLineGap=this.confLimit/15,this.textTitle1="FOREST RACER",this.textTitle2="13 Levels of Tree Mayhem",this.textWait="PLEASE WAIT!!!",this.textStart="Press S to Start",this.textOver="GAME OVER",this.textCongrats="CONGRATULATIONS!!!",this.textHighScore="New High Score!!!",this.textInstructions1="A - Accelerate",this.textInstructions2="Z - Decelerate",this.textInstructions3="N - Steer Left",this.textInstructions4="M - Steer Right",this.textAuthor="Written by RobeesWorld.com",this.ptrRnd=0,this.ptrRndCrash=Math.floor(Math.random()*10),this.arrRnd=new Array;for(var t=0;t<100+this.confCrashSteps*4;t++)this.arrRnd.push(Math.random());this.debug=!1,this.createMedia(),this.bindEvents()}window.onload=function(){var e=document.getElementById("retrobox");e.getContext&&new ForestRacer(e)},(new Image).src="tree.png",ForestRacer.prototype.bindEvents=function(){var e=this;window.onkeydown=function(t){for(key in e.keys)t.keyCode==e.keys[key]&&(e.keysdown[key]=!0)},window.onkeyup=function(t){for(key in e.keys)t.keyCode==e.keys[key]&&(e.keysdown[key]=!1)},this.timeout=setTimeout(function(){e.processFrame()},this.fr_framems)},ForestRacer.prototype.createMedia=function(){this.gradSunset=this.conCanvas.createLinearGradient(0,0,0,this.height/2),this.gradSunset.addColorStop(0,"#6102B7"),this.gradSunset.addColorStop(1,"#E37A1C"),this.gradFloor=this.conCanvas.createLinearGradient(0,this.height/2,0,this.height),this.gradFloor.addColorStop(0,"#0D2F0F"),this.gradFloor.addColorStop(1,"#1F6D23"),this.conBackground.fillStyle=this.gradSunset,this.conBackground.fillRect(0,0,this.width,this.height/2),this.conBackground.fillStyle=this.gradFloor,this.conBackground.fillRect(0,this.height/2,this.width,this.height/2),this.imgTree=new Image,this.imgTree.src="tree.png",this.audEngine=document.getElementById("engine"),this.explode=document.getElementById("explode"),this.canWorkCanvas=document.createElement("canvas"),this.canWorkCanvas.width=this.imgTree.width,this.canWorkCanvas.height=this.imgTree.height,this.conWorkCanvas=this.canWorkCanvas.getContext("2d"),this.conWorkCanvas.drawImage(this.imgTree,0,0),this.imdTree=this.conWorkCanvas.getImageData(0,0,this.imgTree.width,this.imgTree.height),this.imgScaledTrees={},this.conCanvas.drawImage(this.canBackground,0,0),this.drawMountains(),this.imdMountains=this.conCanvas.getImageData(0,Math.floor(this.height/2)-Math.floor(this.height/this.confMountainHeight),this.width,Math.floor(this.height/this.confMountainHeight))},ForestRacer.prototype.getTree=function(e,t){var n=e+"x"+t;return this.imgScaledTrees[n]||(this.imgScaledTrees[n]=this.createTree(e,t)),this.imgScaledTrees[n]},ForestRacer.prototype.createTree=function(e,t){var n=0,r=this.conCanvas.createImageData(e,t),i=this.imgTree.width/e,s=this.imgTree.height/t;for(var o=0,u=0;o<t;o++,u+=s){var a=Math.floor(u)*this.imgTree.width*4;for(var f=0,l=0;f<e;f++,l+=i){var c=a+Math.floor(l)*4;r.data[n++]=this.imdTree.data[c++],r.data[n++]=this.imdTree.data[c++],r.data[n++]=this.imdTree.data[c++],r.data[n++]=this.imdTree.data[c++]}}var h=document.createElement("canvas");h.width=e,h.height=t;var p=h.getContext("2d");p.putImageData(r,0,0);var d={data:r,imageURL:h.toDataURL()};return d.image=new Image,d.image.src=d.imageURL,d},ForestRacer.prototype.initGame=function(e){this.treeLine=[];for(var t=0;t<this.confLimit;t+=this.treespeed/e){var n={};n.pos=t,n.x=-Math.floor(this.confForestWidth/8),this.treeLine.unshift(n),n={},n.pos=t,n.x=-Math.floor(this.confForestWidth/5),this.treeLine.unshift(n),n={},n.pos=t,n.x=Math.floor(this.confForestWidth/8),this.treeLine.unshift(n),n={},n.pos=t,n.x=Math.floor(this.confForestWidth/5),this.treeLine.unshift(n)}this.crashed=!1,this.score=0,this.level=1,this.speed=.1,this.treespeed=1,this.accelcounter=0,this.ptrRndCrash=Math.floor(Math.random()*10)},ForestRacer.prototype.addTreeLine=function(){if(this.treeLine[this.treeLine.length-1].pos>this.treespeed)for(var e=0;e<3;e++){var t={};t.pos=0,t.x=Math.floor(Math.random()*this.confForestWidth)-Math.floor(this.confForestWidth/2),this.treeLine.push(t)}},ForestRacer.prototype.processFrame=function(){var e=new Date,t=this;this.checkKeys(),this.gamemode.apply(this);var n=new Date,r=n-e,i=this.fr_framems-r;i<1&&(i=1),this.fr_count++,this.debug&&(this.fr_timer+=this.fr_framems>r?this.fr_framems:r,this.fr_timer>1e3&&(this.fr_fps=this.fr_counter,this.fr_counter=0,this.fr_timer-=1e3),this.fr_counter++,this.drawDebug()),this.timeout=setTimeout(function(){t.processFrame()},i)},ForestRacer.prototype.gamePreload=function(){this.treeLine||this.initGame(1);if(this.treeLine.length){this.drawBackdrop(),this.flashCounter||(this.flashCounter=new Date);var e=new Date;if(e-this.flashCounter>500)e-this.flashCounter>1e3&&(this.flashCounter=new Date);else{this.conCanvas.fillStyle="white",this.conCanvas.font="bold 30px Courier New, Courier, monospace";var t=this.conCanvas.measureText(this.textWait);this.conCanvas.fillText(this.textWait,Math.floor((this.width-t.width)/2),Math.floor(this.height/2-7))}this.drawTrees(),this.moveTrees(),this.speed=Math.round((this.speed+.01)*100)/100}else this.gamemode=this.gamePreStart},ForestRacer.prototype.gamePreStart=function(){if(!this.keysdown.start){this.drawBackdrop(),this.drawScores(),this.flashCounter||(this.flashCounter=new Date),this.conCanvas.fillStyle="white",this.conCanvas.font="bold 20px Courier New, Courier, monospace";var e=this.conCanvas.measureText(this.textInstructions1);this.conCanvas.fillText(this.textInstructions1,Math.floor((this.width-e.width)/2),Math.floor(this.height/2+30)),e=this.conCanvas.measureText(this.textInstructions2),this.conCanvas.fillText(this.textInstructions2,Math.floor((this.width-e.width)/2),Math.floor(this.height/2+50)),e=this.conCanvas.measureText(this.textInstructions3),this.conCanvas.fillText(this.textInstructions3,Math.floor((this.width-e.width)/2),Math.floor(this.height/2+70)),e=this.conCanvas.measureText(this.textInstructions4),this.conCanvas.fillText(this.textInstructions4,Math.floor((this.width-e.width)/2),Math.floor(this.height/2+90)),e=this.conCanvas.measureText(this.textAuthor),this.conCanvas.fillText(this.textAuthor,Math.floor((this.width-e.width)/2),Math.floor(this.height/2+150)),e=this.conCanvas.measureText(this.textTitle1),this.conCanvas.fillText(this.textTitle1,Math.floor((this.width-e.width)/2),Math.floor(this.height/2-80)),e=this.conCanvas.measureText(this.textTitle2),this.conCanvas.fillText(this.textTitle2,Math.floor((this.width-e.width)/2),Math.floor(this.height/2-50));var t=new Date;t-this.flashCounter>500?t-this.flashCounter>1e3&&(this.flashCounter=new Date):(e=this.conCanvas.measureText(this.textStart),this.conCanvas.fillText(this.textStart,Math.floor((this.width-e.width)/2),Math.floor(this.height/2-10)))}else this.initGame(2),this.gamemode=this.gameInGame},ForestRacer.prototype.gameInGame=function(){this.moveKeys(),this.moveThings(),this.playAudio(),this.drawBackdrop(),this.drawTrees(),this.drawScores(),this.addTreeLine()},ForestRacer.prototype.gameOver=function(){if(!this.keysdown.start){this.drawBackdrop(),this.drawTrees(),this.drawScores(),this.flashCounter||(this.flashCounter=new Date),this.overtime||(this.overtime=new Date);if(this.crashed){this.drawCrash();var e=this.textOver}else{this.moveTrees();var e=this.textCongrats}this.conCanvas.fillStyle="white",this.conCanvas.font="bold 20px Courier New, Courier, monospace";var t=this.conCanvas.measureText(this.textStart);this.conCanvas.fillText(this.textStart,Math.floor((this.width-t.width)/2),Math.floor(this.height/2+20));var n=new Date;n-this.flashCounter>500?n-this.flashCounter>1e3&&(this.flashCounter=new Date):(t=this.conCanvas.measureText(e),this.conCanvas.fillText(e,Math.floor((this.width-t.width)/2),Math.floor(this.height/2-10)),this.score==this.topscore&&(t=this.conCanvas.measureText(this.textHighScore),this.conCanvas.fillText(this.textHighScore,Math.floor((this.width-t.width)/2),Math.floor(this.height/2+50)))),n-this.overtime>this.confOverTime&&(delete this.overtime,this.gamemode=this.gamePreStart)}else delete this.overtime,this.initGame(2),this.gamemode=this.gameInGame},ForestRacer.prototype.checkKeys=function(){this.keysdown.debug&&(this.debug=!this.debug,this.keysdown.debug=!1)},ForestRacer.prototype.moveKeys=function(){if(this.speed)if(this.keysdown.left){this.horizon--,this.horizon<0&&(this.horizon=this.width-1);for(var e=0,t=this.treeLine.length;e<t;e++)this.treeLine[e].x+=this.turnspeed}else if(this.keysdown.right){this.horizon++,this.horizon==this.width&&(this.horizon=0);for(var e=0,t=this.treeLine.length;e<t;e++)this.treeLine[e].x-=this.turnspeed}this.keysdown.down?this.speed>this.confMinSpeed&&(this.speed=Math.round((this.speed-.01)*100)/100):this.keysdown.up&&this.speed<this.confMaxSpeed&&(this.speed=Math.round((this.speed+.01)*100)/100)},ForestRacer.prototype.playAudio=function(){this.audcounter--;if(this.audcounter<0){this.audcounter=Math.floor(9*(this.confMaxSpeed-this.speed));try{this.audEngine.currentTime=0}catch(e){}this.audEngine.play()}},ForestRacer.prototype.moveThings=function(){this.accelcounter++,this.accelcounter>this.confFramesPerLevel&&(this.level++,this.level>this.confMaxLevel?(this.score+=1e5,this.gamemode=this.gameOver,this.accelcounter=this.confFramesPerLevel):(this.accelcounter=0,this.treespeed>this.confTreeCap&&(this.treespeed=Math.round((this.treespeed-.1)*100)/100))),this.score+=this.speed*100,this.moveTrees()},ForestRacer.prototype.moveTrees=function(){this.linepos=Math.round((this.linepos+this.speed)*100)/100,this.linepos>this.confLineGap&&(this.linepos=0);for(var e=0,t=this.treeLine.length;e<t;e++)this.treeLine[e].pos=Math.round((this.treeLine[e].pos+this.speed)*100)/100;for(var e=this.treeLine.length-1;e>=0;e--)this.treeLine[e].pos>this.confLimit&&(Math.abs(this.treeLine[e].x)<this.confTreeWidth/7?this.gamemode!=this.gameOver?(this.crashed=!0,this.speed=0,this.explode.play(),this.score>this.topscore&&(this.topscore=this.score),this.gamemode=this.gameOver):this.treeLine.splice(e,1):this.treeLine.splice(e,1))},ForestRacer.prototype.drawBackdrop=function(){this.conCanvas.drawImage(this.canBackground,0,0),this.conCanvas.putImageData(this.imdMountains,this.horizon,Math.floor(this.height/2)-Math.floor(this.height/this.confMountainHeight)),this.horizon>0&&this.conCanvas.putImageData(this.imdMountains,this.horizon-this.width,Math.floor(this.height/2)-Math.floor(this.height/this.confMountainHeight));var e=this.linepos;this.conCanvas.strokeStyle="black",this.conCanvas.lineWidth=1;while(e<this.confLimit){var t=Math.pow(e/10,6),n=Math.floor(this.height/2)+e*t;this.conCanvas.beginPath(),this.conCanvas.moveTo(0,n),this.conCanvas.lineTo(this.width,n),this.conCanvas.stroke(),e+=this.confLineGap}},ForestRacer.prototype.drawTrees=function(){for(var e=this.treeLine.length-1;e>=0;e--){var t=Math.pow(this.treeLine[e].pos/10,6),n=this.treeLine[e].x*this.confTreeArea-this.confTreeGap;n*=t,n+=Math.floor(this.width/2);var r=Math.floor(this.confTreeHeight/2);r*=t,r=Math.floor(this.height/2)-r;var i=this.confTreeWidth;i*=t;var s=this.confTreeHeight;s*=t,n+=Math.floor(i%this.confSize/2),r+=Math.floor(s%this.confSize/2),i=Math.round(i/this.confSize)*this.confSize,s=Math.round(s/this.confSize)*this.confSize,i<this.confSize&&(i=this.confSize),s<this.confSize&&(s=this.confSize);var o=this.getTree(i,s);o&&this.conCanvas.drawImage(o.image,n,r)}},ForestRacer.prototype.drawScores=function(){this.conCanvas.fillStyle="white",this.conCanvas.font="bold 13px Courier New, Courier, monospace";var e="TOP SCORE: "+this.topscore,t=this.conCanvas.measureText(e);this.conCanvas.fillText(e,this.width-t.width-10,20),this.conCanvas.fillText("SCORE: "+this.score,10,20);var n=Math.floor((this.width-24)/this.confFramesPerLevel*this.accelcounter);this.conCanvas.fillStyle="blue",this.conCanvas.strokeRect(10,30,this.width-20,20),this.conCanvas.fillRect(12,32,n,16),this.conCanvas.fillStyle="white";var r="LEVEL: "+this.level;t=this.conCanvas.measureText(r),this.conCanvas.fillText(r,(this.width-t.width)/2,43)},ForestRacer.prototype.drawMountains=function(){this.ptrRnd=0;var e=Math.floor(this.height/this.confMountainHeight),t=0,n=0,r=Math.floor(this.height/2);this.conCanvas.fillStyle="black";for(;;){n=t,t=this.drawTriangle(t,r,e);if(t>=this.width)break;t-=Math.floor(e/2)}this.ptrRnd-=3,t=-(this.width-n),this.drawTriangle(t,r,e)},ForestRacer.prototype.drawTriangle=function(e,t,n){return this.conCanvas.beginPath(),this.conCanvas.moveTo(e,t),e+=Math.floor(n/2+this.arrRnd[this.ptrRnd++]*(n/2)),this.conCanvas.lineTo(e,t-Math.floor(n/2+this.arrRnd[this.ptrRnd++]*(n/2))),e+=Math.floor(n/2+this.arrRnd[this.ptrRnd++]*(n/2)),this.conCanvas.lineTo(e,t),this.conCanvas.fill(),e},ForestRacer.prototype.drawCrash=function(){this.ptrRnd=this.ptrRndCrash,this.conCanvas.strokeStyle="rgba(255,255,255,0.7)",this.conCanvas.lineWidth=2,this.drawCrackLine(-1,-1),this.drawCrackLine(1,-1),this.drawCrackLine(-1,1),this.drawCrackLine(1,1)},ForestRacer.prototype.drawCrackLine=function(e,t){var n=Math.floor(this.width/2),r=Math.floor(this.height/2),i=Math.floor(this.width/this.confCrashSteps);this.conCanvas.beginPath(),this.conCanvas.moveTo(n,r);for(var s=0;s<this.confCrashSteps*2;s++)n+=Math.floor(this.arrRnd[this.ptrRnd++]*i*e),r+=Math.floor(this.arrRnd[this.ptrRnd++]*i*t),this.conCanvas.lineTo(n,r);this.conCanvas.stroke()},ForestRacer.prototype.drawDebug=function(){this.conCanvas.fillStyle="white",this.conCanvas.font="13px Courier New, Courier, monospace",this.conCanvas.fillText("Frame: "+this.fr_count,5,this.height-15),this.conCanvas.fillText("FPS: "+this.fr_fps+"/"+this.fr_target,this.width-90,this.height-15),this.conCanvas.fillText("Speed: "+this.speed+", Tree Speed: "+this.treespeed,5,this.height-30),this.conCanvas.fillText("Horizon: "+this.horizon,this.width-120,this.height-30),this.conCanvas.fillText("Keys Pressed: "+(this.keysdown.left?"LEFT ":"")+(this.keysdown.right?"RIGHT ":"")+(this.keysdown.up?"UP ":"")+(this.keysdown.down?"DOWN ":"")+(this.keysdown.start?"START":""),5,this.height-55)}