/g,_=/>/g,m=RegExp(`>|${d}(?:([^\\s"'>=/]+)(${d}*=${d}*(?:[^ \t\n\f\r"'\`<>=]|("|')|))|$)`,"g"),p=/'/g,g=/"/g,$=/^(?:script|style|textarea|title)$/i,y=t=>(i,...s)=>({_$litType$:t,strings:i,values:s}),x=y(1),w=Symbol.for("lit-noChange"),T=Symbol.for("lit-nothing"),A=new WeakMap,E=r$2.createTreeWalker(r$2,129);function C(t,i){if(!Array.isArray(t)||!t.hasOwnProperty("raw"))throw Error("invalid template strings array");return void 0!==s$2?s$2.createHTML(i):i}const P=(t,i)=>{const s=t.length-1,o=[];let r,l=2===i?"":"")),o]};class V{constructor({strings:t,_$litType$:s},n){let r;this.parts=[];let c=0,a=0;const u=t.length-1,d=this.parts,[f,v]=P(t,s);if(this.el=V.createElement(f,n),E.currentNode=this.el.content,2===s){const t=this.el.content.firstChild;t.replaceWith(...t.childNodes);}for(;null!==(r=E.nextNode())&&d.length0){r.textContent=i$1?i$1.emptyScript:"";for(let i=0;i2||""!==s[0]||""!==s[1]?(this._$AH=Array(s.length-1).fill(new String),this.strings=s):this._$AH=T;}_$AI(t,i=this,s,e){const h=this.strings;let o=!1;if(void 0===h)t=N(this,t,i,0),o=!c$1(t)||t!==this._$AH&&t!==w,o&&(this._$AH=t);else {const e=t;let n,r;for(t=h[0],n=0;n{const e=s?.renderBefore??i;let h=e._$litPart$;if(void 0===h){const t=s?.renderBefore??null;e._$litPart$=h=new M(i.insertBefore(l(),t),t,void 0,s??{});}return h._$AI(t),h
/**
* @license
* Copyright 2017 Google LLC
* SPDX-License-Identifier: BSD-3-Clause
*/};let s$1=class s extends b{constructor(){super(...arguments),this.renderOptions={host:this},this._$Do=void 0;}createRenderRoot(){const t=super.createRenderRoot();return this.renderOptions.renderBefore??=t.firstChild,t}update(t){const i=this.render();this.hasUpdated||(this.renderOptions.isConnected=this.isConnected),super.update(t),this._$Do=j(i,this.renderRoot,this.renderOptions);}connectedCallback(){super.connectedCallback(),this._$Do?.setConnected(!0);}disconnectedCallback(){super.disconnectedCallback(),this._$Do?.setConnected(!1);}render(){return w}};s$1._$litElement$=!0,s$1["finalized"]=!0,globalThis.litElementHydrateSupport?.({LitElement:s$1});const r$1=globalThis.litElementPolyfillSupport;r$1?.({LitElement:s$1});(globalThis.litElementVersions??=[]).push("4.0.2");
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
const HIDDEN_CLASS = 'hidden';
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
const IS_IOS = /CriOS/.test(window.navigator.userAgent);
const IS_HIDPI = window.devicePixelRatio > 1;
const IS_MOBILE = /Android/.test(window.navigator.userAgent) || IS_IOS;
const IS_RTL = document.documentElement.dir === 'rtl';
/**
* Frames per second.
* @const
*/
const FPS = 60;
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* Get random number.
* @param {number} min
* @param {number} max
*/
function getRandomNum(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
/**
* Return the current timestamp.
* @return {number}
*/
function getTimeStamp() {
return IS_IOS ? new Date().getTime() : performance.now();
}
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
class DistanceMeter {
/**
* Handles displaying the distance meter.
* @param {!HTMLCanvasElement} canvas
* @param {Object} spritePos Image position in sprite.
* @param {number} canvasWidth
*/
constructor(canvas, spritePos, canvasWidth) {
this.canvas = canvas;
this.canvasCtx =
/** @type {CanvasRenderingContext2D} */ (canvas.getContext('2d'));
this.image = Runner.imageSprite;
this.spritePos = spritePos;
this.x = 0;
this.y = 5;
this.currentDistance = 0;
this.maxScore = 0;
this.highScore = '0';
this.container = null;
this.digits = [];
this.achievement = false;
this.defaultString = '';
this.flashTimer = 0;
this.flashIterations = 0;
this.invertTrigger = false;
this.flashingRafId = null;
this.highScoreBounds = {};
this.highScoreFlashing = false;
this.config = DistanceMeter.config;
this.maxScoreUnits = this.config.MAX_DISTANCE_UNITS;
this.canvasWidth = canvasWidth;
this.init(canvasWidth);
}
/**
* Initialise the distance meter to '00000'.
* @param {number} width Canvas width in px.
*/
init(width) {
let maxDistanceStr = '';
this.calcXPos(width);
this.maxScore = this.maxScoreUnits;
for (let i = 0; i < this.maxScoreUnits; i++) {
this.draw(i, 0);
this.defaultString += '0';
maxDistanceStr += '9';
}
this.maxScore = parseInt(maxDistanceStr, 10);
}
/**
* Calculate the xPos in the canvas.
* @param {number} canvasWidth
*/
calcXPos(canvasWidth) {
this.x = canvasWidth -
(DistanceMeter.dimensions.DEST_WIDTH * (this.maxScoreUnits + 1));
}
/**
* Draw a digit to canvas.
* @param {number} digitPos Position of the digit.
* @param {number} value Digit value 0-9.
* @param {boolean=} opt_highScore Whether drawing the high score.
*/
draw(digitPos, value, opt_highScore) {
let sourceWidth = DistanceMeter.dimensions.WIDTH;
let sourceHeight = DistanceMeter.dimensions.HEIGHT;
let sourceX = DistanceMeter.dimensions.WIDTH * value;
let sourceY = 0;
const targetX = digitPos * DistanceMeter.dimensions.DEST_WIDTH;
const targetY = this.y;
const targetWidth = DistanceMeter.dimensions.WIDTH;
const targetHeight = DistanceMeter.dimensions.HEIGHT;
// For high DPI we 2x source values.
if (IS_HIDPI) {
sourceWidth *= 2;
sourceHeight *= 2;
sourceX *= 2;
}
sourceX += this.spritePos.x;
sourceY += this.spritePos.y;
this.canvasCtx.save();
if (IS_RTL) {
if (opt_highScore) {
this.canvasCtx.translate(this.canvasWidth -
(DistanceMeter.dimensions.WIDTH * (this.maxScoreUnits + 3)), this.y);
}
else {
this.canvasCtx.translate(this.canvasWidth - DistanceMeter.dimensions.WIDTH, this.y);
}
this.canvasCtx.scale(-1, 1);
}
else {
const highScoreX = this.x - (this.maxScoreUnits * 2) * DistanceMeter.dimensions.WIDTH;
if (opt_highScore) {
this.canvasCtx.translate(highScoreX, this.y);
}
else {
this.canvasCtx.translate(this.x, this.y);
}
}
this.canvasCtx.drawImage(this.image, sourceX, sourceY, sourceWidth, sourceHeight, targetX, targetY, targetWidth, targetHeight);
this.canvasCtx.restore();
}
/**
* Covert pixel distance to a 'real' distance.
* @param {number} distance Pixel distance ran.
* @return {number} The 'real' distance ran.
*/
getActualDistance(distance) {
return distance ? Math.round(distance * this.config.COEFFICIENT) : 0;
}
/**
* Update the distance meter.
* @param {number} distance
* @param {number} deltaTime
* @return {boolean} Whether the achievement sound fx should be played.
*/
update(deltaTime, distance) {
let paint = true;
let playSound = false;
if (!this.achievement) {
distance = this.getActualDistance(distance);
// Score has gone beyond the initial digit count.
if (distance > this.maxScore &&
this.maxScoreUnits === this.config.MAX_DISTANCE_UNITS) {
this.maxScoreUnits++;
this.maxScore = parseInt(this.maxScore + '9', 10);
}
else {
this.distance = 0;
}
if (distance > 0) {
// Achievement unlocked.
if (distance % this.config.ACHIEVEMENT_DISTANCE === 0) {
// Flash score and play sound.
this.achievement = true;
this.flashTimer = 0;
playSound = true;
}
// Create a string representation of the distance with leading 0.
const distanceStr = (this.defaultString + distance).substr(-this.maxScoreUnits);
this.digits = distanceStr.split('');
}
else {
this.digits = this.defaultString.split('');
}
}
else {
// Control flashing of the score on reaching achievement.
if (this.flashIterations <= this.config.FLASH_ITERATIONS) {
this.flashTimer += deltaTime;
if (this.flashTimer < this.config.FLASH_DURATION) {
paint = false;
}
else if (this.flashTimer > this.config.FLASH_DURATION * 2) {
this.flashTimer = 0;
this.flashIterations++;
}
}
else {
this.achievement = false;
this.flashIterations = 0;
this.flashTimer = 0;
}
}
// Draw the digits if not flashing.
if (paint) {
for (let i = this.digits.length - 1; i >= 0; i--) {
this.draw(i, parseInt(this.digits[i], 10));
}
}
this.drawHighScore();
return playSound;
}
/**
* Draw the high score.
*/
drawHighScore() {
if (parseInt(this.highScore, 10) > 0) {
this.canvasCtx.save();
this.canvasCtx.globalAlpha = .8;
for (let i = this.highScore.length - 1; i >= 0; i--) {
this.draw(i, parseInt(this.highScore[i], 10), true);
}
this.canvasCtx.restore();
}
}
/**
* Set the highscore as a array string.
* Position of char in the sprite: H - 10, I - 11.
* @param {number} distance Distance ran in pixels.
*/
setHighScore(distance) {
distance = this.getActualDistance(distance);
const highScoreStr = (this.defaultString + distance).substr(-this.maxScoreUnits);
this.highScore = ['10', '11', ''].concat(highScoreStr.split(''));
}
/**
* Whether a clicked is in the high score area.
* @param {Event} e Event object.
* @return {boolean} Whether the click was in the high score bounds.
*/
hasClickedOnHighScore(e) {
let x = 0;
let y = 0;
if (e.touches) {
// Bounds for touch differ from pointer.
const canvasBounds = this.canvas.getBoundingClientRect();
x = e.touches[0].clientX - canvasBounds.left;
y = e.touches[0].clientY - canvasBounds.top;
}
else {
x = e.offsetX;
y = e.offsetY;
}
this.highScoreBounds = this.getHighScoreBounds();
return x >= this.highScoreBounds.x &&
x <= this.highScoreBounds.x + this.highScoreBounds.width &&
y >= this.highScoreBounds.y &&
y <= this.highScoreBounds.y + this.highScoreBounds.height;
}
/**
* Get the bounding box for the high score.
* @return {Object} Object with x, y, width and height properties.
*/
getHighScoreBounds() {
return {
x: (this.x - (this.maxScoreUnits * 2) * DistanceMeter.dimensions.WIDTH) -
DistanceMeter.config.HIGH_SCORE_HIT_AREA_PADDING,
y: this.y,
width: DistanceMeter.dimensions.WIDTH * (this.highScore.length + 1) +
DistanceMeter.config.HIGH_SCORE_HIT_AREA_PADDING,
height: DistanceMeter.dimensions.HEIGHT +
(DistanceMeter.config.HIGH_SCORE_HIT_AREA_PADDING * 2),
};
}
/**
* Animate flashing the high score to indicate ready for resetting.
* The flashing stops following this.config.FLASH_ITERATIONS x 2 flashes.
*/
flashHighScore() {
const now = getTimeStamp();
const deltaTime = now - (this.frameTimeStamp || now);
let paint = true;
this.frameTimeStamp = now;
// Reached the max number of flashes.
if (this.flashIterations > this.config.FLASH_ITERATIONS * 2) {
this.cancelHighScoreFlashing();
return;
}
this.flashTimer += deltaTime;
if (this.flashTimer < this.config.FLASH_DURATION) {
paint = false;
}
else if (this.flashTimer > this.config.FLASH_DURATION * 2) {
this.flashTimer = 0;
this.flashIterations++;
}
if (paint) {
this.drawHighScore();
}
else {
this.clearHighScoreBounds();
}
// Frame update.
this.flashingRafId = requestAnimationFrame(this.flashHighScore.bind(this));
}
/**
* Draw empty rectangle over high score.
*/
clearHighScoreBounds() {
this.canvasCtx.save();
this.canvasCtx.fillStyle = '#fff';
this.canvasCtx.rect(this.highScoreBounds.x, this.highScoreBounds.y, this.highScoreBounds.width, this.highScoreBounds.height);
this.canvasCtx.fill();
this.canvasCtx.restore();
}
/**
* Starts the flashing of the high score.
*/
startHighScoreFlashing() {
this.highScoreFlashing = true;
this.flashHighScore();
}
/**
* Whether high score is flashing.
* @return {boolean}
*/
isHighScoreFlashing() {
return this.highScoreFlashing;
}
/**
* Stop flashing the high score.
*/
cancelHighScoreFlashing() {
if (this.flashingRafId) {
cancelAnimationFrame(this.flashingRafId);
}
this.flashIterations = 0;
this.flashTimer = 0;
this.highScoreFlashing = false;
this.clearHighScoreBounds();
this.drawHighScore();
}
/**
* Clear the high score.
*/
resetHighScore() {
this.setHighScore(0);
this.cancelHighScoreFlashing();
}
/**
* Reset the distance meter back to '00000'.
*/
reset() {
this.update(0, 0);
this.achievement = false;
}
}
/**
* @enum {number}
*/
DistanceMeter.dimensions = {
WIDTH: 10,
HEIGHT: 13,
DEST_WIDTH: 11,
};
/**
* Y positioning of the digits in the sprite sheet.
* X position is always 0.
* @type {Array}
*/
DistanceMeter.yPos = [0, 13, 27, 40, 53, 67, 80, 93, 107, 120];
/**
* Distance meter config.
* @enum {number}
*/
DistanceMeter.config = {
// Number of digits.
MAX_DISTANCE_UNITS: 5,
// Distance that causes achievement animation.
ACHIEVEMENT_DISTANCE: 100,
// Used for conversion from pixel distance to a scaled unit.
COEFFICIENT: 0.025,
// Flash duration in milliseconds.
FLASH_DURATION: 1000 / 4,
// Flash iterations for achievement animation.
FLASH_ITERATIONS: 3,
// Padding around the high score hit area.
HIGH_SCORE_HIT_AREA_PADDING: 4,
};
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/* @const
* Add matching sprite definition and config to spriteDefinitionByType.
*/
const GAME_TYPE = [];
//******************************************************************************
/**
* Collision box object.
* @param {number} x X position.
* @param {number} y Y Position.
* @param {number} w Width.
* @param {number} h Height.
* @constructor
*/
function CollisionBox(x, y, w, h) {
this.x = x;
this.y = y;
this.width = w;
this.height = h;
}
/**
* T-Rex runner sprite definitions.
*/
const spriteDefinitionByType = {
original: {
LDPI: {
BACKGROUND_EL: { x: 86, y: 2 },
CACTUS_LARGE: { x: 332, y: 2 },
CACTUS_SMALL: { x: 228, y: 2 },
OBSTACLE_2: { x: 332, y: 2 },
OBSTACLE: { 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: 68 },
TEXT_SPRITE: { x: 655, y: 2 },
TREX: { x: 848, y: 2 },
STAR: { x: 645, y: 2 },
COLLECTABLE: { x: 0, y: 0 },
ALT_GAME_END: { x: 32, y: 0 },
},
HDPI: {
BACKGROUND_EL: { x: 166, y: 2 },
CACTUS_LARGE: { x: 652, y: 2 },
CACTUS_SMALL: { x: 446, y: 2 },
OBSTACLE_2: { x: 652, y: 2 },
OBSTACLE: { 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: 130 },
TEXT_SPRITE: { x: 1294, y: 2 },
TREX: { x: 1678, y: 2 },
STAR: { x: 1276, y: 2 },
COLLECTABLE: { x: 0, y: 0 },
ALT_GAME_END: { x: 64, y: 0 },
},
MAX_GAP_COEFFICIENT: 1.5,
MAX_OBSTACLE_LENGTH: 3,
HAS_CLOUDS: 1,
BOTTOM_PAD: 10,
TREX: {
WAITING_1: { x: 44, w: 44, h: 47, xOffset: 0 },
WAITING_2: { x: 0, w: 44, h: 47, xOffset: 0 },
RUNNING_1: { x: 88, w: 44, h: 47, xOffset: 0 },
RUNNING_2: { x: 132, w: 44, h: 47, xOffset: 0 },
JUMPING: { x: 0, w: 44, h: 47, xOffset: 0 },
CRASHED: { x: 220, w: 44, h: 47, xOffset: 0 },
COLLISION_BOXES: [
new CollisionBox(22, 0, 17, 16),
new CollisionBox(1, 18, 30, 9),
new CollisionBox(10, 35, 14, 8),
new CollisionBox(1, 24, 29, 5),
new CollisionBox(5, 30, 21, 4),
new CollisionBox(9, 34, 15, 4),
],
},
/** @type {Array} */
OBSTACLES: [
{
type: 'CACTUS_SMALL',
width: 17,
height: 35,
yPos: 105,
multipleSpeed: 4,
minGap: 120,
minSpeed: 0,
collisionBoxes: [
new CollisionBox(0, 7, 5, 27),
new CollisionBox(4, 0, 6, 34),
new CollisionBox(10, 4, 7, 14),
],
},
{
type: 'CACTUS_LARGE',
width: 25,
height: 50,
yPos: 90,
multipleSpeed: 7,
minGap: 120,
minSpeed: 0,
collisionBoxes: [
new CollisionBox(0, 12, 7, 38),
new CollisionBox(8, 0, 7, 49),
new CollisionBox(13, 10, 10, 38),
],
},
{
type: 'PTERODACTYL',
width: 46,
height: 40,
yPos: [100, 75, 50], // Variable height.
yPosMobile: [100, 50], // Variable height mobile.
multipleSpeed: 999,
minSpeed: 8.5,
minGap: 150,
collisionBoxes: [
new CollisionBox(15, 15, 16, 5),
new CollisionBox(18, 21, 24, 6),
new CollisionBox(2, 14, 4, 3),
new CollisionBox(6, 10, 4, 7),
new CollisionBox(10, 8, 6, 9),
],
numFrames: 2,
frameRate: 1000 / 6,
speedOffset: .8,
},
{
type: 'COLLECTABLE',
width: 31,
height: 24,
yPos: 104,
multipleSpeed: 1000,
minGap: 9999,
minSpeed: 0,
collisionBoxes: [
new CollisionBox(0, 0, 32, 25),
],
},
],
BACKGROUND_EL: {
'CLOUD': {
HEIGHT: 14,
MAX_CLOUD_GAP: 400,
MAX_SKY_LEVEL: 30,
MIN_CLOUD_GAP: 100,
MIN_SKY_LEVEL: 71,
OFFSET: 4,
WIDTH: 46,
X_POS: 1,
Y_POS: 120,
},
},
BACKGROUND_EL_CONFIG: {
MAX_BG_ELS: 1,
MAX_GAP: 400,
MIN_GAP: 100,
POS: 0,
SPEED: 0.5,
Y_POS: 125,
},
LINES: [
{ SOURCE_X: 2, SOURCE_Y: 52, WIDTH: 600, HEIGHT: 12, YPOS: 127 },
],
ALT_GAME_OVER_TEXT_CONFIG: {
TEXT_X: 32,
TEXT_Y: 0,
TEXT_WIDTH: 246,
TEXT_HEIGHT: 17,
FLASH_DURATION: 1500,
FLASHING: false,
},
},
};
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
class Trex {
/**
* T-rex game character.
* @param {HTMLCanvasElement} canvas
* @param {Object} spritePos Positioning within image sprite.
*/
constructor(canvas, spritePos) {
this.canvas = canvas;
this.canvasCtx =
/** @type {CanvasRenderingContext2D} */ (canvas.getContext('2d'));
this.spritePos = spritePos;
this.xPos = 0;
this.yPos = 0;
this.xInitialPos = 0;
// Position when on the ground.
this.groundYPos = 0;
this.currentFrame = 0;
this.currentAnimFrames = [];
this.blinkDelay = 0;
this.blinkCount = 0;
this.animStartTime = 0;
this.timer = 0;
this.msPerFrame = 1000 / FPS;
this.config = Object.assign(Trex.config, Trex.normalJumpConfig);
// Current status.
this.status = Trex.status.WAITING;
this.jumping = false;
this.ducking = false;
this.jumpVelocity = 0;
this.reachedMinHeight = false;
this.speedDrop = false;
this.jumpCount = 0;
this.jumpspotX = 0;
this.altGameModeEnabled = false;
this.flashing = false;
this.init();
}
/**
* T-rex player initialiser.
* Sets the t-rex to blink at random intervals.
*/
init() {
this.groundYPos = Runner.defaultDimensions.HEIGHT - this.config.HEIGHT -
Runner.config.BOTTOM_PAD;
this.yPos = this.groundYPos;
this.minJumpHeight = this.groundYPos - this.config.MIN_JUMP_HEIGHT;
this.draw(0, 0);
this.update(0, Trex.status.WAITING);
}
/**
* Assign the appropriate jump parameters based on the game speed.
*/
enableSlowConfig() {
const jumpConfig = Runner.slowDown ? Trex.slowJumpConfig : Trex.normalJumpConfig;
Trex.config = Object.assign(Trex.config, jumpConfig);
this.adjustAltGameConfigForSlowSpeed();
}
/**
* Enables the alternative game. Redefines the dino config.
* @param {Object} spritePos New positioning within image sprite.
*/
enableAltGameMode(spritePos) {
this.altGameModeEnabled = true;
this.spritePos = spritePos;
const spriteDefinition = Runner.spriteDefinition['TREX'];
// Update animation frames.
Trex.animFrames.RUNNING.frames =
[spriteDefinition.RUNNING_1.x, spriteDefinition.RUNNING_2.x];
Trex.animFrames.CRASHED.frames = [spriteDefinition.CRASHED.x];
if (typeof spriteDefinition.JUMPING.x === 'object') {
Trex.animFrames.JUMPING.frames = spriteDefinition.JUMPING.x;
}
else {
Trex.animFrames.JUMPING.frames = [spriteDefinition.JUMPING.x];
}
Trex.animFrames.DUCKING.frames =
[spriteDefinition.DUCKING_1.x, spriteDefinition.DUCKING_2.x];
// Update Trex config
Trex.config.GRAVITY = spriteDefinition.GRAVITY || Trex.config.GRAVITY;
Trex.config.HEIGHT = spriteDefinition.RUNNING_1.h,
Trex.config.INITIAL_JUMP_VELOCITY = spriteDefinition.INITIAL_JUMP_VELOCITY;
Trex.config.MAX_JUMP_HEIGHT = spriteDefinition.MAX_JUMP_HEIGHT;
Trex.config.MIN_JUMP_HEIGHT = spriteDefinition.MIN_JUMP_HEIGHT;
Trex.config.WIDTH = spriteDefinition.RUNNING_1.w;
Trex.config.WIDTH_CRASHED = spriteDefinition.CRASHED.w;
Trex.config.WIDTH_JUMP = spriteDefinition.JUMPING.w;
Trex.config.INVERT_JUMP = spriteDefinition.INVERT_JUMP;
this.adjustAltGameConfigForSlowSpeed(spriteDefinition.GRAVITY);
this.config = Trex.config;
// Adjust bottom horizon placement.
this.groundYPos = Runner.defaultDimensions.HEIGHT - this.config.HEIGHT -
Runner.spriteDefinition['BOTTOM_PAD'];
this.yPos = this.groundYPos;
this.reset();
}
/**
* Slow speeds adjustments for the alt game modes.
* @param {number=} opt_gravityValue
*/
adjustAltGameConfigForSlowSpeed(opt_gravityValue) {
if (Runner.slowDown) {
if (opt_gravityValue) {
Trex.config.GRAVITY = opt_gravityValue / 1.5;
}
Trex.config.MIN_JUMP_HEIGHT *= 1.5;
Trex.config.MAX_JUMP_HEIGHT *= 1.5;
Trex.config.INITIAL_JUMP_VELOCITY =
Trex.config.INITIAL_JUMP_VELOCITY * 1.5;
}
}
/**
* Setter whether dino is flashing.
* @param {boolean} status
*/
setFlashing(status) {
this.flashing = status;
}
/**
* Setter for the jump velocity.
* The appropriate drop velocity is also set.
* @param {number} setting
*/
setJumpVelocity(setting) {
this.config.INITIAL_JUMP_VELOCITY = -setting;
this.config.DROP_VELOCITY = -setting / 2;
}
/**
* Set the animation status.
* @param {!number} deltaTime
* @param {Trex.status=} opt_status Optional status to switch to.
*/
update(deltaTime, opt_status) {
this.timer += deltaTime;
// Update the status.
if (opt_status) {
this.status = opt_status;
this.currentFrame = 0;
this.msPerFrame = Trex.animFrames[opt_status].msPerFrame;
this.currentAnimFrames = Trex.animFrames[opt_status].frames;
if (opt_status === Trex.status.WAITING) {
this.animStartTime = getTimeStamp();
this.setBlinkDelay();
}
}
// Game intro animation, T-rex moves in from the left.
if (this.playingIntro && this.xPos < this.config.START_X_POS) {
this.xPos += Math.round((this.config.START_X_POS / this.config.INTRO_DURATION) * deltaTime);
this.xInitialPos = this.xPos;
}
if (this.status === Trex.status.WAITING) {
this.blink(getTimeStamp());
}
else {
this.draw(this.currentAnimFrames[this.currentFrame], 0);
}
// Update the frame position.
if (!this.flashing && this.timer >= this.msPerFrame) {
this.currentFrame =
this.currentFrame === this.currentAnimFrames.length - 1 ?
0 :
this.currentFrame + 1;
this.timer = 0;
}
// Speed drop becomes duck if the down key is still being pressed.
if (this.speedDrop && this.yPos === this.groundYPos) {
this.speedDrop = false;
this.setDuck(true);
}
}
/**
* Draw the t-rex to a particular position.
* @param {number} x
* @param {number} y
*/
draw(x, y) {
let sourceX = x;
let sourceY = y;
let sourceWidth = this.ducking && this.status !== Trex.status.CRASHED ?
this.config.WIDTH_DUCK :
this.config.WIDTH;
let sourceHeight = this.config.HEIGHT;
const outputHeight = sourceHeight;
const outputWidth = this.altGameModeEnabled && this.status === Trex.status.CRASHED ?
this.config.WIDTH_CRASHED :
this.config.WIDTH;
let jumpOffset = Runner.spriteDefinition.TREX.JUMPING.xOffset;
// Width of sprite can change on jump or crashed.
if (this.altGameModeEnabled) {
if (this.jumping && this.status !== Trex.status.CRASHED) {
sourceWidth = this.config.WIDTH_JUMP;
}
else if (this.status === Trex.status.CRASHED) {
sourceWidth = this.config.WIDTH_CRASHED;
}
}
if (IS_HIDPI) {
sourceX *= 2;
sourceY *= 2;
sourceWidth *= 2;
sourceHeight *= 2;
jumpOffset *= 2;
}
// Adjustments for sprite sheet position.
sourceX += this.spritePos.x;
sourceY += this.spritePos.y;
// Flashing.
if (this.flashing) {
if (this.timer < this.config.FLASH_ON) {
this.canvasCtx.globalAlpha = 0.5;
}
else if (this.timer > this.config.FLASH_OFF) {
this.timer = 0;
}
}
// Ducking.
if (this.ducking && this.status !== Trex.status.CRASHED) {
this.canvasCtx.drawImage(Runner.imageSprite, sourceX, sourceY, sourceWidth, sourceHeight, this.xPos, this.yPos, this.config.WIDTH_DUCK, outputHeight);
}
else if (this.altGameModeEnabled && this.jumping &&
this.status !== Trex.status.CRASHED) {
// Jumping with adjustments.
this.canvasCtx.drawImage(Runner.imageSprite, sourceX, sourceY, sourceWidth, sourceHeight, this.xPos - jumpOffset, this.yPos, this.config.WIDTH_JUMP, outputHeight);
}
else {
// Crashed whilst ducking. Trex is standing up so needs adjustment.
if (this.ducking && this.status === Trex.status.CRASHED) {
this.xPos++;
}
// Standing / running
this.canvasCtx.drawImage(Runner.imageSprite, sourceX, sourceY, sourceWidth, sourceHeight, this.xPos, this.yPos, outputWidth, outputHeight);
}
this.canvasCtx.globalAlpha = 1;
}
/**
* Sets a random time for the blink to happen.
*/
setBlinkDelay() {
this.blinkDelay = Math.ceil(Math.random() * Trex.BLINK_TIMING);
}
/**
* Make t-rex blink at random intervals.
* @param {number} time Current time in milliseconds.
*/
blink(time) {
const deltaTime = time - this.animStartTime;
if (deltaTime >= this.blinkDelay) {
this.draw(this.currentAnimFrames[this.currentFrame], 0);
if (this.currentFrame === 1) {
// Set new random delay to blink.
this.setBlinkDelay();
this.animStartTime = time;
this.blinkCount++;
}
}
}
/**
* Initialise a jump.
* @param {number} speed
*/
startJump(speed) {
if (!this.jumping) {
this.update(0, Trex.status.JUMPING);
// Tweak the jump velocity based on the speed.
this.jumpVelocity = this.config.INITIAL_JUMP_VELOCITY - (speed / 10);
this.jumping = true;
this.reachedMinHeight = false;
this.speedDrop = false;
if (this.config.INVERT_JUMP) {
this.minJumpHeight = this.groundYPos + this.config.MIN_JUMP_HEIGHT;
}
}
}
/**
* Jump is complete, falling down.
*/
endJump() {
if (this.reachedMinHeight &&
this.jumpVelocity < this.config.DROP_VELOCITY) {
this.jumpVelocity = this.config.DROP_VELOCITY;
}
}
/**
* Update frame for a jump.
* @param {number} deltaTime
*/
updateJump(deltaTime) {
const msPerFrame = Trex.animFrames[this.status].msPerFrame;
const framesElapsed = deltaTime / msPerFrame;
// Speed drop makes Trex fall faster.
if (this.speedDrop) {
this.yPos += Math.round(this.jumpVelocity * this.config.SPEED_DROP_COEFFICIENT *
framesElapsed);
}
else if (this.config.INVERT_JUMP) {
this.yPos -= Math.round(this.jumpVelocity * framesElapsed);
}
else {
this.yPos += Math.round(this.jumpVelocity * framesElapsed);
}
this.jumpVelocity += this.config.GRAVITY * framesElapsed;
// Minimum height has been reached.
if (this.config.INVERT_JUMP && (this.yPos > this.minJumpHeight) ||
!this.config.INVERT_JUMP && (this.yPos < this.minJumpHeight) ||
this.speedDrop) {
this.reachedMinHeight = true;
}
// Reached max height.
if (this.config.INVERT_JUMP && (this.yPos > -this.config.MAX_JUMP_HEIGHT) ||
!this.config.INVERT_JUMP && (this.yPos < this.config.MAX_JUMP_HEIGHT) ||
this.speedDrop) {
this.endJump();
}
// Back down at ground level. Jump completed.
if ((this.config.INVERT_JUMP && this.yPos) < this.groundYPos ||
(!this.config.INVERT_JUMP && this.yPos) > this.groundYPos) {
this.reset();
this.jumpCount++;
if (Runner.audioCues) {
Runner.generatedSoundFx.loopFootSteps();
}
}
}
/**
* Set the speed drop. Immediately cancels the current jump.
*/
setSpeedDrop() {
this.speedDrop = true;
this.jumpVelocity = 1;
}
/**
* @param {boolean} isDucking
*/
setDuck(isDucking) {
if (isDucking && this.status !== Trex.status.DUCKING) {
this.update(0, Trex.status.DUCKING);
this.ducking = true;
}
else if (this.status === Trex.status.DUCKING) {
this.update(0, Trex.status.RUNNING);
this.ducking = false;
}
}
/**
* Reset the t-rex to running at start of game.
*/
reset() {
this.xPos = this.xInitialPos;
this.yPos = this.groundYPos;
this.jumpVelocity = 0;
this.jumping = false;
this.ducking = false;
this.update(0, Trex.status.RUNNING);
this.midair = false;
this.speedDrop = false;
this.jumpCount = 0;
}
}
/**
* T-rex player config.
*/
Trex.config = {
DROP_VELOCITY: -5,
FLASH_OFF: 175,
FLASH_ON: 100,
HEIGHT: 47,
HEIGHT_DUCK: 25,
INTRO_DURATION: 1500,
SPEED_DROP_COEFFICIENT: 3,
SPRITE_WIDTH: 262,
START_X_POS: 50,
WIDTH: 44,
WIDTH_DUCK: 59,
};
Trex.slowJumpConfig = {
GRAVITY: 0.25,
MAX_JUMP_HEIGHT: 50,
MIN_JUMP_HEIGHT: 45,
INITIAL_JUMP_VELOCITY: -20,
};
Trex.normalJumpConfig = {
GRAVITY: 0.6,
MAX_JUMP_HEIGHT: 30,
MIN_JUMP_HEIGHT: 30,
INITIAL_JUMP_VELOCITY: -10,
};
/**
* Used in collision detection.
* @enum {Array}
*/
Trex.collisionBoxes = {
DUCKING: [new CollisionBox(1, 18, 55, 25)],
RUNNING: [
new CollisionBox(22, 0, 17, 16),
new CollisionBox(1, 18, 30, 9),
new CollisionBox(10, 35, 14, 8),
new CollisionBox(1, 24, 29, 5),
new CollisionBox(5, 30, 21, 4),
new CollisionBox(9, 34, 15, 4),
],
};
/**
* Animation states.
* @enum {string}
*/
Trex.status = {
CRASHED: 'CRASHED',
DUCKING: 'DUCKING',
JUMPING: 'JUMPING',
RUNNING: 'RUNNING',
WAITING: 'WAITING',
};
/**
* Blinking coefficient.
* @const
*/
Trex.BLINK_TIMING = 7000;
/**
* Animation config for different states.
* @enum {Object}
*/
Trex.animFrames = {
WAITING: {
frames: [44, 0],
msPerFrame: 1000 / 3,
},
RUNNING: {
frames: [88, 132],
msPerFrame: 1000 / 12,
},
CRASHED: {
frames: [220],
msPerFrame: 1000 / 60,
},
JUMPING: {
frames: [0],
msPerFrame: 1000 / 60,
},
DUCKING: {
frames: [264, 323],
msPerFrame: 1000 / 8,
},
};
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
class GameOverPanel {
/**
* Game over panel.
* @param {!HTMLCanvasElement} canvas
* @param {Object} textImgPos
* @param {Object} restartImgPos
* @param {!Object} dimensions Canvas dimensions.
* @param {Object=} opt_altGameEndImgPos
* @param {boolean=} opt_altGameActive
*/
constructor(canvas, textImgPos, restartImgPos, dimensions, opt_altGameEndImgPos, opt_altGameActive) {
this.canvas = canvas;
this.canvasCtx =
/** @type {CanvasRenderingContext2D} */ (canvas.getContext('2d'));
this.canvasDimensions = dimensions;
this.textImgPos = textImgPos;
this.restartImgPos = restartImgPos;
this.altGameEndImgPos = opt_altGameEndImgPos;
this.altGameModeActive = opt_altGameActive;
// Retry animation.
this.frameTimeStamp = 0;
this.animTimer = 0;
this.currentFrame = 0;
this.gameOverRafId = null;
this.flashTimer = 0;
this.flashCounter = 0;
this.originalText = true;
}
/**
* Update the panel dimensions.
* @param {number} width New canvas width.
* @param {number} opt_height Optional new canvas height.
*/
updateDimensions(width, opt_height) {
this.canvasDimensions.WIDTH = width;
if (opt_height) {
this.canvasDimensions.HEIGHT = opt_height;
}
this.currentFrame = GameOverPanel.animConfig.frames.length - 1;
}
drawGameOverText(dimensions, opt_useAltText) {
const centerX = this.canvasDimensions.WIDTH / 2;
let textSourceX = dimensions.TEXT_X;
let textSourceY = dimensions.TEXT_Y;
let textSourceWidth = dimensions.TEXT_WIDTH;
let textSourceHeight = dimensions.TEXT_HEIGHT;
const textTargetX = Math.round(centerX - (dimensions.TEXT_WIDTH / 2));
const textTargetY = Math.round((this.canvasDimensions.HEIGHT - 25) / 3);
const textTargetWidth = dimensions.TEXT_WIDTH;
const textTargetHeight = dimensions.TEXT_HEIGHT;
if (IS_HIDPI) {
textSourceY *= 2;
textSourceX *= 2;
textSourceWidth *= 2;
textSourceHeight *= 2;
}
if (!opt_useAltText) {
textSourceX += this.textImgPos.x;
textSourceY += this.textImgPos.y;
}
const spriteSource = opt_useAltText ? Runner.altCommonImageSprite : Runner.origImageSprite;
this.canvasCtx.save();
if (IS_RTL) {
this.canvasCtx.translate(this.canvasDimensions.WIDTH, 0);
this.canvasCtx.scale(-1, 1);
}
// Game over text from sprite.
this.canvasCtx.drawImage(spriteSource, textSourceX, textSourceY, textSourceWidth, textSourceHeight, textTargetX, textTargetY, textTargetWidth, textTargetHeight);
this.canvasCtx.restore();
}
/**
* Draw additional adornments for alternative game types.
*/
drawAltGameElements(tRex) {
// Additional adornments.
if (this.altGameModeActive && Runner.spriteDefinition.ALT_GAME_END_CONFIG) {
const altGameEndConfig = Runner.spriteDefinition.ALT_GAME_END_CONFIG;
let altGameEndSourceWidth = altGameEndConfig.WIDTH;
let altGameEndSourceHeight = altGameEndConfig.HEIGHT;
const altGameEndTargetX = tRex.xPos + altGameEndConfig.X_OFFSET;
const altGameEndTargetY = tRex.yPos + altGameEndConfig.Y_OFFSET;
if (IS_HIDPI) {
altGameEndSourceWidth *= 2;
altGameEndSourceHeight *= 2;
}
this.canvasCtx.drawImage(Runner.altCommonImageSprite, this.altGameEndImgPos.x, this.altGameEndImgPos.y, altGameEndSourceWidth, altGameEndSourceHeight, altGameEndTargetX, altGameEndTargetY, altGameEndConfig.WIDTH, altGameEndConfig.HEIGHT);
}
}
/**
* Draw restart button.
*/
drawRestartButton() {
const dimensions = GameOverPanel.dimensions;
let framePosX = GameOverPanel.animConfig.frames[this.currentFrame];
let restartSourceWidth = dimensions.RESTART_WIDTH;
let restartSourceHeight = dimensions.RESTART_HEIGHT;
const restartTargetX = (this.canvasDimensions.WIDTH / 2) - (dimensions.RESTART_WIDTH / 2);
const restartTargetY = this.canvasDimensions.HEIGHT / 2;
if (IS_HIDPI) {
restartSourceWidth *= 2;
restartSourceHeight *= 2;
framePosX *= 2;
}
this.canvasCtx.save();
if (IS_RTL) {
this.canvasCtx.translate(this.canvasDimensions.WIDTH, 0);
this.canvasCtx.scale(-1, 1);
}
this.canvasCtx.drawImage(Runner.origImageSprite, this.restartImgPos.x + framePosX, this.restartImgPos.y, restartSourceWidth, restartSourceHeight, restartTargetX, restartTargetY, dimensions.RESTART_WIDTH, dimensions.RESTART_HEIGHT);
this.canvasCtx.restore();
}
/**
* Draw the panel.
* @param {boolean} opt_altGameModeActive
* @param {!Trex} opt_tRex
*/
draw(opt_altGameModeActive, opt_tRex) {
if (opt_altGameModeActive) {
this.altGameModeActive = opt_altGameModeActive;
}
this.drawGameOverText(GameOverPanel.dimensions, false);
this.drawRestartButton();
this.drawAltGameElements(opt_tRex);
this.update();
}
/**
* Update animation frames.
*/
update() {
const now = getTimeStamp();
const deltaTime = now - (this.frameTimeStamp || now);
this.frameTimeStamp = now;
this.animTimer += deltaTime;
this.flashTimer += deltaTime;
// Restart Button
if (this.currentFrame === 0 &&
this.animTimer > GameOverPanel.LOGO_PAUSE_DURATION) {
this.animTimer = 0;
this.currentFrame++;
this.drawRestartButton();
}
else if (this.currentFrame > 0 &&
this.currentFrame < GameOverPanel.animConfig.frames.length) {
if (this.animTimer >= GameOverPanel.animConfig.msPerFrame) {
this.currentFrame++;
this.drawRestartButton();
}
}
else if (!this.altGameModeActive &&
this.currentFrame === GameOverPanel.animConfig.frames.length) {
this.reset();
return;
}
// Game over text
if (this.altGameModeActive &&
spriteDefinitionByType.original.ALT_GAME_OVER_TEXT_CONFIG) {
const altTextConfig = spriteDefinitionByType.original.ALT_GAME_OVER_TEXT_CONFIG;
if (altTextConfig.FLASHING) {
if (this.flashCounter < GameOverPanel.FLASH_ITERATIONS &&
this.flashTimer > altTextConfig.FLASH_DURATION) {
this.flashTimer = 0;
this.originalText = !this.originalText;
this.clearGameOverTextBounds();
if (this.originalText) {
this.drawGameOverText(GameOverPanel.dimensions, false);
this.flashCounter++;
}
else {
this.drawGameOverText(altTextConfig, true);
}
}
else if (this.flashCounter >= GameOverPanel.FLASH_ITERATIONS) {
this.reset();
return;
}
}
else {
this.clearGameOverTextBounds(altTextConfig);
this.drawGameOverText(altTextConfig, true);
}
}
this.gameOverRafId = requestAnimationFrame(this.update.bind(this));
}
/**
* Clear game over text.
* @param {Object} dimensions Game over text config.
*/
clearGameOverTextBounds(dimensions) {
this.canvasCtx.save();
this.canvasCtx.clearRect(Math.round(this.canvasDimensions.WIDTH / 2 - (dimensions.TEXT_WIDTH / 2)), Math.round((this.canvasDimensions.HEIGHT - 25) / 3), dimensions.TEXT_WIDTH, dimensions.TEXT_HEIGHT + 4);
this.canvasCtx.restore();
}
reset() {
if (this.gameOverRafId) {
cancelAnimationFrame(this.gameOverRafId);
this.gameOverRafId = null;
}
this.animTimer = 0;
this.frameTimeStamp = 0;
this.currentFrame = 0;
this.flashTimer = 0;
this.flashCounter = 0;
this.originalText = true;
}
}
GameOverPanel.RESTART_ANIM_DURATION = 875;
GameOverPanel.LOGO_PAUSE_DURATION = 875;
GameOverPanel.FLASH_ITERATIONS = 5;
/**
* Animation frames spec.
*/
GameOverPanel.animConfig = {
frames: [0, 36, 72, 108, 144, 180, 216, 252],
msPerFrame: GameOverPanel.RESTART_ANIM_DURATION / 8,
};
/**
* Dimensions used in the panel.
* @enum {number}
*/
GameOverPanel.dimensions = {
TEXT_X: 0,
TEXT_Y: 13,
TEXT_WIDTH: 191,
TEXT_HEIGHT: 11,
RESTART_WIDTH: 36,
RESTART_HEIGHT: 32,
};
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* Generated sound FX class for audio cues.
*/
class GeneratedSoundFx {
audioCues = false;
context = null;
panner = null;
bgSoundIntervalId = null;
init() {
this.audioCues = true;
if (!this.context) {
this.context = new AudioContext();
if (IS_IOS) {
this.context.onstatechange = () => {
assert(this.context);
if (this.context.state !== 'running') {
this.context.resume();
}
};
this.context.resume();
}
this.panner = this.context.createStereoPanner ?
this.context.createStereoPanner() :
null;
}
}
stopAll() {
this.cancelFootSteps();
}
/**
* Play oscillators at certain frequency and for a certain time.
*/
playNote(frequency, startTime, duration, vol = 0.01, pan = 0) {
assert(this.context);
const osc1 = this.context.createOscillator();
const osc2 = this.context.createOscillator();
const volume = this.context.createGain();
// Set oscillator wave type
osc1.type = 'triangle';
osc2.type = 'triangle';
volume.gain.value = 0.1;
// Set up node routing
if (this.panner) {
this.panner.pan.value = pan;
osc1.connect(volume).connect(this.panner);
osc2.connect(volume).connect(this.panner);
this.panner.connect(this.context.destination);
}
else {
osc1.connect(volume);
osc2.connect(volume);
volume.connect(this.context.destination);
}
// Detune oscillators for chorus effect
osc1.frequency.value = frequency + 1;
osc2.frequency.value = frequency - 2;
// Fade out
volume.gain.setValueAtTime(vol, startTime + duration - 0.05);
volume.gain.linearRampToValueAtTime(0.00001, startTime + duration);
// Start oscillators
osc1.start(startTime);
osc2.start(startTime);
// Stop oscillators
osc1.stop(startTime + duration);
osc2.stop(startTime + duration);
}
background() {
assert(this.context);
if (this.audioCues) {
const now = this.context.currentTime;
this.playNote(493.883, now, 0.116);
this.playNote(659.255, now + 0.116, 0.232);
this.loopFootSteps();
}
}
loopFootSteps() {
if (this.audioCues && !this.bgSoundIntervalId) {
this.bgSoundIntervalId = setInterval(() => {
assert(this.context);
this.playNote(73.42, this.context.currentTime, 0.05, 0.16);
this.playNote(69.30, this.context.currentTime + 0.116, 0.116, 0.16);
}, 280);
}
}
cancelFootSteps() {
if (this.audioCues && this.bgSoundIntervalId) {
assert(this.context);
clearInterval(this.bgSoundIntervalId);
this.bgSoundIntervalId = null;
this.playNote(103.83, this.context.currentTime, 0.232, 0.02);
this.playNote(116.54, this.context.currentTime + 0.116, 0.232, 0.02);
}
}
collect() {
if (this.audioCues) {
assert(this.context);
this.cancelFootSteps();
const now = this.context.currentTime;
this.playNote(830.61, now, 0.116);
this.playNote(1318.51, now + 0.116, 0.232);
}
}
jump() {
if (this.audioCues) {
assert(this.context);
const now = this.context.currentTime;
this.playNote(659.25, now, 0.116, 0.3, -0.6);
this.playNote(880, now + 0.116, 0.232, 0.3, -0.6);
}
}
}
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
class BackgroundEl {
/**
* Background item.
* Similar to cloud, without random y position.
* @param {HTMLCanvasElement} canvas Canvas element.
* @param {Object} spritePos Position of image in sprite.
* @param {number} containerWidth
* @param {string} type Element type.
*/
constructor(canvas, spritePos, containerWidth, type) {
this.canvas = canvas;
this.canvasCtx =
/** @type {CanvasRenderingContext2D} */ (this.canvas.getContext('2d'));
this.spritePos = spritePos;
this.containerWidth = containerWidth;
this.xPos = containerWidth;
this.yPos = 0;
this.remove = false;
this.type = type;
this.gap =
getRandomNum(BackgroundEl.config.MIN_GAP, BackgroundEl.config.MAX_GAP);
this.animTimer = 0;
this.switchFrames = false;
this.spriteConfig = {};
this.init();
}
/**
* Initialise the element setting the y position.
*/
init() {
this.spriteConfig = Runner.spriteDefinition.BACKGROUND_EL[this.type];
if (this.spriteConfig.FIXED) {
this.xPos = this.spriteConfig.FIXED_X_POS;
}
this.yPos = BackgroundEl.config.Y_POS - this.spriteConfig.HEIGHT +
this.spriteConfig.OFFSET;
this.draw();
}
/**
* Draw the element.
*/
draw() {
this.canvasCtx.save();
let sourceWidth = this.spriteConfig.WIDTH;
let sourceHeight = this.spriteConfig.HEIGHT;
let sourceX = this.spriteConfig.X_POS;
const outputWidth = sourceWidth;
const outputHeight = sourceHeight;
if (IS_HIDPI) {
sourceWidth *= 2;
sourceHeight *= 2;
sourceX *= 2;
}
this.canvasCtx.drawImage(Runner.imageSprite, sourceX, this.spritePos.y, sourceWidth, sourceHeight, this.xPos, this.yPos, outputWidth, outputHeight);
this.canvasCtx.restore();
}
/**
* Update the background element position.
* @param {number} speed
*/
update(speed) {
if (!this.remove) {
if (this.spriteConfig.FIXED) {
this.animTimer += speed;
if (this.animTimer > BackgroundEl.config.MS_PER_FRAME) {
this.animTimer = 0;
this.switchFrames = !this.switchFrames;
}
if (this.spriteConfig.FIXED_Y_POS_1 &&
this.spriteConfig.FIXED_Y_POS_2) {
this.yPos = this.switchFrames ? this.spriteConfig.FIXED_Y_POS_1 :
this.spriteConfig.FIXED_Y_POS_2;
}
}
else {
// Fixed speed, regardless of actual game speed.
this.xPos -= BackgroundEl.config.SPEED;
}
this.draw();
// Mark as removable if no longer in the canvas.
if (!this.isVisible()) {
this.remove = true;
}
}
}
/**
* Check if the element is visible on the stage.
* @return {boolean}
*/
isVisible() {
return this.xPos + this.spriteConfig.WIDTH > 0;
}
}
/**
* Background element object config.
* Real values assigned when game type changes.
* @enum {number}
*/
BackgroundEl.config = {
MAX_BG_ELS: 0,
MAX_GAP: 0,
MIN_GAP: 0,
POS: 0,
SPEED: 0,
Y_POS: 0,
MS_PER_FRAME: 0, // only needed when BACKGROUND_EL.FIXED is true
};
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
class Cloud {
/**
* Cloud background item.
* Similar to an obstacle object but without collision boxes.
* @param {HTMLCanvasElement} canvas Canvas element.
* @param {Object} spritePos Position of image in sprite.
* @param {number} containerWidth
*/
constructor(canvas, spritePos, containerWidth) {
this.canvas = canvas;
this.canvasCtx =
/** @type {CanvasRenderingContext2D} */ (this.canvas.getContext('2d'));
this.spritePos = spritePos;
this.containerWidth = containerWidth;
this.xPos = containerWidth;
this.yPos = 0;
this.remove = false;
this.gap =
getRandomNum(Cloud.config.MIN_CLOUD_GAP, Cloud.config.MAX_CLOUD_GAP);
this.init();
}
/**
* Initialise the cloud. Sets the Cloud height.
*/
init() {
this.yPos =
getRandomNum(Cloud.config.MAX_SKY_LEVEL, Cloud.config.MIN_SKY_LEVEL);
this.draw();
}
/**
* Draw the cloud.
*/
draw() {
this.canvasCtx.save();
let sourceWidth = Cloud.config.WIDTH;
let sourceHeight = Cloud.config.HEIGHT;
const outputWidth = sourceWidth;
const outputHeight = sourceHeight;
if (IS_HIDPI) {
sourceWidth = sourceWidth * 2;
sourceHeight = sourceHeight * 2;
}
this.canvasCtx.drawImage(Runner.imageSprite, this.spritePos.x, this.spritePos.y, sourceWidth, sourceHeight, this.xPos, this.yPos, outputWidth, outputHeight);
this.canvasCtx.restore();
}
/**
* Update the cloud position.
* @param {number} speed
*/
update(speed) {
if (!this.remove) {
this.xPos -= Math.ceil(speed);
this.draw();
// Mark as removable if no longer in the canvas.
if (!this.isVisible()) {
this.remove = true;
}
}
}
/**
* Check if the cloud is visible on the stage.
* @return {boolean}
*/
isVisible() {
return this.xPos + Cloud.config.WIDTH > 0;
}
}
/**
* Cloud object config.
* @enum {number}
*/
Cloud.config = {
HEIGHT: 14,
MAX_CLOUD_GAP: 400,
MAX_SKY_LEVEL: 30,
MIN_CLOUD_GAP: 100,
MIN_SKY_LEVEL: 71,
WIDTH: 46,
};
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
class HorizonLine {
/**
* Horizon Line.
* Consists of two connecting lines. Randomly assigns a flat / bumpy horizon.
* @param {HTMLCanvasElement} canvas
* @param {Object} lineConfig Configuration object.
*/
constructor(canvas, lineConfig) {
let sourceX = lineConfig.SOURCE_X;
let sourceY = lineConfig.SOURCE_Y;
if (IS_HIDPI) {
sourceX *= 2;
sourceY *= 2;
}
this.spritePos = { x: sourceX, y: sourceY };
this.canvas = canvas;
this.canvasCtx =
/** @type {CanvasRenderingContext2D} */ (canvas.getContext('2d'));
this.sourceDimensions = {};
this.dimensions = lineConfig;
this.sourceXPos =
[this.spritePos.x, this.spritePos.x + this.dimensions.WIDTH];
this.xPos = [];
this.yPos = 0;
this.bumpThreshold = 0.5;
this.setSourceDimensions(lineConfig);
this.draw();
}
/**
* Set the source dimensions of the horizon line.
*/
setSourceDimensions(newDimensions) {
for (const dimension in newDimensions) {
if (dimension !== 'SOURCE_X' && dimension !== 'SOURCE_Y') {
if (IS_HIDPI) {
if (dimension !== 'YPOS') {
this.sourceDimensions[dimension] = newDimensions[dimension] * 2;
}
}
else {
this.sourceDimensions[dimension] = newDimensions[dimension];
}
this.dimensions[dimension] = newDimensions[dimension];
}
}
this.xPos = [0, newDimensions.WIDTH];
this.yPos = newDimensions.YPOS;
}
/**
* Return the crop x position of a type.
*/
getRandomType() {
return Math.random() > this.bumpThreshold ? this.dimensions.WIDTH : 0;
}
/**
* Draw the horizon line.
*/
draw() {
this.canvasCtx.drawImage(Runner.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(Runner.imageSprite, this.sourceXPos[1], this.spritePos.y, this.sourceDimensions.WIDTH, this.sourceDimensions.HEIGHT, this.xPos[1], this.yPos, this.dimensions.WIDTH, this.dimensions.HEIGHT);
}
/**
* Update the x position of an individual piece of the line.
* @param {number} pos Line position.
* @param {number} increment
*/
updateXPos(pos, increment) {
const line1 = pos;
const line2 = pos === 0 ? 1 : 0;
this.xPos[line1] -= increment;
this.xPos[line2] = this.xPos[line1] + this.dimensions.WIDTH;
if (this.xPos[line1] <= -this.dimensions.WIDTH) {
this.xPos[line1] += this.dimensions.WIDTH * 2;
this.xPos[line2] = this.xPos[line1] - this.dimensions.WIDTH;
this.sourceXPos[line1] = this.getRandomType() + this.spritePos.x;
}
}
/**
* Update the horizon line.
* @param {number} deltaTime
* @param {number} speed
*/
update(deltaTime, speed) {
const increment = Math.floor(speed * (FPS / 1000) * deltaTime);
if (this.xPos[0] <= 0) {
this.updateXPos(0, increment);
}
else {
this.updateXPos(1, increment);
}
this.draw();
}
/**
* Reset horizon to the starting position.
*/
reset() {
this.xPos[0] = 0;
this.xPos[1] = this.dimensions.WIDTH;
}
}
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
class NightMode {
/**
* Nightmode shows a moon and stars on the horizon.
* @param {HTMLCanvasElement} canvas
* @param {number} spritePos
* @param {number} containerWidth
*/
constructor(canvas, spritePos, containerWidth) {
this.spritePos = spritePos;
this.canvas = canvas;
this.canvasCtx =
/** @type {CanvasRenderingContext2D} */ (canvas.getContext('2d'));
this.xPos = containerWidth - 50;
this.yPos = 30;
this.currentPhase = 0;
this.opacity = 0;
this.containerWidth = containerWidth;
this.stars = [];
this.drawStars = false;
this.placeStars();
}
/**
* Update moving moon, changing phases.
* @param {boolean} activated Whether night mode is activated.
*/
update(activated) {
// Moon phase.
if (activated && this.opacity === 0) {
this.currentPhase++;
if (this.currentPhase >= NightMode.phases.length) {
this.currentPhase = 0;
}
}
// Fade in / out.
if (activated && (this.opacity < 1 || this.opacity === 0)) {
this.opacity += NightMode.config.FADE_SPEED;
}
else if (this.opacity > 0) {
this.opacity -= NightMode.config.FADE_SPEED;
}
// Set moon positioning.
if (this.opacity > 0) {
this.xPos = this.updateXPos(this.xPos, NightMode.config.MOON_SPEED);
// Update stars.
if (this.drawStars) {
for (let i = 0; i < NightMode.config.NUM_STARS; i++) {
this.stars[i].x =
this.updateXPos(this.stars[i].x, NightMode.config.STAR_SPEED);
}
}
this.draw();
}
else {
this.opacity = 0;
this.placeStars();
}
this.drawStars = true;
}
updateXPos(currentPos, speed) {
if (currentPos < -NightMode.config.WIDTH) {
currentPos = this.containerWidth;
}
else {
currentPos -= speed;
}
return currentPos;
}
draw() {
let moonSourceWidth = this.currentPhase === 3 ? NightMode.config.WIDTH * 2 :
NightMode.config.WIDTH;
let moonSourceHeight = NightMode.config.HEIGHT;
let moonSourceX = this.spritePos.x + NightMode.phases[this.currentPhase];
const moonOutputWidth = moonSourceWidth;
let starSize = NightMode.config.STAR_SIZE;
let starSourceX = spriteDefinitionByType.original.LDPI.STAR.x;
if (IS_HIDPI) {
moonSourceWidth *= 2;
moonSourceHeight *= 2;
moonSourceX =
this.spritePos.x + (NightMode.phases[this.currentPhase] * 2);
starSize *= 2;
starSourceX = spriteDefinitionByType.original.HDPI.STAR.x;
}
this.canvasCtx.save();
this.canvasCtx.globalAlpha = this.opacity;
// Stars.
if (this.drawStars) {
for (let i = 0; i < NightMode.config.NUM_STARS; i++) {
this.canvasCtx.drawImage(Runner.origImageSprite, starSourceX, this.stars[i].sourceY, starSize, starSize, Math.round(this.stars[i].x), this.stars[i].y, NightMode.config.STAR_SIZE, NightMode.config.STAR_SIZE);
}
}
// Moon.
this.canvasCtx.drawImage(Runner.origImageSprite, moonSourceX, this.spritePos.y, moonSourceWidth, moonSourceHeight, Math.round(this.xPos), this.yPos, moonOutputWidth, NightMode.config.HEIGHT);
this.canvasCtx.globalAlpha = 1;
this.canvasCtx.restore();
}
// Do star placement.
placeStars() {
const segmentSize = Math.round(this.containerWidth / NightMode.config.NUM_STARS);
for (let i = 0; i < NightMode.config.NUM_STARS; i++) {
this.stars[i] = {};
this.stars[i].x = getRandomNum(segmentSize * i, segmentSize * (i + 1));
this.stars[i].y = getRandomNum(0, NightMode.config.STAR_MAX_Y);
if (IS_HIDPI) {
this.stars[i].sourceY = spriteDefinitionByType.original.HDPI.STAR.y +
NightMode.config.STAR_SIZE * 2 * i;
}
else {
this.stars[i].sourceY = spriteDefinitionByType.original.LDPI.STAR.y +
NightMode.config.STAR_SIZE * i;
}
}
}
reset() {
this.currentPhase = 0;
this.opacity = 0;
this.update(false);
}
}
/**
* @enum {number}
*/
NightMode.config = {
FADE_SPEED: 0.035,
HEIGHT: 40,
MOON_SPEED: 0.25,
NUM_STARS: 2,
STAR_SIZE: 9,
STAR_SPEED: 0.3,
STAR_MAX_Y: 70,
WIDTH: 20,
};
NightMode.phases = [140, 120, 100, 60, 40, 20, 0];
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
class Obstacle {
/**
* Obstacle.
* @param {CanvasRenderingContext2D} canvasCtx
* @param {ObstacleType} type
* @param {Object} spriteImgPos Obstacle position in sprite.
* @param {Object} dimensions
* @param {number} gapCoefficient Mutipler in determining the gap.
* @param {number} speed
* @param {number=} opt_xOffset
* @param {boolean=} opt_isAltGameMode
*/
constructor(canvasCtx, type, spriteImgPos, dimensions, gapCoefficient, speed, opt_xOffset, opt_isAltGameMode) {
this.canvasCtx = canvasCtx;
this.spritePos = spriteImgPos;
this.typeConfig = type;
this.gapCoefficient = Runner.slowDown ? gapCoefficient * 2 : gapCoefficient;
this.size = getRandomNum(1, Obstacle.MAX_OBSTACLE_LENGTH);
this.dimensions = dimensions;
this.remove = false;
this.xPos = dimensions.WIDTH + (opt_xOffset || 0);
this.yPos = 0;
this.width = 0;
this.collisionBoxes = [];
this.gap = 0;
this.speedOffset = 0;
this.altGameModeActive = opt_isAltGameMode;
this.imageSprite = this.typeConfig.type === 'COLLECTABLE' ?
Runner.altCommonImageSprite :
this.altGameModeActive ? Runner.altGameImageSprite :
Runner.imageSprite;
// For animated obstacles.
this.currentFrame = 0;
this.timer = 0;
this.init(speed);
}
/**
* Initialise the DOM for the obstacle.
* @param {number} speed
*/
init(speed) {
this.cloneCollisionBoxes();
// Only allow sizing if we're at the right speed.
if (this.size > 1 && this.typeConfig.multipleSpeed > speed) {
this.size = 1;
}
this.width = this.typeConfig.width * this.size;
// Check if obstacle can be positioned at various heights.
if (Array.isArray(this.typeConfig.yPos)) {
const yPosConfig = IS_MOBILE ? this.typeConfig.yPosMobile : this.typeConfig.yPos;
this.yPos = yPosConfig[getRandomNum(0, yPosConfig.length - 1)];
}
else {
this.yPos = this.typeConfig.yPos;
}
this.draw();
// Make collision box adjustments,
// Central box is adjusted to the size as one box.
// ____ ______ ________
// _| |-| _| |-| _| |-|
// | |<->| | | |<--->| | | |<----->| |
// | | 1 | | | | 2 | | | | 3 | |
// |_|___|_| |_|_____|_| |_|_______|_|
//
if (this.size > 1) {
this.collisionBoxes[1].width = this.width - this.collisionBoxes[0].width -
this.collisionBoxes[2].width;
this.collisionBoxes[2].x = this.width - this.collisionBoxes[2].width;
}
// For obstacles that go at a different speed from the horizon.
if (this.typeConfig.speedOffset) {
this.speedOffset = Math.random() > 0.5 ? this.typeConfig.speedOffset :
-this.typeConfig.speedOffset;
}
this.gap = this.getGap(this.gapCoefficient, speed);
// Increase gap for audio cues enabled.
if (Runner.audioCues) {
this.gap *= 2;
}
}
/**
* Draw and crop based on size.
*/
draw() {
let sourceWidth = this.typeConfig.width;
let sourceHeight = this.typeConfig.height;
if (IS_HIDPI) {
sourceWidth = sourceWidth * 2;
sourceHeight = sourceHeight * 2;
}
// X position in sprite.
let sourceX = (sourceWidth * this.size) * (0.5 * (this.size - 1)) + this.spritePos.x;
// Animation frames.
if (this.currentFrame > 0) {
sourceX += sourceWidth * this.currentFrame;
}
this.canvasCtx.drawImage(this.imageSprite, sourceX, this.spritePos.y, sourceWidth * this.size, sourceHeight, this.xPos, this.yPos, this.typeConfig.width * this.size, this.typeConfig.height);
}
/**
* Obstacle frame update.
* @param {number} deltaTime
* @param {number} speed
*/
update(deltaTime, speed) {
if (!this.remove) {
if (this.typeConfig.speedOffset) {
speed += this.speedOffset;
}
this.xPos -= Math.floor((speed * FPS / 1000) * deltaTime);
// Update frame
if (this.typeConfig.numFrames) {
this.timer += deltaTime;
if (this.timer >= this.typeConfig.frameRate) {
this.currentFrame =
this.currentFrame === this.typeConfig.numFrames - 1 ?
0 :
this.currentFrame + 1;
this.timer = 0;
}
}
this.draw();
if (!this.isVisible()) {
this.remove = true;
}
}
}
/**
* Calculate a random gap size.
* - Minimum gap gets wider as speed increases
* @param {number} gapCoefficient
* @param {number} speed
* @return {number} The gap size.
*/
getGap(gapCoefficient, speed) {
const minGap = Math.round(this.width * speed + this.typeConfig.minGap * gapCoefficient);
const maxGap = Math.round(minGap * Obstacle.MAX_GAP_COEFFICIENT);
return getRandomNum(minGap, maxGap);
}
/**
* Check if obstacle is visible.
* @return {boolean} Whether the obstacle is in the game area.
*/
isVisible() {
return this.xPos + this.width > 0;
}
/**
* Make a copy of the collision boxes, since these will change based on
* obstacle type and size.
*/
cloneCollisionBoxes() {
const collisionBoxes = this.typeConfig.collisionBoxes;
for (let i = collisionBoxes.length - 1; i >= 0; i--) {
this.collisionBoxes[i] = new CollisionBox(collisionBoxes[i].x, collisionBoxes[i].y, collisionBoxes[i].width, collisionBoxes[i].height);
}
}
}
/**
* Coefficient for calculating the maximum gap.
*/
Obstacle.MAX_GAP_COEFFICIENT = 1.5;
/**
* Maximum obstacle grouping count.
*/
Obstacle.MAX_OBSTACLE_LENGTH = 3;
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* Horizon background class.
*/
class Horizon {
/**
* @param {HTMLCanvasElement} canvas
* @param {Object} spritePos Sprite positioning.
* @param {Object} dimensions Canvas dimensions.
* @param {number} gapCoefficient
*/
constructor(canvas, spritePos, dimensions, gapCoefficient) {
this.canvas = canvas;
this.canvasCtx =
/** @type {CanvasRenderingContext2D} */ (this.canvas.getContext('2d'));
this.config = Horizon.config;
this.dimensions = dimensions;
this.gapCoefficient = gapCoefficient;
this.obstacles = [];
this.obstacleHistory = [];
this.horizonOffsets = [0, 0];
this.cloudFrequency = this.config.CLOUD_FREQUENCY;
this.spritePos = spritePos;
this.nightMode = null;
this.altGameModeActive = false;
// Cloud
this.clouds = [];
this.cloudSpeed = this.config.BG_CLOUD_SPEED;
// Background elements
this.backgroundEls = [];
this.lastEl = null;
this.backgroundSpeed = this.config.BG_CLOUD_SPEED;
// Horizon
this.horizonLine = null;
this.horizonLines = [];
this.init();
}
/**
* Initialise the horizon. Just add the line and a cloud. No obstacles.
*/
init() {
Obstacle.types = spriteDefinitionByType.original.OBSTACLES;
this.addCloud();
// Multiple Horizon lines
for (let i = 0; i < Runner.spriteDefinition.LINES.length; i++) {
this.horizonLines.push(new HorizonLine(this.canvas, Runner.spriteDefinition.LINES[i]));
}
this.nightMode =
new NightMode(this.canvas, this.spritePos.MOON, this.dimensions.WIDTH);
}
/**
* Update obstacle definitions based on the speed of the game.
*/
adjustObstacleSpeed() {
for (let i = 0; i < Obstacle.types.length; i++) {
if (Runner.slowDown) {
Obstacle.types[i].multipleSpeed = Obstacle.types[i].multipleSpeed / 2;
Obstacle.types[i].minGap *= 1.5;
Obstacle.types[i].minSpeed = Obstacle.types[i].minSpeed / 2;
// Convert variable y position obstacles to fixed.
if (typeof (Obstacle.types[i].yPos) === 'object') {
Obstacle.types[i].yPos = Obstacle.types[i].yPos[0];
Obstacle.types[i].yPosMobile = Obstacle.types[i].yPos[0];
}
}
}
}
/**
* Update sprites to correspond to change in sprite sheet.
* @param {number} spritePos
*/
enableAltGameMode(spritePos) {
// Clear existing horizon objects.
this.clouds = [];
this.backgroundEls = [];
this.altGameModeActive = true;
this.spritePos = spritePos;
Obstacle.types = Runner.spriteDefinition.OBSTACLES;
this.adjustObstacleSpeed();
Obstacle.MAX_GAP_COEFFICIENT = Runner.spriteDefinition.MAX_GAP_COEFFICIENT;
Obstacle.MAX_OBSTACLE_LENGTH = Runner.spriteDefinition.MAX_OBSTACLE_LENGTH;
BackgroundEl.config = Runner.spriteDefinition.BACKGROUND_EL_CONFIG;
this.horizonLines = [];
for (let i = 0; i < Runner.spriteDefinition.LINES.length; i++) {
this.horizonLines.push(new HorizonLine(this.canvas, Runner.spriteDefinition.LINES[i]));
}
this.reset();
}
/**
* @param {number} deltaTime
* @param {number} currentSpeed
* @param {boolean} updateObstacles Used as an override to prevent
* the obstacles from being updated / added. This happens in the
* ease in section.
* @param {boolean} showNightMode Night mode activated.
*/
update(deltaTime, currentSpeed, updateObstacles, showNightMode) {
this.runningTime += deltaTime;
if (this.altGameModeActive) {
this.updateBackgroundEls(deltaTime, currentSpeed);
}
for (let i = 0; i < this.horizonLines.length; i++) {
this.horizonLines[i].update(deltaTime, currentSpeed);
}
if (!this.altGameModeActive || Runner.spriteDefinition.HAS_CLOUDS) {
this.nightMode.update(showNightMode);
this.updateClouds(deltaTime, currentSpeed);
}
if (updateObstacles) {
this.updateObstacles(deltaTime, currentSpeed);
}
}
/**
* Update background element positions. Also handles creating new elements.
* @param {number} elSpeed
* @param {Array