-
Notifications
You must be signed in to change notification settings - Fork 3
/
game-logic.min.js
11 lines (10 loc) · 32 KB
/
game-logic.min.js
1
2
3
4
5
6
7
8
9
10
11
/*!
* game-logic.js | Dinosaur Game WordPress Plugin | https://wordpress.org/plugins/dinosaur-game
* This plugin is free to use under the GPLv2 license. | https://www.gnu.org/licenses/gpl-2.0.html
*
* This file contains JavaScript forked from the 'T-Rex runner' game in Chromium.
* Chromium is under Copyright 2015 by The Chromium Authors. All rights reserved.
* See also: https://cs.chromium.org/chromium/src/LICENSE
*
*/
!function(){"use strict";function T(t,i){if(T.instance_)return T.instance_;(T.instance_=this).outerContainerEl=document.querySelector(t),this.containerEl=null,this.snackbarEl=null,this.detailsButton=this.outerContainerEl.querySelector("#details-button"),this.config=i||T.config,this.dimensions=T.defaultDimensions,this.canvas=null,this.canvasCtx=null,this.tRex=null,this.distanceMeter=null,this.distanceRan=0,this.highestScore=0,this.time=0,this.runningTime=0,this.msPerFrame=1e3/e,this.currentSpeed=this.config.SPEED,this.obstacles=[],this.activated=!1,this.playing=!1,this.crashed=!1,this.paused=!1,this.inverted=!1,this.invertTimer=0,this.resizeTimerId_=null,this.playCount=0,this.audioBuffer=null,this.soundFx={},this.audioContext=null,this.images={},this.imagesLoaded=0,this.isDisabled()?this.setupDisabledRunner():this.loadImages()}window.Runner=T;var e=60,p=1<window.devicePixelRatio,h=/iPad|iPhone|iPod/.test(window.navigator.platform),o=/Android/.test(window.navigator.userAgent)||h;window;function a(t,i){return Math.floor(Math.random()*(i-t+1))+t}function r(t){for(var i=t.length/4*3,s=atob(t),e=new ArrayBuffer(i),n=new Uint8Array(e),h=0;h<i;h++)n[h]=s.charCodeAt(h);return n.buffer}function n(){return h?(new Date).getTime():performance.now()}function f(t,i,s,e){this.canvas=t,this.canvasCtx=t.getContext("2d"),this.canvasDimensions=e,this.textImgPos=i,this.restartImgPos=s,this.draw()}function l(t,i){return new E(t.x+i.x,t.y+i.y,t.width,t.height)}function m(t,i,s){t.save(),t.strokeStyle="#f00",t.strokeRect(i.x,i.y,i.width,i.height),t.strokeStyle="#0f0",t.strokeRect(s.x,s.y,s.width,s.height),t.restore()}function g(t,i){var s=!1,e=(t.x,t.y,i.x);i.y;return t.x<e+i.width&&t.x+t.width>e&&t.y<i.y+i.height&&t.height+t.y>i.y&&(s=!0),s}function E(t,i,s,e){this.x=t,this.y=i,this.width=s,this.height=e}function c(t,i,s,e,n,h,o){this.canvasCtx=t,this.spritePos=s,this.typeConfig=i,this.gapCoefficient=n,this.size=a(1,c.MAX_OBSTACLE_LENGTH),this.dimensions=e,this.remove=!1,this.xPos=e.WIDTH+(o||0),this.yPos=0,this.width=0,this.collisionBoxes=[],this.gap=0,this.speedOffset=0,this.currentFrame=0,this.timer=0,this.init(h)}function I(t,i){this.canvas=t,this.canvasCtx=t.getContext("2d"),this.spritePos=i,this.xPos=0,this.yPos=0,this.groundYPos=0,this.currentFrame=0,this.currentAnimFrames=[],this.blinkDelay=0,this.blinkCount=0,this.animStartTime=0,this.timer=0,this.msPerFrame=1e3/e,this.config=I.config,this.status=I.status.WAITING,this.jumping=!1,this.ducking=!1,this.jumpVelocity=0,this.reachedMinHeight=!1,this.speedDrop=!1,this.jumpCount=0,this.jumpspotX=0,this.init()}function C(t,i,s){this.canvas=t,this.canvasCtx=t.getContext("2d"),this.image=T.imageSprite,this.spritePos=i,this.x=0,this.y=5,this.currentDistance=0,this.maxScore=0,this.highScore=0,this.container=null,this.digits=[],this.acheivement=!1,this.defaultString="",this.flashTimer=0,this.flashIterations=0,this.invertTrigger=!1,this.config=C.config,this.maxScoreUnits=this.config.MAX_DISTANCE_UNITS,this.init(s)}function d(t,i,s){this.canvas=t,this.canvasCtx=this.canvas.getContext("2d"),this.spritePos=i,this.containerWidth=s,this.xPos=s,this.yPos=0,this.remove=!1,this.cloudGap=a(d.config.MIN_CLOUD_GAP,d.config.MAX_CLOUD_GAP),this.init()}function u(t,i,s){this.spritePos=i,this.canvas=t,this.canvasCtx=t.getContext("2d"),this.xPos=s-50,this.yPos=30,this.currentPhase=0,this.opacity=0,this.containerWidth=s,this.stars=[],this.drawStars=!1,this.placeStars()}function s(t,i){this.spritePos=i,this.canvas=t,this.canvasCtx=t.getContext("2d"),this.sourceDimensions={},this.dimensions=s.dimensions,this.sourceXPos=[this.spritePos.x,this.spritePos.x+this.dimensions.WIDTH],this.xPos=[],this.yPos=0,this.bumpThreshold=.5,this.setSourceDimensions(),this.draw()}function S(t,i,s,e){this.canvas=t,this.canvasCtx=this.canvas.getContext("2d"),this.config=S.config,this.dimensions=s,this.gapCoefficient=e,this.obstacles=[],this.obstacleHistory=[],this.horizonOffsets=[0,0],this.cloudFrequency=this.config.CLOUD_FREQUENCY,this.spritePos=i,this.nightMode=null,this.clouds=[],this.cloudSpeed=this.config.BG_CLOUD_SPEED,this.horizonLine=null,this.init()}T.config={ACCELERATION:.001,BG_CLOUD_SPEED:.2,BOTTOM_PAD:10,CLEAR_TIME:3e3,CLOUD_FREQUENCY:.5,GAMEOVER_CLEAR_TIME:750,GAP_COEFFICIENT:.6,GRAVITY:.6,INITIAL_JUMP_VELOCITY:12,INVERT_FADE_DURATION:12e3,INVERT_DISTANCE:700,MAX_BLINK_COUNT:3,MAX_CLOUDS:6,MAX_OBSTACLE_LENGTH:3,MAX_OBSTACLE_DUPLICATION:2,MAX_SPEED:13,MIN_JUMP_HEIGHT:35,MOBILE_SPEED_COEFFICIENT:1.2,RESOURCE_TEMPLATE_ID:"audio-resources",SPEED:6,SPEED_DROP_COEFFICIENT:3},T.defaultDimensions={WIDTH:600,HEIGHT:150},T.classes={CANVAS:"dino-canvas",CONTAINER:"dino-container",CRASHED:"crashed",ICON:"dino-initial-icon",INVERTED:"inverted",SNACKBAR:"dino-snackbar",SNACKBAR_SHOW:"dino-snackbar-show",TOUCH_CONTROLLER:"controller"},T.spriteDefinition={LDPI:{CACTUS_LARGE:{x:332,y:2},CACTUS_SMALL:{x:228,y:2},CLOUD:{x:86,y:2},HORIZON:{x:2,y:54},MOON:{x:484,y:2},PTERODACTYL:{x:134,y:2},RESTART:{x:2,y:2},TEXT_SPRITE:{x:655,y:2},TREX:{x:848,y:2},STAR:{x:645,y:2}},HDPI:{CACTUS_LARGE:{x:652,y:2},CACTUS_SMALL:{x:446,y:2},CLOUD:{x:166,y:2},HORIZON:{x:2,y:104},MOON:{x:954,y:2},PTERODACTYL:{x:260,y:2},RESTART:{x:2,y:2},TEXT_SPRITE:{x:1294,y:2},TREX:{x:1678,y:2},STAR:{x:1276,y:2}}},T.sounds={BUTTON_PRESS:"offline-sound-press",HIT:"offline-sound-hit",SCORE:"offline-sound-reached"},T.keycodes={JUMP:{38:1,32:1},DUCK:{40:1},RESTART:{13:1}},T.events={ANIM_END:"webkitAnimationEnd",CLICK:"click",KEYDOWN:"keydown",KEYUP:"keyup",MOUSEDOWN:"mousedown",MOUSEUP:"mouseup",RESIZE:"resize",TOUCHEND:"touchend",TOUCHSTART:"touchstart",VISIBILITY:"visibilitychange",BLUR:"blur",FOCUS:"focus",LOAD:"load"},T.prototype={isDisabled:function(){return!1},setupDisabledRunner:function(){this.containerEl=document.createElement("div"),this.containerEl.className=T.classes.SNACKBAR,this.containerEl.textContent=loadTimeData.getValue("disabledEasterEgg"),this.outerContainerEl.appendChild(this.containerEl),document.addEventListener(T.events.KEYDOWN,function(t){T.keycodes.JUMP[t.keyCode]&&(this.containerEl.classList.add(T.classes.SNACKBAR_SHOW),document.querySelector(".dino-icon").classList.add("icon-disabled"))}.bind(this))},updateConfigSetting:function(t,i){if(t in this.config&&null!=i)switch(this.config[t]=i,t){case"GRAVITY":case"MIN_JUMP_HEIGHT":case"SPEED_DROP_COEFFICIENT":this.tRex.config[t]=i;break;case"INITIAL_JUMP_VELOCITY":this.tRex.setJumpVelocity(i);break;case"SPEED":this.setSpeed(i)}},loadImages:function(){p?(T.imageSprite=document.getElementById("dinogame-sprites-2x"),this.spriteDef=T.spriteDefinition.HDPI):(T.imageSprite=document.getElementById("dinogame-sprites-1x"),this.spriteDef=T.spriteDefinition.LDPI),T.imageSprite.complete?this.init():T.imageSprite.addEventListener(T.events.LOAD,this.init.bind(this))},loadSounds:function(){if(!h){var t=window.AudioContext||window.webkitAudioContext;this.audioContext=new t;var i=document.getElementById(this.config.RESOURCE_TEMPLATE_ID).content;for(var s in T.sounds){var e=i.getElementById(T.sounds[s]).src,n=r(e=e.substr(e.indexOf(",")+1));this.audioContext.decodeAudioData(n,function(t,i){this.soundFx[t]=i}.bind(this,s))}}},setSpeed:function(t){var i=t||this.currentSpeed;if(this.dimensions.WIDTH<600){var s=i*this.dimensions.WIDTH/600*this.config.MOBILE_SPEED_COEFFICIENT;this.currentSpeed=i<s?i:s}else t&&(this.currentSpeed=t)},init:function(){var t,i,s,e,n;document.querySelector("."+T.classes.ICON).style.visibility="hidden",this.adjustDimensions(),this.setSpeed(),this.containerEl=document.createElement("div"),this.containerEl.className=T.classes.CONTAINER,this.canvas=(t=this.containerEl,i=this.dimensions.WIDTH,s=this.dimensions.HEIGHT,e=T.classes.PLAYER,(n=document.createElement("canvas")).className=e?T.classes.CANVAS+" "+e:T.classes.CANVAS,n.width=i,n.height=s,t.appendChild(n),n),this.canvasCtx=this.canvas.getContext("2d"),this.canvasCtx.fillStyle="#f7f7f7",this.canvasCtx.fill(),T.updateCanvasScaling(this.canvas),this.horizon=new S(this.canvas,this.spriteDef,this.dimensions,this.config.GAP_COEFFICIENT),this.distanceMeter=new C(this.canvas,this.spriteDef.TEXT_SPRITE,this.dimensions.WIDTH),this.tRex=new I(this.canvas,this.spriteDef.TREX),this.outerContainerEl.appendChild(this.containerEl),o&&this.createTouchController(),this.startListening(),this.update(),window.addEventListener(T.events.RESIZE,this.debounceResize.bind(this))},createTouchController:function(){this.touchController=document.createElement("div"),this.touchController.className=T.classes.TOUCH_CONTROLLER,this.outerContainerEl.appendChild(this.touchController)},debounceResize:function(){this.resizeTimerId_||(this.resizeTimerId_=setInterval(this.adjustDimensions.bind(this),250))},adjustDimensions:function(){clearInterval(this.resizeTimerId_),this.resizeTimerId_=null;var t=window.getComputedStyle(this.outerContainerEl),i=Number(t.paddingLeft.substr(0,t.paddingLeft.length-2));this.dimensions.WIDTH=this.outerContainerEl.offsetWidth-2*i,this.canvas&&(this.canvas.width=this.dimensions.WIDTH,this.canvas.height=this.dimensions.HEIGHT,T.updateCanvasScaling(this.canvas),this.distanceMeter.calcXPos(this.dimensions.WIDTH),this.clearCanvas(),this.horizon.update(0,0,!0),this.tRex.update(0),this.playing||this.crashed||this.paused?(this.containerEl.style.width=this.dimensions.WIDTH+"px",this.containerEl.style.height=this.dimensions.HEIGHT+"px",this.distanceMeter.update(0,Math.ceil(this.distanceRan)),this.stop()):this.tRex.draw(0,0),this.crashed&&this.gameOverPanel&&(this.gameOverPanel.updateDimensions(this.dimensions.WIDTH),this.gameOverPanel.draw()))},playIntro:function(){if(this.activated||this.crashed)this.crashed&&this.restart();else{this.playingIntro=!0,this.tRex.playingIntro=!0;var t="@-webkit-keyframes intro { from { width:"+I.config.WIDTH+"px }to { width: "+this.dimensions.WIDTH+"px }}",i=document.createElement("style");i.innerHTML=t,document.head.appendChild(i),this.containerEl.addEventListener(T.events.ANIM_END,this.startGame.bind(this)),this.containerEl.style.webkitAnimation="intro .4s ease-out 1 both",this.containerEl.style.width=this.dimensions.WIDTH+"px",this.playing=!0,this.activated=!0}},startGame:function(){this.runningTime=0,this.playingIntro=!1,this.tRex.playingIntro=!1,this.containerEl.style.webkitAnimation="",this.playCount++,document.addEventListener(T.events.VISIBILITY,this.onVisibilityChange.bind(this)),window.addEventListener(T.events.BLUR,this.onVisibilityChange.bind(this)),window.addEventListener(T.events.FOCUS,this.onVisibilityChange.bind(this))},clearCanvas:function(){this.canvasCtx.clearRect(0,0,this.dimensions.WIDTH,this.dimensions.HEIGHT)},update:function(){this.updatePending=!1;var t=n(),i=t-(this.time||t);if(this.time=t,this.playing){this.clearCanvas(),this.tRex.jumping&&this.tRex.updateJump(i),this.runningTime+=i;var s=this.runningTime>this.config.CLEAR_TIME;if(1!=this.tRex.jumpCount||this.playingIntro||this.playIntro(),this.playingIntro?this.horizon.update(0,this.currentSpeed,s):(i=this.activated?i:0,this.horizon.update(i,this.currentSpeed,s,this.inverted)),s&&function(t,i,s){T.defaultDimensions.WIDTH,t.xPos;var e=new E(i.xPos+1,i.yPos+1,i.config.WIDTH-2,i.config.HEIGHT-2),n=new E(t.xPos+1,t.yPos+1,t.typeConfig.width*t.size-2,t.typeConfig.height-2);s&&m(s,e,n);if(g(e,n))for(var h=t.collisionBoxes,o=i.ducking?I.collisionBoxes.DUCKING:I.collisionBoxes.RUNNING,a=0;a<o.length;a++)for(var r=0;r<h.length;r++){var c=l(o[a],e),d=l(h[r],n),u=g(c,d);if(s&&m(s,c,d),u)return[c,d]}return!1}(this.horizon.obstacles[0],this.tRex)?this.gameOver():(this.distanceRan+=this.currentSpeed*i/this.msPerFrame,this.currentSpeed<this.config.MAX_SPEED&&(this.currentSpeed+=this.config.ACCELERATION)),this.distanceMeter.update(i,Math.ceil(this.distanceRan))&&this.playSound(this.soundFx.SCORE),this.invertTimer>this.config.INVERT_FADE_DURATION)this.invertTimer=0,this.invertTrigger=!1,this.invert();else if(this.invertTimer)this.invertTimer+=i;else{var e=this.distanceMeter.getActualDistance(Math.ceil(this.distanceRan));0<e&&(this.invertTrigger=!(e%this.config.INVERT_DISTANCE),this.invertTrigger&&0===this.invertTimer&&(this.invertTimer+=i,this.invert()))}}(this.playing||!this.activated&&this.tRex.blinkCount<T.config.MAX_BLINK_COUNT)&&(this.tRex.update(i),this.scheduleNextUpdate())},handleEvent:function(s){return function(t,i){switch(t){case i.KEYDOWN:case i.TOUCHSTART:case i.MOUSEDOWN:this.onKeyDown(s);break;case i.KEYUP:case i.TOUCHEND:case i.MOUSEUP:this.onKeyUp(s)}}.bind(this)(s.type,T.events)},startListening:function(){document.addEventListener(T.events.KEYDOWN,this),document.addEventListener(T.events.KEYUP,this),o?(this.touchController.addEventListener(T.events.TOUCHSTART,this),this.touchController.addEventListener(T.events.TOUCHEND,this),this.containerEl.addEventListener(T.events.TOUCHSTART,this)):(document.addEventListener(T.events.MOUSEDOWN,this),document.addEventListener(T.events.MOUSEUP,this))},stopListening:function(){document.removeEventListener(T.events.KEYDOWN,this),document.removeEventListener(T.events.KEYUP,this),o?(this.touchController.removeEventListener(T.events.TOUCHSTART,this),this.touchController.removeEventListener(T.events.TOUCHEND,this),this.containerEl.removeEventListener(T.events.TOUCHSTART,this)):(document.removeEventListener(T.events.MOUSEDOWN,this),document.removeEventListener(T.events.MOUSEUP,this))},onKeyDown:function(t){this.playing&&t.preventDefault(),t.target!=this.detailsButton&&(this.crashed||!T.keycodes.JUMP[t.keyCode]&&t.type!=T.events.TOUCHSTART||(this.playing||(this.loadSounds(),this.playing=!0,this.update(),window.errorPageController&&errorPageController.trackEasterEgg()),this.tRex.jumping||this.tRex.ducking||(this.playSound(this.soundFx.BUTTON_PRESS),this.tRex.startJump(this.currentSpeed))),this.crashed&&t.type==T.events.TOUCHSTART&&t.currentTarget==this.containerEl&&this.restart()),this.playing&&!this.crashed&&T.keycodes.DUCK[t.keyCode]&&(t.preventDefault(),this.tRex.jumping?this.tRex.setSpeedDrop():this.tRex.jumping||this.tRex.ducking||this.tRex.setDuck(!0))},onKeyUp:function(t){var i=String(t.keyCode),s=T.keycodes.JUMP[i]||t.type==T.events.TOUCHEND||t.type==T.events.MOUSEDOWN;if(this.isRunning()&&s)this.tRex.endJump();else if(T.keycodes.DUCK[i])this.tRex.speedDrop=!1,this.tRex.setDuck(!1);else if(this.crashed){var e=n()-this.time;(T.keycodes.RESTART[i]||this.isLeftClickOnCanvas(t)||e>=this.config.GAMEOVER_CLEAR_TIME&&T.keycodes.JUMP[i])&&this.restart()}else this.paused&&s&&(this.tRex.reset(),this.play())},isLeftClickOnCanvas:function(t){return null!=t.button&&t.button<2&&t.type==T.events.MOUSEUP&&t.target==this.canvas},scheduleNextUpdate:function(){this.updatePending||(this.updatePending=!0,this.raqId=requestAnimationFrame(this.update.bind(this)))},isRunning:function(){return!!this.raqId},gameOver:function(){var t;this.playSound(this.soundFx.HIT),t=200,o&&window.navigator.vibrate&&window.navigator.vibrate(t),this.stop(),this.crashed=!0,this.distanceMeter.acheivement=!1,this.tRex.update(100,I.status.CRASHED),this.gameOverPanel?this.gameOverPanel.draw():this.gameOverPanel=new f(this.canvas,this.spriteDef.TEXT_SPRITE,this.spriteDef.RESTART,this.dimensions),this.distanceRan>this.highestScore&&(this.highestScore=Math.ceil(this.distanceRan),this.distanceMeter.setHighScore(this.highestScore)),this.time=n()},stop:function(){this.playing=!1,this.paused=!0,cancelAnimationFrame(this.raqId),this.raqId=0},play:function(){this.crashed||(this.playing=!0,this.paused=!1,this.tRex.update(0,I.status.RUNNING),this.time=n(),this.update())},restart:function(){this.raqId||(this.playCount++,this.runningTime=0,this.playing=!0,this.crashed=!1,this.distanceRan=0,this.setSpeed(this.config.SPEED),this.time=n(),this.containerEl.classList.remove(T.classes.CRASHED),this.clearCanvas(),this.distanceMeter.reset(this.highestScore),this.horizon.reset(),this.tRex.reset(),this.playSound(this.soundFx.BUTTON_PRESS),this.invert(!0),this.update())},onVisibilityChange:function(t){document.hidden||document.webkitHidden||"blur"==t.type||"visible"!=document.visibilityState?this.stop():this.crashed||(this.tRex.reset(),this.play())},playSound:function(t){if(t){var i=this.audioContext.createBufferSource();i.buffer=t,i.connect(this.audioContext.destination),i.start(0)}},invert:function(t){t?(document.body.classList.toggle(T.classes.INVERTED,!1),this.invertTimer=0,this.inverted=!1):this.inverted=document.body.classList.toggle(T.classes.INVERTED,this.invertTrigger)}},T.updateCanvasScaling=function(t,i,s){var e=t.getContext("2d"),n=Math.floor(window.devicePixelRatio)||1,h=Math.floor(e.webkitBackingStorePixelRatio)||1,o=n/h;if(n!==h){var a=i||t.width,r=s||t.height;return t.width=a*o,t.height=r*o,t.style.width=a+"px",t.style.height=r+"px",e.scale(o,o),!0}return 1==n&&(t.style.width=t.width+"px",t.style.height=t.height+"px"),!1},f.dimensions={TEXT_X:0,TEXT_Y:13,TEXT_WIDTH:191,TEXT_HEIGHT:11,RESTART_WIDTH:36,RESTART_HEIGHT:32},f.prototype={updateDimensions:function(t,i){this.canvasDimensions.WIDTH=t,i&&(this.canvasDimensions.HEIGHT=i)},draw:function(){var t=f.dimensions,i=this.canvasDimensions.WIDTH/2,s=t.TEXT_X,e=t.TEXT_Y,n=t.TEXT_WIDTH,h=t.TEXT_HEIGHT,o=Math.round(i-t.TEXT_WIDTH/2),a=Math.round((this.canvasDimensions.HEIGHT-25)/3),r=t.TEXT_WIDTH,c=t.TEXT_HEIGHT,d=t.RESTART_WIDTH,u=t.RESTART_HEIGHT,l=i-t.RESTART_WIDTH/2,m=this.canvasDimensions.HEIGHT/2;p&&(e*=2,s*=2,n*=2,h*=2,d*=2,u*=2),s+=this.textImgPos.x,e+=this.textImgPos.y,this.canvasCtx.drawImage(T.imageSprite,s,e,n,h,o,a,r,c),this.canvasCtx.drawImage(T.imageSprite,this.restartImgPos.x,this.restartImgPos.y,d,u,l,m,t.RESTART_WIDTH,t.RESTART_HEIGHT)}},c.MAX_GAP_COEFFICIENT=1.5,c.MAX_OBSTACLE_LENGTH=3,c.prototype={init:function(t){if(this.cloneCollisionBoxes(),1<this.size&&this.typeConfig.multipleSpeed>t&&(this.size=1),this.width=this.typeConfig.width*this.size,Array.isArray(this.typeConfig.yPos)){var i=o?this.typeConfig.yPosMobile:this.typeConfig.yPos;this.yPos=i[a(0,i.length-1)]}else this.yPos=this.typeConfig.yPos;this.draw(),1<this.size&&(this.collisionBoxes[1].width=this.width-this.collisionBoxes[0].width-this.collisionBoxes[2].width,this.collisionBoxes[2].x=this.width-this.collisionBoxes[2].width),this.typeConfig.speedOffset&&(this.speedOffset=.5<Math.random()?this.typeConfig.speedOffset:-this.typeConfig.speedOffset),this.gap=this.getGap(this.gapCoefficient,t)},draw:function(){var t=this.typeConfig.width,i=this.typeConfig.height;p&&(t*=2,i*=2);var s=t*this.size*(.5*(this.size-1))+this.spritePos.x;0<this.currentFrame&&(s+=t*this.currentFrame),this.canvasCtx.drawImage(T.imageSprite,s,this.spritePos.y,t*this.size,i,this.xPos,this.yPos,this.typeConfig.width*this.size,this.typeConfig.height)},update:function(t,i){this.remove||(this.typeConfig.speedOffset&&(i+=this.speedOffset),this.xPos-=Math.floor(i*e/1e3*t),this.typeConfig.numFrames&&(this.timer+=t,this.timer>=this.typeConfig.frameRate&&(this.currentFrame=this.currentFrame==this.typeConfig.numFrames-1?0:this.currentFrame+1,this.timer=0)),this.draw(),this.isVisible()||(this.remove=!0))},getGap:function(t,i){var s=Math.round(this.width*i+this.typeConfig.minGap*t);return a(s,Math.round(s*c.MAX_GAP_COEFFICIENT))},isVisible:function(){return 0<this.xPos+this.width},cloneCollisionBoxes:function(){for(var t=this.typeConfig.collisionBoxes,i=t.length-1;0<=i;i--)this.collisionBoxes[i]=new E(t[i].x,t[i].y,t[i].width,t[i].height)}},c.types=[{type:"CACTUS_SMALL",width:17,height:35,yPos:105,multipleSpeed:4,minGap:120,minSpeed:0,collisionBoxes:[new E(0,7,5,27),new E(4,0,6,34),new E(10,4,7,14)]},{type:"CACTUS_LARGE",width:25,height:50,yPos:90,multipleSpeed:7,minGap:120,minSpeed:0,collisionBoxes:[new E(0,12,7,38),new E(8,0,7,49),new E(13,10,10,38)]},{type:"PTERODACTYL",width:46,height:40,yPos:[100,75,50],yPosMobile:[100,50],multipleSpeed:999,minSpeed:8.5,minGap:150,collisionBoxes:[new E(15,15,16,5),new E(18,21,24,6),new E(2,14,4,3),new E(6,10,4,7),new E(10,8,6,9)],numFrames:2,frameRate:1e3/6,speedOffset:.8}],I.config={DROP_VELOCITY:-5,GRAVITY:.6,HEIGHT:47,HEIGHT_DUCK:25,INIITAL_JUMP_VELOCITY:-10,INTRO_DURATION:1500,MAX_JUMP_HEIGHT:30,MIN_JUMP_HEIGHT:30,SPEED_DROP_COEFFICIENT:3,SPRITE_WIDTH:262,START_X_POS:50,WIDTH:44,WIDTH_DUCK:59},I.collisionBoxes={DUCKING:[new E(1,18,55,25)],RUNNING:[new E(22,0,17,16),new E(1,18,30,9),new E(10,35,14,8),new E(1,24,29,5),new E(5,30,21,4),new E(9,34,15,4)]},I.status={CRASHED:"CRASHED",DUCKING:"DUCKING",JUMPING:"JUMPING",RUNNING:"RUNNING",WAITING:"WAITING"},I.BLINK_TIMING=7e3,I.animFrames={WAITING:{frames:[44,0],msPerFrame:1e3/3},RUNNING:{frames:[88,132],msPerFrame:1e3/12},CRASHED:{frames:[220],msPerFrame:1e3/60},JUMPING:{frames:[0],msPerFrame:1e3/60},DUCKING:{frames:[264,323],msPerFrame:125}},I.prototype={init:function(){this.groundYPos=T.defaultDimensions.HEIGHT-this.config.HEIGHT-T.config.BOTTOM_PAD,this.yPos=this.groundYPos,this.minJumpHeight=this.groundYPos-this.config.MIN_JUMP_HEIGHT,this.draw(0,0),this.update(0,I.status.WAITING)},setJumpVelocity:function(t){this.config.INIITAL_JUMP_VELOCITY=-t,this.config.DROP_VELOCITY=-t/2},update:function(t,i){this.timer+=t,i&&(this.status=i,this.currentFrame=0,this.msPerFrame=I.animFrames[i].msPerFrame,this.currentAnimFrames=I.animFrames[i].frames,i==I.status.WAITING&&(this.animStartTime=n(),this.setBlinkDelay())),this.playingIntro&&this.xPos<this.config.START_X_POS&&(this.xPos+=Math.round(this.config.START_X_POS/this.config.INTRO_DURATION*t)),this.status==I.status.WAITING?this.blink(n()):this.draw(this.currentAnimFrames[this.currentFrame],0),this.timer>=this.msPerFrame&&(this.currentFrame=this.currentFrame==this.currentAnimFrames.length-1?0:this.currentFrame+1,this.timer=0),this.speedDrop&&this.yPos==this.groundYPos&&(this.speedDrop=!1,this.setDuck(!0))},draw:function(t,i){var s=t,e=i,n=this.ducking&&this.status!=I.status.CRASHED?this.config.WIDTH_DUCK:this.config.WIDTH,h=this.config.HEIGHT;p&&(s*=2,e*=2,n*=2,h*=2),s+=this.spritePos.x,e+=this.spritePos.y,this.ducking&&this.status!=I.status.CRASHED?this.canvasCtx.drawImage(T.imageSprite,s,e,n,h,this.xPos,this.yPos,this.config.WIDTH_DUCK,this.config.HEIGHT):(this.ducking&&this.status==I.status.CRASHED&&this.xPos++,this.canvasCtx.drawImage(T.imageSprite,s,e,n,h,this.xPos,this.yPos,this.config.WIDTH,this.config.HEIGHT))},setBlinkDelay:function(){this.blinkDelay=Math.ceil(Math.random()*I.BLINK_TIMING)},blink:function(t){t-this.animStartTime>=this.blinkDelay&&(this.draw(this.currentAnimFrames[this.currentFrame],0),1==this.currentFrame&&(this.setBlinkDelay(),this.animStartTime=t,this.blinkCount++))},startJump:function(t){this.jumping||(this.update(0,I.status.JUMPING),this.jumpVelocity=this.config.INIITAL_JUMP_VELOCITY-t/10,this.jumping=!0,this.reachedMinHeight=!1,this.speedDrop=!1)},endJump:function(){this.reachedMinHeight&&this.jumpVelocity<this.config.DROP_VELOCITY&&(this.jumpVelocity=this.config.DROP_VELOCITY)},updateJump:function(t,i){var s=t/I.animFrames[this.status].msPerFrame;this.speedDrop?this.yPos+=Math.round(this.jumpVelocity*this.config.SPEED_DROP_COEFFICIENT*s):this.yPos+=Math.round(this.jumpVelocity*s),this.jumpVelocity+=this.config.GRAVITY*s,(this.yPos<this.minJumpHeight||this.speedDrop)&&(this.reachedMinHeight=!0),(this.yPos<this.config.MAX_JUMP_HEIGHT||this.speedDrop)&&this.endJump(),this.yPos>this.groundYPos&&(this.reset(),this.jumpCount++),this.update(t)},setSpeedDrop:function(){this.speedDrop=!0,this.jumpVelocity=1},setDuck:function(t){t&&this.status!=I.status.DUCKING?(this.update(0,I.status.DUCKING),this.ducking=!0):this.status==I.status.DUCKING&&(this.update(0,I.status.RUNNING),this.ducking=!1)},reset:function(){this.yPos=this.groundYPos,this.jumpVelocity=0,this.jumping=!1,this.ducking=!1,this.update(0,I.status.RUNNING),this.midair=!1,this.speedDrop=!1,this.jumpCount=0}},C.dimensions={WIDTH:10,HEIGHT:13,DEST_WIDTH:11},C.yPos=[0,13,27,40,53,67,80,93,107,120],C.config={MAX_DISTANCE_UNITS:5,ACHIEVEMENT_DISTANCE:100,COEFFICIENT:.025,FLASH_DURATION:250,FLASH_ITERATIONS:3},C.prototype={init:function(t){var i="";this.calcXPos(t),this.maxScore=this.maxScoreUnits;for(var s=0;s<this.maxScoreUnits;s++)this.draw(s,0),this.defaultString+="0",i+="9";this.maxScore=parseInt(i)},calcXPos:function(t){this.x=t-C.dimensions.DEST_WIDTH*(this.maxScoreUnits+1)},draw:function(t,i,s){var e=C.dimensions.WIDTH,n=C.dimensions.HEIGHT,h=C.dimensions.WIDTH*i,o=0,a=t*C.dimensions.DEST_WIDTH,r=this.y,c=C.dimensions.WIDTH,d=C.dimensions.HEIGHT;if(p&&(e*=2,n*=2,h*=2),h+=this.spritePos.x,o+=this.spritePos.y,this.canvasCtx.save(),s){var u=this.x-2*this.maxScoreUnits*C.dimensions.WIDTH;this.canvasCtx.translate(u,this.y)}else this.canvasCtx.translate(this.x,this.y);this.canvasCtx.drawImage(this.image,h,o,e,n,a,r,c,d),this.canvasCtx.restore()},getActualDistance:function(t){return t?Math.round(t*this.config.COEFFICIENT):0},update:function(t,i){var s=!0,e=!1;if(this.acheivement)this.flashIterations<=this.config.FLASH_ITERATIONS?(this.flashTimer+=t,this.flashTimer<this.config.FLASH_DURATION?s=!1:this.flashTimer>2*this.config.FLASH_DURATION&&(this.flashTimer=0,this.flashIterations++)):(this.acheivement=!1,this.flashIterations=0,this.flashTimer=0);else if((i=this.getActualDistance(i))>this.maxScore&&this.maxScoreUnits==this.config.MAX_DISTANCE_UNITS?(this.maxScoreUnits++,this.maxScore=parseInt(this.maxScore+"9")):this.distance=0,0<i){i%this.config.ACHIEVEMENT_DISTANCE==0&&(this.acheivement=!0,e=!(this.flashTimer=0));var n=(this.defaultString+i).substr(-this.maxScoreUnits);this.digits=n.split("")}else this.digits=this.defaultString.split("");if(s)for(var h=this.digits.length-1;0<=h;h--)this.draw(h,parseInt(this.digits[h]));return this.drawHighScore(),e},drawHighScore:function(){this.canvasCtx.save(),this.canvasCtx.globalAlpha=.8;for(var t=this.highScore.length-1;0<=t;t--)this.draw(t,parseInt(this.highScore[t],10),!0);this.canvasCtx.restore()},setHighScore:function(t){t=this.getActualDistance(t);var i=(this.defaultString+t).substr(-this.maxScoreUnits);this.highScore=["10","11",""].concat(i.split(""))},reset:function(){this.update(0),this.acheivement=!1}},d.config={HEIGHT:14,MAX_CLOUD_GAP:400,MAX_SKY_LEVEL:30,MIN_CLOUD_GAP:100,MIN_SKY_LEVEL:71,WIDTH:46},d.prototype={init:function(){this.yPos=a(d.config.MAX_SKY_LEVEL,d.config.MIN_SKY_LEVEL),this.draw()},draw:function(){this.canvasCtx.save();var t=d.config.WIDTH,i=d.config.HEIGHT;p&&(t*=2,i*=2),this.canvasCtx.drawImage(T.imageSprite,this.spritePos.x,this.spritePos.y,t,i,this.xPos,this.yPos,d.config.WIDTH,d.config.HEIGHT),this.canvasCtx.restore()},update:function(t){this.remove||(this.xPos-=Math.ceil(t),this.draw(),this.isVisible()||(this.remove=!0))},isVisible:function(){return 0<this.xPos+d.config.WIDTH}},u.config={FADE_SPEED:.035,HEIGHT:40,MOON_SPEED:.25,NUM_STARS:2,STAR_SIZE:9,STAR_SPEED:.3,STAR_MAX_Y:70,WIDTH:20},u.phases=[140,120,100,60,40,20,0],u.prototype={update:function(t,i){if(t&&0==this.opacity&&(this.currentPhase++,this.currentPhase>=u.phases.length&&(this.currentPhase=0)),t&&(this.opacity<1||0==this.opacity)?this.opacity+=u.config.FADE_SPEED:0<this.opacity&&(this.opacity-=u.config.FADE_SPEED),0<this.opacity){if(this.xPos=this.updateXPos(this.xPos,u.config.MOON_SPEED),this.drawStars)for(var s=0;s<u.config.NUM_STARS;s++)this.stars[s].x=this.updateXPos(this.stars[s].x,u.config.STAR_SPEED);this.draw()}else this.opacity=0,this.placeStars();this.drawStars=!0},updateXPos:function(t,i){return t<-u.config.WIDTH?t=this.containerWidth:t-=i,t},draw:function(){var t=3==this.currentPhase?2*u.config.WIDTH:u.config.WIDTH,i=u.config.HEIGHT,s=this.spritePos.x+u.phases[this.currentPhase],e=t,n=u.config.STAR_SIZE,h=T.spriteDefinition.LDPI.STAR.x;if(p&&(t*=2,i*=2,s=this.spritePos.x+2*u.phases[this.currentPhase],n*=2,h=T.spriteDefinition.HDPI.STAR.x),this.canvasCtx.save(),this.canvasCtx.globalAlpha=this.opacity,this.drawStars)for(var o=0;o<u.config.NUM_STARS;o++)this.canvasCtx.drawImage(T.imageSprite,h,this.stars[o].sourceY,n,n,Math.round(this.stars[o].x),this.stars[o].y,u.config.STAR_SIZE,u.config.STAR_SIZE);this.canvasCtx.drawImage(T.imageSprite,s,this.spritePos.y,t,i,Math.round(this.xPos),this.yPos,e,u.config.HEIGHT),this.canvasCtx.globalAlpha=1,this.canvasCtx.restore()},placeStars:function(){for(var t=Math.round(this.containerWidth/u.config.NUM_STARS),i=0;i<u.config.NUM_STARS;i++)this.stars[i]={},this.stars[i].x=a(t*i,t*(i+1)),this.stars[i].y=a(0,u.config.STAR_MAX_Y),this.stars[i].sourceY=p?T.spriteDefinition.HDPI.STAR.y+2*u.config.STAR_SIZE*i:T.spriteDefinition.LDPI.STAR.y+u.config.STAR_SIZE*i},reset:function(){this.currentPhase=0,this.opacity=0,this.update(!1)}},s.dimensions={WIDTH:600,HEIGHT:12,YPOS:127},s.prototype={setSourceDimensions:function(){for(var t in s.dimensions)p?"YPOS"!=t&&(this.sourceDimensions[t]=2*s.dimensions[t]):this.sourceDimensions[t]=s.dimensions[t],this.dimensions[t]=s.dimensions[t];this.xPos=[0,s.dimensions.WIDTH],this.yPos=s.dimensions.YPOS},getRandomType:function(){return Math.random()>this.bumpThreshold?this.dimensions.WIDTH:0},draw:function(){this.canvasCtx.drawImage(T.imageSprite,this.sourceXPos[0],this.spritePos.y,this.sourceDimensions.WIDTH,this.sourceDimensions.HEIGHT,this.xPos[0],this.yPos,this.dimensions.WIDTH,this.dimensions.HEIGHT),this.canvasCtx.drawImage(T.imageSprite,this.sourceXPos[1],this.spritePos.y,this.sourceDimensions.WIDTH,this.sourceDimensions.HEIGHT,this.xPos[1],this.yPos,this.dimensions.WIDTH,this.dimensions.HEIGHT)},updateXPos:function(t,i){var s=t,e=0==t?1:0;this.xPos[s]-=i,this.xPos[e]=this.xPos[s]+this.dimensions.WIDTH,this.xPos[s]<=-this.dimensions.WIDTH&&(this.xPos[s]+=2*this.dimensions.WIDTH,this.xPos[e]=this.xPos[s]-this.dimensions.WIDTH,this.sourceXPos[s]=this.getRandomType()+this.spritePos.x)},update:function(t,i){var s=Math.floor(i*(e/1e3)*t);this.xPos[0]<=0?this.updateXPos(0,s):this.updateXPos(1,s),this.draw()},reset:function(){this.xPos[0]=0,this.xPos[1]=s.dimensions.WIDTH}},S.config={BG_CLOUD_SPEED:.2,BUMPY_THRESHOLD:.3,CLOUD_FREQUENCY:.5,HORIZON_HEIGHT:16,MAX_CLOUDS:6},S.prototype={init:function(){this.addCloud(),this.horizonLine=new s(this.canvas,this.spritePos.HORIZON),this.nightMode=new u(this.canvas,this.spritePos.MOON,this.dimensions.WIDTH)},update:function(t,i,s,e){this.runningTime+=t,this.horizonLine.update(t,i),this.nightMode.update(e),this.updateClouds(t,i),s&&this.updateObstacles(t,i)},updateClouds:function(t,i){var s=this.cloudSpeed/1e3*t*i,e=this.clouds.length;if(e){for(var n=e-1;0<=n;n--)this.clouds[n].update(s);var h=this.clouds[e-1];e<this.config.MAX_CLOUDS&&this.dimensions.WIDTH-h.xPos>h.cloudGap&&this.cloudFrequency>Math.random()&&this.addCloud(),this.clouds=this.clouds.filter(function(t){return!t.remove})}else this.addCloud()},updateObstacles:function(t,i){for(var s=this.obstacles.slice(0),e=0;e<this.obstacles.length;e++){var n=this.obstacles[e];n.update(t,i),n.remove&&s.shift()}if(this.obstacles=s,0<this.obstacles.length){var h=this.obstacles[this.obstacles.length-1];h&&!h.followingObstacleCreated&&h.isVisible()&&h.xPos+h.width+h.gap<this.dimensions.WIDTH&&(this.addNewObstacle(i),h.followingObstacleCreated=!0)}else this.addNewObstacle(i)},removeFirstObstacle:function(){this.obstacles.shift()},addNewObstacle:function(t){var i=a(0,c.types.length-1),s=c.types[i];if(this.duplicateObstacleCheck(s.type)||t<s.minSpeed)this.addNewObstacle(t);else{var e=this.spritePos[s.type];this.obstacles.push(new c(this.canvasCtx,s,e,this.dimensions,this.gapCoefficient,t,s.width)),this.obstacleHistory.unshift(s.type),1<this.obstacleHistory.length&&this.obstacleHistory.splice(T.config.MAX_OBSTACLE_DUPLICATION)}},duplicateObstacleCheck:function(t){for(var i=0,s=0;s<this.obstacleHistory.length;s++)i=this.obstacleHistory[s]==t?i+1:0;return i>=T.config.MAX_OBSTACLE_DUPLICATION},reset:function(){this.obstacles=[],this.horizonLine.reset(),this.nightMode.reset()},resize:function(t,i){this.canvas.width=t,this.canvas.height=i},addCloud:function(){this.clouds.push(new d(this.canvas,this.spritePos.CLOUD,this.dimensions.WIDTH))}}}();function startupDinosaurGame(){var e=document.getElementById("dino-outer").getBoundingClientRect();-1<e.top&&e.bottom<=jQuery(window).height()&&(new Runner(".dino-inner"),document.removeEventListener("scroll",startupDinosaurGame),document.removeEventListener("resize",startupDinosaurGame))}document.addEventListener("DOMContentLoaded",startupDinosaurGame),document.addEventListener("scroll",startupDinosaurGame),document.addEventListener("resize",startupDinosaurGame);