-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathbaddepot.min.js
1 lines (1 loc) · 12 KB
/
baddepot.min.js
1
window.requestAnimFrame=(function(){return window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||function(a){window.setTimeout(a,1000/60)}})();function lightenColor(a,e){var b=parseInt(a.slice(1),16),g=Math.round(2.55*e),d=(b>>16)+g,f=(b>>8&255)+g,c=(b&255)+g;return"#"+(16777216+(d<255?d<1?0:d:255)*65536+(f<255?f<1?0:f:255)*256+(c<255?c<1?0:c:255)).toString(16).slice(1)}function randomMax(a){return ~~(Math.random()*a)}function distance(d,c){return ~~Math.sqrt(Math.pow(d.x-c.x,2)+Math.pow(d.y-c.y,2))}var canvas=document.getElementById("mainCanvas"),ctx=canvas.getContext("2d"),twoPI=2*Math.PI;var Boss=function(a,b){this.headSize=10;this.bodySize=30;this.limbWidth=15;this.sayTimePerLetter=5;this.messages=[];this.x=a;this.y=b;this.necky=this.y+this.headSize;this.sayCounter=0;this.sayCounterStart=0;this.message=""};Boss.prototype.draw=function(){ctx.fillStyle=ctx.strokeStyle="#696969";ctx.lineWidth=2;ctx.beginPath();ctx.arc(this.x,this.y,this.headSize,0,twoPI);ctx.fill();ctx.beginPath();ctx.moveTo(this.x,this.necky);ctx.lineTo(this.x,this.necky+this.bodySize);ctx.stroke();ctx.beginPath();ctx.moveTo(this.x,this.necky);ctx.lineTo(this.x+this.limbWidth,this.necky+this.limbWidth);ctx.stroke();ctx.beginPath();ctx.moveTo(this.x,this.necky);ctx.lineTo(this.x-this.limbWidth,this.necky+this.limbWidth);ctx.stroke();ctx.beginPath();ctx.moveTo(this.x,this.necky+this.bodySize);ctx.lineTo(this.x+this.limbWidth,this.necky+this.limbWidth+this.bodySize);ctx.stroke();ctx.beginPath();ctx.moveTo(this.x,this.necky+this.bodySize);ctx.lineTo(this.x-this.limbWidth,this.necky+this.limbWidth+this.bodySize);ctx.stroke();if(this.sayCounter>0){this.sayCounter--;var b=-100*((this.sayCounterStart-this.sayCounter)/this.sayCounterStart);ctx.fillStyle=lightenColor("#7FFF00",b);ctx.font="20px arial";var a=ctx.measureText(this.message);ctx.fillText(this.message,this.x-20,this.y-20);if(this.sayCounter===0){this.messages.splice(0,1)}}else{if(this.messages.length>0){this.message=this.messages[0];this.sayCounterStart=this.sayTimePerLetter*this.message.length;this.sayCounter=this.sayCounterStart}}};Boss.prototype.say=function(b){var a=false;if(this.messages.length>0){if(this.messages[this.messages.length-1]!==b){a=true}}else{a=true}if(a){this.messages.push(b)}};var Player=function(a,b){this.x=a;this.y=b;this.playerColor="#7B68EE";this.ordinaryLight="#FFA500";this.carryLight="#FF4500";this.ligthMaxContrast=20;this.speed=5;this.playerSize=15;this.lightSize=5;this.lightCounterMax=40;this.lightCounter=0;this.state="free";this.lastActionKeyState=false;this.carryColor=undefined};Player.prototype.draw=function(b){if(b.activeKeys.left){this.x-=this.speed}if(b.activeKeys.right){this.x+=this.speed}if(b.activeKeys.up){this.y-=this.speed}if(b.activeKeys.down){this.y+=this.speed}if(b.activeKeys.action!==this.lastActionKeyState&&b.activeKeys.action){if(this.state==="free"){var a=b.attach(this);if(a!==undefined){this.state="carry";this.carryColor=a;if(Math.random()<b.containerExplodeOnLiftRate){b.explosion=new Explosion(this.x,this.y,this.carryColor);this.state="free";b.boss.say("Bad luck man!")}}}else{if(this.state==="carry"){if(b.drop(this)){this.state="free"}}}}this.lastActionKeyState=b.activeKeys.action;if(this.x-this.playerSize<0){this.x=this.playerSize}if(this.y-this.playerSize<0){this.y=this.playerSize}if(this.x+this.playerSize>b.border){this.x=b.border-this.playerSize}if(this.y+this.playerSize>canvas.height){this.y=canvas.height-this.playerSize}if(this.state==="carry"){ctx.fillStyle=this.carryColor;ctx.beginPath();ctx.arc(this.x,this.y,~~(b.containerSize/2),0,twoPI);ctx.fill()}ctx.fillStyle=this.playerColor;ctx.beginPath();ctx.arc(this.x,this.y,this.playerSize,0,twoPI);ctx.fill();var c;if(this.state==="free"){c=this.ordinaryLight}else{if(this.state==="carry"){c=this.carryLight}}this.lightCounter=(this.lightCounter+1)%this.lightCounterMax;var d=this.ligthMaxContrast*(this.lightCounterMax-this.lightCounter)/this.lightCounterMax;ctx.fillStyle=lightenColor(c,d);ctx.beginPath();ctx.arc(this.x,this.y,this.lightSize,0,twoPI);ctx.fill()};var ContainerSystem=function(a){this.containers=[];this.freeContainerNum=a.containerNum};ContainerSystem.prototype.addContainers=function(d,g){var b,h=false,c,f,a;if(this.freeContainerNum>=g){c=randomMax(d.containerwcount);f=randomMax(d.containerhcount);a=c*d.containerhcount+f;for(var e=0;e<g;e++){b=d.containerColors[randomMax(d.containerColors.length)];while(!this.isFree(a)){a=(a+1)%d.containerNum}d.containerSystem.fill(a,b)}h=true}return h};ContainerSystem.prototype.getIndex=function(e,b,f){var d=~~(b/e.containerSize);var a=~~(f/e.containerSize);var c=a*e.containerhcount+d;if(d>=e.containerhcount){c=undefined}return c};ContainerSystem.prototype.releaseInd=function(b){var a=this.containers[b];if(this.containers[b]!==undefined){this.containers[b]=undefined;this.freeContainerNum++}return a};ContainerSystem.prototype.isFree=function(a){return this.containers[a]===undefined};ContainerSystem.prototype.fill=function(b,a){this.containers[b]=a;this.freeContainerNum--};ContainerSystem.prototype.draw=function(c){var b=~~(c.containerSize/2);for(var d=0;d<this.containers.length;d++){if(this.containers[d]!==undefined){ctx.fillStyle=this.containers[d];var a=d%c.containerhcount;var e=~~(d/c.containerhcount);ctx.beginPath();ctx.arc(b+a*c.containerSize,b+e*c.containerSize,b,0,twoPI);ctx.fill()}}};var DropPoint=function(a){this.x=a.border-a.dropSize;this.y=~~(canvas.height/2);this.dy=a.dropPointSpeed;this.dropSize=a.dropSize;this.timePerDigit=60;this.timeCounter=0;this.pickColor(a)};DropPoint.prototype.pickColor=function(a){this.acceptingColor=a.containerColors[randomMax(a.containerColors.length)];this.dropCountDown=a.dropCountDown};DropPoint.prototype.draw=function(a){this.timeCounter=(this.timeCounter+1)%60;if(this.timeCounter===0){this.dropCountDown--}if(this.dropCountDown===0){this.pickColor(a)}this.y+=this.dy;if(this.y+this.dropSize>=canvas.height){this.y=canvas.height-this.dropSize;this.dy*=-1}if(this.y-this.dropSize<=0){this.y=this.dropSize;this.dy*=-1}ctx.strokeStyle=ctx.fillStyle=this.acceptingColor;ctx.lineWidth=3;ctx.font="16px arial";var b=ctx.measureText(this.dropCountDown);ctx.fillText(this.dropCountDown,this.x-(b.width/2),this.y+5);ctx.beginPath();ctx.arc(this.x,this.y,this.dropSize-ctx.lineWidth,0,twoPI);ctx.stroke()};var ScoreBoard=function(a,b){this.x=a;this.y=b;this.rowSpacing=20};ScoreBoard.prototype.draw=function(a){ctx.font="16px arial";ctx.fillStyle="Cyan";ctx.fillText("Points",this.x,this.y);ctx.font="30px arial";ctx.fillText(a.points,this.x,this.y+1.5*this.rowSpacing);ctx.font="16px arial";ctx.fillStyle="#FF8C00";ctx.fillText("Delivery Size",this.x,this.y+3*this.rowSpacing);ctx.fillText(a.nextDeliveryCount,this.x,this.y+4*this.rowSpacing);ctx.fillStyle="#FF1493";ctx.fillText("Delivery Countdown",this.x,this.y+5.5*this.rowSpacing);ctx.fillText(a.deliveryCountDown,this.x,this.y+6.5*this.rowSpacing)};var InstructionBoard=function(a,b){this.x=a;this.y=b;this.rowSpacing=20};InstructionBoard.prototype.draw=function(a){ctx.font="16px arial";ctx.fillStyle="yellow";ctx.fillText("space - attach / drop container",this.x,this.y);ctx.fillText("arrow keys - move carrier",this.x,this.y-this.rowSpacing);ctx.fillText("Drop containers to counter",this.x,this.y-2*this.rowSpacing)};var Particle=function(c,b,e,d){var a=5;this.color=c;this.x=b;this.y=e;this.dx=(Math.random()>0.5?-1:1)*a+randomMax(d);this.dy=(Math.random()>0.5?-1:1)*a+randomMax(d)};Particle.prototype.draw=function(){this.x+=this.dx;this.y+=this.dy;ctx.fillStyle=this.color;ctx.beginPath();ctx.arc(this.x,this.y,2,0,twoPI);ctx.fill();if(this.x<0||this.x>canvas.width||this.y<0||this.y>canvas.height){return true}};var Explosion=function(a,e,b){this.particles=[];var d=5;for(var c=0;c<30;c++){this.particles.push(new Particle(b,a,e,d))}};Explosion.prototype.draw=function(){var b=0;for(var a=0;a<this.particles.length;a++){if(this.particles[a].draw()){b++}}if(b===this.particles.length){return true}};var Game=function(){this.activeKeys=[];this.over=false;this.points=0;this.nextDeliveryCount=5;this.lastDeliveryCountDown=30;this.deliveryCountDown=30;this.deliveryCountCycle=60;this.deliveryCounter=0;this.border=550;this.borderColor="#DEB887";this.boss=new Boss(620,~~(canvas.height/2));this.containerColors=["#6495ED","#DC143C","#808080","#8B4513","#FFFF00"];this.containerSize=40;this.containerhcount=~~(this.border/this.containerSize)-1;this.containerwcount=~~(canvas.height/this.containerSize);this.containerNum=this.containerwcount*this.containerhcount;this.containerSystem=new ContainerSystem(this);this.dropSize=30;this.dropCountDown=10;this.dropPointSpeed=2;this.dropPoint=new DropPoint(this);this.player=new Player(this.border-this.dropSize,~~(canvas.height/2)-2*this.dropSize);this.scoreBoard=new ScoreBoard(this.border+20,20);this.instructionBoard=new InstructionBoard(this.border+20,canvas.height-20);this.containerSystem.addContainers(this,10);this.containerExplodeOnLiftRate=0.1;this.explosion=undefined;this.animatedStuff=[this.containerSystem,this.boss,this.dropPoint,this.player,this.scoreBoard,this.instructionBoard]};Game.prototype.attach=function(a){var b=this.containerSystem.getIndex(this,this.player.x,this.player.y);if(b!==undefined){var c=this.containerSystem.releaseInd(b);if(c!==undefined){return c}else{this.boss.say("can't pick up empty!")}}else{this.boss.say("Nothing there!")}};Game.prototype.drop=function(a){var c=this.containerSystem.getIndex(this,this.player.x,this.player.y);var b=false;if(c!==undefined){if(this.containerSystem.isFree(c)){this.containerSystem.fill(c,a.carryColor);b=true}else{this.boss.say("watch it!")}}else{if(distance(a,this.dropPoint)<=this.dropSize){if(this.dropPoint.acceptingColor===a.carryColor){b=true;this.points+=10}else{this.boss.say("Wrong color!")}}else{this.boss.say("Drop at counter!")}}return b};Game.prototype.draw=function(){ctx.strokeStyle=this.borderColor;ctx.beginPath();ctx.moveTo(this.border,0);ctx.lineTo(this.border,canvas.height);ctx.stroke();if(this.over){this.scoreBoard.draw(this);this.boss.draw(this);this.instructionBoard.draw(this);ctx.font="30px arial";ctx.fillStyle="red";ctx.fillText("Game Over",canvas.width/4,canvas.height/2);ctx.font="15px arial";ctx.fillText("Click with mouse to restart...",canvas.width/4,2*canvas.height/3)}else{this.deliveryCounter=(this.deliveryCounter+1)%this.deliveryCountCycle;if(this.deliveryCounter===0){this.deliveryCountDown--}if(this.deliveryCountDown===0){if(game.containerSystem.addContainers(game,this.nextDeliveryCount)){this.deliveryCountDown=this.lastDeliveryCountDown;this.nextDeliveryCount++;if(this.nextDeliveryCount%10===0){this.lastDeliveryCountDown--}}else{this.boss.say("The storage is full!");this.boss.say("Aw man ...");this.boss.say("The Game is over!");this.nextDeliveryCount=0;this.over=true}}for(var a=0;a<this.animatedStuff.length;a++){this.animatedStuff[a].draw(this)}if(this.explosion){if(this.explosion.draw()){this.explosion=undefined}}}};Game.prototype.setKeyTo=function(a,b){this.activeKeys[a]=b};var game;function setKeysTo(c,a,b){if(c.keyCode===37){a.setKeyTo("left",b)}else{if(c.keyCode===39){a.setKeyTo("right",b)}else{if(c.keyCode===38){a.setKeyTo("up",b)}else{if(c.keyCode===40){a.setKeyTo("down",b)}else{if(c.keyCode===32){a.setKeyTo("action",b)}}}}}return false}document.onkeydown=function(a){return setKeysTo(a,game,true)};document.onkeyup=function(a){return setKeysTo(a,game,false)};document.onclick=function(){if(game){if(game.over){game=new Game()}}else{game=new Game()}};function drawWelcome(){var b=~~(canvas.width/4);var a=~~(canvas.height/3);ctx.fillStyle="#00BFFF";ctx.font="30px arial";ctx.fillText("Bad Depot",b,a);a+=30;ctx.fillStyle="#7CFC00";ctx.font="20px arial";ctx.fillText("- arrow keys move the carrier around (blinking)",b,a);a+=30;ctx.fillText("- space key lifts / drops containers",b,a);a+=30;ctx.fillText("- drop containers to countdown zone (moving)",b,a);a+=30;ctx.fillText("- container color and zone color must match",b,a);a+=30;ctx.fillText("- sometimes crates explode when picked up",b,a);ctx.fillStyle="#00BFFF";a+=60;ctx.fillText("Click with mouse to start ...",b,a)}(function animloop(){requestAnimFrame(animloop);ctx.clearRect(0,0,canvas.width,canvas.height);if(game){game.draw()}else{drawWelcome()}})();