| OLD | NEW |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 (function() { | 4 (function() { |
| 5 'use strict'; | 5 'use strict'; |
| 6 /** | 6 /** |
| 7 * T-Rex runner. | 7 * T-Rex runner. |
| 8 * @param {string} outerContainerId Outer containing element id. | 8 * @param {string} outerContainerId Outer containing element id. |
| 9 * @param {Object} opt_config | 9 * @param {Object} opt_config |
| 10 * @constructor | 10 * @constructor |
| 11 * @export | 11 * @export |
| 12 */ | 12 */ |
| 13 function Runner(outerContainerId, opt_config) { | 13 function Runner(outerContainerId, opt_config) { |
| 14 // Singleton | 14 // Singleton |
| 15 if (Runner.instance_) { | 15 if (Runner.instance_) { |
| 16 return Runner.instance_; | 16 return Runner.instance_; |
| 17 } | 17 } |
| 18 Runner.instance_ = this; | 18 Runner.instance_ = this; |
| 19 | 19 |
| 20 this.outerContainerEl = document.querySelector(outerContainerId); | 20 this.outerContainerEl = document.querySelector(outerContainerId); |
| 21 this.containerEl = null; | 21 this.containerEl = null; |
| 22 this.snackbarEl = null; | 22 this.snackbarEl = null; |
| 23 this.detailsButton = this.outerContainerEl.querySelector('#details-button'); | |
| 24 | 23 |
| 25 this.config = opt_config || Runner.config; | 24 this.config = opt_config || Runner.config; |
| 26 | 25 |
| 27 this.dimensions = Runner.defaultDimensions; | 26 this.dimensions = Runner.defaultDimensions; |
| 28 | 27 |
| 29 this.canvas = null; | 28 this.canvas = null; |
| 30 this.canvasCtx = null; | 29 this.canvasCtx = null; |
| 31 | 30 |
| 32 this.tRex = null; | 31 this.tRex = null; |
| 33 | 32 |
| (...skipping 620 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 654 /** | 653 /** |
| 655 * Process keydown. | 654 * Process keydown. |
| 656 * @param {Event} e | 655 * @param {Event} e |
| 657 */ | 656 */ |
| 658 onKeyDown: function(e) { | 657 onKeyDown: function(e) { |
| 659 // Prevent native page scrolling whilst tapping on mobile. | 658 // Prevent native page scrolling whilst tapping on mobile. |
| 660 if (IS_MOBILE && this.playing) { | 659 if (IS_MOBILE && this.playing) { |
| 661 e.preventDefault(); | 660 e.preventDefault(); |
| 662 } | 661 } |
| 663 | 662 |
| 664 if (e.target != this.detailsButton) { | 663 if (!this.crashed && !this.paused) { |
| 665 if (!this.crashed && (Runner.keycodes.JUMP[e.keyCode] || | 664 if (Runner.keycodes.JUMP[e.keyCode] || |
| 666 e.type == Runner.events.TOUCHSTART)) { | 665 e.type == Runner.events.TOUCHSTART) { |
| 666 e.preventDefault(); |
| 667 // Starting the game for the first time. |
| 667 if (!this.playing) { | 668 if (!this.playing) { |
| 668 this.loadSounds(); | 669 this.loadSounds(); |
| 669 this.playing = true; | 670 this.playing = true; |
| 670 this.update(); | 671 this.update(); |
| 671 if (window.errorPageController) { | 672 if (window.errorPageController) { |
| 672 errorPageController.trackEasterEgg(); | 673 errorPageController.trackEasterEgg(); |
| 673 } | 674 } |
| 674 } | 675 } |
| 675 // Play sound effect and jump on starting the game for the first time. | 676 // Start jump. |
| 676 if (!this.tRex.jumping && !this.tRex.ducking) { | 677 if (!this.tRex.jumping && !this.tRex.ducking) { |
| 677 this.playSound(this.soundFx.BUTTON_PRESS); | 678 this.playSound(this.soundFx.BUTTON_PRESS); |
| 678 this.tRex.startJump(this.currentSpeed); | 679 this.tRex.startJump(this.currentSpeed); |
| 679 } | 680 } |
| 681 } else if (this.playing && Runner.keycodes.DUCK[e.keyCode]) { |
| 682 e.preventDefault(); |
| 683 if (this.tRex.jumping) { |
| 684 // Speed drop, activated only when jump key is not pressed. |
| 685 this.tRex.setSpeedDrop(); |
| 686 } else if (!this.tRex.jumping && !this.tRex.ducking) { |
| 687 // Duck. |
| 688 this.tRex.setDuck(true); |
| 689 } |
| 680 } | 690 } |
| 681 | 691 } else if (this.crashed && e.type == Runner.events.TOUCHSTART && |
| 682 if (this.crashed && e.type == Runner.events.TOUCHSTART && | 692 e.currentTarget == this.containerEl) { |
| 683 e.currentTarget == this.containerEl) { | 693 this.restart(); |
| 684 this.restart(); | |
| 685 } | |
| 686 } | |
| 687 | |
| 688 if (this.playing && !this.crashed && Runner.keycodes.DUCK[e.keyCode]) { | |
| 689 e.preventDefault(); | |
| 690 if (this.tRex.jumping) { | |
| 691 // Speed drop, activated only when jump key is not pressed. | |
| 692 this.tRex.setSpeedDrop(); | |
| 693 } else if (!this.tRex.jumping && !this.tRex.ducking) { | |
| 694 // Duck. | |
| 695 this.tRex.setDuck(true); | |
| 696 } | |
| 697 } | 694 } |
| 698 }, | 695 }, |
| 699 | 696 |
| 700 | 697 |
| 701 /** | 698 /** |
| 702 * Process key up. | 699 * Process key up. |
| 703 * @param {Event} e | 700 * @param {Event} e |
| 704 */ | 701 */ |
| 705 onKeyUp: function(e) { | 702 onKeyUp: function(e) { |
| 706 var keyCode = String(e.keyCode); | 703 var keyCode = String(e.keyCode); |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 805 this.time = getTimeStamp(); | 802 this.time = getTimeStamp(); |
| 806 this.update(); | 803 this.update(); |
| 807 } | 804 } |
| 808 }, | 805 }, |
| 809 | 806 |
| 810 restart: function() { | 807 restart: function() { |
| 811 if (!this.raqId) { | 808 if (!this.raqId) { |
| 812 this.playCount++; | 809 this.playCount++; |
| 813 this.runningTime = 0; | 810 this.runningTime = 0; |
| 814 this.playing = true; | 811 this.playing = true; |
| 812 this.paused = false; |
| 815 this.crashed = false; | 813 this.crashed = false; |
| 816 this.distanceRan = 0; | 814 this.distanceRan = 0; |
| 817 this.setSpeed(this.config.SPEED); | 815 this.setSpeed(this.config.SPEED); |
| 818 this.time = getTimeStamp(); | 816 this.time = getTimeStamp(); |
| 819 this.containerEl.classList.remove(Runner.classes.CRASHED); | 817 this.containerEl.classList.remove(Runner.classes.CRASHED); |
| 820 this.clearCanvas(); | 818 this.clearCanvas(); |
| 821 this.distanceMeter.reset(this.highestScore); | 819 this.distanceMeter.reset(this.highestScore); |
| 822 this.horizon.reset(); | 820 this.horizon.reset(); |
| 823 this.tRex.reset(); | 821 this.tRex.reset(); |
| 824 this.playSound(this.soundFx.BUTTON_PRESS); | 822 this.playSound(this.soundFx.BUTTON_PRESS); |
| (...skipping 1866 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2691 | 2689 |
| 2692 /** | 2690 /** |
| 2693 * Add a new cloud to the horizon. | 2691 * Add a new cloud to the horizon. |
| 2694 */ | 2692 */ |
| 2695 addCloud: function() { | 2693 addCloud: function() { |
| 2696 this.clouds.push(new Cloud(this.canvas, this.spritePos.CLOUD, | 2694 this.clouds.push(new Cloud(this.canvas, this.spritePos.CLOUD, |
| 2697 this.dimensions.WIDTH)); | 2695 this.dimensions.WIDTH)); |
| 2698 } | 2696 } |
| 2699 }; | 2697 }; |
| 2700 })(); | 2698 })(); |
| OLD | NEW |