/**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
	currentLevel: 1,
	currentWorld: 1,
	currency: 0,
	selectedCharacter: "artemida",
	playerHealth: 100,
	playerAttack: 10,
	playerDefense: 5
});
/**** 
* Classes
****/ 
var Enemy = Container.expand(function (type) {
	var self = Container.call(this);
	self.type = type || 'normal'; // normal, boss, ultimateBoss
	self.projectiles = [];
	self.attackTimer = 0;
	// Set properties based on enemy type
	if (self.type === 'normal') {
		self.health = 30 + (currentWorld - 1) * 10 + (currentLevel - 1);
		self.maxHealth = self.health;
		self.attack = 5 + (currentWorld - 1) * 2 + Math.floor((currentLevel - 1) / 10);
		self.attackInterval = 120; // 2 seconds
		self.reward = 25;
		var assetId = 'enemyShape';
	} else if (self.type === 'boss') {
		self.health = 100 + (currentWorld - 1) * 30 + (currentLevel - 1) * 2;
		self.maxHealth = self.health;
		self.attack = 8 + (currentWorld - 1) * 3 + Math.floor((currentLevel - 1) / 10) * 2;
		self.attackInterval = 90; // 1.5 seconds
		self.reward = 50;
		var assetId = 'bossShape';
	} else {
		// ultimateBoss
		self.health = 200 + (currentWorld - 1) * 50 + (currentLevel - 1) * 3;
		self.maxHealth = self.health;
		self.attack = 12 + (currentWorld - 1) * 4 + Math.floor((currentLevel - 1) / 10) * 3;
		self.attackInterval = 60; // 1 second
		self.reward = 65;
		var assetId = 'ultimateBossShape';
	}
	var enemyGraphics = self.attachAsset(assetId, {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Create health bar
	self.healthBarBg = self.addChild(LK.getAsset('healthBarBg', {
		anchorX: 0.5,
		anchorY: 0.5,
		y: -80
	}));
	self.healthBarFg = self.addChild(LK.getAsset('healthBarFg', {
		anchorX: 0,
		anchorY: 0.5,
		y: -80,
		x: -150
	}));
	self.updateHealthBar = function () {
		var healthPercent = self.health / self.maxHealth;
		self.healthBarFg.width = 300 * healthPercent;
	};
	self.takeDamage = function (amount) {
		self.health -= amount;
		// Flash red when taking damage
		LK.effects.flashObject(enemyGraphics, 0xff0000, 300);
		LK.getSound('enemyHit').play();
		self.updateHealthBar();
		if (self.health <= 0) {
			self.health = 0;
			self.updateHealthBar();
			// Add currency reward
			storage.currency = (storage.currency || 0) + self.reward;
			// Update HUD
			updateCurrencyDisplay();
			// Complete level
			completeLevel();
			// Play appropriate sound
			if (self.type === 'normal') {
				LK.getSound('levelComplete').play();
			} else {
				LK.getSound('bossDefeat').play();
			}
		}
	};
	self.attackPlayer = function () {
		var projectile = new Projectile(false);
		projectile.x = self.x;
		projectile.y = self.y + 50;
		self.projectiles.push(projectile);
		game.addChild(projectile);
	};
	self.update = function () {
		self.attackTimer++;
		if (self.attackTimer >= self.attackInterval) {
			self.attackTimer = 0;
			self.attackPlayer();
		}
		// Add some movement for enemies
		if (self.type === 'normal') {
			self.x += Math.sin(LK.ticks / 30) * 2;
		} else if (self.type === 'boss') {
			self.x += Math.sin(LK.ticks / 40) * 3;
		} else {
			// ultimateBoss
			self.x += Math.sin(LK.ticks / 50) * 4;
			self.y += Math.sin(LK.ticks / 70) * 2;
		}
	};
	return self;
});
var Player = Container.expand(function () {
	var self = Container.call(this);
	self.health = storage.playerHealth || 100;
	self.maxHealth = 100;
	self.attack = storage.playerAttack || 10;
	self.defense = storage.playerDefense || 5;
	self.characterType = storage.selectedCharacter || 'artemida';
	self.projectiles = [];
	self.attackCooldown = 0;
	self.maxAttackCooldown = 30; // half a second at 60fps
	var playerGraphics = self.attachAsset('playerShape', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Create health bar
	self.healthBarBg = self.addChild(LK.getAsset('healthBarBg', {
		anchorX: 0.5,
		anchorY: 0.5,
		y: -80
	}));
	self.healthBarFg = self.addChild(LK.getAsset('healthBarFg', {
		anchorX: 0,
		anchorY: 0.5,
		y: -80,
		x: -150
	}));
	self.updateHealthBar = function () {
		var healthPercent = self.health / self.maxHealth;
		self.healthBarFg.width = 300 * healthPercent;
	};
	self.takeDamage = function (amount) {
		var actualDamage = Math.max(1, amount - self.defense);
		self.health -= actualDamage;
		// Flash red when taking damage
		LK.effects.flashObject(playerGraphics, 0xff0000, 500);
		LK.getSound('playerHit').play();
		self.updateHealthBar();
		if (self.health <= 0) {
			self.health = 0;
			self.updateHealthBar();
			LK.effects.flashScreen(0xff0000, 1000);
			LK.showGameOver();
		}
	};
	self.attack = function () {
		if (self.attackCooldown <= 0) {
			var projectile = new Projectile(true);
			projectile.x = self.x;
			projectile.y = self.y - 50;
			self.projectiles.push(projectile);
			game.addChild(projectile);
			LK.getSound('attack').play();
			self.attackCooldown = self.maxAttackCooldown;
		}
	};
	self.update = function () {
		if (self.attackCooldown > 0) {
			self.attackCooldown--;
		}
	};
	self.down = function (x, y, obj) {
		self.attack();
	};
	return self;
});
var Projectile = Container.expand(function (isPlayerProjectile) {
	var self = Container.call(this);
	self.isPlayerProjectile = isPlayerProjectile || false;
	self.speed = self.isPlayerProjectile ? -10 : 7; // Negative for upward movement
	self.damage = self.isPlayerProjectile ? player.attack : currentEnemy.attack / 2;
	var assetId = self.isPlayerProjectile ? 'projectileShape' : 'enemyProjectileShape';
	var projectileGraphics = self.attachAsset(assetId, {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.update = function () {
		self.y += self.speed;
		// Check if projectile is off screen
		if (self.y < -50 || self.y > 2732 + 50) {
			self.shouldRemove = true;
		}
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x222244
});
/**** 
* Game Code
****/ 
// Game variables
var currentWorld = storage.currentWorld || 1;
var currentLevel = storage.currentLevel || 1;
var player;
var currentEnemy;
var isLevelActive = false;
var levelText;
var currencyText;
// Initialize game
function initGame() {
	player = new Player();
	player.x = 2048 / 2;
	player.y = 2732 - 300;
	game.addChild(player);
	createHUD();
	startLevel();
	LK.playMusic('bgMusic');
}
function createHUD() {
	// Level indicator
	var levelIndicatorBg = LK.getAsset('levelIndicator', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	levelText = new Text2('World ' + currentWorld + ' - Level ' + currentLevel, {
		size: 40,
		fill: 0xFFFFFF
	});
	levelText.anchor.set(0.5, 0.5);
	levelIndicatorBg.addChild(levelText);
	LK.gui.top.addChild(levelIndicatorBg);
	// Currency indicator
	var currencyIndicatorBg = LK.getAsset('currencyIndicator', {
		anchorX: 0.5,
		anchorY: 0.5,
		x: 400
	});
	currencyText = new Text2('$' + (storage.currency || 0), {
		size: 40,
		fill: 0xFFFFFF
	});
	currencyText.anchor.set(0.5, 0.5);
	currencyIndicatorBg.addChild(currencyText);
	LK.gui.top.addChild(currencyIndicatorBg);
}
function updateCurrencyDisplay() {
	currencyText.setText('$' + (storage.currency || 0));
}
function startLevel() {
	isLevelActive = true;
	// Determine enemy type based on level
	var enemyType = 'normal';
	if (currentLevel === 100) {
		enemyType = 'ultimateBoss';
	} else if ([10, 20, 30, 40, 50, 60, 70, 80, 90, 110, 120].indexOf(currentLevel) !== -1) {
		enemyType = 'boss';
	}
	// Create enemy
	currentEnemy = new Enemy(enemyType);
	currentEnemy.x = 2048 / 2;
	currentEnemy.y = 300;
	game.addChild(currentEnemy);
	// Update level display
	levelText.setText('World ' + currentWorld + ' - Level ' + currentLevel);
}
function completeLevel() {
	isLevelActive = false;
	// Save progress
	currentLevel++;
	if (currentLevel > 120) {
		currentLevel = 1;
		currentWorld++;
		if (currentWorld > 6) {
			currentWorld = 6; // Cap at 6 worlds
			LK.showYouWin();
			return;
		}
	}
	storage.currentLevel = currentLevel;
	storage.currentWorld = currentWorld;
	// Start next level after a delay
	LK.setTimeout(function () {
		startLevel();
	}, 2000);
}
function checkCollisions() {
	// Check player projectiles against enemy
	for (var i = player.projectiles.length - 1; i >= 0; i--) {
		var projectile = player.projectiles[i];
		if (projectile.shouldRemove) {
			projectile.destroy();
			player.projectiles.splice(i, 1);
			continue;
		}
		if (projectile.intersects(currentEnemy)) {
			currentEnemy.takeDamage(projectile.damage);
			projectile.destroy();
			player.projectiles.splice(i, 1);
		}
	}
	// Check enemy projectiles against player
	for (var j = currentEnemy.projectiles.length - 1; j >= 0; j--) {
		var enemyProjectile = currentEnemy.projectiles[j];
		if (enemyProjectile.shouldRemove) {
			enemyProjectile.destroy();
			currentEnemy.projectiles.splice(j, 1);
			continue;
		}
		if (enemyProjectile.intersects(player)) {
			player.takeDamage(enemyProjectile.damage);
			enemyProjectile.destroy();
			currentEnemy.projectiles.splice(j, 1);
		}
	}
}
// Touch/mouse events
game.down = function (x, y, obj) {
	if (isLevelActive) {
		player.x = x;
	}
};
game.move = function (x, y, obj) {
	if (isLevelActive) {
		player.x = x;
	}
};
// Game update loop
game.update = function () {
	if (!isLevelActive) {
		return;
	}
	// Check collisions
	checkCollisions();
};
// Initialize the game
initGame(); /**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
	currentLevel: 1,
	currentWorld: 1,
	currency: 0,
	selectedCharacter: "artemida",
	playerHealth: 100,
	playerAttack: 10,
	playerDefense: 5
});
/**** 
* Classes
****/ 
var Enemy = Container.expand(function (type) {
	var self = Container.call(this);
	self.type = type || 'normal'; // normal, boss, ultimateBoss
	self.projectiles = [];
	self.attackTimer = 0;
	// Set properties based on enemy type
	if (self.type === 'normal') {
		self.health = 30 + (currentWorld - 1) * 10 + (currentLevel - 1);
		self.maxHealth = self.health;
		self.attack = 5 + (currentWorld - 1) * 2 + Math.floor((currentLevel - 1) / 10);
		self.attackInterval = 120; // 2 seconds
		self.reward = 25;
		var assetId = 'enemyShape';
	} else if (self.type === 'boss') {
		self.health = 100 + (currentWorld - 1) * 30 + (currentLevel - 1) * 2;
		self.maxHealth = self.health;
		self.attack = 8 + (currentWorld - 1) * 3 + Math.floor((currentLevel - 1) / 10) * 2;
		self.attackInterval = 90; // 1.5 seconds
		self.reward = 50;
		var assetId = 'bossShape';
	} else {
		// ultimateBoss
		self.health = 200 + (currentWorld - 1) * 50 + (currentLevel - 1) * 3;
		self.maxHealth = self.health;
		self.attack = 12 + (currentWorld - 1) * 4 + Math.floor((currentLevel - 1) / 10) * 3;
		self.attackInterval = 60; // 1 second
		self.reward = 65;
		var assetId = 'ultimateBossShape';
	}
	var enemyGraphics = self.attachAsset(assetId, {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Create health bar
	self.healthBarBg = self.addChild(LK.getAsset('healthBarBg', {
		anchorX: 0.5,
		anchorY: 0.5,
		y: -80
	}));
	self.healthBarFg = self.addChild(LK.getAsset('healthBarFg', {
		anchorX: 0,
		anchorY: 0.5,
		y: -80,
		x: -150
	}));
	self.updateHealthBar = function () {
		var healthPercent = self.health / self.maxHealth;
		self.healthBarFg.width = 300 * healthPercent;
	};
	self.takeDamage = function (amount) {
		self.health -= amount;
		// Flash red when taking damage
		LK.effects.flashObject(enemyGraphics, 0xff0000, 300);
		LK.getSound('enemyHit').play();
		self.updateHealthBar();
		if (self.health <= 0) {
			self.health = 0;
			self.updateHealthBar();
			// Add currency reward
			storage.currency = (storage.currency || 0) + self.reward;
			// Update HUD
			updateCurrencyDisplay();
			// Complete level
			completeLevel();
			// Play appropriate sound
			if (self.type === 'normal') {
				LK.getSound('levelComplete').play();
			} else {
				LK.getSound('bossDefeat').play();
			}
		}
	};
	self.attackPlayer = function () {
		var projectile = new Projectile(false);
		projectile.x = self.x;
		projectile.y = self.y + 50;
		self.projectiles.push(projectile);
		game.addChild(projectile);
	};
	self.update = function () {
		self.attackTimer++;
		if (self.attackTimer >= self.attackInterval) {
			self.attackTimer = 0;
			self.attackPlayer();
		}
		// Add some movement for enemies
		if (self.type === 'normal') {
			self.x += Math.sin(LK.ticks / 30) * 2;
		} else if (self.type === 'boss') {
			self.x += Math.sin(LK.ticks / 40) * 3;
		} else {
			// ultimateBoss
			self.x += Math.sin(LK.ticks / 50) * 4;
			self.y += Math.sin(LK.ticks / 70) * 2;
		}
	};
	return self;
});
var Player = Container.expand(function () {
	var self = Container.call(this);
	self.health = storage.playerHealth || 100;
	self.maxHealth = 100;
	self.attack = storage.playerAttack || 10;
	self.defense = storage.playerDefense || 5;
	self.characterType = storage.selectedCharacter || 'artemida';
	self.projectiles = [];
	self.attackCooldown = 0;
	self.maxAttackCooldown = 30; // half a second at 60fps
	var playerGraphics = self.attachAsset('playerShape', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Create health bar
	self.healthBarBg = self.addChild(LK.getAsset('healthBarBg', {
		anchorX: 0.5,
		anchorY: 0.5,
		y: -80
	}));
	self.healthBarFg = self.addChild(LK.getAsset('healthBarFg', {
		anchorX: 0,
		anchorY: 0.5,
		y: -80,
		x: -150
	}));
	self.updateHealthBar = function () {
		var healthPercent = self.health / self.maxHealth;
		self.healthBarFg.width = 300 * healthPercent;
	};
	self.takeDamage = function (amount) {
		var actualDamage = Math.max(1, amount - self.defense);
		self.health -= actualDamage;
		// Flash red when taking damage
		LK.effects.flashObject(playerGraphics, 0xff0000, 500);
		LK.getSound('playerHit').play();
		self.updateHealthBar();
		if (self.health <= 0) {
			self.health = 0;
			self.updateHealthBar();
			LK.effects.flashScreen(0xff0000, 1000);
			LK.showGameOver();
		}
	};
	self.attack = function () {
		if (self.attackCooldown <= 0) {
			var projectile = new Projectile(true);
			projectile.x = self.x;
			projectile.y = self.y - 50;
			self.projectiles.push(projectile);
			game.addChild(projectile);
			LK.getSound('attack').play();
			self.attackCooldown = self.maxAttackCooldown;
		}
	};
	self.update = function () {
		if (self.attackCooldown > 0) {
			self.attackCooldown--;
		}
	};
	self.down = function (x, y, obj) {
		self.attack();
	};
	return self;
});
var Projectile = Container.expand(function (isPlayerProjectile) {
	var self = Container.call(this);
	self.isPlayerProjectile = isPlayerProjectile || false;
	self.speed = self.isPlayerProjectile ? -10 : 7; // Negative for upward movement
	self.damage = self.isPlayerProjectile ? player.attack : currentEnemy.attack / 2;
	var assetId = self.isPlayerProjectile ? 'projectileShape' : 'enemyProjectileShape';
	var projectileGraphics = self.attachAsset(assetId, {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.update = function () {
		self.y += self.speed;
		// Check if projectile is off screen
		if (self.y < -50 || self.y > 2732 + 50) {
			self.shouldRemove = true;
		}
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x222244
});
/**** 
* Game Code
****/ 
// Game variables
var currentWorld = storage.currentWorld || 1;
var currentLevel = storage.currentLevel || 1;
var player;
var currentEnemy;
var isLevelActive = false;
var levelText;
var currencyText;
// Initialize game
function initGame() {
	player = new Player();
	player.x = 2048 / 2;
	player.y = 2732 - 300;
	game.addChild(player);
	createHUD();
	startLevel();
	LK.playMusic('bgMusic');
}
function createHUD() {
	// Level indicator
	var levelIndicatorBg = LK.getAsset('levelIndicator', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	levelText = new Text2('World ' + currentWorld + ' - Level ' + currentLevel, {
		size: 40,
		fill: 0xFFFFFF
	});
	levelText.anchor.set(0.5, 0.5);
	levelIndicatorBg.addChild(levelText);
	LK.gui.top.addChild(levelIndicatorBg);
	// Currency indicator
	var currencyIndicatorBg = LK.getAsset('currencyIndicator', {
		anchorX: 0.5,
		anchorY: 0.5,
		x: 400
	});
	currencyText = new Text2('$' + (storage.currency || 0), {
		size: 40,
		fill: 0xFFFFFF
	});
	currencyText.anchor.set(0.5, 0.5);
	currencyIndicatorBg.addChild(currencyText);
	LK.gui.top.addChild(currencyIndicatorBg);
}
function updateCurrencyDisplay() {
	currencyText.setText('$' + (storage.currency || 0));
}
function startLevel() {
	isLevelActive = true;
	// Determine enemy type based on level
	var enemyType = 'normal';
	if (currentLevel === 100) {
		enemyType = 'ultimateBoss';
	} else if ([10, 20, 30, 40, 50, 60, 70, 80, 90, 110, 120].indexOf(currentLevel) !== -1) {
		enemyType = 'boss';
	}
	// Create enemy
	currentEnemy = new Enemy(enemyType);
	currentEnemy.x = 2048 / 2;
	currentEnemy.y = 300;
	game.addChild(currentEnemy);
	// Update level display
	levelText.setText('World ' + currentWorld + ' - Level ' + currentLevel);
}
function completeLevel() {
	isLevelActive = false;
	// Save progress
	currentLevel++;
	if (currentLevel > 120) {
		currentLevel = 1;
		currentWorld++;
		if (currentWorld > 6) {
			currentWorld = 6; // Cap at 6 worlds
			LK.showYouWin();
			return;
		}
	}
	storage.currentLevel = currentLevel;
	storage.currentWorld = currentWorld;
	// Start next level after a delay
	LK.setTimeout(function () {
		startLevel();
	}, 2000);
}
function checkCollisions() {
	// Check player projectiles against enemy
	for (var i = player.projectiles.length - 1; i >= 0; i--) {
		var projectile = player.projectiles[i];
		if (projectile.shouldRemove) {
			projectile.destroy();
			player.projectiles.splice(i, 1);
			continue;
		}
		if (projectile.intersects(currentEnemy)) {
			currentEnemy.takeDamage(projectile.damage);
			projectile.destroy();
			player.projectiles.splice(i, 1);
		}
	}
	// Check enemy projectiles against player
	for (var j = currentEnemy.projectiles.length - 1; j >= 0; j--) {
		var enemyProjectile = currentEnemy.projectiles[j];
		if (enemyProjectile.shouldRemove) {
			enemyProjectile.destroy();
			currentEnemy.projectiles.splice(j, 1);
			continue;
		}
		if (enemyProjectile.intersects(player)) {
			player.takeDamage(enemyProjectile.damage);
			enemyProjectile.destroy();
			currentEnemy.projectiles.splice(j, 1);
		}
	}
}
// Touch/mouse events
game.down = function (x, y, obj) {
	if (isLevelActive) {
		player.x = x;
	}
};
game.move = function (x, y, obj) {
	if (isLevelActive) {
		player.x = x;
	}
};
// Game update loop
game.update = function () {
	if (!isLevelActive) {
		return;
	}
	// Check collisions
	checkCollisions();
};
// Initialize the game
initGame();