!function(){if("performance"in window==!1&&(window.performance={}),Date.now=Date.now||function(){return(new Date).getTime()},"now"in window.performance==!1){var n=window.performance.timing&&window.performance.timing.navigationStart?window.performance.timing.navigationStart:Date.now();window.performance.now=function(){return Date.now()-n}}}();var TWEEN=TWEEN||function(){var n=[];return{getAll:function(){return n},removeAll:function(){n=[]},add:function(t){n.push(t)},remove:function(t){var r=n.indexOf(t);-1!==r&&n.splice(r,1)},update:function(t){if(0===n.length)return!1;var r=0;for(t=void 0!==t?t:window.performance.now();r1?1:M),i)if(void 0!==r[f]){var T=r[f]||0,g=i[f];g instanceof Array?t[f]=p(g,m):("string"==typeof g&&(g=g.startsWith("+")||g.startsWith("-")?T+parseFloat(g,10):parseFloat(g,10)),"number"==typeof g&&(t[f]=T+(g-T)*m))}if(null!==I&&I.call(t,m),1===M){if(e>0){for(f in isFinite(e)&&e--,o){if("string"==typeof i[f]&&(o[f]=o[f]+parseFloat(i[f],10)),a){var O=o[f];o[f]=i[f],i[f]=O}r[f]=o[f]}return a&&(c=!c),h=n+s,!0}null!==v&&v.call(t);for(var N=0,W=E.length;N1?u(n[r],n[r-1],r-i):u(n[o],n[o+1>r?r:o+1],i-o)},Bezier:function(n,t){for(var r=0,i=n.length-1,o=Math.pow,u=TWEEN.Interpolation.Utils.Bernstein,e=0;e<=i;e++)r+=o(1-t,i-e)*o(t,e)*n[e]*u(i,e);return r},CatmullRom:function(n,t){var r=n.length-1,i=r*t,o=Math.floor(i),u=TWEEN.Interpolation.Utils.CatmullRom;return n[0]===n[r]?(t<0&&(o=Math.floor(i=r*(1+t))),u(n[(o-1+r)%r],n[o],n[(o+1)%r],n[(o+2)%r],i-o)):t<0?n[0]-(u(n[0],n[0],n[1],n[1],-i)-n[0]):t>1?n[r]-(u(n[r],n[r],n[r-1],n[r-1],i-r)-n[r]):u(n[o?o-1:0],n[o],n[r1;i--)r*=i;return n[t]=r,r}}(),CatmullRom:function(n,t,r,i,o){var u=.5*(r-n),e=.5*(i-t),a=o*o;return(2*t-2*r+u+e)*(o*a)+(-3*t+3*r-2*u-e)*a+u*o+t}}},function(n){"function"==typeof define&&define.amd?define([],function(){return TWEEN}):"undefined"!=typeof module&&"object"==typeof exports?module.exports=TWEEN:void 0!==n&&(n.TWEEN=TWEEN)}(this);var AddToScore=pc.createScript("addToScore");AddToScore.attributes.add("bird",{type:"entity"}),AddToScore.prototype.initialize=function(){this.lastX=this.entity.getPosition().x},AddToScore.prototype.update=function(t){var i=this.app,o=this.bird.getPosition().x,e=this.entity.getPosition().x;e<=o&&this.lastX>o&&i.fire("game:addscore"),this.lastX=e};var Medal=pc.createScript("medal");Medal.attributes.add("blank",{type:"asset",assetType:"material",array:!1}),Medal.attributes.add("bronze",{type:"asset",assetType:"material",array:!1}),Medal.attributes.add("silver",{type:"asset",assetType:"material",array:!1}),Medal.attributes.add("gold",{type:"asset",assetType:"material",array:!1}),Medal.attributes.add("platinum",{type:"asset",assetType:"material",array:!1}),Medal.prototype.initialize=function(){var a=this.app;this.materials={},this.materials.platinum=this.platinum.resource,this.materials.gold=this.gold.resource,this.materials.silver=this.silver.resource,this.materials.bronze=this.bronze.resource,this.materials.blank=this.blank.resource,a.root.findByName("Sparkle").enabled=!1,this.entity.model.material=this.materials.blank,a.on("ui:setmedal",function(e){this.entity.model.material=e>=40?this.materials.platinum:e>=30?this.materials.gold:e>=20?this.materials.silver:e>=10?this.materials.bronze:this.materials.blank,a.root.findByName("Sparkle").enabled=e>=10},this),this.on("enable",function(){a.root.findByName("Sparkle").enabled=!1,this.entity.model.material=this.materials.blank})};function storageAvailable(e){try{var t=window[e],o="__storage_test__";return t.setItem(o,o),t.removeItem(o),!0}catch(e){return!1}}var Game=pc.createScript("game");Game.prototype.initialize=function(){var e=this.app;this.score=0,this.bestScore=0,storageAvailable("localStorage")&&(this.bestScore=localStorage.getItem("Flappy Bird Best Score"),null===this.bestScore&&(this.bestScore=0)),e.on("game:menu",function(){e.fire("screen:fade"),setTimeout(function(){e.root.findByName("Game").findByName("Bird").enabled=!1,e.fire("pipes:reset"),e.fire("ground:start"),e.root.findByName("Game Over Screen").enabled=!1,e.root.findByName("Menu Screen").enabled=!0},250)},this),e.on("game:getready",function(){this.score=0,e.fire("ui:score",this.score),e.fire("screen:fade"),setTimeout(function(){e.root.findByName("Menu Screen").enabled=!1,e.root.findByName("Game Screen").enabled=!0,e.root.findByName("Get Ready").model.enabled=!0,e.root.findByName("Tap").model.enabled=!0;var t=e.root.findByName("Game").findByName("Bird");t.enabled=!0,t.script.bird.reset()},250)},this),e.on("game:play",function(){e.fire("pipes:start"),e.fire("ui:fadegetready")},this),e.on("game:pause",function(){e.root.findByName("Pause Button").enabled=!1,e.root.findByName("Play Button").enabled=!0},this),e.on("game:unpause",function(){e.root.findByName("Pause Button").enabled=!0,e.root.findByName("Play Button").enabled=!1},this),e.on("game:gameover",function(){e.root.findByName("Game Screen").enabled=!1,e.root.findByName("Game Over Screen").enabled=!0,e.root.findByName("New").enabled=this.score>this.bestScore,this.bestScore=Math.max(this.score,this.bestScore),storageAvailable("localStorage")&&localStorage.setItem("Flappy Bird Best Score",this.bestScore.toString()),e.fire("pipes:stop"),e.fire("ground:stop"),e.fire("ui:fade"),e.fire("game:audio","Swoosh"),setTimeout(function(){e.fire("ui:showscoreboard",this.score),e.fire("ui:current",this.score),e.fire("ui:best",this.bestScore),e.fire("game:audio","Swoosh")}.bind(this),500),setTimeout(function(){e.fire("ui:setmedal",this.score)}.bind(this),1e3)},this),e.on("game:addscore",function(){this.score++,e.fire("game:audio","Point"),e.fire("ui:score",this.score)},this),e.on("game:share",function(){var e=screen.width/2-320,t=screen.height/2-190,o="https://twitter.com/intent/tweet?text="+encodeURIComponent("I scored "+this.score+" in Flappy Bird! Beat that! http://flappybird.playcanvas.com/ Powered by @playcanvas #webgl #html5"),i=window.open(o,"name","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=640, height=380, top="+t+", left="+e);window.focus&&i&&i.focus()},this),e.on("game:audio",function(e){this.entity.sound.play(e)},this)},Game.prototype.update=function(e){TWEEN.update()};var Button=pc.createScript("button");Button.attributes.add("diplacement",{type:"number",default:.00390625}),Button.attributes.add("event",{type:"string"}),Button.prototype.initialize=function(){this.pressed=!1,this.min=new pc.Vec3,this.max=new pc.Vec3,this.mouseDownListener=function(e){this.press(e.clientX,e.clientY)}.bind(this),this.mouseUpListener=function(e){this.release()}.bind(this),this.touchStartListener=function(e){var t=e.changedTouches[0];this.press(t.clientX,t.clientY)}.bind(this),this.touchEndListener=function(e){this.release()}.bind(this),window.addEventListener("mousedown",this.mouseDownListener,{passive:!1}),window.addEventListener("mouseup",this.mouseUpListener,{passive:!1}),window.addEventListener("touchstart",this.touchStartListener,{passive:!1}),window.addEventListener("touchend",this.touchEndListener,{passive:!1}),this.on("enable",function(){window.addEventListener("mousedown",this.mouseDownListener,{passive:!1}),window.addEventListener("mouseup",this.mouseUpListener,{passive:!1}),window.addEventListener("touchstart",this.touchStartListener,{passive:!1}),window.addEventListener("touchend",this.touchEndListener,{passive:!1})}),this.on("disable",function(){this.pressed=!1,window.removeEventListener("mousedown",this.mouseDownListener,{passive:!1}),window.removeEventListener("mouseup",this.mouseUpListener,{passive:!1}),window.removeEventListener("touchstart",this.touchStartListener,{passive:!1}),window.removeEventListener("touchend",this.touchEndListener,{passive:!1})})},Button.prototype.checkForClick=function(e,t){var s=this.app.root.findByName("Camera"),i=this.entity.model.model.meshInstances[0].aabb;return s.camera.worldToScreen(i.getMin(),this.min),s.camera.worldToScreen(i.getMax(),this.max),e>=this.min.x&&e<=this.max.x&&t>=this.max.y&&t<=this.min.y},Button.prototype.press=function(e,t){this.checkForClick(e,t)&&(this.pressed=!0,this.entity.translate(0,-this.diplacement,0))},Button.prototype.release=function(){var e=this.app;this.pressed&&(this.pressed=!1,this.entity.translate(0,this.diplacement,0),e.fire(this.event),e.fire("game:audio","Swoosh"))};var Sparkle=pc.createScript("sparkle");Sparkle.attributes.add("radius",{type:"number",default:1}),Sparkle.prototype.initialize=function(){var i=this.app;this.initialPos=this.entity.getLocalPosition().clone(),i.on("ui:sparkle",function(){var i=Math.random()*Math.PI*2,t=Math.cos(i)*this.radius*Math.random(),a=Math.sin(i)*this.radius*Math.random();this.entity.setLocalPosition(this.initialPos.x+t,this.initialPos.y+a,this.initialPos.z)},this)};var Tween=pc.createScript("tween");Tween.attributes.add("event",{type:"string"}),Tween.attributes.add("duration",{type:"number",default:1}),Tween.attributes.add("startPos",{type:"vec3",default:[0,0,0]}),Tween.attributes.add("endPos",{type:"vec3",default:[0,0,0]}),Tween.attributes.add("startScale",{type:"vec3",default:[1,1,1]}),Tween.attributes.add("endScale",{type:"vec3",default:[1,1,1]}),Tween.attributes.add("easing",{type:"number",enum:[{Linear:0},{Quadratic:1},{Cubic:2},{Quartic:3},{Quintic:4},{Sinusoidal:5},{Exponential:6},{Circular:7},{Elastic:8},{Back:9},{Bounce:10}]}),Tween.attributes.add("inOut",{type:"number",enum:[{In:0},{Out:1},{InOut:2}]});var easingFuncs=[[TWEEN.Easing.Linear.None,TWEEN.Easing.Linear.None,TWEEN.Easing.Linear.None],[TWEEN.Easing.Quadratic.In,TWEEN.Easing.Quadratic.Out,TWEEN.Easing.Quadratic.InOut],[TWEEN.Easing.Cubic.In,TWEEN.Easing.Cubic.Out,TWEEN.Easing.Cubic.InOut],[TWEEN.Easing.Quartic.In,TWEEN.Easing.Quartic.Out,TWEEN.Easing.Quartic.InOut],[TWEEN.Easing.Quintic.In,TWEEN.Easing.Quintic.Out,TWEEN.Easing.Quintic.InOut],[TWEEN.Easing.Sinusoidal.In,TWEEN.Easing.Sinusoidal.Out,TWEEN.Easing.Sinusoidal.InOut],[TWEEN.Easing.Exponential.In,TWEEN.Easing.Exponential.Out,TWEEN.Easing.Exponential.InOut],[TWEEN.Easing.Circular.In,TWEEN.Easing.Circular.Out,TWEEN.Easing.Circular.InOut],[TWEEN.Easing.Elastic.In,TWEEN.Easing.Elastic.Out,TWEEN.Easing.Elastic.InOut],[TWEEN.Easing.Back.In,TWEEN.Easing.Back.Out,TWEEN.Easing.Back.InOut],[TWEEN.Easing.Bounce.In,TWEEN.Easing.Bounce.Out,TWEEN.Easing.Bounce.InOut]];Tween.prototype.initialize=function(){var t=this.app,i=this.entity;this.initialPos=this.entity.getPosition().clone(),this.initialScl=this.entity.getLocalScale().clone(),t.on(this.event,function(){var t={px:this.startPos.x,py:this.startPos.y,pz:this.startPos.z,sx:this.startScale.x,sy:this.startScale.y,sz:this.startScale.z},n={px:this.endPos.x,py:this.endPos.y,pz:this.endPos.z,sx:this.endScale.x,sy:this.endScale.y,sz:this.endScale.z};this.tween=new TWEEN.Tween(t).to(n,Math.floor(1e3*this.duration)).easing(easingFuncs[this.easing][this.inOut]).onUpdate(function(){i.setPosition(this.px,this.py,this.pz),i.setLocalScale(this.sx,this.sy,this.sz)}),this.tween.start()},this),this.on("enable",function(){this.entity.setPosition(this.initialPos),this.entity.setLocalScale(this.initialScl)}),this.on("attr",function(i,n,a){t.fire(this.event)})},Tween.prototype.update=function(t){},Tween.prototype.swap=function(t){};var Bird=pc.createScript("bird");Bird.attributes.add("flapVelocity",{type:"number",default:1}),Bird.attributes.add("gravity",{type:"number",default:5}),Bird.attributes.add("lowestHeight",{type:"number",default:-.25}),Bird.attributes.add("radius",{type:"number",default:.068}),Bird.prototype.initialize=function(){var t=this.app;this.velocity=0,this.state="ready",this.pipes=[],this.circle={x:0,y:0,r:0},this.rect={x:0,y:0,w:0,h:0},this.initialPos=this.entity.getPosition().clone(),this.initialRot=this.entity.getRotation().clone();var i=t.root.findByName("Pipes").findByName("Pipe 1");this.pipes.push(i.findByName("Pipe Up")),this.pipes.push(i.findByName("Pipe Down")),i=t.root.findByName("Pipes").findByName("Pipe 2"),this.pipes.push(i.findByName("Pipe Up")),this.pipes.push(i.findByName("Pipe Down")),t.on("game:pause",function(){this.state="paused"},this),t.on("game:unpause",function(){this.state="play"},this),this.flapListener=this.flap.bind(this),window.addEventListener("mousedown",this.flapListener,{passive:!1}),window.addEventListener("touchstart",this.flapListener,{passive:!1}),this.on("enable",function(){window.addEventListener("mousedown",this.flapListener,{passive:!1}),window.addEventListener("touchstart",this.flapListener,{passive:!1}),this.state="getready"}),this.on("disable",function(){window.removeEventListener("mousedown",this.flapListener,{passive:!1}),window.removeEventListener("touchstart",this.flapListener,{passive:!1})})},Bird.prototype.reset=function(){var t=this.app;this.velocity=0,this.state="getready",this.entity.setPosition(this.initialPos),this.entity.setRotation(this.initialRot),t.fire("bird:flapstart")},Bird.prototype.flap=function(t){var i=this.app;t&&t.preventDefault(),"getready"===this.state&&(i.fire("game:play"),this.state="play"),"play"===this.state&&(i.fire("game:audio","Flap"),this.velocity=this.flapVelocity)},Bird.prototype.die=function(t){var i=this.app;this.state="dead",i.fire("game:audio","Hit"),i.fire("bird:flapstop"),i.fire("game:gameover"),t&&setTimeout(function(){i.fire("game:audio","Die")},500)},Bird.prototype.circleRectangleIntersect=function(t,i){var e=t.x,s=t.y,a=t.r,r=i.x,n=i.y,p=i.w,o=i.h,h=Math.abs(e-r-p/2),d=Math.abs(s-n-o/2);if(h>p/2+a)return!1;if(d>o/2+a)return!1;if(h<=p/2)return!0;if(d<=o/2)return!0;var l=h-p/2,y=d-o/2;return l*l+y*y<=a*a},Bird.prototype.update=function(t){var i=this.app,e="play"===this.state,s="dead"===this.state;e&&i.keyboard.wasPressed(pc.KEY_SPACE)&&this.flap();var a=this.entity.getPosition();if((e||s)&&a.y>=this.lowestHeight){this.velocity-=this.gravity*t,this.entity.translate(0,this.velocity*t,0);var r=pc.math.clamp(this.velocity,-2,-.75);r+=1,this.entity.setLocalEulerAngles(0,0,90*r)}if(e){a.y0){this.timer=Math.max(this.timer-t,0);var e,a=(this.duration-this.timer)/this.duration;switch(this.type){case 0:e=a;break;case 1:e=1-a;break;case 2:e=4*Math.abs((a/2+.25)%.5-.25)}var i=this.entity.model,r=i.enabled;r&&0===e?(i.enabled=!1,e=1):!r&&e>0&&(i.enabled=!0);var d=i.material;d.opacity=e,d.update()}};