/**** 
* Classes
****/ 
// No need to manually define assets, LK will handle this automatically.
// Define a simple class for the light indicator
var LightIndicator = Container.expand(function () {
	var self = Container.call(this);
	var light = self.attachAsset('light', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.setColor = function (color) {
		light.tint = color;
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0xFFFFFF // Init game with a white background
});
/**** 
* Game Code
****/ 
// Initialize variables
var lightIndicator;
var startTime;
var reactionTimeText;
var gameStarted = false;
var countdown = 5;
var lightsOff = false;
var jumpstart = false;
var scoreRecords = [{
	name: 'Alice',
	score: 150
}, {
	name: 'Bob',
	score: 180
}, {
	name: 'Charlie',
	score: 200
}, {
	name: 'David',
	score: 220
}, {
	name: 'Eve',
	score: 260
}, {
	name: 'Frank',
	score: 300
}, {
	name: 'Grace',
	score: 360
}, {
	name: 'Hannah',
	score: 360
}, {
	name: 'Ivy',
	score: 400
}, {
	name: 'Jack',
	score: 450
}];
// Function to start the game
function startGame() {
	gameStarted = true;
	startTime = Date.now();
	var countdownInterval = LK.setInterval(function () {
		countdown--;
		if (countdown >= 0 && !jumpstart) {
			lightIndicators[5 - countdown - 1].setColor(0xFF0000); // Set light to red
			lightIndicators2[5 - countdown - 1].setColor(0xFF0000); // Set light to red
			LK.getSound('lights').play(); // Play sound when light is turned on
		}
		if (countdown === 0 && !jumpstart) {
			LK.clearInterval(countdownInterval);
			if (!jumpstart) {
				LK.setTimeout(turnOffLight, Math.random() * 3000 + 1000); // Turn off light after a random time between 1-4 seconds
			}
		}
	}, 1000);
}
// Function to turn off the light
function turnOffLight() {
	lightIndicators.forEach(function (lightIndicator) {
		lightIndicator.setColor(0x9f9d9d); // Set light to offlight color
	});
	lightIndicators2.forEach(function (lightIndicator) {
		lightIndicator.setColor(0x9f9d9d); // Set light to offlight color
	});
	startTime = Date.now(); // Record the time when the light turns off
	lightsOff = true; // Set the flag to true when lights turn off
	if (!jumpstart) {
		LK.getSound('start').play(); // Play sound when lights go off
	}
}
// Function to handle screen tap
var touchCount = 0;
function handleTap(x, y, obj) {
	touchCount++;
	if (!gameStarted) {
		startGame();
		instructionText.visible = false; // Hide instructions text after first tap
	} else if (touchCount == 2 && !lightsOff) {
		reactionTimeText.setText('JUMP START!');
		gameStarted = false;
		jumpstart = true;
		game.down = null; // Disable further taps
		LK.getSound('jumpstart').play(); // Play 'jumpstart' sound when jumpstart happens
		LK.setTimeout(function () {
			LK.showGameOver();
			LK.setTimeout(function () {
				game.down = handleTap; // Re-enable tap after game over
			}, 2000); // Re-enable tap after 2 seconds
		}, 2000); // Show game over after 2 seconds
	} else {
		var reactionTime = Date.now() - startTime;
		LK.setScore(reactionTime);
		reactionTimeText.setText((LK.getScore() / 1000).toFixed(3).padStart(6, '0'));
		reactionTimeText.scale.x = 1.1;
		reactionTimeText.scale.y = 1.1;
		LK.setTimeout(function () {
			reactionTimeText.scale.x = 1;
			reactionTimeText.scale.y = 1;
		}, 100);
		LK.getSound('speed').play(); // Play 'speed' sound when reaction time is updated
		gameStarted = false;
		game.down = null; // Disable further taps
		// Move lights, light background, and center asset offscreen to the right
		var moveOffscreenSpeed = 40; // Adjust speed as needed
		lightIndicators.forEach(function (lightIndicator) {
			lightIndicator.update = function () {
				lightIndicator.x += moveOffscreenSpeed;
				if (lightIndicator.x > 2048 + lightIndicator.width) {
					lightIndicator.visible = false;
				}
			};
		});
		lightIndicators2.forEach(function (lightIndicator) {
			lightIndicator.update = function () {
				lightIndicator.x += moveOffscreenSpeed;
				if (lightIndicator.x > 2048 + lightIndicator.width) {
					lightIndicator.visible = false;
				}
			};
		});
		staticLightIndicators1.forEach(function (lightIndicator) {
			lightIndicator.update = function () {
				lightIndicator.x += moveOffscreenSpeed;
				if (lightIndicator.x > 2048 + lightIndicator.width) {
					lightIndicator.visible = false;
				}
			};
		});
		staticLightIndicators2.forEach(function (lightIndicator) {
			lightIndicator.update = function () {
				lightIndicator.x += moveOffscreenSpeed;
				if (lightIndicator.x > 2048 + lightIndicator.width) {
					lightIndicator.visible = false;
				}
			};
		});
		lightBackgrounds.forEach(function (lightBackground) {
			lightBackground.update = function () {
				lightBackground.x += moveOffscreenSpeed;
				if (lightBackground.x > 2048 + lightBackground.width) {
					lightBackground.visible = false;
				}
			};
		});
		centerAsset.update = function () {
			centerAsset.x += moveOffscreenSpeed;
			if (centerAsset.x > 2048 + centerAsset.width) {
				centerAsset.visible = false;
			}
		};
	}
	if (touchCount == 2 && !jumpstart) {
		game.down = null; // Disable further taps
		// Add the current score to the score records with 'YOU' as the name
		var youScore = {
			name: 'YOU',
			score: LK.getScore()
		};
		var inserted = false;
		for (var i = 0; i < scoreRecords.length; i++) {
			if (scoreRecords[i].name === 'YOU') {
				continue;
			}
			if (youScore.score < scoreRecords[i].score) {
				scoreRecords.splice(i, 0, youScore);
				inserted = true;
				break;
			}
		}
		if (!inserted) {
			if (youScore.score >= 450 && !scoreRecords.some(function (record) {
				return record.name === 'YOU';
			})) {
				scoreRecords.push(youScore);
			} else if (!scoreRecords.some(function (record) {
				return record.name === 'YOU';
			})) {
				scoreRecords.push(youScore);
				scoreRecords = scoreRecords.slice(0, 10); // Limit to top 10 scores including the player
			}
		}
		// Ensure names in the score table remain in the same order
		var names = ['Verstappen', 'Leclerc', 'Norris', 'Colapinto', 'Sainz', 'Hamilton', 'Piastri', 'Russel', 'Tsunoda', 'Albon'];
		var youIndex = scoreRecords.findIndex(function (record) {
			return record.name === 'YOU';
		});
		for (var i = 0; i < scoreRecords.length; i++) {
			if (i !== youIndex) {
				scoreRecords[i].name = names[i < youIndex ? i : i - 1];
			}
		}
		// Create a table for the score records
		var scoreTable = new Container();
		scoreTable.x = 2048 / 2 - 100;
		scoreTable.y = 2732 / 2 - 650;
		scoreTable.pivot.set(scoreTable.width / 2, 0);
		// Add table headers
		var headers = ["Rank", "Driver", "Time (s)"];
		for (var i = 0; i < headers.length; i++) {
			var headerText = new Text2(headers[i], {
				size: 90,
				fill: "#000000",
				stroke: "#000000",
				strokeThickness: 1,
				font: "'Formula 1', sans-serif",
				align: 'center'
			});
			headerText.x = i * 600 - 700 + (i === 0 ? 100 : 0); // Adjust spacing between columns, move rank header 100 pixels to the right
			headerText.y = 0;
			scoreTable.addChild(headerText);
		}
		// Add score records to the table
		for (var i = 0; i < scoreRecords.length; i++) {
			var rankText = new Text2(scoreRecords[i].name === 'YOU' && youScore.score > 450 ? scoreRecords.length.toString() : (i + 1).toString(), {
				size: scoreRecords[i].name === 'YOU' ? 90 : 80,
				// Increase size by 30% if it's the player
				fill: scoreRecords[i].name === 'YOU' ? "#FF1801" : "#000000",
				stroke: "#000000",
				strokeThickness: 1,
				font: "'Formula 1', sans-serif",
				align: 'center'
			});
			rankText.x = -600; // Align rank column
			rankText.y = (i + 1) * 100;
			scoreTable.addChild(rankText);
			if (scoreRecords[i].name && scoreRecords[i].name !== 'YOU') {
				var flagBackground = LK.getAsset('flagBackground', {
					anchorX: 0.5,
					anchorY: 0.1,
					x: -200,
					y: (i + 1) * 100 + 18
				});
				scoreTable.addChild(flagBackground);
				var flagAsset = LK.getAsset('flag_' + scoreRecords[i].name.toLowerCase(), {
					anchorX: 0.5,
					anchorY: 0.1,
					x: -200,
					y: (i + 1) * 100 + 20
				});
				scoreTable.addChild(flagAsset);
			}
			var nameText = new Text2(scoreRecords[i].name || "", {
				size: scoreRecords[i].name === 'YOU' ? 90 : 80,
				fill: scoreRecords[i].name === 'YOU' ? "#FF1801" : "#000000",
				stroke: "#000000",
				strokeThickness: 1,
				font: "'Formula 1', sans-serif",
				align: 'center'
			});
			nameText.x = -100; // Align name column
			nameText.y = (i + 1) * 100;
			scoreTable.addChild(nameText);
			var timeText = new Text2((scoreRecords[i].score / 1000).toFixed(3) + "s", {
				size: scoreRecords[i].name === 'YOU' ? 90 : 80,
				fill: scoreRecords[i].name === 'YOU' ? "#FF1801" : "#000000",
				stroke: "#000000",
				strokeThickness: 1,
				font: "'Formula 1', sans-serif",
				align: 'center'
			});
			timeText.x = 500; // Align time column
			timeText.y = (i + 1) * 100;
			scoreTable.addChild(timeText);
		}
		if (youScore.score > 450 && !scoreRecords.some(function (record) {
			return record.name === 'YOU';
		})) {
			var rankText = new Text2(scoreRecords.length.toString(), {
				size: 90,
				fill: "#FF1801",
				stroke: "#000000",
				strokeThickness: 0,
				font: "'Formula 1', sans-serif",
				align: 'center'
			});
			rankText.x = -600;
			rankText.y = (scoreRecords.length + 1) * 100;
			scoreTable.addChild(rankText);
			var nameText = new Text2('YOU', {
				size: 90,
				fill: "#FF1801",
				stroke: "#000000",
				strokeThickness: 0,
				font: "'Formula 1', sans-serif",
				align: 'center'
			});
			nameText.x = -100;
			nameText.y = (scoreRecords.length + 1) * 100;
			scoreTable.addChild(nameText);
			var timeText = new Text2((youScore.score / 1000).toFixed(3) + "s", {
				size: 90,
				fill: "#FF1801",
				stroke: "#000000",
				strokeThickness: 0,
				font: "'Formula 1', sans-serif",
				align: 'center'
			});
			timeText.x = 500;
			timeText.y = (scoreRecords.length + 1) * 100;
			scoreTable.addChild(timeText);
		}
		scoreTable.x = -scoreTable.width; // Start offscreen to the left
		game.addChild(scoreTable);
		// Animate the score table to its final position
		var targetX = 2048 / 2 - 100;
		var animationDuration = 60; // Duration in ticks (1 second)
		var animationStep = (targetX - scoreTable.x) / animationDuration;
		game.update = function () {
			if (scoreTable.x < targetX) {
				scoreTable.x += animationStep;
				if (scoreTable.x >= targetX) {
					scoreTable.x = targetX; // Ensure it stops exactly at the target position
				}
			}
		};
		// No background image needed for the records table
		// Removed grid lines for the ranked table
		LK.setTimeout(function () {
			touchCount = 0; // Reset touch count
			game.down = handleTap; // Re-enable tap
			LK.showGameOver();
		}, 5000); // Show game over after 5 seconds to allow time to read the scores
	}
}
// Initialize light indicators
var lightIndicators = [];
var lightIndicators2 = [];
var lightBackgrounds = [];
var staticLightIndicators1 = [];
var staticLightIndicators2 = [];
for (var i = 0; i < 5; i++) {
	var lightBackground = game.addChild(LK.getAsset('lightbackground', {
		anchorX: 0.5,
		anchorY: 0.5,
		x: 2048 / 2 + (i - 2) * 200,
		y: 2732 / 2 - 70
	}));
	lightBackgrounds.push(lightBackground);
	var lightIndicator = game.addChild(new LightIndicator());
	lightIndicator.x = 2048 / 2 + (i - 2) * 200; // Position lights horizontally with some spacing
	lightIndicator.y = 2732 / 2;
	lightIndicator.setColor(0x9f9d9d); // Set initial light color to white
	lightIndicators.push(lightIndicator);
	var lightIndicator2 = game.addChild(new LightIndicator());
	lightIndicator2.x = 2048 / 2 + (i - 2) * 200; // Position lights horizontally with some spacing
	lightIndicator2.y = 2732 / 2 + 150; // Position the second line of lights 200px below the first line
	lightIndicator2.setColor(0x9f9d9d); // Set initial light color to white
	lightIndicators2.push(lightIndicator2);
	// Add static lights
	var staticLight1 = game.addChild(new LightIndicator());
	staticLight1.x = 2048 / 2 + (i - 2) * 200; // Position lights horizontally with some spacing
	staticLight1.y = 2732 / 2 - 150; // Position the first line of static lights 200px above the first line
	staticLight1.setColor(0x9f9d9d); // Set initial light color to white
	staticLightIndicators1.push(staticLight1);
	var staticLight2 = game.addChild(new LightIndicator());
	staticLight2.x = 2048 / 2 + (i - 2) * 200; // Position lights horizontally with some spacing
	staticLight2.y = 2732 / 2 - 300; // Position the second line of static lights 400px above the first line
	staticLight2.setColor(0x9f9d9d); // Set initial light color to white
	staticLightIndicators2.push(staticLight2);
}
// Initialize reaction time text
reactionTimeText = new Text2('00.000', {
	size: 200,
	fill: "#000000"
});
reactionTimeText.anchor.set(0.5, 1);
reactionTimeText.y -= 250; // Move reaction time 400 pixels up
LK.gui.bottom.addChild(reactionTimeText);
// Add instruction text below the lights
var instructionText = new Text2("Tap to start the race. Tap again when the lights go out.", {
	size: 70,
	fill: "#000000",
	font: "'Formula 1', sans-serif"
});
instructionText.anchor.set(0.5, 0);
instructionText.x = 2048 / 2;
instructionText.y = 2732 / 2 + 450;
game.addChild(instructionText);
// Add 'f1' asset to the top of the screen
var f1Asset = game.addChild(LK.getAsset('f1', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: 2048 / 2,
	y: 400
}));
// Add 'centerAsset' to the center of the lights
var centerAsset = game.addChildAt(LK.getAsset('centerAsset', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: 2048 / 2,
	y: 2732 / 2 - 60
}), 0);
// Set up event listeners
game.down = handleTap;
game.update = function () {
	// Add a slight increase and decrease in size to the instruction text
	instructionText.scale.x = 1 + 0.02 * Math.sin(LK.ticks / 20);
	instructionText.scale.y = 1 + 0.02 * Math.sin(LK.ticks / 20);
	// Update positions of elements moving offscreen
	lightIndicators.forEach(function (lightIndicator) {
		if (lightIndicator.update) {
			lightIndicator.update();
		}
	});
	lightIndicators2.forEach(function (lightIndicator) {
		if (lightIndicator.update) {
			lightIndicator.update();
		}
	});
	staticLightIndicators1.forEach(function (lightIndicator) {
		if (lightIndicator.update) {
			lightIndicator.update();
		}
	});
	staticLightIndicators2.forEach(function (lightIndicator) {
		if (lightIndicator.update) {
			lightIndicator.update();
		}
	});
	lightBackgrounds.forEach(function (lightBackground) {
		if (lightBackground.update) {
			lightBackground.update();
		}
	});
	if (centerAsset.update) {
		centerAsset.update();
	}
}; /**** 
* Classes
****/ 
// No need to manually define assets, LK will handle this automatically.
// Define a simple class for the light indicator
var LightIndicator = Container.expand(function () {
	var self = Container.call(this);
	var light = self.attachAsset('light', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.setColor = function (color) {
		light.tint = color;
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0xFFFFFF // Init game with a white background
});
/**** 
* Game Code
****/ 
// Initialize variables
var lightIndicator;
var startTime;
var reactionTimeText;
var gameStarted = false;
var countdown = 5;
var lightsOff = false;
var jumpstart = false;
var scoreRecords = [{
	name: 'Alice',
	score: 150
}, {
	name: 'Bob',
	score: 180
}, {
	name: 'Charlie',
	score: 200
}, {
	name: 'David',
	score: 220
}, {
	name: 'Eve',
	score: 260
}, {
	name: 'Frank',
	score: 300
}, {
	name: 'Grace',
	score: 360
}, {
	name: 'Hannah',
	score: 360
}, {
	name: 'Ivy',
	score: 400
}, {
	name: 'Jack',
	score: 450
}];
// Function to start the game
function startGame() {
	gameStarted = true;
	startTime = Date.now();
	var countdownInterval = LK.setInterval(function () {
		countdown--;
		if (countdown >= 0 && !jumpstart) {
			lightIndicators[5 - countdown - 1].setColor(0xFF0000); // Set light to red
			lightIndicators2[5 - countdown - 1].setColor(0xFF0000); // Set light to red
			LK.getSound('lights').play(); // Play sound when light is turned on
		}
		if (countdown === 0 && !jumpstart) {
			LK.clearInterval(countdownInterval);
			if (!jumpstart) {
				LK.setTimeout(turnOffLight, Math.random() * 3000 + 1000); // Turn off light after a random time between 1-4 seconds
			}
		}
	}, 1000);
}
// Function to turn off the light
function turnOffLight() {
	lightIndicators.forEach(function (lightIndicator) {
		lightIndicator.setColor(0x9f9d9d); // Set light to offlight color
	});
	lightIndicators2.forEach(function (lightIndicator) {
		lightIndicator.setColor(0x9f9d9d); // Set light to offlight color
	});
	startTime = Date.now(); // Record the time when the light turns off
	lightsOff = true; // Set the flag to true when lights turn off
	if (!jumpstart) {
		LK.getSound('start').play(); // Play sound when lights go off
	}
}
// Function to handle screen tap
var touchCount = 0;
function handleTap(x, y, obj) {
	touchCount++;
	if (!gameStarted) {
		startGame();
		instructionText.visible = false; // Hide instructions text after first tap
	} else if (touchCount == 2 && !lightsOff) {
		reactionTimeText.setText('JUMP START!');
		gameStarted = false;
		jumpstart = true;
		game.down = null; // Disable further taps
		LK.getSound('jumpstart').play(); // Play 'jumpstart' sound when jumpstart happens
		LK.setTimeout(function () {
			LK.showGameOver();
			LK.setTimeout(function () {
				game.down = handleTap; // Re-enable tap after game over
			}, 2000); // Re-enable tap after 2 seconds
		}, 2000); // Show game over after 2 seconds
	} else {
		var reactionTime = Date.now() - startTime;
		LK.setScore(reactionTime);
		reactionTimeText.setText((LK.getScore() / 1000).toFixed(3).padStart(6, '0'));
		reactionTimeText.scale.x = 1.1;
		reactionTimeText.scale.y = 1.1;
		LK.setTimeout(function () {
			reactionTimeText.scale.x = 1;
			reactionTimeText.scale.y = 1;
		}, 100);
		LK.getSound('speed').play(); // Play 'speed' sound when reaction time is updated
		gameStarted = false;
		game.down = null; // Disable further taps
		// Move lights, light background, and center asset offscreen to the right
		var moveOffscreenSpeed = 40; // Adjust speed as needed
		lightIndicators.forEach(function (lightIndicator) {
			lightIndicator.update = function () {
				lightIndicator.x += moveOffscreenSpeed;
				if (lightIndicator.x > 2048 + lightIndicator.width) {
					lightIndicator.visible = false;
				}
			};
		});
		lightIndicators2.forEach(function (lightIndicator) {
			lightIndicator.update = function () {
				lightIndicator.x += moveOffscreenSpeed;
				if (lightIndicator.x > 2048 + lightIndicator.width) {
					lightIndicator.visible = false;
				}
			};
		});
		staticLightIndicators1.forEach(function (lightIndicator) {
			lightIndicator.update = function () {
				lightIndicator.x += moveOffscreenSpeed;
				if (lightIndicator.x > 2048 + lightIndicator.width) {
					lightIndicator.visible = false;
				}
			};
		});
		staticLightIndicators2.forEach(function (lightIndicator) {
			lightIndicator.update = function () {
				lightIndicator.x += moveOffscreenSpeed;
				if (lightIndicator.x > 2048 + lightIndicator.width) {
					lightIndicator.visible = false;
				}
			};
		});
		lightBackgrounds.forEach(function (lightBackground) {
			lightBackground.update = function () {
				lightBackground.x += moveOffscreenSpeed;
				if (lightBackground.x > 2048 + lightBackground.width) {
					lightBackground.visible = false;
				}
			};
		});
		centerAsset.update = function () {
			centerAsset.x += moveOffscreenSpeed;
			if (centerAsset.x > 2048 + centerAsset.width) {
				centerAsset.visible = false;
			}
		};
	}
	if (touchCount == 2 && !jumpstart) {
		game.down = null; // Disable further taps
		// Add the current score to the score records with 'YOU' as the name
		var youScore = {
			name: 'YOU',
			score: LK.getScore()
		};
		var inserted = false;
		for (var i = 0; i < scoreRecords.length; i++) {
			if (scoreRecords[i].name === 'YOU') {
				continue;
			}
			if (youScore.score < scoreRecords[i].score) {
				scoreRecords.splice(i, 0, youScore);
				inserted = true;
				break;
			}
		}
		if (!inserted) {
			if (youScore.score >= 450 && !scoreRecords.some(function (record) {
				return record.name === 'YOU';
			})) {
				scoreRecords.push(youScore);
			} else if (!scoreRecords.some(function (record) {
				return record.name === 'YOU';
			})) {
				scoreRecords.push(youScore);
				scoreRecords = scoreRecords.slice(0, 10); // Limit to top 10 scores including the player
			}
		}
		// Ensure names in the score table remain in the same order
		var names = ['Verstappen', 'Leclerc', 'Norris', 'Colapinto', 'Sainz', 'Hamilton', 'Piastri', 'Russel', 'Tsunoda', 'Albon'];
		var youIndex = scoreRecords.findIndex(function (record) {
			return record.name === 'YOU';
		});
		for (var i = 0; i < scoreRecords.length; i++) {
			if (i !== youIndex) {
				scoreRecords[i].name = names[i < youIndex ? i : i - 1];
			}
		}
		// Create a table for the score records
		var scoreTable = new Container();
		scoreTable.x = 2048 / 2 - 100;
		scoreTable.y = 2732 / 2 - 650;
		scoreTable.pivot.set(scoreTable.width / 2, 0);
		// Add table headers
		var headers = ["Rank", "Driver", "Time (s)"];
		for (var i = 0; i < headers.length; i++) {
			var headerText = new Text2(headers[i], {
				size: 90,
				fill: "#000000",
				stroke: "#000000",
				strokeThickness: 1,
				font: "'Formula 1', sans-serif",
				align: 'center'
			});
			headerText.x = i * 600 - 700 + (i === 0 ? 100 : 0); // Adjust spacing between columns, move rank header 100 pixels to the right
			headerText.y = 0;
			scoreTable.addChild(headerText);
		}
		// Add score records to the table
		for (var i = 0; i < scoreRecords.length; i++) {
			var rankText = new Text2(scoreRecords[i].name === 'YOU' && youScore.score > 450 ? scoreRecords.length.toString() : (i + 1).toString(), {
				size: scoreRecords[i].name === 'YOU' ? 90 : 80,
				// Increase size by 30% if it's the player
				fill: scoreRecords[i].name === 'YOU' ? "#FF1801" : "#000000",
				stroke: "#000000",
				strokeThickness: 1,
				font: "'Formula 1', sans-serif",
				align: 'center'
			});
			rankText.x = -600; // Align rank column
			rankText.y = (i + 1) * 100;
			scoreTable.addChild(rankText);
			if (scoreRecords[i].name && scoreRecords[i].name !== 'YOU') {
				var flagBackground = LK.getAsset('flagBackground', {
					anchorX: 0.5,
					anchorY: 0.1,
					x: -200,
					y: (i + 1) * 100 + 18
				});
				scoreTable.addChild(flagBackground);
				var flagAsset = LK.getAsset('flag_' + scoreRecords[i].name.toLowerCase(), {
					anchorX: 0.5,
					anchorY: 0.1,
					x: -200,
					y: (i + 1) * 100 + 20
				});
				scoreTable.addChild(flagAsset);
			}
			var nameText = new Text2(scoreRecords[i].name || "", {
				size: scoreRecords[i].name === 'YOU' ? 90 : 80,
				fill: scoreRecords[i].name === 'YOU' ? "#FF1801" : "#000000",
				stroke: "#000000",
				strokeThickness: 1,
				font: "'Formula 1', sans-serif",
				align: 'center'
			});
			nameText.x = -100; // Align name column
			nameText.y = (i + 1) * 100;
			scoreTable.addChild(nameText);
			var timeText = new Text2((scoreRecords[i].score / 1000).toFixed(3) + "s", {
				size: scoreRecords[i].name === 'YOU' ? 90 : 80,
				fill: scoreRecords[i].name === 'YOU' ? "#FF1801" : "#000000",
				stroke: "#000000",
				strokeThickness: 1,
				font: "'Formula 1', sans-serif",
				align: 'center'
			});
			timeText.x = 500; // Align time column
			timeText.y = (i + 1) * 100;
			scoreTable.addChild(timeText);
		}
		if (youScore.score > 450 && !scoreRecords.some(function (record) {
			return record.name === 'YOU';
		})) {
			var rankText = new Text2(scoreRecords.length.toString(), {
				size: 90,
				fill: "#FF1801",
				stroke: "#000000",
				strokeThickness: 0,
				font: "'Formula 1', sans-serif",
				align: 'center'
			});
			rankText.x = -600;
			rankText.y = (scoreRecords.length + 1) * 100;
			scoreTable.addChild(rankText);
			var nameText = new Text2('YOU', {
				size: 90,
				fill: "#FF1801",
				stroke: "#000000",
				strokeThickness: 0,
				font: "'Formula 1', sans-serif",
				align: 'center'
			});
			nameText.x = -100;
			nameText.y = (scoreRecords.length + 1) * 100;
			scoreTable.addChild(nameText);
			var timeText = new Text2((youScore.score / 1000).toFixed(3) + "s", {
				size: 90,
				fill: "#FF1801",
				stroke: "#000000",
				strokeThickness: 0,
				font: "'Formula 1', sans-serif",
				align: 'center'
			});
			timeText.x = 500;
			timeText.y = (scoreRecords.length + 1) * 100;
			scoreTable.addChild(timeText);
		}
		scoreTable.x = -scoreTable.width; // Start offscreen to the left
		game.addChild(scoreTable);
		// Animate the score table to its final position
		var targetX = 2048 / 2 - 100;
		var animationDuration = 60; // Duration in ticks (1 second)
		var animationStep = (targetX - scoreTable.x) / animationDuration;
		game.update = function () {
			if (scoreTable.x < targetX) {
				scoreTable.x += animationStep;
				if (scoreTable.x >= targetX) {
					scoreTable.x = targetX; // Ensure it stops exactly at the target position
				}
			}
		};
		// No background image needed for the records table
		// Removed grid lines for the ranked table
		LK.setTimeout(function () {
			touchCount = 0; // Reset touch count
			game.down = handleTap; // Re-enable tap
			LK.showGameOver();
		}, 5000); // Show game over after 5 seconds to allow time to read the scores
	}
}
// Initialize light indicators
var lightIndicators = [];
var lightIndicators2 = [];
var lightBackgrounds = [];
var staticLightIndicators1 = [];
var staticLightIndicators2 = [];
for (var i = 0; i < 5; i++) {
	var lightBackground = game.addChild(LK.getAsset('lightbackground', {
		anchorX: 0.5,
		anchorY: 0.5,
		x: 2048 / 2 + (i - 2) * 200,
		y: 2732 / 2 - 70
	}));
	lightBackgrounds.push(lightBackground);
	var lightIndicator = game.addChild(new LightIndicator());
	lightIndicator.x = 2048 / 2 + (i - 2) * 200; // Position lights horizontally with some spacing
	lightIndicator.y = 2732 / 2;
	lightIndicator.setColor(0x9f9d9d); // Set initial light color to white
	lightIndicators.push(lightIndicator);
	var lightIndicator2 = game.addChild(new LightIndicator());
	lightIndicator2.x = 2048 / 2 + (i - 2) * 200; // Position lights horizontally with some spacing
	lightIndicator2.y = 2732 / 2 + 150; // Position the second line of lights 200px below the first line
	lightIndicator2.setColor(0x9f9d9d); // Set initial light color to white
	lightIndicators2.push(lightIndicator2);
	// Add static lights
	var staticLight1 = game.addChild(new LightIndicator());
	staticLight1.x = 2048 / 2 + (i - 2) * 200; // Position lights horizontally with some spacing
	staticLight1.y = 2732 / 2 - 150; // Position the first line of static lights 200px above the first line
	staticLight1.setColor(0x9f9d9d); // Set initial light color to white
	staticLightIndicators1.push(staticLight1);
	var staticLight2 = game.addChild(new LightIndicator());
	staticLight2.x = 2048 / 2 + (i - 2) * 200; // Position lights horizontally with some spacing
	staticLight2.y = 2732 / 2 - 300; // Position the second line of static lights 400px above the first line
	staticLight2.setColor(0x9f9d9d); // Set initial light color to white
	staticLightIndicators2.push(staticLight2);
}
// Initialize reaction time text
reactionTimeText = new Text2('00.000', {
	size: 200,
	fill: "#000000"
});
reactionTimeText.anchor.set(0.5, 1);
reactionTimeText.y -= 250; // Move reaction time 400 pixels up
LK.gui.bottom.addChild(reactionTimeText);
// Add instruction text below the lights
var instructionText = new Text2("Tap to start the race. Tap again when the lights go out.", {
	size: 70,
	fill: "#000000",
	font: "'Formula 1', sans-serif"
});
instructionText.anchor.set(0.5, 0);
instructionText.x = 2048 / 2;
instructionText.y = 2732 / 2 + 450;
game.addChild(instructionText);
// Add 'f1' asset to the top of the screen
var f1Asset = game.addChild(LK.getAsset('f1', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: 2048 / 2,
	y: 400
}));
// Add 'centerAsset' to the center of the lights
var centerAsset = game.addChildAt(LK.getAsset('centerAsset', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: 2048 / 2,
	y: 2732 / 2 - 60
}), 0);
// Set up event listeners
game.down = handleTap;
game.update = function () {
	// Add a slight increase and decrease in size to the instruction text
	instructionText.scale.x = 1 + 0.02 * Math.sin(LK.ticks / 20);
	instructionText.scale.y = 1 + 0.02 * Math.sin(LK.ticks / 20);
	// Update positions of elements moving offscreen
	lightIndicators.forEach(function (lightIndicator) {
		if (lightIndicator.update) {
			lightIndicator.update();
		}
	});
	lightIndicators2.forEach(function (lightIndicator) {
		if (lightIndicator.update) {
			lightIndicator.update();
		}
	});
	staticLightIndicators1.forEach(function (lightIndicator) {
		if (lightIndicator.update) {
			lightIndicator.update();
		}
	});
	staticLightIndicators2.forEach(function (lightIndicator) {
		if (lightIndicator.update) {
			lightIndicator.update();
		}
	});
	lightBackgrounds.forEach(function (lightBackground) {
		if (lightBackground.update) {
			lightBackground.update();
		}
	});
	if (centerAsset.update) {
		centerAsset.update();
	}
};