Chromium Code Reviews| 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) { |
|
mmenke
2017/03/21 18:59:58
Removing the e.target != this.detailsButton doesn'
edwardjung
2017/03/23 10:31:46
In the past yes, but we no longer have the details
| |
| 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 // Starting the game for the first time. | |
|
mmenke
2017/03/21 18:59:58
One more question: Should this also call e.preven
edwardjung
2017/03/23 10:31:46
Good point. The vast majority of the time the page
| |
| 667 if (!this.playing) { | 667 if (!this.playing) { |
| 668 this.loadSounds(); | 668 this.loadSounds(); |
| 669 this.playing = true; | 669 this.playing = true; |
| 670 this.update(); | 670 this.update(); |
| 671 if (window.errorPageController) { | 671 if (window.errorPageController) { |
| 672 errorPageController.trackEasterEgg(); | 672 errorPageController.trackEasterEgg(); |
| 673 } | 673 } |
| 674 } | 674 } |
| 675 // Play sound effect and jump on starting the game for the first time. | 675 // Start jump. |
| 676 if (!this.tRex.jumping && !this.tRex.ducking) { | 676 if (!this.tRex.jumping && !this.tRex.ducking) { |
| 677 this.playSound(this.soundFx.BUTTON_PRESS); | 677 this.playSound(this.soundFx.BUTTON_PRESS); |
| 678 this.tRex.startJump(this.currentSpeed); | 678 this.tRex.startJump(this.currentSpeed); |
| 679 } | 679 } |
| 680 } else if (this.playing && Runner.keycodes.DUCK[e.keyCode]) { | |
| 681 e.preventDefault(); | |
| 682 if (this.tRex.jumping) { | |
| 683 // Speed drop, activated only when jump key is not pressed. | |
| 684 this.tRex.setSpeedDrop(); | |
| 685 } else if (!this.tRex.jumping && !this.tRex.ducking) { | |
| 686 // Duck. | |
| 687 this.tRex.setDuck(true); | |
| 688 } | |
| 680 } | 689 } |
| 681 | 690 } else if (this.crashed && e.type == Runner.events.TOUCHSTART && |
| 682 if (this.crashed && e.type == Runner.events.TOUCHSTART && | 691 e.currentTarget == this.containerEl) { |
|
mmenke
2017/03/21 18:59:59
nit: Fix indentation (Align after (, or use 4-spa
edwardjung
2017/03/23 10:31:46
Done.
| |
| 683 e.currentTarget == this.containerEl) { | 692 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 } | 693 } |
| 698 }, | 694 }, |
| 699 | 695 |
| 700 | 696 |
| 701 /** | 697 /** |
| 702 * Process key up. | 698 * Process key up. |
| 703 * @param {Event} e | 699 * @param {Event} e |
| 704 */ | 700 */ |
| 705 onKeyUp: function(e) { | 701 onKeyUp: function(e) { |
| 706 var keyCode = String(e.keyCode); | 702 var keyCode = String(e.keyCode); |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 805 this.time = getTimeStamp(); | 801 this.time = getTimeStamp(); |
| 806 this.update(); | 802 this.update(); |
| 807 } | 803 } |
| 808 }, | 804 }, |
| 809 | 805 |
| 810 restart: function() { | 806 restart: function() { |
| 811 if (!this.raqId) { | 807 if (!this.raqId) { |
| 812 this.playCount++; | 808 this.playCount++; |
| 813 this.runningTime = 0; | 809 this.runningTime = 0; |
| 814 this.playing = true; | 810 this.playing = true; |
| 811 this.paused = false; | |
| 815 this.crashed = false; | 812 this.crashed = false; |
| 816 this.distanceRan = 0; | 813 this.distanceRan = 0; |
| 817 this.setSpeed(this.config.SPEED); | 814 this.setSpeed(this.config.SPEED); |
| 818 this.time = getTimeStamp(); | 815 this.time = getTimeStamp(); |
| 819 this.containerEl.classList.remove(Runner.classes.CRASHED); | 816 this.containerEl.classList.remove(Runner.classes.CRASHED); |
| 820 this.clearCanvas(); | 817 this.clearCanvas(); |
| 821 this.distanceMeter.reset(this.highestScore); | 818 this.distanceMeter.reset(this.highestScore); |
| 822 this.horizon.reset(); | 819 this.horizon.reset(); |
| 823 this.tRex.reset(); | 820 this.tRex.reset(); |
| 824 this.playSound(this.soundFx.BUTTON_PRESS); | 821 this.playSound(this.soundFx.BUTTON_PRESS); |
| (...skipping 1866 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2691 | 2688 |
| 2692 /** | 2689 /** |
| 2693 * Add a new cloud to the horizon. | 2690 * Add a new cloud to the horizon. |
| 2694 */ | 2691 */ |
| 2695 addCloud: function() { | 2692 addCloud: function() { |
| 2696 this.clouds.push(new Cloud(this.canvas, this.spritePos.CLOUD, | 2693 this.clouds.push(new Cloud(this.canvas, this.spritePos.CLOUD, |
| 2697 this.dimensions.WIDTH)); | 2694 this.dimensions.WIDTH)); |
| 2698 } | 2695 } |
| 2699 }; | 2696 }; |
| 2700 })(); | 2697 })(); |
| OLD | NEW |