runner.js 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024
  1. // Copyright (c) 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. (function() {
  5. 'use strict';
  6. /**
  7. * T-Rex runner.
  8. * @param {string} outerContainerId Outer containing element id.
  9. * @param {object} opt_config
  10. * @constructor
  11. * @export
  12. */
  13. function Runner(outerContainerId, opt_config) {
  14. // Singleton
  15. if (Runner.instance_) {
  16. return Runner.instance_;
  17. }
  18. Runner.instance_ = this;
  19. this.outerContainerEl = document.querySelector(outerContainerId);
  20. this.containerEl = null;
  21. this.config = opt_config || Runner.config;
  22. this.dimensions = Runner.defaultDimensions;
  23. this.canvas = null;
  24. this.canvasCtx = null;
  25. this.tRex = null;
  26. this.distanceMeter = null;
  27. this.distanceRan = 0;
  28. this.highestScore = 0;
  29. this.time = 0;
  30. this.runningTime = 0;
  31. this.msPerFrame = 1000 / FPS;
  32. this.currentSpeed = this.config.SPEED;
  33. this.obstacles = [];
  34. this.started = false;
  35. this.activated = false;
  36. this.crashed = false;
  37. this.paused = false;
  38. this.resizeTimerId_ = null;
  39. this.playCount = 0;
  40. // Sound FX.
  41. this.audioBuffer = null;
  42. this.soundFx = {};
  43. // Global web audio context for playing sounds.
  44. this.audioContext = null;
  45. // Images.
  46. this.images = {};
  47. this.imagesLoaded = 0;
  48. this.loadImages();
  49. }
  50. window['Runner'] = Runner;
  51. /**
  52. * Default game width.
  53. * @const
  54. */
  55. var DEFAULT_WIDTH = 600;
  56. /**
  57. * Frames per second.
  58. * @const
  59. */
  60. var FPS = 60;
  61. /** @const */
  62. var IS_HIDPI = window.devicePixelRatio > 1;
  63. /** @const */
  64. var IS_MOBILE = window.navigator.userAgent.indexOf('Mobi') > -1;
  65. /** @const */
  66. var IS_TOUCH_ENABLED = 'ontouchstart' in window;
  67. /**
  68. * Default game configuration.
  69. * @enum {number}
  70. */
  71. Runner.config = {
  72. ACCELERATION: 0.001,
  73. BG_CLOUD_SPEED: 0.2,
  74. BOTTOM_PAD: 10,
  75. CLEAR_TIME: 3000,
  76. CLOUD_FREQUENCY: 0.5,
  77. GAMEOVER_CLEAR_TIME: 750,
  78. GAP_COEFFICIENT: 0.6,
  79. GRAVITY: 0.6,
  80. INITIAL_JUMP_VELOCITY: 12,
  81. MAX_CLOUDS: 6,
  82. MAX_OBSTACLE_LENGTH: 3,
  83. MAX_SPEED: 12,
  84. MIN_JUMP_HEIGHT: 35,
  85. MOBILE_SPEED_COEFFICIENT: 1.2,
  86. RESOURCE_TEMPLATE_ID: 'audio-resources',
  87. SPEED: 6,
  88. SPEED_DROP_COEFFICIENT: 3
  89. };
  90. /**
  91. * Default dimensions.
  92. * @enum {string}
  93. */
  94. Runner.defaultDimensions = {
  95. WIDTH: DEFAULT_WIDTH,
  96. HEIGHT: 150
  97. };
  98. /**
  99. * CSS class names.
  100. * @enum {string}
  101. */
  102. Runner.classes = {
  103. CANVAS: 'runner-canvas',
  104. CONTAINER: 'runner-container',
  105. CRASHED: 'crashed',
  106. ICON: 'icon-offline',
  107. TOUCH_CONTROLLER: 'controller'
  108. };
  109. /**
  110. * Image source urls.
  111. * @enum {array.<object>}
  112. */
  113. Runner.imageSources = {
  114. LDPI: [{
  115. name: 'CACTUS_LARGE',
  116. id: '1x-obstacle-large'
  117. }, {
  118. name: 'CACTUS_SMALL',
  119. id: '1x-obstacle-small'
  120. }, {
  121. name: 'CLOUD',
  122. id: '1x-cloud'
  123. }, {
  124. name: 'HORIZON',
  125. id: '1x-horizon'
  126. }, {
  127. name: 'RESTART',
  128. id: '1x-restart'
  129. }, {
  130. name: 'TEXT_SPRITE',
  131. id: '1x-text'
  132. }, {
  133. name: 'TREX',
  134. id: '1x-trex'
  135. }],
  136. HDPI: [{
  137. name: 'CACTUS_LARGE',
  138. id: '2x-obstacle-large'
  139. }, {
  140. name: 'CACTUS_SMALL',
  141. id: '2x-obstacle-small'
  142. }, {
  143. name: 'CLOUD',
  144. id: '2x-cloud'
  145. }, {
  146. name: 'HORIZON',
  147. id: '2x-horizon'
  148. }, {
  149. name: 'RESTART',
  150. id: '2x-restart'
  151. }, {
  152. name: 'TEXT_SPRITE',
  153. id: '2x-text'
  154. }, {
  155. name: 'TREX',
  156. id: '2x-trex'
  157. }]
  158. };
  159. /**
  160. * Sound FX. Reference to the ID of the audio tag on interstitial page.
  161. * @enum {string}
  162. */
  163. Runner.sounds = {
  164. BUTTON_PRESS: 'offline-sound-press',
  165. HIT: 'offline-sound-hit',
  166. SCORE: 'offline-sound-reached'
  167. };
  168. /**
  169. * Key code mapping.
  170. * @enum {object}
  171. */
  172. Runner.keycodes = {
  173. JUMP: {
  174. '38': 1,
  175. '32': 1
  176. }, // Up, spacebar
  177. DUCK: {
  178. '40': 1
  179. }, // Down
  180. RESTART: {
  181. '13': 1
  182. } // Enter
  183. };
  184. /**
  185. * Runner event names.
  186. * @enum {string}
  187. */
  188. Runner.events = {
  189. ANIM_END: 'webkitAnimationEnd',
  190. CLICK: 'click',
  191. KEYDOWN: 'keydown',
  192. KEYUP: 'keyup',
  193. MOUSEDOWN: 'mousedown',
  194. MOUSEUP: 'mouseup',
  195. RESIZE: 'resize',
  196. TOUCHEND: 'touchend',
  197. TOUCHSTART: 'touchstart',
  198. VISIBILITY: 'visibilitychange',
  199. BLUR: 'blur',
  200. FOCUS: 'focus',
  201. LOAD: 'load'
  202. };
  203. Runner.prototype = {
  204. /**
  205. * Setting individual settings for debugging.
  206. * @param {string} setting
  207. * @param {*} value
  208. */
  209. updateConfigSetting: function(setting, value) {
  210. if (setting in this.config && value != undefined) {
  211. this.config[setting] = value;
  212. switch (setting) {
  213. case 'GRAVITY':
  214. case 'MIN_JUMP_HEIGHT':
  215. case 'SPEED_DROP_COEFFICIENT':
  216. this.tRex.config[setting] = value;
  217. break;
  218. case 'INITIAL_JUMP_VELOCITY':
  219. this.tRex.setJumpVelocity(value);
  220. break;
  221. case 'SPEED':
  222. this.setSpeed(value);
  223. break;
  224. }
  225. }
  226. },
  227. /**
  228. * Load and cache the image assets from the page.
  229. */
  230. loadImages: function() {
  231. var imageSources = IS_HIDPI ? Runner.imageSources.HDPI :
  232. Runner.imageSources.LDPI;
  233. var numImages = imageSources.length;
  234. for (var i = numImages - 1; i >= 0; i--) {
  235. var imgSource = imageSources[i];
  236. this.images[imgSource.name] = document.getElementById(imgSource.id);
  237. }
  238. this.init();
  239. },
  240. /**
  241. * Load and decode base 64 encoded sounds.
  242. */
  243. loadSounds: function() {
  244. this.audioContext = new AudioContext();
  245. var resourceTemplate =
  246. document.getElementById(this.config.RESOURCE_TEMPLATE_ID).content;
  247. for (var sound in Runner.sounds) {
  248. // var soundSrc = resourceTemplate.getElementById(Runner.sounds[sound]).src;
  249. // soundSrc = soundSrc.substr(soundSrc.indexOf(',') + 1);
  250. // var buffer = decodeBase64ToArrayBuffer(soundSrc);
  251. // // Async, so no guarantee of order in array.
  252. // this.audioContext.decodeAudioData(buffer, function(index, audioData) {
  253. // this.soundFx[index] = audioData;
  254. // }.bind(this, sound));
  255. var soundSrc = resourceTemplate.getElementById(Runner.sounds[sound]).getAttribute("src");
  256. var request = new XMLHttpRequest();
  257. request.open('GET', soundSrc, true);
  258. request.responseType = 'arraybuffer';
  259. // Decode asynchronously
  260. request.onload = function() {
  261. this.audioContext.decodeAudioData(request.response, function(index, audioData) {
  262. this.soundFx[index] = audioData;
  263. }.bind(this, sound), function(err) {
  264. console.log('err loading sound:' + sound);
  265. });
  266. };
  267. request.send();
  268. }
  269. },
  270. /**
  271. * Sets the game speed. Adjust the speed accordingly if on a smaller screen.
  272. * @param {number} opt_speed
  273. */
  274. setSpeed: function(opt_speed) {
  275. var speed = opt_speed || this.currentSpeed;
  276. // Reduce the speed on smaller mobile screens.
  277. if (this.dimensions.WIDTH < DEFAULT_WIDTH) {
  278. var mobileSpeed = speed * this.dimensions.WIDTH / DEFAULT_WIDTH *
  279. this.config.MOBILE_SPEED_COEFFICIENT;
  280. this.currentSpeed = mobileSpeed > speed ? speed : mobileSpeed;
  281. } else if (opt_speed) {
  282. this.currentSpeed = opt_speed;
  283. }
  284. },
  285. /**
  286. * Game initialiser.
  287. */
  288. init: function() {
  289. // Hide the static icon.
  290. document.querySelector('.' + Runner.classes.ICON).style.visibility =
  291. 'hidden';
  292. this.adjustDimensions();
  293. this.setSpeed();
  294. this.containerEl = document.createElement('div');
  295. this.containerEl.className = Runner.classes.CONTAINER;
  296. // Player canvas container.
  297. this.canvas = createCanvas(this.containerEl, this.dimensions.WIDTH,
  298. this.dimensions.HEIGHT, Runner.classes.PLAYER);
  299. this.canvasCtx = this.canvas.getContext('2d');
  300. this.canvasCtx.fillStyle = '#f7f7f7';
  301. this.canvasCtx.fill();
  302. Runner.updateCanvasScaling(this.canvas);
  303. // Horizon contains clouds, obstacles and the ground.
  304. this.horizon = new Horizon(this.canvas, this.images, this.dimensions,
  305. this.config.GAP_COEFFICIENT);
  306. // Distance meter
  307. this.distanceMeter = new DistanceMeter(this.canvas,
  308. this.images.TEXT_SPRITE, this.dimensions.WIDTH);
  309. // Draw t-rex
  310. this.tRex = new Trex(this.canvas, this.images.TREX);
  311. this.outerContainerEl.appendChild(this.containerEl);
  312. if (IS_MOBILE) {
  313. this.createTouchController();
  314. }
  315. this.startListening();
  316. this.update();
  317. window.addEventListener(Runner.events.RESIZE,
  318. this.debounceResize.bind(this));
  319. },
  320. /**
  321. * Create the touch controller. A div that covers whole screen.
  322. */
  323. createTouchController: function() {
  324. this.touchController = document.createElement('div');
  325. this.touchController.className = Runner.classes.TOUCH_CONTROLLER;
  326. },
  327. /**
  328. * Debounce the resize event.
  329. */
  330. debounceResize: function() {
  331. if (!this.resizeTimerId_) {
  332. this.resizeTimerId_ =
  333. setInterval(this.adjustDimensions.bind(this), 250);
  334. }
  335. },
  336. /**
  337. * Adjust game space dimensions on resize.
  338. */
  339. adjustDimensions: function() {
  340. clearInterval(this.resizeTimerId_);
  341. this.resizeTimerId_ = null;
  342. var boxStyles = window.getComputedStyle(this.outerContainerEl);
  343. var padding = Number(boxStyles.paddingLeft.substr(0,
  344. boxStyles.paddingLeft.length - 2));
  345. this.dimensions.WIDTH = this.outerContainerEl.offsetWidth - padding * 2;
  346. // Redraw the elements back onto the canvas.
  347. if (this.canvas) {
  348. this.canvas.width = this.dimensions.WIDTH;
  349. this.canvas.height = this.dimensions.HEIGHT;
  350. Runner.updateCanvasScaling(this.canvas);
  351. this.distanceMeter.calcXPos(this.dimensions.WIDTH);
  352. this.clearCanvas();
  353. this.horizon.update(0, 0, true);
  354. this.tRex.update(0);
  355. // Outer container and distance meter.
  356. if (this.activated || this.crashed) {
  357. this.containerEl.style.width = this.dimensions.WIDTH + 'px';
  358. this.containerEl.style.height = this.dimensions.HEIGHT + 'px';
  359. this.distanceMeter.update(0, Math.ceil(this.distanceRan));
  360. this.stop();
  361. } else {
  362. this.tRex.draw(0, 0);
  363. }
  364. // Game over panel.
  365. if (this.crashed && this.gameOverPanel) {
  366. this.gameOverPanel.updateDimensions(this.dimensions.WIDTH);
  367. this.gameOverPanel.draw();
  368. }
  369. }
  370. },
  371. /**
  372. * Play the game intro.
  373. * Canvas container width expands out to the full width.
  374. */
  375. playIntro: function() {
  376. if (!this.started && !this.crashed) {
  377. this.playingIntro = true;
  378. this.tRex.playingIntro = true;
  379. // CSS animation definition.
  380. var keyframes = '@-webkit-keyframes intro { ' +
  381. 'from { width:' + Trex.config.WIDTH + 'px }' +
  382. 'to { width: ' + this.dimensions.WIDTH + 'px }' +
  383. '}';
  384. document.styleSheets[0].insertRule(keyframes, 0);
  385. this.containerEl.addEventListener(Runner.events.ANIM_END,
  386. this.startGame.bind(this));
  387. this.containerEl.style.webkitAnimation = 'intro .4s ease-out 1 both';
  388. this.containerEl.style.width = this.dimensions.WIDTH + 'px';
  389. if (this.touchController) {
  390. this.outerContainerEl.appendChild(this.touchController);
  391. }
  392. this.activated = true;
  393. this.started = true;
  394. } else if (this.crashed) {
  395. this.restart();
  396. }
  397. },
  398. /**
  399. * Update the game status to started.
  400. */
  401. startGame: function() {
  402. this.runningTime = 0;
  403. this.playingIntro = false;
  404. this.tRex.playingIntro = false;
  405. this.containerEl.style.webkitAnimation = '';
  406. this.playCount++;
  407. // Handle tabbing off the page. Pause the current game.
  408. window.addEventListener(Runner.events.VISIBILITY,
  409. this.onVisibilityChange.bind(this));
  410. window.addEventListener(Runner.events.BLUR,
  411. this.onVisibilityChange.bind(this));
  412. window.addEventListener(Runner.events.FOCUS,
  413. this.onVisibilityChange.bind(this));
  414. },
  415. clearCanvas: function() {
  416. this.canvasCtx.clearRect(0, 0, this.dimensions.WIDTH,
  417. this.dimensions.HEIGHT);
  418. },
  419. /**
  420. * Update the game frame.
  421. */
  422. update: function() {
  423. this.drawPending = false;
  424. var now = performance.now();
  425. var deltaTime = now - (this.time || now);
  426. this.time = now;
  427. if (this.activated) {
  428. this.clearCanvas();
  429. if (this.tRex.jumping) {
  430. this.tRex.updateJump(deltaTime, this.config);
  431. }
  432. this.runningTime += deltaTime;
  433. var hasObstacles = this.runningTime > this.config.CLEAR_TIME;
  434. // First jump triggers the intro.
  435. if (this.tRex.jumpCount == 1 && !this.playingIntro) {
  436. this.playIntro();
  437. }
  438. // The horizon doesn't move until the intro is over.
  439. if (this.playingIntro) {
  440. this.horizon.update(0, this.currentSpeed, hasObstacles);
  441. } else {
  442. deltaTime = !this.started ? 0 : deltaTime;
  443. this.horizon.update(deltaTime, this.currentSpeed, hasObstacles);
  444. }
  445. // Check for collisions.
  446. var collision = hasObstacles &&
  447. checkForCollision(this.horizon.obstacles[0], this.tRex);
  448. if (!collision) {
  449. this.distanceRan += this.currentSpeed * deltaTime / this.msPerFrame;
  450. if (this.currentSpeed < this.config.MAX_SPEED) {
  451. this.currentSpeed += this.config.ACCELERATION;
  452. }
  453. } else {
  454. this.gameOver();
  455. }
  456. if (this.distanceMeter.getActualDistance(this.distanceRan) >
  457. this.distanceMeter.maxScore) {
  458. this.distanceRan = 0;
  459. }
  460. var playAcheivementSound = this.distanceMeter.update(deltaTime,
  461. Math.ceil(this.distanceRan));
  462. if (playAcheivementSound) {
  463. this.playSound(this.soundFx.SCORE);
  464. }
  465. }
  466. if (!this.crashed) {
  467. this.tRex.update(deltaTime);
  468. this.raq();
  469. }
  470. },
  471. /**
  472. * Event handler.
  473. */
  474. handleEvent: function(e) {
  475. return (function(evtType, events) {
  476. switch (evtType) {
  477. case events.KEYDOWN:
  478. case events.TOUCHSTART:
  479. case events.MOUSEDOWN:
  480. this.onKeyDown(e);
  481. break;
  482. case events.KEYUP:
  483. case events.TOUCHEND:
  484. case events.MOUSEUP:
  485. this.onKeyUp(e);
  486. break;
  487. }
  488. }.bind(this))(e.type, Runner.events);
  489. },
  490. /**
  491. * Bind relevant key / mouse / touch listeners.
  492. */
  493. startListening: function() {
  494. // Keys.
  495. document.addEventListener(Runner.events.KEYDOWN, this);
  496. document.addEventListener(Runner.events.KEYUP, this);
  497. if (IS_MOBILE) {
  498. // Mobile only touch devices.
  499. this.touchController.addEventListener(Runner.events.TOUCHSTART, this);
  500. this.touchController.addEventListener(Runner.events.TOUCHEND, this);
  501. this.containerEl.addEventListener(Runner.events.TOUCHSTART, this);
  502. } else {
  503. // Mouse.
  504. document.addEventListener(Runner.events.MOUSEDOWN, this);
  505. document.addEventListener(Runner.events.MOUSEUP, this);
  506. }
  507. },
  508. /**
  509. * Remove all listeners.
  510. */
  511. stopListening: function() {
  512. document.removeEventListener(Runner.events.KEYDOWN, this);
  513. document.removeEventListener(Runner.events.KEYUP, this);
  514. if (IS_MOBILE) {
  515. this.touchController.removeEventListener(Runner.events.TOUCHSTART, this);
  516. this.touchController.removeEventListener(Runner.events.TOUCHEND, this);
  517. this.containerEl.removeEventListener(Runner.events.TOUCHSTART, this);
  518. } else {
  519. document.removeEventListener(Runner.events.MOUSEDOWN, this);
  520. document.removeEventListener(Runner.events.MOUSEUP, this);
  521. }
  522. },
  523. /**
  524. * Process keydown.
  525. * @param {Event} e
  526. */
  527. onKeyDown: function(e) {
  528. if (!this.crashed && (Runner.keycodes.JUMP[String(e.keyCode)] ||
  529. e.type == Runner.events.TOUCHSTART)) {
  530. if (!this.activated) {
  531. this.loadSounds();
  532. this.activated = true;
  533. }
  534. if (!this.tRex.jumping) {
  535. this.playSound(this.soundFx.BUTTON_PRESS);
  536. this.tRex.startJump();
  537. }
  538. }
  539. if (this.crashed && e.type == Runner.events.TOUCHSTART &&
  540. e.currentTarget == this.containerEl) {
  541. this.restart();
  542. }
  543. // Speed drop, activated only when jump key is not pressed.
  544. if (Runner.keycodes.DUCK[e.keyCode] && this.tRex.jumping) {
  545. e.preventDefault();
  546. this.tRex.setSpeedDrop();
  547. }
  548. },
  549. /**
  550. * Process key up.
  551. * @param {Event} e
  552. */
  553. onKeyUp: function(e) {
  554. var keyCode = String(e.keyCode);
  555. var isjumpKey = Runner.keycodes.JUMP[keyCode] ||
  556. e.type == Runner.events.TOUCHEND ||
  557. e.type == Runner.events.MOUSEDOWN;
  558. if (this.isRunning() && isjumpKey) {
  559. this.tRex.endJump();
  560. } else if (Runner.keycodes.DUCK[keyCode]) {
  561. this.tRex.speedDrop = false;
  562. } else if (this.crashed) {
  563. // Check that enough time has elapsed before allowing jump key to restart.
  564. var deltaTime = performance.now() - this.time;
  565. if (Runner.keycodes.RESTART[keyCode] ||
  566. (e.type == Runner.events.MOUSEUP && e.target == this.canvas) ||
  567. (deltaTime >= this.config.GAMEOVER_CLEAR_TIME &&
  568. Runner.keycodes.JUMP[keyCode])) {
  569. this.restart();
  570. }
  571. } else if (this.paused && isjumpKey) {
  572. this.play();
  573. }
  574. },
  575. /**
  576. * RequestAnimationFrame wrapper.
  577. */
  578. raq: function() {
  579. if (!this.drawPending) {
  580. this.drawPending = true;
  581. this.raqId = requestAnimationFrame(this.update.bind(this));
  582. }
  583. },
  584. /**
  585. * Whether the game is running.
  586. * @return {boolean}
  587. */
  588. isRunning: function() {
  589. return !!this.raqId;
  590. },
  591. /**
  592. * Game over state.
  593. */
  594. gameOver: function() {
  595. this.playSound(this.soundFx.HIT);
  596. vibrate(200);
  597. this.stop();
  598. this.crashed = true;
  599. this.distanceMeter.acheivement = false;
  600. this.tRex.update(100, Trex.status.CRASHED);
  601. // Game over panel.
  602. if (!this.gameOverPanel) {
  603. this.gameOverPanel = new GameOverPanel(this.canvas,
  604. this.images.TEXT_SPRITE, this.images.RESTART,
  605. this.dimensions);
  606. } else {
  607. this.gameOverPanel.draw();
  608. }
  609. // Update the high score.
  610. if (this.distanceRan > this.highestScore) {
  611. this.highestScore = Math.ceil(this.distanceRan);
  612. this.distanceMeter.setHighScore(this.highestScore);
  613. }
  614. // Reset the time clock.
  615. this.time = performance.now();
  616. },
  617. stop: function() {
  618. this.activated = false;
  619. this.paused = true;
  620. cancelAnimationFrame(this.raqId);
  621. this.raqId = 0;
  622. },
  623. play: function() {
  624. if (!this.crashed) {
  625. this.activated = true;
  626. this.paused = false;
  627. this.tRex.update(0, Trex.status.RUNNING);
  628. this.time = performance.now();
  629. this.update();
  630. }
  631. },
  632. restart: function() {
  633. if (!this.raqId) {
  634. this.playCount++;
  635. this.runningTime = 0;
  636. this.activated = true;
  637. this.crashed = false;
  638. this.distanceRan = 0;
  639. this.setSpeed(this.config.SPEED);
  640. this.time = performance.now();
  641. this.containerEl.classList.remove(Runner.classes.CRASHED);
  642. this.clearCanvas();
  643. this.distanceMeter.reset(this.highestScore);
  644. this.horizon.reset();
  645. this.tRex.reset();
  646. this.playSound(this.soundFx.BUTTON_PRESS);
  647. this.update();
  648. }
  649. },
  650. /**
  651. * Pause the game if the tab is not in focus.
  652. */
  653. onVisibilityChange: function(e) {
  654. if (document.hidden || document.webkitHidden || e.type == 'blur') {
  655. this.stop();
  656. } else {
  657. this.play();
  658. }
  659. },
  660. /**
  661. * Play a sound.
  662. * @param {SoundBuffer} soundBuffer
  663. */
  664. playSound: function(soundBuffer) {
  665. if (soundBuffer) {
  666. var sourceNode = this.audioContext.createBufferSource();
  667. sourceNode.buffer = soundBuffer;
  668. sourceNode.connect(this.audioContext.destination);
  669. sourceNode.start(0);
  670. }
  671. }
  672. };
  673. /**
  674. * Updates the canvas size taking into
  675. * account the backing store pixel ratio and
  676. * the device pixel ratio.
  677. *
  678. * See article by Paul Lewis:
  679. * http://www.html5rocks.com/en/tutorials/canvas/hidpi/
  680. *
  681. * @param {HTMLCanvasElement} canvas
  682. * @param {number} opt_width
  683. * @param {number} opt_height
  684. * @return {boolean} Whether the canvas was scaled.
  685. */
  686. Runner.updateCanvasScaling = function(canvas, opt_width, opt_height) {
  687. var context = canvas.getContext('2d');
  688. // Query the various pixel ratios
  689. var devicePixelRatio = Math.floor(window.devicePixelRatio) || 1;
  690. var backingStoreRatio = Math.floor(context.webkitBackingStorePixelRatio) || 1;
  691. var ratio = devicePixelRatio / backingStoreRatio;
  692. // Upscale the canvas if the two ratios don't match
  693. if (devicePixelRatio !== backingStoreRatio) {
  694. var oldWidth = opt_width || canvas.width;
  695. var oldHeight = opt_height || canvas.height;
  696. canvas.width = oldWidth * ratio;
  697. canvas.height = oldHeight * ratio;
  698. canvas.style.width = oldWidth + 'px';
  699. canvas.style.height = oldHeight + 'px';
  700. // Scale the context to counter the fact that we've manually scaled
  701. // our canvas element.
  702. context.scale(ratio, ratio);
  703. return true;
  704. }
  705. return false;
  706. };
  707. /**
  708. * Get random number.
  709. * @param {number} min
  710. * @param {number} max
  711. * @param {number}
  712. */
  713. function getRandomNum(min, max) {
  714. return Math.floor(Math.random() * (max - min + 1)) + min;
  715. }
  716. /**
  717. * Vibrate on mobile devices.
  718. * @param {number} duration Duration of the vibration in milliseconds.
  719. */
  720. function vibrate(duration) {
  721. if (IS_MOBILE) {
  722. window.navigator['vibrate'](duration);
  723. }
  724. }
  725. /**
  726. * Create canvas element.
  727. * @param {HTMLElement} container Element to append canvas to.
  728. * @param {number} width
  729. * @param {number} height
  730. * @param {string} opt_classname
  731. * @return {HTMLCanvasElement}
  732. */
  733. function createCanvas(container, width, height, opt_classname) {
  734. var canvas = document.createElement('canvas');
  735. canvas.className = opt_classname ? Runner.classes.CANVAS + ' ' +
  736. opt_classname : Runner.classes.CANVAS;
  737. canvas.width = width;
  738. canvas.height = height;
  739. container.appendChild(canvas);
  740. return canvas;
  741. }
  742. /**
  743. * Decodes the base 64 audio to ArrayBuffer used by Web Audio.
  744. * @param {string} base64String
  745. */
  746. function decodeBase64ToArrayBuffer(base64String) {
  747. var len = (base64String.length / 4) * 3;
  748. var str = atob(base64String);
  749. var arrayBuffer = new ArrayBuffer(len);
  750. var bytes = new Uint8Array(arrayBuffer);
  751. for (var i = 0; i < len; i++) {
  752. bytes[i] = str.charCodeAt(i);
  753. }
  754. return bytes.buffer;
  755. }
  756. //******************************************************************************
  757. /**
  758. * Game over panel.
  759. * @param {!HTMLCanvasElement} canvas
  760. * @param {!HTMLImage} textSprite
  761. * @param {!HTMLImage} restartImg
  762. * @param {!Object} dimensions Canvas dimensions.
  763. * @constructor
  764. */
  765. function GameOverPanel(canvas, textSprite, restartImg, dimensions) {
  766. this.canvas = canvas;
  767. this.canvasCtx = canvas.getContext('2d');
  768. this.canvasDimensions = dimensions;
  769. this.textSprite = textSprite;
  770. this.restartImg = restartImg;
  771. this.draw();
  772. };
  773. /**
  774. * Dimensions used in the panel.
  775. * @enum {number}
  776. */
  777. GameOverPanel.dimensions = {
  778. TEXT_X: 0,
  779. TEXT_Y: 13,
  780. TEXT_WIDTH: 191,
  781. TEXT_HEIGHT: 11,
  782. RESTART_WIDTH: 36,
  783. RESTART_HEIGHT: 32
  784. };
  785. GameOverPanel.prototype = {
  786. /**
  787. * Update the panel dimensions.
  788. * @param {number} width New canvas width.
  789. * @param {number} opt_height Optional new canvas height.
  790. */
  791. updateDimensions: function(width, opt_height) {
  792. this.canvasDimensions.WIDTH = width;
  793. if (opt_height) {
  794. this.canvasDimensions.HEIGHT = opt_height;
  795. }
  796. },
  797. /**
  798. * Draw the panel.
  799. */
  800. draw: function() {
  801. var dimensions = GameOverPanel.dimensions;
  802. var centerX = this.canvasDimensions.WIDTH / 2;
  803. // Game over text.
  804. var textSourceX = dimensions.TEXT_X;
  805. var textSourceY = dimensions.TEXT_Y;
  806. var textSourceWidth = dimensions.TEXT_WIDTH;
  807. var textSourceHeight = dimensions.TEXT_HEIGHT;
  808. var textTargetX = Math.round(centerX - (dimensions.TEXT_WIDTH / 2));
  809. var textTargetY = Math.round((this.canvasDimensions.HEIGHT - 25) / 3);
  810. var textTargetWidth = dimensions.TEXT_WIDTH;
  811. var textTargetHeight = dimensions.TEXT_HEIGHT;
  812. var restartSourceWidth = dimensions.RESTART_WIDTH;
  813. var restartSourceHeight = dimensions.RESTART_HEIGHT;
  814. var restartTargetX = centerX - (dimensions.RESTART_WIDTH / 2);
  815. var restartTargetY = this.canvasDimensions.HEIGHT / 2;
  816. if (IS_HIDPI) {
  817. textSourceY *= 2;
  818. textSourceX *= 2;
  819. textSourceWidth *= 2;
  820. textSourceHeight *= 2;
  821. restartSourceWidth *= 2;
  822. restartSourceHeight *= 2;
  823. }
  824. // Game over text from sprite.
  825. this.canvasCtx.drawImage(this.textSprite,
  826. textSourceX, textSourceY, textSourceWidth, textSourceHeight,
  827. textTargetX, textTargetY, textTargetWidth, textTargetHeight);
  828. // Restart button.
  829. this.canvasCtx.drawImage(this.restartImg, 0, 0,
  830. restartSourceWidth, restartSourceHeight,
  831. restartTargetX, restartTargetY, dimensions.RESTART_WIDTH,
  832. dimensions.RESTART_HEIGHT);
  833. }
  834. };
  835. //******************************************************************************
  836. /**
  837. * Check for a collision.
  838. * @param {!Obstacle} obstacle
  839. * @param {!Trex} tRex T-rex object.
  840. * @param {HTMLCanvasContext} opt_canvasCtx Optional canvas context for drawing
  841. * collision boxes.
  842. * @return {Array.<CollisionBox>}
  843. */
  844. function checkForCollision(obstacle, tRex, opt_canvasCtx) {
  845. var obstacleBoxXPos = Runner.defaultDimensions.WIDTH + obstacle.xPos;
  846. // Adjustments are made to the bounding box as there is a 1 pixel white
  847. // border around the t-rex and obstacles.
  848. var tRexBox = new CollisionBox(
  849. tRex.xPos + 1,
  850. tRex.yPos + 1,
  851. tRex.config.WIDTH - 2,
  852. tRex.config.HEIGHT - 2);
  853. var obstacleBox = new CollisionBox(
  854. obstacle.xPos + 1,
  855. obstacle.yPos + 1,
  856. obstacle.typeConfig.width * obstacle.size - 2,
  857. obstacle.typeConfig.height - 2);
  858. // Debug outer box
  859. if (opt_canvasCtx) {
  860. drawCollisionBoxes(opt_canvasCtx, tRexBox, obstacleBox);
  861. }
  862. // Simple outer bounds check.
  863. if (boxCompare(tRexBox, obstacleBox)) {
  864. var collisionBoxes = obstacle.collisionBoxes;
  865. var tRexCollisionBoxes = Trex.collisionBoxes;
  866. // Detailed axis aligned box check.
  867. for (var t = 0; t < tRexCollisionBoxes.length; t++) {
  868. for (var i = 0; i < collisionBoxes.length; i++) {
  869. // Adjust the box to actual positions.
  870. var adjTrexBox =
  871. createAdjustedCollisionBox(tRexCollisionBoxes[t], tRexBox);
  872. var adjObstacleBox =
  873. createAdjustedCollisionBox(collisionBoxes[i], obstacleBox);
  874. var crashed = boxCompare(adjTrexBox, adjObstacleBox);
  875. // Draw boxes for debug.
  876. if (opt_canvasCtx) {
  877. drawCollisionBoxes(opt_canvasCtx, adjTrexBox, adjObstacleBox);
  878. }
  879. if (crashed) {
  880. return [adjTrexBox, adjObstacleBox];
  881. }
  882. }
  883. }
  884. }
  885. return false;
  886. };
  887. /**
  888. * Adjust the collision box.
  889. * @param {!CollisionBox} box The original box.
  890. * @param {!CollisionBox} adjustment Adjustment box.
  891. * @return {CollisionBox} The adjusted collision box object.
  892. */
  893. function createAdjustedCollisionBox(box, adjustment) {
  894. return new CollisionBox(
  895. box.x + adjustment.x,
  896. box.y + adjustment.y,
  897. box.width,
  898. box.height);
  899. };
  900. /**
  901. * Draw the collision boxes for debug.
  902. */
  903. function drawCollisionBoxes(canvasCtx, tRexBox, obstacleBox) {
  904. canvasCtx.save();
  905. canvasCtx.strokeStyle = '#f00';
  906. canvasCtx.strokeRect(tRexBox.x, tRexBox.y,
  907. tRexBox.width, tRexBox.height);
  908. canvasCtx.strokeStyle = '#0f0';
  909. canvasCtx.strokeRect(obstacleBox.x, obstacleBox.y,
  910. obstacleBox.width, obstacleBox.height);
  911. canvasCtx.restore();
  912. };
  913. /**
  914. * Compare two collision boxes for a collision.
  915. * @param {CollisionBox} tRexBox
  916. * @param {CollisionBox} obstacleBox
  917. * @return {boolean} Whether the boxes intersected.
  918. */
  919. function boxCompare(tRexBox, obstacleBox) {
  920. var crashed = false;
  921. var tRexBoxX = tRexBox.x;
  922. var tRexBoxY = tRexBox.y;
  923. var obstacleBoxX = obstacleBox.x;
  924. var obstacleBoxY = obstacleBox.y;
  925. // Axis-Aligned Bounding Box method.
  926. if (tRexBox.x < obstacleBoxX + obstacleBox.width &&
  927. tRexBox.x + tRexBox.width > obstacleBoxX &&
  928. tRexBox.y < obstacleBox.y + obstacleBox.height &&
  929. tRexBox.height + tRexBox.y > obstacleBox.y) {
  930. crashed = true;
  931. }
  932. return crashed;
  933. };
  934. //******************************************************************************
  935. /**
  936. * Collision box object.
  937. * @param {number} x X position.
  938. * @param {number} y Y Position.
  939. * @param {number} w Width.
  940. * @param {number} h Height.
  941. */
  942. function CollisionBox(x, y, w, h) {
  943. this.x = x;
  944. this.y = y;
  945. this.width = w;
  946. this.height = h;
  947. };
  948. //******************************************************************************
  949. /**
  950. * Obstacle.
  951. * @param {HTMLCanvasCtx} canvasCtx
  952. * @param {Obstacle.type} type
  953. * @param {image} obstacleImg Image sprite.
  954. * @param {Object} dimensions
  955. * @param {number} gapCoefficient Mutipler in determining the gap.
  956. * @param {number} speed
  957. */
  958. function Obstacle(canvasCtx, type, obstacleImg, dimensions,
  959. gapCoefficient, speed) {
  960. this.canvasCtx = canvasCtx;
  961. this.image = obstacleImg;
  962. this.typeConfig = type;
  963. this.gapCoefficient = gapCoefficient;
  964. this.size = getRandomNum(1, Obstacle.MAX_OBSTACLE_LENGTH);
  965. this.dimensions = dimensions;
  966. this.remove = false;
  967. this.xPos = 0;
  968. this.yPos = this.typeConfig.yPos;
  969. this.width = 0;
  970. this.collisionBoxes = [];
  971. this.gap = 0;
  972. this.init(speed);
  973. };
  974. /**
  975. * Coefficient for calculating the maximum gap.
  976. * @const
  977. */
  978. Obstacle.MAX_GAP_COEFFICIENT = 1.5;
  979. /**
  980. * Maximum obstacle grouping count.
  981. * @const
  982. */
  983. Obstacle.MAX_OBSTACLE_LENGTH = 3,
  984. Obstacle.prototype = {
  985. /**
  986. * Initialise the DOM for the obstacle.
  987. * @param {number} speed
  988. */
  989. init: function(speed) {
  990. this.cloneCollisionBoxes();
  991. // Only allow sizing if we're at the right speed.
  992. if (this.size > 1 && this.typeConfig.multipleSpeed > speed) {
  993. this.size = 1;
  994. }
  995. this.width = this.typeConfig.width * this.size;
  996. this.xPos = this.dimensions.WIDTH - this.width;
  997. this.draw();
  998. // Make collision box adjustments,
  999. // Central box is adjusted to the size as one box.
  1000. // ____ ______ ________
  1001. // _| |-| _| |-| _| |-|
  1002. // | |<->| | | |<--->| | | |<----->| |
  1003. // | | 1 | | | | 2 | | | | 3 | |
  1004. // |_|___|_| |_|_____|_| |_|_______|_|
  1005. //
  1006. if (this.size > 1) {
  1007. this.collisionBoxes[1].width = this.width - this.collisionBoxes[0].width -
  1008. this.collisionBoxes[2].width;
  1009. this.collisionBoxes[2].x = this.width - this.collisionBoxes[2].width;
  1010. }
  1011. this.gap = this.getGap(this.gapCoefficient, speed);
  1012. },
  1013. /**
  1014. * Draw and crop based on size.
  1015. */
  1016. draw: function() {
  1017. var sourceWidth = this.typeConfig.width;
  1018. var sourceHeight = this.typeConfig.height;
  1019. if (IS_HIDPI) {
  1020. sourceWidth = sourceWidth * 2;
  1021. sourceHeight = sourceHeight * 2;
  1022. }
  1023. // Sprite
  1024. var sourceX = (sourceWidth * this.size) * (0.5 * (this.size - 1));
  1025. this.canvasCtx.drawImage(this.image,
  1026. sourceX, 0,
  1027. sourceWidth * this.size, sourceHeight,
  1028. this.xPos, this.yPos,
  1029. this.typeConfig.width * this.size, this.typeConfig.height);
  1030. },
  1031. /**
  1032. * Obstacle frame update.
  1033. * @param {number} deltaTime
  1034. * @param {number} speed
  1035. */
  1036. update: function(deltaTime, speed) {
  1037. if (!this.remove) {
  1038. this.xPos -= Math.floor((speed * FPS / 1000) * deltaTime);
  1039. this.draw();
  1040. if (!this.isVisible()) {
  1041. this.remove = true;
  1042. }
  1043. }
  1044. },
  1045. /**
  1046. * Calculate a random gap size.
  1047. * - Minimum gap gets wider as speed increses
  1048. * @param {number} gapCoefficient
  1049. * @param {number} speed
  1050. * @return {number} The gap size.
  1051. */
  1052. getGap: function(gapCoefficient, speed) {
  1053. var minGap = Math.round(this.width * speed +
  1054. this.typeConfig.minGap * gapCoefficient);
  1055. var maxGap = Math.round(minGap * Obstacle.MAX_GAP_COEFFICIENT);
  1056. return getRandomNum(minGap, maxGap);
  1057. },
  1058. /**
  1059. * Check if obstacle is visible.
  1060. * @return {boolean} Whether the obstacle is in the game area.
  1061. */
  1062. isVisible: function() {
  1063. return this.xPos + this.width > 0;
  1064. },
  1065. /**
  1066. * Make a copy of the collision boxes, since these will change based on
  1067. * obstacle type and size.
  1068. */
  1069. cloneCollisionBoxes: function() {
  1070. var collisionBoxes = this.typeConfig.collisionBoxes;
  1071. for (var i = collisionBoxes.length - 1; i >= 0; i--) {
  1072. this.collisionBoxes[i] = new CollisionBox(collisionBoxes[i].x,
  1073. collisionBoxes[i].y, collisionBoxes[i].width,
  1074. collisionBoxes[i].height);
  1075. }
  1076. }
  1077. };
  1078. /**
  1079. * Obstacle definitions.
  1080. * minGap: minimum pixel space betweeen obstacles.
  1081. * multipleSpeed: Speed at which multiples are allowed.
  1082. */
  1083. Obstacle.types = [{
  1084. type: 'CACTUS_SMALL',
  1085. className: ' cactus cactus-small ',
  1086. width: 17,
  1087. height: 35,
  1088. yPos: 105,
  1089. multipleSpeed: 3,
  1090. minGap: 120,
  1091. collisionBoxes: [
  1092. new CollisionBox(0, 7, 5, 27),
  1093. new CollisionBox(4, 0, 6, 34),
  1094. new CollisionBox(10, 4, 7, 14)
  1095. ]
  1096. }, {
  1097. type: 'CACTUS_LARGE',
  1098. className: ' cactus cactus-large ',
  1099. width: 25,
  1100. height: 50,
  1101. yPos: 90,
  1102. multipleSpeed: 6,
  1103. minGap: 120,
  1104. collisionBoxes: [
  1105. new CollisionBox(0, 12, 7, 38),
  1106. new CollisionBox(8, 0, 7, 49),
  1107. new CollisionBox(13, 10, 10, 38)
  1108. ]
  1109. }];
  1110. //******************************************************************************
  1111. /**
  1112. * T-rex game character.
  1113. * @param {HTMLCanvas} canvas
  1114. * @param {HTMLImage} image Character image.
  1115. * @constructor
  1116. */
  1117. function Trex(canvas, image) {
  1118. this.canvas = canvas;
  1119. this.canvasCtx = canvas.getContext('2d');
  1120. this.image = image;
  1121. this.xPos = 0;
  1122. this.yPos = 0;
  1123. // Position when on the ground.
  1124. this.groundYPos = 0;
  1125. this.currentFrame = 0;
  1126. this.currentAnimFrames = [];
  1127. this.blinkDelay = 0;
  1128. this.animStartTime = 0;
  1129. this.timer = 0;
  1130. this.msPerFrame = 1000 / FPS;
  1131. this.config = Trex.config;
  1132. // Current status.
  1133. this.status = Trex.status.WAITING;
  1134. this.jumping = false;
  1135. this.jumpVelocity = 0;
  1136. this.reachedMinHeight = false;
  1137. this.speedDrop = false;
  1138. this.jumpCount = 0;
  1139. this.jumpspotX = 0;
  1140. this.init();
  1141. };
  1142. /**
  1143. * T-rex player config.
  1144. * @enum {number}
  1145. */
  1146. Trex.config = {
  1147. DROP_VELOCITY: -5,
  1148. GRAVITY: 0.6,
  1149. HEIGHT: 47,
  1150. INIITAL_JUMP_VELOCITY: -10,
  1151. INTRO_DURATION: 1500,
  1152. MAX_JUMP_HEIGHT: 30,
  1153. MIN_JUMP_HEIGHT: 30,
  1154. SPEED_DROP_COEFFICIENT: 3,
  1155. SPRITE_WIDTH: 262,
  1156. START_X_POS: 50,
  1157. WIDTH: 44
  1158. };
  1159. /**
  1160. * Used in collision detection.
  1161. * @type {Array.<CollisionBox>}
  1162. */
  1163. Trex.collisionBoxes = [
  1164. new CollisionBox(1, -1, 30, 26),
  1165. new CollisionBox(32, 0, 8, 16),
  1166. new CollisionBox(10, 35, 14, 8),
  1167. new CollisionBox(1, 24, 29, 5),
  1168. new CollisionBox(5, 30, 21, 4),
  1169. new CollisionBox(9, 34, 15, 4)
  1170. ];
  1171. /**
  1172. * Animation states.
  1173. * @enum {string}
  1174. */
  1175. Trex.status = {
  1176. CRASHED: 'CRASHED',
  1177. JUMPING: 'JUMPING',
  1178. RUNNING: 'RUNNING',
  1179. WAITING: 'WAITING'
  1180. };
  1181. /**
  1182. * Blinking coefficient.
  1183. * @const
  1184. */
  1185. Trex.BLINK_TIMING = 7000;
  1186. /**
  1187. * Animation config for different states.
  1188. * @enum {object}
  1189. */
  1190. Trex.animFrames = {
  1191. WAITING: {
  1192. frames: [44, 0],
  1193. msPerFrame: 1000 / 3
  1194. },
  1195. RUNNING: {
  1196. frames: [88, 132],
  1197. msPerFrame: 1000 / 12
  1198. },
  1199. CRASHED: {
  1200. frames: [220],
  1201. msPerFrame: 1000 / 60
  1202. },
  1203. JUMPING: {
  1204. frames: [0],
  1205. msPerFrame: 1000 / 60
  1206. }
  1207. };
  1208. Trex.prototype = {
  1209. /**
  1210. * T-rex player initaliser.
  1211. * Sets the t-rex to blink at random intervals.
  1212. */
  1213. init: function() {
  1214. this.blinkDelay = this.setBlinkDelay();
  1215. this.groundYPos = Runner.defaultDimensions.HEIGHT - this.config.HEIGHT -
  1216. Runner.config.BOTTOM_PAD;
  1217. this.yPos = this.groundYPos;
  1218. this.minJumpHeight = this.groundYPos - this.config.MIN_JUMP_HEIGHT;
  1219. this.draw(0, 0);
  1220. this.update(0, Trex.status.WAITING);
  1221. },
  1222. /**
  1223. * Setter for the jump velocity.
  1224. * The approriate drop velocity is also set.
  1225. */
  1226. setJumpVelocity: function(setting) {
  1227. this.config.INIITAL_JUMP_VELOCITY = -setting;
  1228. this.config.DROP_VELOCITY = -setting / 2;
  1229. },
  1230. /**
  1231. * Set the animation status.
  1232. * @param {!number} deltaTime
  1233. * @param {Trex.status} status Optional status to switch to.
  1234. */
  1235. update: function(deltaTime, opt_status) {
  1236. this.timer += deltaTime;
  1237. // Update the status.
  1238. if (opt_status) {
  1239. this.status = opt_status;
  1240. this.currentFrame = 0;
  1241. this.msPerFrame = Trex.animFrames[opt_status].msPerFrame;
  1242. this.currentAnimFrames = Trex.animFrames[opt_status].frames;
  1243. if (opt_status == Trex.status.WAITING) {
  1244. this.animStartTime = performance.now();
  1245. this.setBlinkDelay();
  1246. }
  1247. }
  1248. // Game intro animation, T-rex moves in from the left.
  1249. if (this.playingIntro && this.xPos < this.config.START_X_POS) {
  1250. this.xPos += Math.round((this.config.START_X_POS /
  1251. this.config.INTRO_DURATION) * deltaTime);
  1252. }
  1253. if (this.status == Trex.status.WAITING) {
  1254. this.blink(performance.now());
  1255. } else {
  1256. this.draw(this.currentAnimFrames[this.currentFrame], 0);
  1257. }
  1258. // Update the frame position.
  1259. if (this.timer >= this.msPerFrame) {
  1260. this.currentFrame = this.currentFrame ==
  1261. this.currentAnimFrames.length - 1 ? 0 : this.currentFrame + 1;
  1262. this.timer = 0;
  1263. }
  1264. },
  1265. /**
  1266. * Draw the t-rex to a particular position.
  1267. * @param {number} x
  1268. * @param {number} y
  1269. */
  1270. draw: function(x, y) {
  1271. var sourceX = x;
  1272. var sourceY = y;
  1273. var sourceWidth = this.config.WIDTH;
  1274. var sourceHeight = this.config.HEIGHT;
  1275. if (IS_HIDPI) {
  1276. sourceX *= 2;
  1277. sourceY *= 2;
  1278. sourceWidth *= 2;
  1279. sourceHeight *= 2;
  1280. }
  1281. this.canvasCtx.drawImage(this.image, sourceX, sourceY,
  1282. sourceWidth, sourceHeight,
  1283. this.xPos, this.yPos,
  1284. this.config.WIDTH, this.config.HEIGHT);
  1285. },
  1286. /**
  1287. * Sets a random time for the blink to happen.
  1288. */
  1289. setBlinkDelay: function() {
  1290. this.blinkDelay = Math.ceil(Math.random() * Trex.BLINK_TIMING);
  1291. },
  1292. /**
  1293. * Make t-rex blink at random intervals.
  1294. * @param {number} time Current time in milliseconds.
  1295. */
  1296. blink: function(time) {
  1297. var deltaTime = time - this.animStartTime;
  1298. if (deltaTime >= this.blinkDelay) {
  1299. this.draw(this.currentAnimFrames[this.currentFrame], 0);
  1300. if (this.currentFrame == 1) {
  1301. // Set new random delay to blink.
  1302. this.setBlinkDelay();
  1303. this.animStartTime = time;
  1304. }
  1305. }
  1306. },
  1307. /**
  1308. * Initialise a jump.
  1309. */
  1310. startJump: function() {
  1311. if (!this.jumping) {
  1312. this.update(0, Trex.status.JUMPING);
  1313. this.jumpVelocity = this.config.INIITAL_JUMP_VELOCITY;
  1314. this.jumping = true;
  1315. this.reachedMinHeight = false;
  1316. this.speedDrop = false;
  1317. }
  1318. },
  1319. /**
  1320. * Jump is complete, falling down.
  1321. */
  1322. endJump: function() {
  1323. if (this.reachedMinHeight &&
  1324. this.jumpVelocity < this.config.DROP_VELOCITY) {
  1325. this.jumpVelocity = this.config.DROP_VELOCITY;
  1326. }
  1327. },
  1328. /**
  1329. * Update frame for a jump.
  1330. * @param {number} deltaTime
  1331. */
  1332. updateJump: function(deltaTime) {
  1333. var msPerFrame = Trex.animFrames[this.status].msPerFrame;
  1334. var framesElapsed = deltaTime / msPerFrame;
  1335. // Speed drop makes Trex fall faster.
  1336. if (this.speedDrop) {
  1337. this.yPos += Math.round(this.jumpVelocity *
  1338. this.config.SPEED_DROP_COEFFICIENT * framesElapsed);
  1339. } else {
  1340. this.yPos += Math.round(this.jumpVelocity * framesElapsed);
  1341. }
  1342. this.jumpVelocity += this.config.GRAVITY * framesElapsed;
  1343. // Minimum height has been reached.
  1344. if (this.yPos < this.minJumpHeight || this.speedDrop) {
  1345. this.reachedMinHeight = true;
  1346. }
  1347. // Reached max height
  1348. if (this.yPos < this.config.MAX_JUMP_HEIGHT || this.speedDrop) {
  1349. this.endJump();
  1350. }
  1351. // Back down at ground level. Jump completed.
  1352. if (this.yPos > this.groundYPos) {
  1353. this.reset();
  1354. this.jumpCount++;
  1355. }
  1356. this.update(deltaTime);
  1357. },
  1358. /**
  1359. * Set the speed drop. Immediately cancels the current jump.
  1360. */
  1361. setSpeedDrop: function() {
  1362. this.speedDrop = true;
  1363. this.jumpVelocity = 1;
  1364. },
  1365. /**
  1366. * Reset the t-rex to running at start of game.
  1367. */
  1368. reset: function() {
  1369. this.yPos = this.groundYPos;
  1370. this.jumpVelocity = 0;
  1371. this.jumping = false;
  1372. this.update(0, Trex.status.RUNNING);
  1373. this.midair = false;
  1374. this.speedDrop = false;
  1375. this.jumpCount = 0;
  1376. }
  1377. };
  1378. //******************************************************************************
  1379. /**
  1380. * Handles displaying the distance meter.
  1381. * @param {!HTMLCanvasElement} canvas
  1382. * @param {!HTMLImage} spriteSheet Image sprite.
  1383. * @param {number} canvasWidth
  1384. * @constructor
  1385. */
  1386. function DistanceMeter(canvas, spriteSheet, canvasWidth) {
  1387. this.canvas = canvas;
  1388. this.canvasCtx = canvas.getContext('2d');
  1389. this.image = spriteSheet;
  1390. this.x = 0;
  1391. this.y = 5;
  1392. this.currentDistance = 0;
  1393. this.maxScore = 0;
  1394. this.highScore = 0;
  1395. this.container = null;
  1396. this.digits = [];
  1397. this.acheivement = false;
  1398. this.defaultString = '';
  1399. this.flashTimer = 0;
  1400. this.flashIterations = 0;
  1401. this.config = DistanceMeter.config;
  1402. this.init(canvasWidth);
  1403. };
  1404. /**
  1405. * @enum {number}
  1406. */
  1407. DistanceMeter.dimensions = {
  1408. WIDTH: 10,
  1409. HEIGHT: 13,
  1410. DEST_WIDTH: 11
  1411. };
  1412. /**
  1413. * Y positioning of the digits in the sprite sheet.
  1414. * X position is always 0.
  1415. * @type {array.<number>}
  1416. */
  1417. DistanceMeter.yPos = [0, 13, 27, 40, 53, 67, 80, 93, 107, 120];
  1418. /**
  1419. * Distance meter config.
  1420. * @enum {number}
  1421. */
  1422. DistanceMeter.config = {
  1423. // Number of digits.
  1424. MAX_DISTANCE_UNITS: 5,
  1425. // Distance that causes achievement animation.
  1426. ACHIEVEMENT_DISTANCE: 100,
  1427. // Used for conversion from pixel distance to a scaled unit.
  1428. COEFFICIENT: 0.025,
  1429. // Flash duration in milliseconds.
  1430. FLASH_DURATION: 1000 / 4,
  1431. // Flash iterations for achievement animation.
  1432. FLASH_ITERATIONS: 3
  1433. };
  1434. DistanceMeter.prototype = {
  1435. /**
  1436. * Initialise the distance meter to '00000'.
  1437. * @param {number} width Canvas width in px.
  1438. */
  1439. init: function(width) {
  1440. var maxDistanceStr = '';
  1441. this.calcXPos(width);
  1442. this.maxScore = this.config.MAX_DISTANCE_UNITS;
  1443. for (var i = 0; i < this.config.MAX_DISTANCE_UNITS; i++) {
  1444. this.draw(i, 0);
  1445. this.defaultString += '0';
  1446. maxDistanceStr += '9';
  1447. }
  1448. this.maxScore = parseInt(maxDistanceStr);
  1449. },
  1450. /**
  1451. * Calculate the xPos in the canvas.
  1452. * @param {number} canvasWidth
  1453. */
  1454. calcXPos: function(canvasWidth) {
  1455. this.x = canvasWidth - (DistanceMeter.dimensions.DEST_WIDTH *
  1456. (this.config.MAX_DISTANCE_UNITS + 1));
  1457. },
  1458. /**
  1459. * Draw a digit to canvas.
  1460. * @param {number} digitPos Position of the digit.
  1461. * @param {number} value Digit value 0-9.
  1462. * @param {boolean} opt_highScore Whether drawing the high score.
  1463. */
  1464. draw: function(digitPos, value, opt_highScore) {
  1465. var sourceWidth = DistanceMeter.dimensions.WIDTH;
  1466. var sourceHeight = DistanceMeter.dimensions.HEIGHT;
  1467. var sourceX = DistanceMeter.dimensions.WIDTH * value;
  1468. var targetX = digitPos * DistanceMeter.dimensions.DEST_WIDTH;
  1469. var targetY = this.y;
  1470. var targetWidth = DistanceMeter.dimensions.WIDTH;
  1471. var targetHeight = DistanceMeter.dimensions.HEIGHT;
  1472. // For high DPI we 2x source values.
  1473. if (IS_HIDPI) {
  1474. sourceWidth *= 2;
  1475. sourceHeight *= 2;
  1476. sourceX *= 2;
  1477. }
  1478. this.canvasCtx.save();
  1479. if (opt_highScore) {
  1480. // Left of the current score.
  1481. var highScoreX = this.x - (this.config.MAX_DISTANCE_UNITS * 2) *
  1482. DistanceMeter.dimensions.WIDTH;
  1483. this.canvasCtx.translate(highScoreX, this.y);
  1484. } else {
  1485. this.canvasCtx.translate(this.x, this.y);
  1486. }
  1487. this.canvasCtx.drawImage(this.image, sourceX, 0,
  1488. sourceWidth, sourceHeight,
  1489. targetX, targetY,
  1490. targetWidth, targetHeight
  1491. );
  1492. this.canvasCtx.restore();
  1493. },
  1494. /**
  1495. * Covert pixel distance to a 'real' distance.
  1496. * @param {number} distance Pixel distance ran.
  1497. * @return {number} The 'real' distance ran.
  1498. */
  1499. getActualDistance: function(distance) {
  1500. return distance ?
  1501. Math.round(distance * this.config.COEFFICIENT) : 0;
  1502. },
  1503. /**
  1504. * Update the distance meter.
  1505. * @param {number} deltaTime
  1506. * @param {number} distance
  1507. * @return {boolean} Whether the acheivement sound fx should be played.
  1508. */
  1509. update: function(deltaTime, distance) {
  1510. var paint = true;
  1511. var playSound = false;
  1512. if (!this.acheivement) {
  1513. distance = this.getActualDistance(distance);
  1514. if (distance > 0) {
  1515. // Acheivement unlocked
  1516. if (distance % this.config.ACHIEVEMENT_DISTANCE == 0) {
  1517. // Flash score and play sound.
  1518. this.acheivement = true;
  1519. this.flashTimer = 0;
  1520. playSound = true;
  1521. }
  1522. // Create a string representation of the distance with leading 0.
  1523. var distanceStr = (this.defaultString +
  1524. distance).substr(-this.config.MAX_DISTANCE_UNITS);
  1525. this.digits = distanceStr.split('');
  1526. } else {
  1527. this.digits = this.defaultString.split('');
  1528. }
  1529. } else {
  1530. // Control flashing of the score on reaching acheivement.
  1531. if (this.flashIterations <= this.config.FLASH_ITERATIONS) {
  1532. this.flashTimer += deltaTime;
  1533. if (this.flashTimer < this.config.FLASH_DURATION) {
  1534. paint = false;
  1535. } else if (this.flashTimer >
  1536. this.config.FLASH_DURATION * 2) {
  1537. this.flashTimer = 0;
  1538. this.flashIterations++;
  1539. }
  1540. } else {
  1541. this.acheivement = false;
  1542. this.flashIterations = 0;
  1543. this.flashTimer = 0;
  1544. }
  1545. }
  1546. // Draw the digits if not flashing.
  1547. if (paint) {
  1548. for (var i = this.digits.length - 1; i >= 0; i--) {
  1549. this.draw(i, parseInt(this.digits[i]));
  1550. }
  1551. }
  1552. this.drawHighScore();
  1553. return playSound;
  1554. },
  1555. /**
  1556. * Draw the high score.
  1557. */
  1558. drawHighScore: function() {
  1559. this.canvasCtx.save();
  1560. this.canvasCtx.globalAlpha = .8;
  1561. for (var i = this.highScore.length - 1; i >= 0; i--) {
  1562. this.draw(i, parseInt(this.highScore[i], 10), true);
  1563. }
  1564. this.canvasCtx.restore();
  1565. },
  1566. /**
  1567. * Set the highscore as a array string.
  1568. * Position of char in the sprite: H - 10, I - 11.
  1569. * @param {number} distance Distance ran in pixels.
  1570. */
  1571. setHighScore: function(distance) {
  1572. distance = this.getActualDistance(distance);
  1573. var highScoreStr = (this.defaultString +
  1574. distance).substr(-this.config.MAX_DISTANCE_UNITS);
  1575. this.highScore = ['10', '11', ''].concat(highScoreStr.split(''));
  1576. },
  1577. /**
  1578. * Reset the distance meter back to '00000'.
  1579. */
  1580. reset: function() {
  1581. this.update(0);
  1582. this.acheivement = false;
  1583. }
  1584. };
  1585. //******************************************************************************
  1586. /**
  1587. * Cloud background item.
  1588. * Similar to an obstacle object but without collision boxes.
  1589. * @param {HTMLCanvasElement} canvas Canvas element.
  1590. * @param {Image} cloudImg
  1591. * @param {number} containerWidth
  1592. */
  1593. function Cloud(canvas, cloudImg, containerWidth) {
  1594. this.canvas = canvas;
  1595. this.canvasCtx = this.canvas.getContext('2d');
  1596. this.image = cloudImg;
  1597. this.containerWidth = containerWidth;
  1598. this.xPos = containerWidth;
  1599. this.yPos = 0;
  1600. this.remove = false;
  1601. this.cloudGap = getRandomNum(Cloud.config.MIN_CLOUD_GAP,
  1602. Cloud.config.MAX_CLOUD_GAP);
  1603. this.init();
  1604. };
  1605. /**
  1606. * Cloud object config.
  1607. * @enum {number}
  1608. */
  1609. Cloud.config = {
  1610. HEIGHT: 13,
  1611. MAX_CLOUD_GAP: 400,
  1612. MAX_SKY_LEVEL: 30,
  1613. MIN_CLOUD_GAP: 100,
  1614. MIN_SKY_LEVEL: 71,
  1615. WIDTH: 46
  1616. };
  1617. Cloud.prototype = {
  1618. /**
  1619. * Initialise the cloud. Sets the Cloud height.
  1620. */
  1621. init: function() {
  1622. this.yPos = getRandomNum(Cloud.config.MAX_SKY_LEVEL,
  1623. Cloud.config.MIN_SKY_LEVEL);
  1624. this.draw();
  1625. },
  1626. /**
  1627. * Draw the cloud.
  1628. */
  1629. draw: function() {
  1630. this.canvasCtx.save();
  1631. var sourceWidth = Cloud.config.WIDTH;
  1632. var sourceHeight = Cloud.config.HEIGHT;
  1633. if (IS_HIDPI) {
  1634. sourceWidth = sourceWidth * 2;
  1635. sourceHeight = sourceHeight * 2;
  1636. }
  1637. this.canvasCtx.drawImage(this.image, 0, 0,
  1638. sourceWidth, sourceHeight,
  1639. this.xPos, this.yPos,
  1640. Cloud.config.WIDTH, Cloud.config.HEIGHT);
  1641. this.canvasCtx.restore();
  1642. },
  1643. /**
  1644. * Update the cloud position.
  1645. * @param {number} speed
  1646. */
  1647. update: function(speed) {
  1648. if (!this.remove) {
  1649. this.xPos -= Math.ceil(speed);
  1650. this.draw();
  1651. // Mark as removeable if no longer in the canvas.
  1652. if (!this.isVisible()) {
  1653. this.remove = true;
  1654. }
  1655. }
  1656. },
  1657. /**
  1658. * Check if the cloud is visible on the stage.
  1659. * @return {boolean}
  1660. */
  1661. isVisible: function() {
  1662. return this.xPos + Cloud.config.WIDTH > 0;
  1663. }
  1664. };
  1665. //******************************************************************************
  1666. /**
  1667. * Horizon Line.
  1668. * Consists of two connecting lines. Randomly assigns a flat / bumpy horizon.
  1669. * @param {HTMLCanvasElement} canvas
  1670. * @param {HTMLImage} bgImg Horizon line sprite.
  1671. * @constructor
  1672. */
  1673. function HorizonLine(canvas, bgImg) {
  1674. this.image = bgImg;
  1675. this.canvas = canvas;
  1676. this.canvasCtx = canvas.getContext('2d');
  1677. this.sourceDimensions = {};
  1678. this.dimensions = HorizonLine.dimensions;
  1679. this.sourceXPos = [0, this.dimensions.WIDTH];
  1680. this.xPos = [];
  1681. this.yPos = 0;
  1682. this.bumpThreshold = 0.5;
  1683. this.setSourceDimensions();
  1684. this.draw();
  1685. };
  1686. /**
  1687. * Horizon line dimensions.
  1688. * @enum {number}
  1689. */
  1690. HorizonLine.dimensions = {
  1691. WIDTH: 600,
  1692. HEIGHT: 12,
  1693. YPOS: 127
  1694. };
  1695. HorizonLine.prototype = {
  1696. /**
  1697. * Set the source dimensions of the horizon line.
  1698. */
  1699. setSourceDimensions: function() {
  1700. for (var dimension in HorizonLine.dimensions) {
  1701. if (IS_HIDPI) {
  1702. if (dimension != 'YPOS') {
  1703. this.sourceDimensions[dimension] =
  1704. HorizonLine.dimensions[dimension] * 2;
  1705. }
  1706. } else {
  1707. this.sourceDimensions[dimension] =
  1708. HorizonLine.dimensions[dimension];
  1709. }
  1710. this.dimensions[dimension] = HorizonLine.dimensions[dimension];
  1711. }
  1712. this.xPos = [0, HorizonLine.dimensions.WIDTH];
  1713. this.yPos = HorizonLine.dimensions.YPOS;
  1714. },
  1715. /**
  1716. * Return the crop x position of a type.
  1717. */
  1718. getRandomType: function() {
  1719. return Math.random() > this.bumpThreshold ? this.dimensions.WIDTH : 0;
  1720. },
  1721. /**
  1722. * Draw the horizon line.
  1723. */
  1724. draw: function() {
  1725. this.canvasCtx.drawImage(this.image, this.sourceXPos[0], 0,
  1726. this.sourceDimensions.WIDTH, this.sourceDimensions.HEIGHT,
  1727. this.xPos[0], this.yPos,
  1728. this.dimensions.WIDTH, this.dimensions.HEIGHT);
  1729. this.canvasCtx.drawImage(this.image, this.sourceXPos[1], 0,
  1730. this.sourceDimensions.WIDTH, this.sourceDimensions.HEIGHT,
  1731. this.xPos[1], this.yPos,
  1732. this.dimensions.WIDTH, this.dimensions.HEIGHT);
  1733. },
  1734. /**
  1735. * Update the x position of an indivdual piece of the line.
  1736. * @param {number} pos Line position.
  1737. * @param {number} increment
  1738. */
  1739. updateXPos: function(pos, increment) {
  1740. var line1 = pos;
  1741. var line2 = pos == 0 ? 1 : 0;
  1742. this.xPos[line1] -= increment;
  1743. this.xPos[line2] = this.xPos[line1] + this.dimensions.WIDTH;
  1744. if (this.xPos[line1] <= -this.dimensions.WIDTH) {
  1745. this.xPos[line1] += this.dimensions.WIDTH * 2;
  1746. this.xPos[line2] = this.xPos[line1] - this.dimensions.WIDTH;
  1747. this.sourceXPos[line1] = this.getRandomType();
  1748. }
  1749. },
  1750. /**
  1751. * Update the horizon line.
  1752. * @param {number} deltaTime
  1753. * @param {number} speed
  1754. */
  1755. update: function(deltaTime, speed) {
  1756. var increment = Math.floor(speed * (FPS / 1000) * deltaTime);
  1757. if (this.xPos[0] <= 0) {
  1758. this.updateXPos(0, increment);
  1759. } else {
  1760. this.updateXPos(1, increment);
  1761. }
  1762. this.draw();
  1763. },
  1764. /**
  1765. * Reset horizon to the starting position.
  1766. */
  1767. reset: function() {
  1768. this.xPos[0] = 0;
  1769. this.xPos[1] = HorizonLine.dimensions.WIDTH;
  1770. }
  1771. };
  1772. //******************************************************************************
  1773. /**
  1774. * Horizon background class.
  1775. * @param {HTMLCanvasElement} canvas
  1776. * @param {Array.<HTMLImageElement>} images
  1777. * @param {object} dimensions Canvas dimensions.
  1778. * @param {number} gapCoefficient
  1779. * @constructor
  1780. */
  1781. function Horizon(canvas, images, dimensions, gapCoefficient) {
  1782. this.canvas = canvas;
  1783. this.canvasCtx = this.canvas.getContext('2d');
  1784. this.config = Horizon.config;
  1785. this.dimensions = dimensions;
  1786. this.gapCoefficient = gapCoefficient;
  1787. this.obstacles = [];
  1788. this.horizonOffsets = [0, 0];
  1789. this.cloudFrequency = this.config.CLOUD_FREQUENCY;
  1790. // Cloud
  1791. this.clouds = [];
  1792. this.cloudImg = images.CLOUD;
  1793. this.cloudSpeed = this.config.BG_CLOUD_SPEED;
  1794. // Horizon
  1795. this.horizonImg = images.HORIZON;
  1796. this.horizonLine = null;
  1797. // Obstacles
  1798. this.obstacleImgs = {
  1799. CACTUS_SMALL: images.CACTUS_SMALL,
  1800. CACTUS_LARGE: images.CACTUS_LARGE
  1801. };
  1802. this.init();
  1803. };
  1804. /**
  1805. * Horizon config.
  1806. * @enum {number}
  1807. */
  1808. Horizon.config = {
  1809. BG_CLOUD_SPEED: 0.2,
  1810. BUMPY_THRESHOLD: .3,
  1811. CLOUD_FREQUENCY: .5,
  1812. HORIZON_HEIGHT: 16,
  1813. MAX_CLOUDS: 6
  1814. };
  1815. Horizon.prototype = {
  1816. /**
  1817. * Initialise the horizon. Just add the line and a cloud. No obstacles.
  1818. */
  1819. init: function() {
  1820. this.addCloud();
  1821. this.horizonLine = new HorizonLine(this.canvas, this.horizonImg);
  1822. },
  1823. /**
  1824. * @param {number} deltaTime
  1825. * @param {number} currentSpeed
  1826. * @param {boolean} updateObstacles Used as an override to prevent
  1827. * the obstacles from being updated / added. This happens in the
  1828. * ease in section.
  1829. */
  1830. update: function(deltaTime, currentSpeed, updateObstacles) {
  1831. this.runningTime += deltaTime;
  1832. this.horizonLine.update(deltaTime, currentSpeed);
  1833. this.updateClouds(deltaTime, currentSpeed);
  1834. if (updateObstacles) {
  1835. this.updateObstacles(deltaTime, currentSpeed);
  1836. }
  1837. },
  1838. /**
  1839. * Update the cloud positions.
  1840. * @param {number} deltaTime
  1841. * @param {number} currentSpeed
  1842. */
  1843. updateClouds: function(deltaTime, speed) {
  1844. var cloudSpeed = this.cloudSpeed / 1000 * deltaTime * speed;
  1845. var numClouds = this.clouds.length;
  1846. if (numClouds) {
  1847. for (var i = numClouds - 1; i >= 0; i--) {
  1848. this.clouds[i].update(cloudSpeed);
  1849. }
  1850. var lastCloud = this.clouds[numClouds - 1];
  1851. // Check for adding a new cloud.
  1852. if (numClouds < this.config.MAX_CLOUDS &&
  1853. (this.dimensions.WIDTH - lastCloud.xPos) > lastCloud.cloudGap &&
  1854. this.cloudFrequency > Math.random()) {
  1855. this.addCloud();
  1856. }
  1857. // Remove expired clouds.
  1858. this.clouds = this.clouds.filter(function(obj) {
  1859. return !obj.remove;
  1860. });
  1861. }
  1862. },
  1863. /**
  1864. * Update the obstacle positions.
  1865. * @param {number} deltaTime
  1866. * @param {number} currentSpeed
  1867. */
  1868. updateObstacles: function(deltaTime, currentSpeed) {
  1869. // Obstacles, move to Horizon layer.
  1870. var updatedObstacles = this.obstacles.slice(0);
  1871. for (var i = 0; i < this.obstacles.length; i++) {
  1872. var obstacle = this.obstacles[i];
  1873. obstacle.update(deltaTime, currentSpeed);
  1874. // Clean up existing obstacles.
  1875. if (obstacle.remove) {
  1876. updatedObstacles.shift();
  1877. }
  1878. }
  1879. this.obstacles = updatedObstacles;
  1880. if (this.obstacles.length > 0) {
  1881. var lastObstacle = this.obstacles[this.obstacles.length - 1];
  1882. if (lastObstacle && !lastObstacle.followingObstacleCreated &&
  1883. lastObstacle.isVisible() &&
  1884. (lastObstacle.xPos + lastObstacle.width + lastObstacle.gap) <
  1885. this.dimensions.WIDTH) {
  1886. this.addNewObstacle(currentSpeed);
  1887. lastObstacle.followingObstacleCreated = true;
  1888. }
  1889. } else {
  1890. // Create new obstacles.
  1891. this.addNewObstacle(currentSpeed);
  1892. }
  1893. },
  1894. /**
  1895. * Add a new obstacle.
  1896. * @param {number} currentSpeed
  1897. */
  1898. addNewObstacle: function(currentSpeed) {
  1899. var obstacleTypeIndex =
  1900. getRandomNum(0, Obstacle.types.length - 1);
  1901. var obstacleType = Obstacle.types[obstacleTypeIndex];
  1902. var obstacleImg = this.obstacleImgs[obstacleType.type];
  1903. this.obstacles.push(new Obstacle(this.canvasCtx, obstacleType,
  1904. obstacleImg, this.dimensions, this.gapCoefficient, currentSpeed));
  1905. },
  1906. /**
  1907. * Reset the horizon layer.
  1908. * Remove existing obstacles and reposition the horizon line.
  1909. */
  1910. reset: function() {
  1911. this.obstacles = [];
  1912. this.horizonLine.reset();
  1913. },
  1914. /**
  1915. * Update the canvas width and scaling.
  1916. * @param {number} width Canvas width.
  1917. * @param {number} height Canvas height.
  1918. */
  1919. resize: function(width, height) {
  1920. this.canvas.width = width;
  1921. this.canvas.height = height;
  1922. },
  1923. /**
  1924. * Add a new cloud to the horizon.
  1925. */
  1926. addCloud: function() {
  1927. this.clouds.push(new Cloud(this.canvas, this.cloudImg,
  1928. this.dimensions.WIDTH));
  1929. }
  1930. };
  1931. })();
  1932. new Runner('.interstitial-wrapper');