/**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
/**** 
* Classes
****/ 
var FireTrigger = Container.expand(function () {
	var self = Container.call(this);
	var fireTriggerGraphics = self.attachAsset('fireTrigger', {
		anchorX: 0.5,
		anchorY: 0.5,
		alpha: 0 // Start with full transparency
	});
	// Add fade-in effect
	tween(fireTriggerGraphics, {
		alpha: 1 // Fade to full opacity
	}, {
		duration: 1000,
		// 1 second duration
		easing: tween.easeInOut
	});
	self.update = function () {
		if (!self.fireWall) {
			self.fireWall = LK.getAsset('fireWall', {
				anchorX: 0.5,
				anchorY: 0.5
			});
			self.fireWall.width = 2048; // Set width to match screen width
			self.fireWall.x = 2048 / 2; // Start from the center of the screen
			self.fireWall.y = 2732 + self.fireWall.height; // Start from bottom off-screen
			self.fireWall.speedX = 0; // No horizontal movement
			self.fireWall.speedY = -15; // Move upwards
			self.fireWall.update = function () {
				this.x += this.speedX;
				this.y += this.speedY;
				// Add flickering effect
				this.alpha = Math.random() * 0.5 + 0.5; // Random alpha between 0.5 and 1
			};
			game.addChild(self.fireWall);
			// Play firewall sound when it spawns
			LK.getSound('firewall').play();
			// Reposition the fire trigger to a random location
			self.x = Math.random() * (2048 - 400) + 200; // Ensure at least 200 pixels from left and right edges
			self.y = Math.random() * (2732 - 400) + 200; // Ensure at least 200 pixels from top and bottom edges
			// Add fade-in effect when repositioning
			tween(fireTriggerGraphics, {
				alpha: 1 // Fade to full opacity
			}, {
				duration: 1000,
				// 1 second duration
				easing: tween.easeInOut
			});
		}
		if (self.intersects(hero)) {
			self.fireWall = LK.getAsset('fireWall', {
				anchorX: 0.5,
				anchorY: 0.5
			});
			self.fireWall.width = 2048; // Set width to match screen width
			self.fireWall.x = 2048 / 2; // Start from the center of the screen
			self.fireWall.y = 2732 + self.fireWall.height; // Start from bottom off-screen
			self.fireWall.speedX = 0; // No horizontal movement
			self.fireWall.speedY = -15; // Move upwards
			self.fireWall.update = function () {
				this.x += this.speedX;
				this.y += this.speedY;
				// Add flickering effect
				this.alpha = Math.random() * 0.5 + 0.5; // Random alpha between 0.5 and 1
			};
			game.addChild(self.fireWall);
			// Play firewall sound when it spawns
			LK.getSound('firewall').play();
			// Reposition the fire trigger to a random location
			self.x = Math.random() * 2048;
			self.y = Math.random() * 2732;
		}
	};
});
// Hero class representing the player character
var Hero = Container.expand(function () {
	var self = Container.call(this);
	var heroGraphics = self.attachAsset('hero', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 10;
	// Add a shadow below the hero
	var shadow = self.attachAsset('heroShadow', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	shadow.y = heroGraphics.height / 2 + 10; // Position the shadow slightly below the hero
	shadow.alpha = 0.5; // Make the shadow semi-transparent
	// Add hitbox for collision detection
	self.hitbox = new Container();
	self.hitbox.width = heroGraphics.width * 1.2; // Increase size for better collision detection
	self.hitbox.height = heroGraphics.height * 1.2; // Increase size for better collision detection
	self.hitbox.x = -self.hitbox.width;
	self.hitbox.y = -self.hitbox.height;
	// Display the hitbox with a transparent asset
	var hitboxGraphics = LK.getAsset('transparentAsset', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	hitboxGraphics.width = self.hitbox.width;
	hitboxGraphics.height = self.hitbox.height;
	self.hitbox.addChild(hitboxGraphics);
	self.addChild(self.hitbox);
	self.update = function () {
		// Update logic for hero, if needed
	};
});
var Lifebar = Container.expand(function () {
	var self = Container.call(this);
	var lifebarGraphics = self.attachAsset('lifebar', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Removed life percentage text display
	self.update = function () {
		// Removed life percentage text display
		if (hero.health > 0) {
			lifebarGraphics.width = hero.health / 100 * 1500; // Update lifebar width based on health
		} else {
			lifebarGraphics.width = 0; // Ensure lifebar does not grow after emptying
		}
	};
});
var Spear = Container.expand(function () {
	var self = Container.call(this);
	var spearGraphics = self.attachAsset('spear', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 20;
	self.update = function () {
		self.y += self.speed;
	};
});
var SpearTrigger = Container.expand(function () {
	var self = Container.call(this);
	self.spearTriggerGraphics = self.attachAsset('spearTrigger', {
		anchorX: 0.5,
		anchorY: 0.5,
		alpha: 0 // Start with full transparency
	});
	// Add fade-in effect
	tween(self.spearTriggerGraphics, {
		alpha: 1 // Fade to full opacity
	}, {
		duration: 1000,
		// 1 second duration
		easing: tween.easeInOut
	});
});
var StartScreen = Container.expand(function () {
	var self = Container.call(this);
	// Add a blury cloud moving in the background
	var cloud = self.attachAsset('background', {
		anchorX: 0.5,
		// Center the cloud horizontally
		anchorY: 0.5 // Center the cloud vertically
	});
	cloud.x = 2048 / 2; // Start from the center of the screen
	cloud.y = 2732 / 2; // Start from the center of the screen
	cloud.alpha = 0.3; // Make the cloud semi-transparent for a blury effect
	// Animate the cloud to move slowly across the screen
	function animateCloud() {
		tween(cloud, {
			x: 2048 / 2 + 100,
			// Move the cloud 100 pixels to the right
			y: 2732 / 2 + 50 // Move the cloud 50 pixels down
		}, {
			duration: 5000,
			// Move over 5 seconds
			easing: tween.easeInOut,
			onFinish: function onFinish() {
				tween(cloud, {
					x: 2048 / 2 - 100,
					// Move the cloud back 100 pixels to the left
					y: 2732 / 2 - 50 // Move the cloud back 50 pixels up
				}, {
					duration: 5000,
					// Move back over 5 seconds
					easing: tween.easeInOut,
					onFinish: animateCloud // Loop the animation
				});
			}
		});
	}
	animateCloud();
	var gameTitle = self.attachAsset('gamename', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	var startButton = self.attachAsset('startButton', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	gameTitle.x = 2048 / 2;
	gameTitle.y = 2732 / 2 - 500; // Move the game title 500 pixels up
	// Add a tween to animate the game title
	function animateTitle() {
		tween(gameTitle, {
			y: gameTitle.y - 20
		}, {
			duration: 1000,
			easing: tween.easeInOut,
			onFinish: function onFinish() {
				tween(gameTitle, {
					y: gameTitle.y + 20
				}, {
					duration: 1000,
					easing: tween.easeInOut,
					onFinish: animateTitle // Loop the animation
				});
			}
		});
	}
	animateTitle();
	// Removed stretching of the game title image
	startButton.x = 2048 / 2; // Center the start button horizontally
	startButton.y = 2732 - 500; // Move the start button 400 pixels up from the bottom of the screen
	// Add a tilting effect to the start button
	function animateButton() {
		tween(startButton, {
			rotation: 0.1
		}, {
			duration: 1000,
			easing: tween.easeInOut,
			onFinish: function onFinish() {
				tween(startButton, {
					rotation: -0.1
				}, {
					duration: 1000,
					easing: tween.easeInOut,
					onFinish: animateButton // Loop the tilting effect
				});
			}
		});
	}
	animateButton();
	startButton.down = function (x, y, obj) {
		LK.getSound('Dieknightdie').play();
		self.destroy();
		initGame();
	};
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000000 // Init game with black background
});
/**** 
* Game Code
****/ 
LK.playMusic('Menumusic'); // Play menu music on game load
var startScreen = game.addChild(new StartScreen());
// Check if hero's health is 0 to end the game
if (timeCounter) {
	// Ensure timeCounter is defined before accessing its text property
	// Set the final score to the time counter
	LK.setScore(parseFloat(timeCounter.text));
}
function shakeScreen() {
	var shakeIntensity = 10;
	var shakeDuration = 500; // in milliseconds
	var elapsedTime = 0;
	var originalX = game.x;
	var originalY = game.y;
	var shakeInterval = LK.setInterval(function () {
		if (elapsedTime < shakeDuration) {
			game.x = originalX + (Math.random() - 0.5) * shakeIntensity;
			game.y = originalY + (Math.random() - 0.5) * shakeIntensity;
			elapsedTime += 16; // approximately 60 FPS
		} else {
			game.x = originalX;
			game.y = originalY;
			LK.clearInterval(shakeInterval);
		}
	}, 16);
}
// Initialize game variables
var hero;
var spearTrigger;
var fireTrigger;
var spearCount = 0;
// Declare scoreText and timeCounter in the global scope
var scoreText;
var timeCounter;
var currentLevel = 1; // Initialize current level
// Function to initialize game elements
function initGame() {
	// Play game music on game start
	LK.playMusic('Gamemusic');
	// Create and position the time counter
	timeCounter = new Text2("0", {
		size: 210,
		// 70% of the original size
		fill: 0xFFFFFF
	});
	// Add tween effect to increase the size of the time counter
	tween(timeCounter, {
		scaleX: 1.5,
		scaleY: 1.5
	}, {
		duration: 1000,
		easing: tween.easeInOut
	});
	timeCounter.anchor.set(0.5, 1); // Sets anchor to the center of the bottom edge of the text.
	LK.gui.bottom.addChild(timeCounter); // Add the time counter to the GUI overlay at the bottom of the screen.
	// Update the time counter every second
	var startTime = Date.now();
	var timeInterval = LK.setInterval(function () {
		var elapsedTime = (Date.now() - startTime) / 1000;
		timeCounter.setText(elapsedTime.toFixed(3));
		// Stop the time counter if hero's health is zero
		if (hero.health <= 0) {
			LK.clearInterval(timeInterval);
		}
	}, 1);
	// Add background image
	var background = LK.getAsset('background', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	background.x = 2048 / 2;
	background.y = 2732 / 2;
	background.alpha = 0.5; // Set background transparency to dim it
	game.addChild(background);
	hero = game.addChild(new Hero());
	hero.health = 100; // Initialize hero health as a number
	hero.x = 2048 / 2;
	hero.y = 2732 / 2;
	// Create and position the lifebar
	var lifebar = new Lifebar();
	lifebar.x = 2048 / 2;
	lifebar.y = 25; // Centered at the top of the screen
	game.addChild(lifebar);
	// Initialize the score without displaying it
	LK.setScore(0);
	// Create and position the spear trigger within the playable area
	spearTrigger = game.addChild(new SpearTrigger());
	spearTrigger.x = Math.random() * (2048 - 400) + 200; // Ensure at least 200 pixels from left and right edges
	spearTrigger.y = Math.random() * (2732 - 400) + 200; // Ensure at least 200 pixels from top and bottom edges
	// Create and position the fire trigger within the playable area
	fireTrigger = game.addChild(new FireTrigger());
	fireTrigger.x = Math.random() * 2048 * 0.8 + 2048 * 0.1;
	fireTrigger.y = Math.random() * 2732 * 0.8 + 2732 * 0.1;
}
// Function to handle hero movement
function handleMove(x, y, obj) {
	// Add a wobble and tilting effect to the hero's movement
	var wobbleIntensity = 5;
	var tiltIntensity = 0.1;
	if (hero && !game.ending) {
		hero.x = x + (Math.random() - 0.5) * wobbleIntensity;
		hero.y = y + (Math.random() - 0.5) * wobbleIntensity;
	}
	if (hero) {
		hero.rotation = (Math.random() - 0.5) * tiltIntensity;
	}
}
// Function to update game state
game.update = function () {
	// Check if the hero intersects with the firewall
	if (fireTrigger && fireTrigger.fireWall && hero.intersects(fireTrigger.fireWall)) {
		// Decrease hero's health by 1 every third of a second
		if (LK.ticks % 20 === 0) {
			// Assuming 60 ticks per second
			hero.health -= 1;
			// Play firewall sound
			LK.getSound('firewall').play();
		}
	}
	if (hero && spearTrigger && hero.intersects(spearTrigger)) {
		var spear = game.addChild(new Spear());
		spear.x = Math.random() * 2048; // Random x position
		spear.y = Math.random() < 0.5 ? 0 : 2732; // Randomly choose top or bottom for y position
		var dx = hero.x - spear.x;
		var dy = hero.y - spear.y;
		var angle = Math.atan2(dy, dx);
		spear.speedX = Math.cos(angle) * spear.speed;
		spear.speedY = Math.sin(angle) * spear.speed;
		spear.rotation = angle; // Rotate the spear in the direction it's moving
		// Play spear sound
		LK.getSound('spear').play();
		spear.update = function () {
			this.x += this.speedX;
			this.y += this.speedY;
			if (this.intersects(hero.hitbox)) {
				// Trigger screen shake effect
				shakeScreen();
				// Attach the spear to the hero at the position it first touches him
				this.speedX = 0;
				this.speedY = 0;
				var relativeX = this.x - hero.x;
				var relativeY = this.y - hero.y;
				this.update = function () {
					var wobbleIntensity = 2;
					var tiltIntensity = 0.05;
					this.x = hero.x + relativeX + (Math.random() - 0.5) * wobbleIntensity;
					this.y = hero.y + relativeY + (Math.random() - 0.5) * wobbleIntensity;
					this.rotation = angle + (Math.random() - 0.5) * tiltIntensity;
				};
				// Play spearhit sound
				LK.getSound('spearhit').play();
				// Reduce player's health
				hero.health -= 10;
				// Check if hero's health is 0 to end the game
				if (hero.health <= 0) {
					tween(timeCounter, {
						scaleX: timeCounter.scaleX + 0.5,
						scaleY: timeCounter.scaleY + 0.5
					}, {
						duration: 2000,
						easing: tween.linear
					});
					// Set the final score to the time counter
					LK.setScore(parseFloat(timeCounter.text));
					// Show an endscreen for 2 seconds before game over
					game.ending = true;
					var endScreen = LK.getAsset('hudBackground', {
						anchorX: 0.5,
						anchorY: 0.5
					});
					endScreen.x = 2048 / 2 + 100;
					endScreen.y = 2732 / 2 - 200;
					endScreen.scaleX = 1;
					endScreen.scaleY = 1;
					game.addChild(endScreen);
					tween(endScreen, {
						scaleX: 1.5,
						scaleY: 1.5
					}, {
						duration: 4000,
						easing: tween.easeInOut
					}); // Tween to increase size
					// Play evil laugh sound when endscreen appears
					LK.getSound('Evillaugh').play();
					LK.setTimeout(function () {
						game.removeChild(endScreen);
						game.ending = false;
						LK.showGameOver();
					}, 4000);
				}
			}
		};
		// Reposition the spear trigger to a random location
		spearTrigger.x = Math.random() * 2048; // Random x position
		spearTrigger.y = Math.random() * 2732; // Random y position
		// Add fade-in effect when repositioning
		tween(spearTrigger.spearTriggerGraphics, {
			alpha: 1 // Fade to full opacity
		}, {
			duration: 1000,
			// 1 second duration
			easing: tween.easeInOut
		});
	}
};
// Function to handle transition to the next level
function moveToNextLevel() {
	// Reset hero's health
	hero.health = 100;
	// Increment level (removed spear count and level display)
	currentLevel++;
	// Reposition hero to the center
	hero.x = 2048 / 2;
	hero.y = 2732 / 2;
	// Reposition spear trigger to a new random location
	spearTrigger.x = Math.random() * (2048 - 400) + 200; // Ensure at least 200 pixels from left and right edges
	spearTrigger.y = Math.random() * (2732 - 400) + 200; // Ensure at least 200 pixels from top and bottom edges
	// Optionally, increase difficulty or change level parameters here
}
// Set up event listeners for touch/mouse interactions
game.down = function (x, y, obj) {
	handleMove(x, y, obj);
};
game.move = function (x, y, obj) {
	handleMove(x, y, obj);
};
game.up = function (x, y, obj) {
	// No specific action needed on release
}; /**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
/**** 
* Classes
****/ 
var FireTrigger = Container.expand(function () {
	var self = Container.call(this);
	var fireTriggerGraphics = self.attachAsset('fireTrigger', {
		anchorX: 0.5,
		anchorY: 0.5,
		alpha: 0 // Start with full transparency
	});
	// Add fade-in effect
	tween(fireTriggerGraphics, {
		alpha: 1 // Fade to full opacity
	}, {
		duration: 1000,
		// 1 second duration
		easing: tween.easeInOut
	});
	self.update = function () {
		if (!self.fireWall) {
			self.fireWall = LK.getAsset('fireWall', {
				anchorX: 0.5,
				anchorY: 0.5
			});
			self.fireWall.width = 2048; // Set width to match screen width
			self.fireWall.x = 2048 / 2; // Start from the center of the screen
			self.fireWall.y = 2732 + self.fireWall.height; // Start from bottom off-screen
			self.fireWall.speedX = 0; // No horizontal movement
			self.fireWall.speedY = -15; // Move upwards
			self.fireWall.update = function () {
				this.x += this.speedX;
				this.y += this.speedY;
				// Add flickering effect
				this.alpha = Math.random() * 0.5 + 0.5; // Random alpha between 0.5 and 1
			};
			game.addChild(self.fireWall);
			// Play firewall sound when it spawns
			LK.getSound('firewall').play();
			// Reposition the fire trigger to a random location
			self.x = Math.random() * (2048 - 400) + 200; // Ensure at least 200 pixels from left and right edges
			self.y = Math.random() * (2732 - 400) + 200; // Ensure at least 200 pixels from top and bottom edges
			// Add fade-in effect when repositioning
			tween(fireTriggerGraphics, {
				alpha: 1 // Fade to full opacity
			}, {
				duration: 1000,
				// 1 second duration
				easing: tween.easeInOut
			});
		}
		if (self.intersects(hero)) {
			self.fireWall = LK.getAsset('fireWall', {
				anchorX: 0.5,
				anchorY: 0.5
			});
			self.fireWall.width = 2048; // Set width to match screen width
			self.fireWall.x = 2048 / 2; // Start from the center of the screen
			self.fireWall.y = 2732 + self.fireWall.height; // Start from bottom off-screen
			self.fireWall.speedX = 0; // No horizontal movement
			self.fireWall.speedY = -15; // Move upwards
			self.fireWall.update = function () {
				this.x += this.speedX;
				this.y += this.speedY;
				// Add flickering effect
				this.alpha = Math.random() * 0.5 + 0.5; // Random alpha between 0.5 and 1
			};
			game.addChild(self.fireWall);
			// Play firewall sound when it spawns
			LK.getSound('firewall').play();
			// Reposition the fire trigger to a random location
			self.x = Math.random() * 2048;
			self.y = Math.random() * 2732;
		}
	};
});
// Hero class representing the player character
var Hero = Container.expand(function () {
	var self = Container.call(this);
	var heroGraphics = self.attachAsset('hero', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 10;
	// Add a shadow below the hero
	var shadow = self.attachAsset('heroShadow', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	shadow.y = heroGraphics.height / 2 + 10; // Position the shadow slightly below the hero
	shadow.alpha = 0.5; // Make the shadow semi-transparent
	// Add hitbox for collision detection
	self.hitbox = new Container();
	self.hitbox.width = heroGraphics.width * 1.2; // Increase size for better collision detection
	self.hitbox.height = heroGraphics.height * 1.2; // Increase size for better collision detection
	self.hitbox.x = -self.hitbox.width;
	self.hitbox.y = -self.hitbox.height;
	// Display the hitbox with a transparent asset
	var hitboxGraphics = LK.getAsset('transparentAsset', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	hitboxGraphics.width = self.hitbox.width;
	hitboxGraphics.height = self.hitbox.height;
	self.hitbox.addChild(hitboxGraphics);
	self.addChild(self.hitbox);
	self.update = function () {
		// Update logic for hero, if needed
	};
});
var Lifebar = Container.expand(function () {
	var self = Container.call(this);
	var lifebarGraphics = self.attachAsset('lifebar', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Removed life percentage text display
	self.update = function () {
		// Removed life percentage text display
		if (hero.health > 0) {
			lifebarGraphics.width = hero.health / 100 * 1500; // Update lifebar width based on health
		} else {
			lifebarGraphics.width = 0; // Ensure lifebar does not grow after emptying
		}
	};
});
var Spear = Container.expand(function () {
	var self = Container.call(this);
	var spearGraphics = self.attachAsset('spear', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 20;
	self.update = function () {
		self.y += self.speed;
	};
});
var SpearTrigger = Container.expand(function () {
	var self = Container.call(this);
	self.spearTriggerGraphics = self.attachAsset('spearTrigger', {
		anchorX: 0.5,
		anchorY: 0.5,
		alpha: 0 // Start with full transparency
	});
	// Add fade-in effect
	tween(self.spearTriggerGraphics, {
		alpha: 1 // Fade to full opacity
	}, {
		duration: 1000,
		// 1 second duration
		easing: tween.easeInOut
	});
});
var StartScreen = Container.expand(function () {
	var self = Container.call(this);
	// Add a blury cloud moving in the background
	var cloud = self.attachAsset('background', {
		anchorX: 0.5,
		// Center the cloud horizontally
		anchorY: 0.5 // Center the cloud vertically
	});
	cloud.x = 2048 / 2; // Start from the center of the screen
	cloud.y = 2732 / 2; // Start from the center of the screen
	cloud.alpha = 0.3; // Make the cloud semi-transparent for a blury effect
	// Animate the cloud to move slowly across the screen
	function animateCloud() {
		tween(cloud, {
			x: 2048 / 2 + 100,
			// Move the cloud 100 pixels to the right
			y: 2732 / 2 + 50 // Move the cloud 50 pixels down
		}, {
			duration: 5000,
			// Move over 5 seconds
			easing: tween.easeInOut,
			onFinish: function onFinish() {
				tween(cloud, {
					x: 2048 / 2 - 100,
					// Move the cloud back 100 pixels to the left
					y: 2732 / 2 - 50 // Move the cloud back 50 pixels up
				}, {
					duration: 5000,
					// Move back over 5 seconds
					easing: tween.easeInOut,
					onFinish: animateCloud // Loop the animation
				});
			}
		});
	}
	animateCloud();
	var gameTitle = self.attachAsset('gamename', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	var startButton = self.attachAsset('startButton', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	gameTitle.x = 2048 / 2;
	gameTitle.y = 2732 / 2 - 500; // Move the game title 500 pixels up
	// Add a tween to animate the game title
	function animateTitle() {
		tween(gameTitle, {
			y: gameTitle.y - 20
		}, {
			duration: 1000,
			easing: tween.easeInOut,
			onFinish: function onFinish() {
				tween(gameTitle, {
					y: gameTitle.y + 20
				}, {
					duration: 1000,
					easing: tween.easeInOut,
					onFinish: animateTitle // Loop the animation
				});
			}
		});
	}
	animateTitle();
	// Removed stretching of the game title image
	startButton.x = 2048 / 2; // Center the start button horizontally
	startButton.y = 2732 - 500; // Move the start button 400 pixels up from the bottom of the screen
	// Add a tilting effect to the start button
	function animateButton() {
		tween(startButton, {
			rotation: 0.1
		}, {
			duration: 1000,
			easing: tween.easeInOut,
			onFinish: function onFinish() {
				tween(startButton, {
					rotation: -0.1
				}, {
					duration: 1000,
					easing: tween.easeInOut,
					onFinish: animateButton // Loop the tilting effect
				});
			}
		});
	}
	animateButton();
	startButton.down = function (x, y, obj) {
		LK.getSound('Dieknightdie').play();
		self.destroy();
		initGame();
	};
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000000 // Init game with black background
});
/**** 
* Game Code
****/ 
LK.playMusic('Menumusic'); // Play menu music on game load
var startScreen = game.addChild(new StartScreen());
// Check if hero's health is 0 to end the game
if (timeCounter) {
	// Ensure timeCounter is defined before accessing its text property
	// Set the final score to the time counter
	LK.setScore(parseFloat(timeCounter.text));
}
function shakeScreen() {
	var shakeIntensity = 10;
	var shakeDuration = 500; // in milliseconds
	var elapsedTime = 0;
	var originalX = game.x;
	var originalY = game.y;
	var shakeInterval = LK.setInterval(function () {
		if (elapsedTime < shakeDuration) {
			game.x = originalX + (Math.random() - 0.5) * shakeIntensity;
			game.y = originalY + (Math.random() - 0.5) * shakeIntensity;
			elapsedTime += 16; // approximately 60 FPS
		} else {
			game.x = originalX;
			game.y = originalY;
			LK.clearInterval(shakeInterval);
		}
	}, 16);
}
// Initialize game variables
var hero;
var spearTrigger;
var fireTrigger;
var spearCount = 0;
// Declare scoreText and timeCounter in the global scope
var scoreText;
var timeCounter;
var currentLevel = 1; // Initialize current level
// Function to initialize game elements
function initGame() {
	// Play game music on game start
	LK.playMusic('Gamemusic');
	// Create and position the time counter
	timeCounter = new Text2("0", {
		size: 210,
		// 70% of the original size
		fill: 0xFFFFFF
	});
	// Add tween effect to increase the size of the time counter
	tween(timeCounter, {
		scaleX: 1.5,
		scaleY: 1.5
	}, {
		duration: 1000,
		easing: tween.easeInOut
	});
	timeCounter.anchor.set(0.5, 1); // Sets anchor to the center of the bottom edge of the text.
	LK.gui.bottom.addChild(timeCounter); // Add the time counter to the GUI overlay at the bottom of the screen.
	// Update the time counter every second
	var startTime = Date.now();
	var timeInterval = LK.setInterval(function () {
		var elapsedTime = (Date.now() - startTime) / 1000;
		timeCounter.setText(elapsedTime.toFixed(3));
		// Stop the time counter if hero's health is zero
		if (hero.health <= 0) {
			LK.clearInterval(timeInterval);
		}
	}, 1);
	// Add background image
	var background = LK.getAsset('background', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	background.x = 2048 / 2;
	background.y = 2732 / 2;
	background.alpha = 0.5; // Set background transparency to dim it
	game.addChild(background);
	hero = game.addChild(new Hero());
	hero.health = 100; // Initialize hero health as a number
	hero.x = 2048 / 2;
	hero.y = 2732 / 2;
	// Create and position the lifebar
	var lifebar = new Lifebar();
	lifebar.x = 2048 / 2;
	lifebar.y = 25; // Centered at the top of the screen
	game.addChild(lifebar);
	// Initialize the score without displaying it
	LK.setScore(0);
	// Create and position the spear trigger within the playable area
	spearTrigger = game.addChild(new SpearTrigger());
	spearTrigger.x = Math.random() * (2048 - 400) + 200; // Ensure at least 200 pixels from left and right edges
	spearTrigger.y = Math.random() * (2732 - 400) + 200; // Ensure at least 200 pixels from top and bottom edges
	// Create and position the fire trigger within the playable area
	fireTrigger = game.addChild(new FireTrigger());
	fireTrigger.x = Math.random() * 2048 * 0.8 + 2048 * 0.1;
	fireTrigger.y = Math.random() * 2732 * 0.8 + 2732 * 0.1;
}
// Function to handle hero movement
function handleMove(x, y, obj) {
	// Add a wobble and tilting effect to the hero's movement
	var wobbleIntensity = 5;
	var tiltIntensity = 0.1;
	if (hero && !game.ending) {
		hero.x = x + (Math.random() - 0.5) * wobbleIntensity;
		hero.y = y + (Math.random() - 0.5) * wobbleIntensity;
	}
	if (hero) {
		hero.rotation = (Math.random() - 0.5) * tiltIntensity;
	}
}
// Function to update game state
game.update = function () {
	// Check if the hero intersects with the firewall
	if (fireTrigger && fireTrigger.fireWall && hero.intersects(fireTrigger.fireWall)) {
		// Decrease hero's health by 1 every third of a second
		if (LK.ticks % 20 === 0) {
			// Assuming 60 ticks per second
			hero.health -= 1;
			// Play firewall sound
			LK.getSound('firewall').play();
		}
	}
	if (hero && spearTrigger && hero.intersects(spearTrigger)) {
		var spear = game.addChild(new Spear());
		spear.x = Math.random() * 2048; // Random x position
		spear.y = Math.random() < 0.5 ? 0 : 2732; // Randomly choose top or bottom for y position
		var dx = hero.x - spear.x;
		var dy = hero.y - spear.y;
		var angle = Math.atan2(dy, dx);
		spear.speedX = Math.cos(angle) * spear.speed;
		spear.speedY = Math.sin(angle) * spear.speed;
		spear.rotation = angle; // Rotate the spear in the direction it's moving
		// Play spear sound
		LK.getSound('spear').play();
		spear.update = function () {
			this.x += this.speedX;
			this.y += this.speedY;
			if (this.intersects(hero.hitbox)) {
				// Trigger screen shake effect
				shakeScreen();
				// Attach the spear to the hero at the position it first touches him
				this.speedX = 0;
				this.speedY = 0;
				var relativeX = this.x - hero.x;
				var relativeY = this.y - hero.y;
				this.update = function () {
					var wobbleIntensity = 2;
					var tiltIntensity = 0.05;
					this.x = hero.x + relativeX + (Math.random() - 0.5) * wobbleIntensity;
					this.y = hero.y + relativeY + (Math.random() - 0.5) * wobbleIntensity;
					this.rotation = angle + (Math.random() - 0.5) * tiltIntensity;
				};
				// Play spearhit sound
				LK.getSound('spearhit').play();
				// Reduce player's health
				hero.health -= 10;
				// Check if hero's health is 0 to end the game
				if (hero.health <= 0) {
					tween(timeCounter, {
						scaleX: timeCounter.scaleX + 0.5,
						scaleY: timeCounter.scaleY + 0.5
					}, {
						duration: 2000,
						easing: tween.linear
					});
					// Set the final score to the time counter
					LK.setScore(parseFloat(timeCounter.text));
					// Show an endscreen for 2 seconds before game over
					game.ending = true;
					var endScreen = LK.getAsset('hudBackground', {
						anchorX: 0.5,
						anchorY: 0.5
					});
					endScreen.x = 2048 / 2 + 100;
					endScreen.y = 2732 / 2 - 200;
					endScreen.scaleX = 1;
					endScreen.scaleY = 1;
					game.addChild(endScreen);
					tween(endScreen, {
						scaleX: 1.5,
						scaleY: 1.5
					}, {
						duration: 4000,
						easing: tween.easeInOut
					}); // Tween to increase size
					// Play evil laugh sound when endscreen appears
					LK.getSound('Evillaugh').play();
					LK.setTimeout(function () {
						game.removeChild(endScreen);
						game.ending = false;
						LK.showGameOver();
					}, 4000);
				}
			}
		};
		// Reposition the spear trigger to a random location
		spearTrigger.x = Math.random() * 2048; // Random x position
		spearTrigger.y = Math.random() * 2732; // Random y position
		// Add fade-in effect when repositioning
		tween(spearTrigger.spearTriggerGraphics, {
			alpha: 1 // Fade to full opacity
		}, {
			duration: 1000,
			// 1 second duration
			easing: tween.easeInOut
		});
	}
};
// Function to handle transition to the next level
function moveToNextLevel() {
	// Reset hero's health
	hero.health = 100;
	// Increment level (removed spear count and level display)
	currentLevel++;
	// Reposition hero to the center
	hero.x = 2048 / 2;
	hero.y = 2732 / 2;
	// Reposition spear trigger to a new random location
	spearTrigger.x = Math.random() * (2048 - 400) + 200; // Ensure at least 200 pixels from left and right edges
	spearTrigger.y = Math.random() * (2732 - 400) + 200; // Ensure at least 200 pixels from top and bottom edges
	// Optionally, increase difficulty or change level parameters here
}
// Set up event listeners for touch/mouse interactions
game.down = function (x, y, obj) {
	handleMove(x, y, obj);
};
game.move = function (x, y, obj) {
	handleMove(x, y, obj);
};
game.up = function (x, y, obj) {
	// No specific action needed on release
};
 pixealrt spear. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
 
 
 
 fireskull button. pixelart.. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
 spearbutton. pixelart.. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
 Pixelart button that says "Start". Dungeon vibes.. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
 2000 by 2800 high quality banner. Pixelart. title reads: "Die Knight, Die!". Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
 Pixealrt. Dungeon. Reads: The Knight is DEAD. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.