/**** 
* Classes
****/ 
// Enemy class
var Enemy = Container.expand(function (side) {
	var self = Container.call(this);
	var enemyGraphics = self.attachAsset('enemy', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.side = side;
	self.update = function () {
		// Enemies do not move
	};
});
//<Assets used in the game will automatically appear here>
// Fighter class
var Fighter = Container.expand(function () {
	var self = Container.call(this);
	var fighterGraphics = self.attachAsset('fighter', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.update = function () {
		// Fighter does not move
	};
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000000 //Init game with black background 
});
/**** 
* Game Code
****/ 
// Initialize variables
var fighter;
var enemies = [];
var score = 0;
var scoreTxt;
// Initialize game elements
function initGame() {
	// Play background music
	LK.playMusic('backgroundMusic');
	// Create and position the background
	var background = game.addChild(LK.getAsset('background', {
		anchorX: 0.5,
		anchorY: 0.5,
		x: 2048 / 2,
		y: 2732 / 2
	}));
	// Create and position the fighter in the middle of the screen
	fighter = new Fighter();
	fighter.x = 2048 / 2;
	fighter.y = 2732 / 2;
	game.addChild(fighter);
	// Add bouncing animation to the fighter
	var bounceDirection = 1;
	fighter.update = function () {
		// Change the y position of the fighter
		this.y += 2 * bounceDirection;
		// Reverse the direction if the fighter reaches the top or bottom of the bounce
		if (this.y > 2732 / 2 + 10 || this.y < 2732 / 2 - 10) {
			bounceDirection *= -1;
		}
	};
	// Create score text
	scoreTxt = new Text2('0', {
		size: 150,
		fill: "#ffffff"
	});
	scoreTxt.anchor.set(0.5, 0);
	LK.gui.top.addChild(scoreTxt);
	// Initialize super combo asset and set it to invisible initially
	superCombo = game.addChild(LK.getAsset('supercombo', {
		anchorX: 0.5,
		anchorY: 0.5,
		x: 2048 / 2,
		y: 2732 / 4,
		// Position it up the screen
		scaleX: 5,
		// Make it 5x bigger
		scaleY: 5 // Make it 5x bigger
	}));
	superCombo.visible = false;
	// Spawn enemies at intervals
	LK.setInterval(spawnEnemy, 2000);
}
// Spawn an enemy on the left or right side
function spawnEnemy() {
	// If an enemy already exists, do not spawn a new one
	if (enemies.length > 0) {
		return;
	}
	var side = Math.random() < 0.5 ? 'left' : 'right';
	var enemy = new Enemy(side);
	if (side === 'left') {
		enemy.x = 100;
		enemy.scale.x = 1; // face right
	} else {
		enemy.x = 2048 - 100;
		enemy.scale.x = -1; // face left
	}
	enemy.y = fighter.y; // adjust y position to match the player's position
	enemies.push(enemy);
	game.addChild(enemy);
	// Set a timeout to trigger game over if the enemy is not defeated within 2 seconds
	enemy.timeout = LK.setTimeout(function () {
		if (enemies.includes(enemy)) {
			LK.effects.flashScreen(0xff0000, 1000);
			LK.showGameOver();
			enemies.splice(enemies.indexOf(enemy), 1);
			enemy.destroy();
		}
	}, 2000);
}
// Handle touch events
game.down = function (x, y, obj) {
	var game_position = game.toLocal(obj.global);
	if (game_position.x < 2048 / 2) {
		handleAttack('left');
	} else {
		handleAttack('right');
	}
};
// Handle attack
function handleAttack(side) {
	for (var i = enemies.length - 1; i >= 0; i--) {
		if (enemies[i].side === side) {
			LK.clearTimeout(enemies[i].timeout);
			// Add flash effect
			LK.effects.flashScreen(0xffffff, 100);
			// Play punch sound effect
			LK.getSound('punchSound').play();
			enemies[i].destroy();
			enemies.splice(i, 1);
			score++;
			scoreTxt.setText(score);
			// Check if 10 enemies are defeated
			if (score % 10 === 0) {
				// Play super combo sound
				LK.getSound('superComboSound').play();
				// Add super combo asset
				var superCombo = game.addChild(LK.getAsset('supercombo', {
					anchorX: 0.5,
					anchorY: 0.5,
					x: 2048 / 2,
					y: 2732 / 4,
					// Position it up the screen
					scaleX: 5,
					// Make it 5x bigger
					scaleY: 5 // Make it 5x bigger
				}));
				// Hide score text and show super combo asset
				scoreTxt.visible = false;
				superCombo.visible = true;
				superCombo.scale.set(5, 5); // Ensure it is 5x bigger
				superCombo.y = 2732 / 4; // Ensure it appears up the screen
				// Remove super combo asset and show score text after 1 second
				LK.setTimeout(function () {
					superCombo.visible = false;
					scoreTxt.visible = true;
				}, 1000);
			}
			break;
		}
	}
}
// Initialize game
initGame();
// Update game state
game.update = function () {
	// No additional update logic needed for this simple game
}; /**** 
* Classes
****/ 
// Enemy class
var Enemy = Container.expand(function (side) {
	var self = Container.call(this);
	var enemyGraphics = self.attachAsset('enemy', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.side = side;
	self.update = function () {
		// Enemies do not move
	};
});
//<Assets used in the game will automatically appear here>
// Fighter class
var Fighter = Container.expand(function () {
	var self = Container.call(this);
	var fighterGraphics = self.attachAsset('fighter', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.update = function () {
		// Fighter does not move
	};
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000000 //Init game with black background 
});
/**** 
* Game Code
****/ 
// Initialize variables
var fighter;
var enemies = [];
var score = 0;
var scoreTxt;
// Initialize game elements
function initGame() {
	// Play background music
	LK.playMusic('backgroundMusic');
	// Create and position the background
	var background = game.addChild(LK.getAsset('background', {
		anchorX: 0.5,
		anchorY: 0.5,
		x: 2048 / 2,
		y: 2732 / 2
	}));
	// Create and position the fighter in the middle of the screen
	fighter = new Fighter();
	fighter.x = 2048 / 2;
	fighter.y = 2732 / 2;
	game.addChild(fighter);
	// Add bouncing animation to the fighter
	var bounceDirection = 1;
	fighter.update = function () {
		// Change the y position of the fighter
		this.y += 2 * bounceDirection;
		// Reverse the direction if the fighter reaches the top or bottom of the bounce
		if (this.y > 2732 / 2 + 10 || this.y < 2732 / 2 - 10) {
			bounceDirection *= -1;
		}
	};
	// Create score text
	scoreTxt = new Text2('0', {
		size: 150,
		fill: "#ffffff"
	});
	scoreTxt.anchor.set(0.5, 0);
	LK.gui.top.addChild(scoreTxt);
	// Initialize super combo asset and set it to invisible initially
	superCombo = game.addChild(LK.getAsset('supercombo', {
		anchorX: 0.5,
		anchorY: 0.5,
		x: 2048 / 2,
		y: 2732 / 4,
		// Position it up the screen
		scaleX: 5,
		// Make it 5x bigger
		scaleY: 5 // Make it 5x bigger
	}));
	superCombo.visible = false;
	// Spawn enemies at intervals
	LK.setInterval(spawnEnemy, 2000);
}
// Spawn an enemy on the left or right side
function spawnEnemy() {
	// If an enemy already exists, do not spawn a new one
	if (enemies.length > 0) {
		return;
	}
	var side = Math.random() < 0.5 ? 'left' : 'right';
	var enemy = new Enemy(side);
	if (side === 'left') {
		enemy.x = 100;
		enemy.scale.x = 1; // face right
	} else {
		enemy.x = 2048 - 100;
		enemy.scale.x = -1; // face left
	}
	enemy.y = fighter.y; // adjust y position to match the player's position
	enemies.push(enemy);
	game.addChild(enemy);
	// Set a timeout to trigger game over if the enemy is not defeated within 2 seconds
	enemy.timeout = LK.setTimeout(function () {
		if (enemies.includes(enemy)) {
			LK.effects.flashScreen(0xff0000, 1000);
			LK.showGameOver();
			enemies.splice(enemies.indexOf(enemy), 1);
			enemy.destroy();
		}
	}, 2000);
}
// Handle touch events
game.down = function (x, y, obj) {
	var game_position = game.toLocal(obj.global);
	if (game_position.x < 2048 / 2) {
		handleAttack('left');
	} else {
		handleAttack('right');
	}
};
// Handle attack
function handleAttack(side) {
	for (var i = enemies.length - 1; i >= 0; i--) {
		if (enemies[i].side === side) {
			LK.clearTimeout(enemies[i].timeout);
			// Add flash effect
			LK.effects.flashScreen(0xffffff, 100);
			// Play punch sound effect
			LK.getSound('punchSound').play();
			enemies[i].destroy();
			enemies.splice(i, 1);
			score++;
			scoreTxt.setText(score);
			// Check if 10 enemies are defeated
			if (score % 10 === 0) {
				// Play super combo sound
				LK.getSound('superComboSound').play();
				// Add super combo asset
				var superCombo = game.addChild(LK.getAsset('supercombo', {
					anchorX: 0.5,
					anchorY: 0.5,
					x: 2048 / 2,
					y: 2732 / 4,
					// Position it up the screen
					scaleX: 5,
					// Make it 5x bigger
					scaleY: 5 // Make it 5x bigger
				}));
				// Hide score text and show super combo asset
				scoreTxt.visible = false;
				superCombo.visible = true;
				superCombo.scale.set(5, 5); // Ensure it is 5x bigger
				superCombo.y = 2732 / 4; // Ensure it appears up the screen
				// Remove super combo asset and show score text after 1 second
				LK.setTimeout(function () {
					superCombo.visible = false;
					scoreTxt.visible = true;
				}, 1000);
			}
			break;
		}
	}
}
// Initialize game
initGame();
// Update game state
game.update = function () {
	// No additional update logic needed for this simple game
};