/**** 
* Classes
****/ 
var BadElf = Container.expand(function (spawnX, spawnY, gameInstance) {
	var self = Container.call(this);
	self.gameInstance = gameInstance;
	self.spawnedMagicCount = 1;
	self.magicShootingInterval = LK.setInterval(function () {
		if (self.visible && self.gameInstance && self.gameInstance.activeMagicCount < self.gameInstance.maxMagicAllowed) {
			var magic = new Magic(self.x + 125, self.y, self.gameInstance);
			self.gameInstance.addChild(magic);
			self.gameInstance.activeMagicCount++;
			self.spawnedMagicCount++;
			if (self.spawnedMagicCount >= 3) {
				self.alpha = 1;
				var fadeOutInterval;
				var fadeOut = function fadeOut() {
					if (self.alpha > 0) {
						self.alpha -= 0.05;
					} else {
						LK.clearInterval(fadeOutInterval);
						self.gameInstance.activeBadElves--;
						self.destroy();
						LK.setTimeout(function () {}, 2000);
					}
				};
				LK.clearInterval(self.magicShootingInterval);
				fadeOutInterval = LK.setInterval(fadeOut, 60);
				self.magicShootingInterval = null;
			}
		}
	}, Math.random() * (1000 - 500) + 500);
	var badElfGraphics = self.attachAsset('badElf', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.x = spawnX;
	self.y = spawnY;
	self.on('down', function (x, y, obj) {
		LK.setScore(LK.getScore() + 3);
		LK.gui.top.children[0].setText(LK.getScore().toString());
		this.gameInstance.activeBadElves--;
		var obstacleBox = new ObstacleBox(this.x, this.y, this.gameInstance);
		obstacleBox.x = this.x;
		obstacleBox.y = this.y;
		this.parent.addChild(obstacleBox);
		LK.setTimeout(function () {
			obstacleBox.destroy();
		}, 1000);
		LK.clearInterval(this.magicShootingInterval);
		this.destroy();
		if (Math.random() < 0.1) {
			this.gameInstance.spawnBadElf();
		}
	});
});
var Crosshair = Container.expand(function () {
	var self = Container.call(this);
	self._move_migrated = function (event) {
		if (self.parent) {
			var pos = self.parent.toLocal(event.global);
			self.x = pos.x;
			self.y = pos.y;
		}
	};
	var crosshairGraphics = self.attachAsset('crosshair', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.y = -crosshairGraphics.height / 2;
});
var Heart = Container.expand(function () {
	var self = Container.call(this);
	var heartGraphics = self.attachAsset('heart', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.x = heartGraphics.width / 2;
	self.y = heartGraphics.height / 2;
});
var Magic = Container.expand(function (x, y, gameInstance) {
	var self = Container.call(this);
	// Play the enemy shot sound when Magic (elfbullet) is instantiated
	LK.getSound('snd_enemyshot').play();
	console.log("magic creation triggered");
	self.gameInstance = gameInstance;
	self.moveEnabled = true;
	self.rotation = 0;
	var magicGraphics = self.attachAsset('magic', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	magicGraphics.width = 125;
	magicGraphics.height = 125;
	self.x = x;
	self.y = y;
	self._move_migrated = function () {
		if (self.moveEnabled) {
			var santaX = self.gameInstance.santa && self.gameInstance.santa.santaGraphics ? self.gameInstance.santa.santaGraphics.x : 2048;
			var santaY = self.gameInstance.santa && self.gameInstance.santa.santaGraphics ? self.gameInstance.santa.santaGraphics.y : 2732;
			var santaWidth = self.gameInstance.santa && self.gameInstance.santa.santaGraphics ? self.gameInstance.santa.santaGraphics.width : 0;
			var magicWidth = magicGraphics ? magicGraphics.width : 0;
			var dx = santaX - self.x;
			var dy = santaY - self.y;
			var distance = Math.sqrt(dx * dx + dy * dy);
			var hitThreshold = 10000;
			var speed = 10;
			if (distance > 0) {
				var normX = dx / distance;
				var normY = dy / distance;
				self.x += normX * speed;
				self.y += normY * speed;
			}
			if (distance <= 450) {
				console.log('Hit threshold met. Game over triggered.');
				self.gameInstance.triggerGameOver();
			}
			if (self.gameInstance.santa) {} else if (self.x < 0 || self.x > 2048 || self.y < 0 || self.y > 2732) {
				self.gameInstance.activeMagicCount--;
				self.destroy();
			}
		}
	};
	self.on('down', function () {
		LK.setScore(LK.getScore() + 2);
		LK.gui.top.children[0].setText(LK.getScore().toString());
		var tingText = new Text2('Ting!', {
			size: 40,
			fill: '#ffffff',
			font: 'Arial Black'
		});
		tingText.x = this.x;
		tingText.y = this.y;
		tingText.anchor.set(0.5, 0.5);
		this.parent.addChild(tingText);
		LK.setTimeout(function () {
			tingText.destroy();
		}, 1000);
		this.gameInstance.activeMagicCount--;
		LK.getSound('snd_ricochet').play();
		this.destroy();
	});
});
var Mistletoe = Container.expand(function (gameInstance) {
	var self = Container.call(this);
	// Play the mistletoe sound when Mistletoe is instantiated
	LK.getSound('snd_mistletoe').play();
	self.gameInstance = gameInstance;
	var mistletoeGraphics = self.attachAsset('mistletoe', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	var scale = Math.random() * 0.15 + 0.85;
	mistletoeGraphics.scale.set(scale);
	var bobbingRange = 10;
	var bobbingSpeed = 0.2;
	var bobbingPosition = 0;
	self.bob = function () {
		bobbingPosition += bobbingSpeed;
		self.y += Math.sin(bobbingPosition) * bobbingRange;
	};
	self.on('down', function (x, y, obj) {
		LK.setScore(LK.getScore() + 10);
		LK.gui.top.children[0].setText(LK.getScore().toString());
		self.parent.respawnMistletoe(self);
		LK.getSound('snd_mistletoeshot').play();
		if (Math.random() < 0.5) {
			if (self.gameInstance && typeof self.gameInstance.spawnBadElf === 'function') {
				self.gameInstance.spawnBadElf();
			}
		}
	});
});
var MuzzleFlash = Container.expand(function () {
	var self = Container.call(this);
	self.visible = false;
	var flashGraphics = self.attachAsset('muzzleFlash', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.show = function () {
		var randomScale = Math.random() * 0.125 + 0.375;
		self.scale.set(randomScale);
		self.visible = true;
		LK.setTimeout(function () {
			self.visible = false;
			self.scale.set(1);
		}, 100);
	};
});
var ObstacleBox = Container.expand(function (x, y, gameInstance) {
	var self = Container.call(this);
	self.gameInstance = gameInstance;
	// Play the obstacle sound when ObstacleBox is instantiated
	LK.getSound('snd_obstacle').play();
	var obstacleBoxGraphics = self.attachAsset('obstacleBox', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.x = x;
	self.y = y;
});
var RandomText = Container.expand(function () {
	var self = Container.call(this);
	self.gameInstance = null;
	self.display = function (x, y, messages) {
		var message = messages[Math.floor(Math.random() * messages.length)];
		var text = new Text2(message, {
			size: 48,
			fill: '#ff0000',
			font: 'Arial Black',
			stroke: '#000000',
			strokeThickness: 6
		});
		text.x = x - 400;
		text.y = y + 175;
		text.anchor.set(0.5, 0.5);
		self.addChild(text);
		LK.getSound('snd_messages').play();
		LK.setTimeout(function () {
			text.destroy();
			self.parent.randomTextActive = false;
		}, 3000);
	};
});
var Reindeer = Container.expand(function (gameInstance) {
	var self = Container.call(this);
	// Play the reindeer sound when Reindeer is instantiated
	LK.getSound('snd_reindeer').play();
	self.moveInArc = function () {
		self.arcAngle += self.arcSpeed * self.arcDirection;
		self.x = self.gameInstance.x + Math.cos(self.arcAngle) * self.arcRadius;
		self.y = self.gameInstance.y + Math.sin(self.arcAngle) * self.arcRadius;
	};
	self.moveHorizontally = function () {
		self.x += self.speed * self.direction;
		if (self.x > 2048 || self.x < 0) {
			self.direction *= -1;
			self.scale.x *= -1;
			self.x = Math.max(0, Math.min(self.x, 2048));
		}
	};
	self.fadeOut = function () {
		if (self.alpha > 0) {
			self.alpha -= 0.01;
		} else if (self.gameInstance) {
			self.gameInstance.respawnReindeer(self);
		}
	};
	self.gameInstance = gameInstance;
	var reindeerGraphics = self.attachAsset('reindeer', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.addChild(reindeerGraphics);
	self.speed = 10;
	self.direction = Math.random() < 0.5 ? 1 : -1;
	self.x = self.direction === 1 ? 0 : 2048;
	self.scale.x = self.direction;
	self.on('down', function (x, y, obj) {
		LK.setScore(LK.getScore() + 25);
		LK.gui.top.children[0].setText(LK.getScore().toString());
		var greatText = new Text2('Great!', {
			size: 40,
			fill: '#ffffff',
			font: 'Arial Black'
		});
		greatText.x = this.x;
		greatText.y = this.y;
		greatText.anchor.set(0.5, 0.5);
		this.parent.addChild(greatText);
		LK.setTimeout(function () {
			greatText.destroy();
		}, 1000);
		if (Math.random() < 0.9) {
			this.gameInstance.spawnBadElf();
		}
		this.parent.respawnReindeer();
		LK.getSound('snd_reindeershot').play();
		this.destroy();
	});
});
var Santa = Container.expand(function () {
	var self = Container.call(this);
	self.santaGraphics = self.attachAsset('santa', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.santaGraphics.width = 800;
	self.santaGraphics.height = 800;
	self.santaGraphics.scale.x = 800 / self.santaGraphics.width;
	self._move_migrated = function () {};
});
var Shield = Container.expand(function (x, y, gameInstance) {
	var self = Container.call(this);
	self.gameInstance = gameInstance;
	self.x = x;
	self.y = y;
	self.on('down', function () {
		this.destroy();
	});
});
var Snowflake = Container.expand(function () {
	var self = Container.call(this);
	var sizeFactor = Math.random() * 0.5 + 0.5;
	var snowflakeGraphics = self.attachAsset('snowflake', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	snowflakeGraphics.scale.set(sizeFactor);
	self.speed = -3 * sizeFactor;
	self._move_migrated = function () {
		self.x += self.speed;
		self.y += 0.5;
		self.scale.x -= 0.003;
		self.scale.y -= 0.003;
		if (self.scale.x <= 0 || self.scale.y <= 0) {
			self.destroy();
		}
	};
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000000
});
/**** 
* Game Code
****/ 
game.barnMovedDown = false;
// Play and loop the Christmas music when the game starts
LK.playMusic('snd_christmasmusic', {
	loop: true
});
game.triggerGameOver = function () {
	this.isGameOver = true;
	LK.showGameOver();
	LK.on('tick', function () {
		if (game.isGameOver) {
			return;
		}
	});
};
game.checkMagicCollisionWithSanta = function (magic) {
	if (!this.santa || !this.santa.santaGraphics) {
		return;
	}
	var santaX = this.santa && this.santa.santaGraphics ? this.santa.santaGraphics.x : 0;
	var santaY = this.santa.santaGraphics.y;
	var distance = Math.sqrt(Math.pow(magic.x - santaX, 2) + Math.pow(magic.y - santaY, 2));
	var hitThreshold = (this.santa.santaGraphics.width + magic.width) / 4;
	if (distance <= hitThreshold) {
		this.triggerGameOver();
		magic.destroy();
		this.activeMagicCount--;
	}
};
game.isGameOver = false;
game.activeMagicCount = 0;
game.magicSpawnX = 1024;
game.magicSpawnY = 1366;
game.triggerGameOver = function () {
	this.isGameOver = true;
	LK.showGameOver();
};
game.maxMagicAllowed = 3;
if (game.activeMagicCount < game.maxMagicAllowed && LK.gui.top.children[0] && LK.gui.top.children[0].visible && game.activeBadElves > 0) {
	var magicInstance = new Magic(game.magicSpawnX, game.magicSpawnY, game);
	game.addChild(magicInstance);
	game.activeMagicCount++;
}
game.respawnReindeer = function (reindeer) {
	LK.setTimeout(function () {
		var newReindeer = new Reindeer(game);
		var spawnLeftSide = Math.random() < 0.5;
		newReindeer.x = spawnLeftSide ? 0 : 2048;
		newReindeer.y = Math.random() * (2732 / 4 - newReindeer.height) + newReindeer.height;
		newReindeer.arcDirection = spawnLeftSide ? 1 : -1;
		newReindeer.arcRadius = 2048 / 2;
		newReindeer.arcAngle = spawnLeftSide ? Math.PI : 0;
		var scale = Math.random() * 0.25 + 0.75;
		newReindeer.scale.set(scale);
		game.addChild(newReindeer);
		LK.on('tick', function () {
			newReindeer.moveHorizontally();
		});
	}, Math.random() < 0.5 ? 8000 : 12000);
};
game.showGreatText = function (x, y) {
	var greatText = new Text2('Nice!', {
		size: 40,
		fill: '#ffffff',
		font: 'Arial Black'
	});
	greatText.x = x;
	greatText.y = y;
	greatText.anchor.set(0.5, 0.5);
	game.addChild(greatText);
	LK.setTimeout(function () {
		greatText.destroy();
	}, 1000);
};
var silentNightText = new Text2('Ready?', {
	size: 135,
	fill: "#ffffff",
	//Optional (this is the default string)
	stroke: '#000000',
	strokeThickness: 6
});
silentNightText.anchor.set(0.5, 0);
silentNightText.x = 5 / 2;
silentNightText.y = 150;
LK.gui.top.addChild(silentNightText);
game.generateSnowflakes = function () {
	var snowflakeInterval = LK.setInterval(function () {
		var snowflake = new Snowflake();
		snowflake.x = Math.random() * 2048;
		snowflake.y = Math.random() * 2732;
		game.addChild(snowflake);
	}, 1000);
};
LK.setTimeout(function () {
	var fadeOutDuration = 2000;
	var fadeOutStep = 0.05;
	var fadeOutInterval = LK.setInterval(function () {
		silentNightText.alpha -= fadeOutStep;
		if (silentNightText.alpha <= 0) {
			LK.clearInterval(fadeOutInterval);
			silentNightText.destroy();
			game.score = 0;
			game.scoreText = new Text2(game.score.toString(), {
				size: 150,
				fill: "#ffffff"
			});
			game.scoreText.anchor.set(0.5, 0);
			game.scoreText.x = 5 / 2;
			game.scoreText.y = 150;
			LK.gui.top.addChild(game.scoreText);
			game.updateScore = function (score) {
				var previousScore = game.score;
				game.score += score;
				game.scoreText.setText(game.score.toString());
				LK.setScore(game.score);
				var scoreIncrease = game.score - previousScore;
				if (scoreIncrease >= 20) {
					var newInterval = game.badElfSpawnInterval / 1.2;
					game.badElfSpawnInterval = Math.max(newInterval, game.minimumSpawnInterval);
					LK.clearInterval(game.badElfSpawnTimer);
					game.badElfSpawnTimer = LK.setInterval(game.spawnBadElf, game.badElfSpawnInterval);
				}
			};
			game.badElfSpawnInterval = 3500;
			game.minimumSpawnInterval = 500;
			game.badElfSpawnTimer = LK.setInterval(game.spawnBadElf, game.badElfSpawnInterval);
		}
	}, fadeOutDuration * fadeOutStep);
	game.generateMistletoes = function () {
		LK.setTimeout(function () {
			var newMistletoe = new Mistletoe();
			var boundaryOffset = 100;
			newMistletoe.x = Math.random() * (2048 - 2 * boundaryOffset) + boundaryOffset;
			newMistletoe.y = Math.random() * (2732 - 2 * boundaryOffset - newMistletoe.height - 300 - 200 - 500) + boundaryOffset + 200;
			game.addChild(newMistletoe);
		}, 8000);
	};
	LK.setInterval(function () {
		game.activeBadElves = 0;
		game.spawnBadElf();
	}, 12000);
	LK.setTimeout(function () {
		var reindeer = new Reindeer(game);
		var boundaryOffset = 200;
		reindeer.x = Math.random() < 0.5 ? boundaryOffset : 2048 - boundaryOffset - reindeer.width;
		reindeer.y = Math.random() * (2732 / 4 - boundaryOffset) + boundaryOffset;
		reindeer.arcDirection = Math.random() < 0.5 ? 1 : -1;
		reindeer.arcRadius = 2048 / 2;
		reindeer.arcAngle = Math.random() < 0.5 ? Math.PI : 0;
		var initialScale = Math.random() * 0.25 + 0.75;
		reindeer.scale.set(initialScale);
		game.addChild(reindeer);
		LK.on('tick', function () {
			reindeer.moveHorizontally();
		});
	}, 12000);
	LK.setTimeout(game.generateMistletoes, 8000);
}, 2000);
game.on('move', function (x, y, obj) {
	crosshair._move_migrated(obj);
});
game.on('down', function (x, y, obj) {
	if (game) {
		var pos = game.toLocal(obj.global);
		crosshair.x = pos.x;
		crosshair.y = pos.y;
		crosshair.visible = true;
		LK.setTimeout(function () {
			crosshair.visible = false;
		}, 500); // Hide the crosshair after 500ms
		LK.setTimeout(function () {
			crosshair.visible = false;
		}, 500); // Hide the crosshair after 500ms
	}
	if (hearts.length > 0) {
		muzzleFlash.show();
		leftClickPressed = true;
		lastClickTime = LK.ticks;
		LK.getSound('snd_pistol').play(); // Play pistol sound on left click
		if (!barnMovedDown) {
			game.children.forEach(function (child) {
				if (child instanceof BadElf) {
					child.y += 100;
				}
			});
			barnBackground.y += 460;
			room1Background.y += 100;
			boxBackground.y += 100;
			crateBackground.y += 100;
			missBackground.y += 460;
			tireBackground.y += 100;
			barnMovedDown = true;
			if (Math.random() < 0.10) {
				var messages = ["Santa's special delivery!", "This is for the naughty list!", "Ho, ho, ho-hold this!"];
				if (!game.randomTextActive) {
					game.randomTextActive = true;
					var randomText = new RandomText();
					randomText.display(santa.x, santa.y - santa.santaGraphics.height / 2, messages);
					game.addChild(randomText);
				}
			}
		}
		var heart = hearts.pop();
		heart.destroy();
	}
	game.children.forEach(function (child) {
		if (child instanceof BadElf && leftClickPressed && LK.ticks - lastClickTime > 30) {
			child.y -= 100;
		}
	});
});
var room1Background = game.attachAsset('Room1', {});
room1Background.anchor.set(0.5, 0.5);
room1Background.x = 2048 / 2;
room1Background.y = 2732 / 2;
game.addChild(room1Background);
var tireBackground = game.attachAsset('Tire', {
	anchorX: 0.5,
	anchorY: 0.5
});
tireBackground.anchor.set(0.5, 0.5);
tireBackground.x = 700;
tireBackground.y = 1825;
game.addChild(tireBackground);
game.respawnMistletoe = function (mistletoe) {
	this.showGreatText(mistletoe.x, mistletoe.y);
	mistletoe.destroy();
	LK.setTimeout(function () {
		var newMistletoe = new Mistletoe();
		var boundaryOffset = 100;
		newMistletoe.x = Math.random() * (2048 - 2 * boundaryOffset) + boundaryOffset;
		newMistletoe.y = Math.random() * (2732 - 2 * boundaryOffset - newMistletoe.height - 300 - 200 - 100 - 500) + boundaryOffset + 300;
		game.addChild(newMistletoe);
	}, Math.random() * (10000 - 5000) + 5000);
};
game.generateSnowflakes();
var missBackground = game.attachAsset('Miss', {});
missBackground.anchor.set(0.5, 0.5);
missBackground.x = 700;
missBackground.y = 2520;
var crateBackground = game.attachAsset('Crate', {});
crateBackground.anchor.set(0, 1);
crateBackground.x = 205;
crateBackground.y = 2645;
game.addChild(crateBackground);
var boxBackground = game.attachAsset('Box', {});
boxBackground.anchor.set(0, 1);
boxBackground.x = 1350;
boxBackground.y = 2200;
game.addChild(boxBackground);
var barnBackground = game.attachAsset('Barn', {});
barnBackground.anchor.set(0.5, 0.5);
barnBackground.x = 2048 / 2;
barnBackground.y = 2732 - barnBackground.height / 4 + 900;
game.addChild(barnBackground);
game.addChild(missBackground);
var santa = game.addChild(new Santa());
santa.x = 2048 / 2 + 650;
santa.y = 2732 - santa.height / 2 + 10;
var shield = new Shield(santa.x, santa.y - 100, game);
game.addChild(shield);
game.addChild(santa);
var spawnLocations = [{
	x: tireBackground.x + 175,
	y: tireBackground.y - 65
}, {
	x: boxBackground.x,
	y: boxBackground.y - 350
}, {
	x: crateBackground.x + 250,
	y: crateBackground.y - 600
}, {
	x: 225,
	y: 1685
}, {
	x: 1625,
	y: 1775
}, {
	x: 100,
	y: 1100
}];
var usedSpawnLocations = [];
var spawnLocationCounter = 0;
game.activeBadElves = 0;
game.spawnBadElf = function () {
	if (game.scoreText && game.activeBadElves < 7) {
		if (usedSpawnLocations.length === spawnLocations.length) {
			usedSpawnLocations = [];
			spawnLocationCounter = 0;
		}
		var availableLocations = spawnLocations.filter(function (location, index) {
			return usedSpawnLocations.indexOf(index) === -1;
		});
		if (availableLocations.length === 0) {
			usedSpawnLocations = [];
			availableLocations = spawnLocations.slice();
		}
		var locationIndex = Math.floor(Math.random() * availableLocations.length);
		var location = availableLocations.splice(locationIndex, 1)[0];
		usedSpawnLocations.push(spawnLocations.indexOf(location));
		spawnLocationCounter++;
		if (location) {
			console.log(location);
			if (location.hasOwnProperty("x") && location.hasOwnProperty("y")) {
				var badElf = new BadElf(location.x, location.y, game);
				var scaleModifier = Math.random() * 0.25 + 0.75;
				badElf.scale.set(scaleModifier, scaleModifier);
				game.addChildAt(badElf, game.getChildIndex(boxBackground) + 1);
				game.activeBadElves++;
				if (Math.random() < 0.1) {
					for (var i = 0; i < 3; i++) {
						var locationIndex = Math.floor(Math.random() * availableLocations.length);
						var location = availableLocations.splice(locationIndex, 1)[0];
						if (location) {
							console.log(location);
							if (location.hasOwnProperty("x") && location.hasOwnProperty("y")) {
								var extraBadElf = new BadElf(location.x, location.y, game);
							}
						}
						game.addChildAt(extraBadElf, game.getChildIndex(boxBackground) + 1);
						game.activeBadElves++;
					}
				}
				if (Math.random() < 0.2) {
					for (var i = 0; i < 2; i++) {
						var locationIndex = Math.floor(Math.random() * availableLocations.length);
						var location = availableLocations.splice(locationIndex, 1)[0];
						if (location) {
							console.log(location);
							if (location.hasOwnProperty("x") && location.hasOwnProperty("y")) {
								var extraBadElf = new BadElf(location.x, location.y, game);
							}
						}
						game.addChildAt(extraBadElf, game.getChildIndex(boxBackground) + 1);
						game.activeBadElves++;
					}
				}
				if (Math.random() < 0.35) {
					var locationIndex = Math.floor(Math.random() * availableLocations.length);
					var location = availableLocations.splice(locationIndex, 1)[0];
					if (location) {
						console.log(location);
						if (location.hasOwnProperty("x") && location.hasOwnProperty("y")) {
							var extraBadElf = new BadElf(location.x, location.y, game);
						}
					}
					game.addChildAt(extraBadElf, game.getChildIndex(boxBackground) + 1);
					game.activeBadElves++;
				}
			}
		}
	}
};
var muzzleFlash = new MuzzleFlash();
muzzleFlash.x = santa.x - santa.santaGraphics.width + 400;
muzzleFlash.y = santa.y - 90;
game.addChild(muzzleFlash);
game.addChild(santa);
var crosshair = game.addChild(new Crosshair());
crosshair.x = 2048 / 2;
crosshair.y = 2732 / 2;
crosshair.visible = false;
var isGameOver = false;
var tickOffset = 0;
var leftClickPressed = false;
var barnMovedDown = false;
var lastClickTime = 0;
var playerLives = 6;
var hearts = [];
for (var i = 0; i < playerLives; i++) {
	var heart = new Heart();
	heart.x = 1125 + i * (heart.width + 5);
	heart.y = santa.y - santa.santaGraphics.height - heart.height / 2 + 350;
	hearts.push(heart);
	LK.gui.topLeft.addChild(heart);
}
LK.on('tick', function () {
	santa._move_migrated();
	for (var i = 0; i < game.children.length; i++) {
		var child = game.children[i];
		if (child instanceof Snowflake) {
			child._move_migrated();
		} else if (child instanceof Mistletoe) {
			child.bob();
		} else if (child instanceof Magic) {
			child._move_migrated();
			game.checkMagicCollisionWithSanta(child);
		}
	}
	if (game.isGameOver) {
		return;
	}
	if (leftClickPressed && LK.ticks - lastClickTime > 30) {
		// Check for intersections with Magic and destroy them
		if (blastWave) {
			game.children.forEach(function (child) {
				if (child instanceof Magic && blastWave.intersects(child)) {
					child.destroy();
					game.activeMagicCount--;
				}
			});
		}
		barnBackground.y -= 460;
		room1Background.y -= 100;
		boxBackground.y -= 100;
		crateBackground.y -= 100;
		missBackground.y -= 460;
		tireBackground.y -= 100;
		game.children.forEach(function (child) {
			if (child instanceof BadElf && barnMovedDown) {
				child.y -= 100;
			}
		});
		leftClickPressed = false;
		barnMovedDown = false;
		game.children.forEach(function (child) {
			if (child instanceof Magic) {
				child.destroy();
				game.activeMagicCount--;
			}
		});
		// Instantiate BlastWave at Santa's position
		var blastWave = LK.getAsset('BlastWave', {
			anchorX: 0.5,
			anchorY: 0.5
		});
		blastWave.x = santa.x;
		blastWave.y = santa.y;
		blastWave.alpha = 0.5; // Set transparency to 50%
		game.addChild(blastWave);
		// Play the blastwave sound
		LK.getSound('snd_blastwave').play();
		// Scale BlastWave over 2 seconds and then destroy it
		var scaleDuration = 120; // 120 ticks for 2 seconds
		var scaleStep = 200 / scaleDuration; // 10000% over 2 seconds
		var scaleInterval = LK.setInterval(function () {
			blastWave.scale.x += scaleStep;
			blastWave.scale.y += scaleStep;
			if (blastWave.scale.x * blastWave.width >= 2048 && blastWave.scale.y * blastWave.height >= 2732) {
				LK.clearInterval(scaleInterval);
				blastWave.destroy();
			}
		}, 1000 / 60); // 60 FPS
		LK.getSound('snd_reload').play();
		while (hearts.length < playerLives) {
			var heart = new Heart();
			heart.x = 1125 + hearts.length * (heart.width + 5);
			heart.y = santa.y - santa.santaGraphics.height - heart.height / 2 + 350;
			hearts.push(heart);
			LK.gui.topLeft.addChild(heart);
		}
	}
}); /**** 
* Classes
****/ 
var BadElf = Container.expand(function (spawnX, spawnY, gameInstance) {
	var self = Container.call(this);
	self.gameInstance = gameInstance;
	self.spawnedMagicCount = 1;
	self.magicShootingInterval = LK.setInterval(function () {
		if (self.visible && self.gameInstance && self.gameInstance.activeMagicCount < self.gameInstance.maxMagicAllowed) {
			var magic = new Magic(self.x + 125, self.y, self.gameInstance);
			self.gameInstance.addChild(magic);
			self.gameInstance.activeMagicCount++;
			self.spawnedMagicCount++;
			if (self.spawnedMagicCount >= 3) {
				self.alpha = 1;
				var fadeOutInterval;
				var fadeOut = function fadeOut() {
					if (self.alpha > 0) {
						self.alpha -= 0.05;
					} else {
						LK.clearInterval(fadeOutInterval);
						self.gameInstance.activeBadElves--;
						self.destroy();
						LK.setTimeout(function () {}, 2000);
					}
				};
				LK.clearInterval(self.magicShootingInterval);
				fadeOutInterval = LK.setInterval(fadeOut, 60);
				self.magicShootingInterval = null;
			}
		}
	}, Math.random() * (1000 - 500) + 500);
	var badElfGraphics = self.attachAsset('badElf', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.x = spawnX;
	self.y = spawnY;
	self.on('down', function (x, y, obj) {
		LK.setScore(LK.getScore() + 3);
		LK.gui.top.children[0].setText(LK.getScore().toString());
		this.gameInstance.activeBadElves--;
		var obstacleBox = new ObstacleBox(this.x, this.y, this.gameInstance);
		obstacleBox.x = this.x;
		obstacleBox.y = this.y;
		this.parent.addChild(obstacleBox);
		LK.setTimeout(function () {
			obstacleBox.destroy();
		}, 1000);
		LK.clearInterval(this.magicShootingInterval);
		this.destroy();
		if (Math.random() < 0.1) {
			this.gameInstance.spawnBadElf();
		}
	});
});
var Crosshair = Container.expand(function () {
	var self = Container.call(this);
	self._move_migrated = function (event) {
		if (self.parent) {
			var pos = self.parent.toLocal(event.global);
			self.x = pos.x;
			self.y = pos.y;
		}
	};
	var crosshairGraphics = self.attachAsset('crosshair', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.y = -crosshairGraphics.height / 2;
});
var Heart = Container.expand(function () {
	var self = Container.call(this);
	var heartGraphics = self.attachAsset('heart', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.x = heartGraphics.width / 2;
	self.y = heartGraphics.height / 2;
});
var Magic = Container.expand(function (x, y, gameInstance) {
	var self = Container.call(this);
	// Play the enemy shot sound when Magic (elfbullet) is instantiated
	LK.getSound('snd_enemyshot').play();
	console.log("magic creation triggered");
	self.gameInstance = gameInstance;
	self.moveEnabled = true;
	self.rotation = 0;
	var magicGraphics = self.attachAsset('magic', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	magicGraphics.width = 125;
	magicGraphics.height = 125;
	self.x = x;
	self.y = y;
	self._move_migrated = function () {
		if (self.moveEnabled) {
			var santaX = self.gameInstance.santa && self.gameInstance.santa.santaGraphics ? self.gameInstance.santa.santaGraphics.x : 2048;
			var santaY = self.gameInstance.santa && self.gameInstance.santa.santaGraphics ? self.gameInstance.santa.santaGraphics.y : 2732;
			var santaWidth = self.gameInstance.santa && self.gameInstance.santa.santaGraphics ? self.gameInstance.santa.santaGraphics.width : 0;
			var magicWidth = magicGraphics ? magicGraphics.width : 0;
			var dx = santaX - self.x;
			var dy = santaY - self.y;
			var distance = Math.sqrt(dx * dx + dy * dy);
			var hitThreshold = 10000;
			var speed = 10;
			if (distance > 0) {
				var normX = dx / distance;
				var normY = dy / distance;
				self.x += normX * speed;
				self.y += normY * speed;
			}
			if (distance <= 450) {
				console.log('Hit threshold met. Game over triggered.');
				self.gameInstance.triggerGameOver();
			}
			if (self.gameInstance.santa) {} else if (self.x < 0 || self.x > 2048 || self.y < 0 || self.y > 2732) {
				self.gameInstance.activeMagicCount--;
				self.destroy();
			}
		}
	};
	self.on('down', function () {
		LK.setScore(LK.getScore() + 2);
		LK.gui.top.children[0].setText(LK.getScore().toString());
		var tingText = new Text2('Ting!', {
			size: 40,
			fill: '#ffffff',
			font: 'Arial Black'
		});
		tingText.x = this.x;
		tingText.y = this.y;
		tingText.anchor.set(0.5, 0.5);
		this.parent.addChild(tingText);
		LK.setTimeout(function () {
			tingText.destroy();
		}, 1000);
		this.gameInstance.activeMagicCount--;
		LK.getSound('snd_ricochet').play();
		this.destroy();
	});
});
var Mistletoe = Container.expand(function (gameInstance) {
	var self = Container.call(this);
	// Play the mistletoe sound when Mistletoe is instantiated
	LK.getSound('snd_mistletoe').play();
	self.gameInstance = gameInstance;
	var mistletoeGraphics = self.attachAsset('mistletoe', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	var scale = Math.random() * 0.15 + 0.85;
	mistletoeGraphics.scale.set(scale);
	var bobbingRange = 10;
	var bobbingSpeed = 0.2;
	var bobbingPosition = 0;
	self.bob = function () {
		bobbingPosition += bobbingSpeed;
		self.y += Math.sin(bobbingPosition) * bobbingRange;
	};
	self.on('down', function (x, y, obj) {
		LK.setScore(LK.getScore() + 10);
		LK.gui.top.children[0].setText(LK.getScore().toString());
		self.parent.respawnMistletoe(self);
		LK.getSound('snd_mistletoeshot').play();
		if (Math.random() < 0.5) {
			if (self.gameInstance && typeof self.gameInstance.spawnBadElf === 'function') {
				self.gameInstance.spawnBadElf();
			}
		}
	});
});
var MuzzleFlash = Container.expand(function () {
	var self = Container.call(this);
	self.visible = false;
	var flashGraphics = self.attachAsset('muzzleFlash', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.show = function () {
		var randomScale = Math.random() * 0.125 + 0.375;
		self.scale.set(randomScale);
		self.visible = true;
		LK.setTimeout(function () {
			self.visible = false;
			self.scale.set(1);
		}, 100);
	};
});
var ObstacleBox = Container.expand(function (x, y, gameInstance) {
	var self = Container.call(this);
	self.gameInstance = gameInstance;
	// Play the obstacle sound when ObstacleBox is instantiated
	LK.getSound('snd_obstacle').play();
	var obstacleBoxGraphics = self.attachAsset('obstacleBox', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.x = x;
	self.y = y;
});
var RandomText = Container.expand(function () {
	var self = Container.call(this);
	self.gameInstance = null;
	self.display = function (x, y, messages) {
		var message = messages[Math.floor(Math.random() * messages.length)];
		var text = new Text2(message, {
			size: 48,
			fill: '#ff0000',
			font: 'Arial Black',
			stroke: '#000000',
			strokeThickness: 6
		});
		text.x = x - 400;
		text.y = y + 175;
		text.anchor.set(0.5, 0.5);
		self.addChild(text);
		LK.getSound('snd_messages').play();
		LK.setTimeout(function () {
			text.destroy();
			self.parent.randomTextActive = false;
		}, 3000);
	};
});
var Reindeer = Container.expand(function (gameInstance) {
	var self = Container.call(this);
	// Play the reindeer sound when Reindeer is instantiated
	LK.getSound('snd_reindeer').play();
	self.moveInArc = function () {
		self.arcAngle += self.arcSpeed * self.arcDirection;
		self.x = self.gameInstance.x + Math.cos(self.arcAngle) * self.arcRadius;
		self.y = self.gameInstance.y + Math.sin(self.arcAngle) * self.arcRadius;
	};
	self.moveHorizontally = function () {
		self.x += self.speed * self.direction;
		if (self.x > 2048 || self.x < 0) {
			self.direction *= -1;
			self.scale.x *= -1;
			self.x = Math.max(0, Math.min(self.x, 2048));
		}
	};
	self.fadeOut = function () {
		if (self.alpha > 0) {
			self.alpha -= 0.01;
		} else if (self.gameInstance) {
			self.gameInstance.respawnReindeer(self);
		}
	};
	self.gameInstance = gameInstance;
	var reindeerGraphics = self.attachAsset('reindeer', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.addChild(reindeerGraphics);
	self.speed = 10;
	self.direction = Math.random() < 0.5 ? 1 : -1;
	self.x = self.direction === 1 ? 0 : 2048;
	self.scale.x = self.direction;
	self.on('down', function (x, y, obj) {
		LK.setScore(LK.getScore() + 25);
		LK.gui.top.children[0].setText(LK.getScore().toString());
		var greatText = new Text2('Great!', {
			size: 40,
			fill: '#ffffff',
			font: 'Arial Black'
		});
		greatText.x = this.x;
		greatText.y = this.y;
		greatText.anchor.set(0.5, 0.5);
		this.parent.addChild(greatText);
		LK.setTimeout(function () {
			greatText.destroy();
		}, 1000);
		if (Math.random() < 0.9) {
			this.gameInstance.spawnBadElf();
		}
		this.parent.respawnReindeer();
		LK.getSound('snd_reindeershot').play();
		this.destroy();
	});
});
var Santa = Container.expand(function () {
	var self = Container.call(this);
	self.santaGraphics = self.attachAsset('santa', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.santaGraphics.width = 800;
	self.santaGraphics.height = 800;
	self.santaGraphics.scale.x = 800 / self.santaGraphics.width;
	self._move_migrated = function () {};
});
var Shield = Container.expand(function (x, y, gameInstance) {
	var self = Container.call(this);
	self.gameInstance = gameInstance;
	self.x = x;
	self.y = y;
	self.on('down', function () {
		this.destroy();
	});
});
var Snowflake = Container.expand(function () {
	var self = Container.call(this);
	var sizeFactor = Math.random() * 0.5 + 0.5;
	var snowflakeGraphics = self.attachAsset('snowflake', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	snowflakeGraphics.scale.set(sizeFactor);
	self.speed = -3 * sizeFactor;
	self._move_migrated = function () {
		self.x += self.speed;
		self.y += 0.5;
		self.scale.x -= 0.003;
		self.scale.y -= 0.003;
		if (self.scale.x <= 0 || self.scale.y <= 0) {
			self.destroy();
		}
	};
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000000
});
/**** 
* Game Code
****/ 
game.barnMovedDown = false;
// Play and loop the Christmas music when the game starts
LK.playMusic('snd_christmasmusic', {
	loop: true
});
game.triggerGameOver = function () {
	this.isGameOver = true;
	LK.showGameOver();
	LK.on('tick', function () {
		if (game.isGameOver) {
			return;
		}
	});
};
game.checkMagicCollisionWithSanta = function (magic) {
	if (!this.santa || !this.santa.santaGraphics) {
		return;
	}
	var santaX = this.santa && this.santa.santaGraphics ? this.santa.santaGraphics.x : 0;
	var santaY = this.santa.santaGraphics.y;
	var distance = Math.sqrt(Math.pow(magic.x - santaX, 2) + Math.pow(magic.y - santaY, 2));
	var hitThreshold = (this.santa.santaGraphics.width + magic.width) / 4;
	if (distance <= hitThreshold) {
		this.triggerGameOver();
		magic.destroy();
		this.activeMagicCount--;
	}
};
game.isGameOver = false;
game.activeMagicCount = 0;
game.magicSpawnX = 1024;
game.magicSpawnY = 1366;
game.triggerGameOver = function () {
	this.isGameOver = true;
	LK.showGameOver();
};
game.maxMagicAllowed = 3;
if (game.activeMagicCount < game.maxMagicAllowed && LK.gui.top.children[0] && LK.gui.top.children[0].visible && game.activeBadElves > 0) {
	var magicInstance = new Magic(game.magicSpawnX, game.magicSpawnY, game);
	game.addChild(magicInstance);
	game.activeMagicCount++;
}
game.respawnReindeer = function (reindeer) {
	LK.setTimeout(function () {
		var newReindeer = new Reindeer(game);
		var spawnLeftSide = Math.random() < 0.5;
		newReindeer.x = spawnLeftSide ? 0 : 2048;
		newReindeer.y = Math.random() * (2732 / 4 - newReindeer.height) + newReindeer.height;
		newReindeer.arcDirection = spawnLeftSide ? 1 : -1;
		newReindeer.arcRadius = 2048 / 2;
		newReindeer.arcAngle = spawnLeftSide ? Math.PI : 0;
		var scale = Math.random() * 0.25 + 0.75;
		newReindeer.scale.set(scale);
		game.addChild(newReindeer);
		LK.on('tick', function () {
			newReindeer.moveHorizontally();
		});
	}, Math.random() < 0.5 ? 8000 : 12000);
};
game.showGreatText = function (x, y) {
	var greatText = new Text2('Nice!', {
		size: 40,
		fill: '#ffffff',
		font: 'Arial Black'
	});
	greatText.x = x;
	greatText.y = y;
	greatText.anchor.set(0.5, 0.5);
	game.addChild(greatText);
	LK.setTimeout(function () {
		greatText.destroy();
	}, 1000);
};
var silentNightText = new Text2('Ready?', {
	size: 135,
	fill: "#ffffff",
	//Optional (this is the default string)
	stroke: '#000000',
	strokeThickness: 6
});
silentNightText.anchor.set(0.5, 0);
silentNightText.x = 5 / 2;
silentNightText.y = 150;
LK.gui.top.addChild(silentNightText);
game.generateSnowflakes = function () {
	var snowflakeInterval = LK.setInterval(function () {
		var snowflake = new Snowflake();
		snowflake.x = Math.random() * 2048;
		snowflake.y = Math.random() * 2732;
		game.addChild(snowflake);
	}, 1000);
};
LK.setTimeout(function () {
	var fadeOutDuration = 2000;
	var fadeOutStep = 0.05;
	var fadeOutInterval = LK.setInterval(function () {
		silentNightText.alpha -= fadeOutStep;
		if (silentNightText.alpha <= 0) {
			LK.clearInterval(fadeOutInterval);
			silentNightText.destroy();
			game.score = 0;
			game.scoreText = new Text2(game.score.toString(), {
				size: 150,
				fill: "#ffffff"
			});
			game.scoreText.anchor.set(0.5, 0);
			game.scoreText.x = 5 / 2;
			game.scoreText.y = 150;
			LK.gui.top.addChild(game.scoreText);
			game.updateScore = function (score) {
				var previousScore = game.score;
				game.score += score;
				game.scoreText.setText(game.score.toString());
				LK.setScore(game.score);
				var scoreIncrease = game.score - previousScore;
				if (scoreIncrease >= 20) {
					var newInterval = game.badElfSpawnInterval / 1.2;
					game.badElfSpawnInterval = Math.max(newInterval, game.minimumSpawnInterval);
					LK.clearInterval(game.badElfSpawnTimer);
					game.badElfSpawnTimer = LK.setInterval(game.spawnBadElf, game.badElfSpawnInterval);
				}
			};
			game.badElfSpawnInterval = 3500;
			game.minimumSpawnInterval = 500;
			game.badElfSpawnTimer = LK.setInterval(game.spawnBadElf, game.badElfSpawnInterval);
		}
	}, fadeOutDuration * fadeOutStep);
	game.generateMistletoes = function () {
		LK.setTimeout(function () {
			var newMistletoe = new Mistletoe();
			var boundaryOffset = 100;
			newMistletoe.x = Math.random() * (2048 - 2 * boundaryOffset) + boundaryOffset;
			newMistletoe.y = Math.random() * (2732 - 2 * boundaryOffset - newMistletoe.height - 300 - 200 - 500) + boundaryOffset + 200;
			game.addChild(newMistletoe);
		}, 8000);
	};
	LK.setInterval(function () {
		game.activeBadElves = 0;
		game.spawnBadElf();
	}, 12000);
	LK.setTimeout(function () {
		var reindeer = new Reindeer(game);
		var boundaryOffset = 200;
		reindeer.x = Math.random() < 0.5 ? boundaryOffset : 2048 - boundaryOffset - reindeer.width;
		reindeer.y = Math.random() * (2732 / 4 - boundaryOffset) + boundaryOffset;
		reindeer.arcDirection = Math.random() < 0.5 ? 1 : -1;
		reindeer.arcRadius = 2048 / 2;
		reindeer.arcAngle = Math.random() < 0.5 ? Math.PI : 0;
		var initialScale = Math.random() * 0.25 + 0.75;
		reindeer.scale.set(initialScale);
		game.addChild(reindeer);
		LK.on('tick', function () {
			reindeer.moveHorizontally();
		});
	}, 12000);
	LK.setTimeout(game.generateMistletoes, 8000);
}, 2000);
game.on('move', function (x, y, obj) {
	crosshair._move_migrated(obj);
});
game.on('down', function (x, y, obj) {
	if (game) {
		var pos = game.toLocal(obj.global);
		crosshair.x = pos.x;
		crosshair.y = pos.y;
		crosshair.visible = true;
		LK.setTimeout(function () {
			crosshair.visible = false;
		}, 500); // Hide the crosshair after 500ms
		LK.setTimeout(function () {
			crosshair.visible = false;
		}, 500); // Hide the crosshair after 500ms
	}
	if (hearts.length > 0) {
		muzzleFlash.show();
		leftClickPressed = true;
		lastClickTime = LK.ticks;
		LK.getSound('snd_pistol').play(); // Play pistol sound on left click
		if (!barnMovedDown) {
			game.children.forEach(function (child) {
				if (child instanceof BadElf) {
					child.y += 100;
				}
			});
			barnBackground.y += 460;
			room1Background.y += 100;
			boxBackground.y += 100;
			crateBackground.y += 100;
			missBackground.y += 460;
			tireBackground.y += 100;
			barnMovedDown = true;
			if (Math.random() < 0.10) {
				var messages = ["Santa's special delivery!", "This is for the naughty list!", "Ho, ho, ho-hold this!"];
				if (!game.randomTextActive) {
					game.randomTextActive = true;
					var randomText = new RandomText();
					randomText.display(santa.x, santa.y - santa.santaGraphics.height / 2, messages);
					game.addChild(randomText);
				}
			}
		}
		var heart = hearts.pop();
		heart.destroy();
	}
	game.children.forEach(function (child) {
		if (child instanceof BadElf && leftClickPressed && LK.ticks - lastClickTime > 30) {
			child.y -= 100;
		}
	});
});
var room1Background = game.attachAsset('Room1', {});
room1Background.anchor.set(0.5, 0.5);
room1Background.x = 2048 / 2;
room1Background.y = 2732 / 2;
game.addChild(room1Background);
var tireBackground = game.attachAsset('Tire', {
	anchorX: 0.5,
	anchorY: 0.5
});
tireBackground.anchor.set(0.5, 0.5);
tireBackground.x = 700;
tireBackground.y = 1825;
game.addChild(tireBackground);
game.respawnMistletoe = function (mistletoe) {
	this.showGreatText(mistletoe.x, mistletoe.y);
	mistletoe.destroy();
	LK.setTimeout(function () {
		var newMistletoe = new Mistletoe();
		var boundaryOffset = 100;
		newMistletoe.x = Math.random() * (2048 - 2 * boundaryOffset) + boundaryOffset;
		newMistletoe.y = Math.random() * (2732 - 2 * boundaryOffset - newMistletoe.height - 300 - 200 - 100 - 500) + boundaryOffset + 300;
		game.addChild(newMistletoe);
	}, Math.random() * (10000 - 5000) + 5000);
};
game.generateSnowflakes();
var missBackground = game.attachAsset('Miss', {});
missBackground.anchor.set(0.5, 0.5);
missBackground.x = 700;
missBackground.y = 2520;
var crateBackground = game.attachAsset('Crate', {});
crateBackground.anchor.set(0, 1);
crateBackground.x = 205;
crateBackground.y = 2645;
game.addChild(crateBackground);
var boxBackground = game.attachAsset('Box', {});
boxBackground.anchor.set(0, 1);
boxBackground.x = 1350;
boxBackground.y = 2200;
game.addChild(boxBackground);
var barnBackground = game.attachAsset('Barn', {});
barnBackground.anchor.set(0.5, 0.5);
barnBackground.x = 2048 / 2;
barnBackground.y = 2732 - barnBackground.height / 4 + 900;
game.addChild(barnBackground);
game.addChild(missBackground);
var santa = game.addChild(new Santa());
santa.x = 2048 / 2 + 650;
santa.y = 2732 - santa.height / 2 + 10;
var shield = new Shield(santa.x, santa.y - 100, game);
game.addChild(shield);
game.addChild(santa);
var spawnLocations = [{
	x: tireBackground.x + 175,
	y: tireBackground.y - 65
}, {
	x: boxBackground.x,
	y: boxBackground.y - 350
}, {
	x: crateBackground.x + 250,
	y: crateBackground.y - 600
}, {
	x: 225,
	y: 1685
}, {
	x: 1625,
	y: 1775
}, {
	x: 100,
	y: 1100
}];
var usedSpawnLocations = [];
var spawnLocationCounter = 0;
game.activeBadElves = 0;
game.spawnBadElf = function () {
	if (game.scoreText && game.activeBadElves < 7) {
		if (usedSpawnLocations.length === spawnLocations.length) {
			usedSpawnLocations = [];
			spawnLocationCounter = 0;
		}
		var availableLocations = spawnLocations.filter(function (location, index) {
			return usedSpawnLocations.indexOf(index) === -1;
		});
		if (availableLocations.length === 0) {
			usedSpawnLocations = [];
			availableLocations = spawnLocations.slice();
		}
		var locationIndex = Math.floor(Math.random() * availableLocations.length);
		var location = availableLocations.splice(locationIndex, 1)[0];
		usedSpawnLocations.push(spawnLocations.indexOf(location));
		spawnLocationCounter++;
		if (location) {
			console.log(location);
			if (location.hasOwnProperty("x") && location.hasOwnProperty("y")) {
				var badElf = new BadElf(location.x, location.y, game);
				var scaleModifier = Math.random() * 0.25 + 0.75;
				badElf.scale.set(scaleModifier, scaleModifier);
				game.addChildAt(badElf, game.getChildIndex(boxBackground) + 1);
				game.activeBadElves++;
				if (Math.random() < 0.1) {
					for (var i = 0; i < 3; i++) {
						var locationIndex = Math.floor(Math.random() * availableLocations.length);
						var location = availableLocations.splice(locationIndex, 1)[0];
						if (location) {
							console.log(location);
							if (location.hasOwnProperty("x") && location.hasOwnProperty("y")) {
								var extraBadElf = new BadElf(location.x, location.y, game);
							}
						}
						game.addChildAt(extraBadElf, game.getChildIndex(boxBackground) + 1);
						game.activeBadElves++;
					}
				}
				if (Math.random() < 0.2) {
					for (var i = 0; i < 2; i++) {
						var locationIndex = Math.floor(Math.random() * availableLocations.length);
						var location = availableLocations.splice(locationIndex, 1)[0];
						if (location) {
							console.log(location);
							if (location.hasOwnProperty("x") && location.hasOwnProperty("y")) {
								var extraBadElf = new BadElf(location.x, location.y, game);
							}
						}
						game.addChildAt(extraBadElf, game.getChildIndex(boxBackground) + 1);
						game.activeBadElves++;
					}
				}
				if (Math.random() < 0.35) {
					var locationIndex = Math.floor(Math.random() * availableLocations.length);
					var location = availableLocations.splice(locationIndex, 1)[0];
					if (location) {
						console.log(location);
						if (location.hasOwnProperty("x") && location.hasOwnProperty("y")) {
							var extraBadElf = new BadElf(location.x, location.y, game);
						}
					}
					game.addChildAt(extraBadElf, game.getChildIndex(boxBackground) + 1);
					game.activeBadElves++;
				}
			}
		}
	}
};
var muzzleFlash = new MuzzleFlash();
muzzleFlash.x = santa.x - santa.santaGraphics.width + 400;
muzzleFlash.y = santa.y - 90;
game.addChild(muzzleFlash);
game.addChild(santa);
var crosshair = game.addChild(new Crosshair());
crosshair.x = 2048 / 2;
crosshair.y = 2732 / 2;
crosshair.visible = false;
var isGameOver = false;
var tickOffset = 0;
var leftClickPressed = false;
var barnMovedDown = false;
var lastClickTime = 0;
var playerLives = 6;
var hearts = [];
for (var i = 0; i < playerLives; i++) {
	var heart = new Heart();
	heart.x = 1125 + i * (heart.width + 5);
	heart.y = santa.y - santa.santaGraphics.height - heart.height / 2 + 350;
	hearts.push(heart);
	LK.gui.topLeft.addChild(heart);
}
LK.on('tick', function () {
	santa._move_migrated();
	for (var i = 0; i < game.children.length; i++) {
		var child = game.children[i];
		if (child instanceof Snowflake) {
			child._move_migrated();
		} else if (child instanceof Mistletoe) {
			child.bob();
		} else if (child instanceof Magic) {
			child._move_migrated();
			game.checkMagicCollisionWithSanta(child);
		}
	}
	if (game.isGameOver) {
		return;
	}
	if (leftClickPressed && LK.ticks - lastClickTime > 30) {
		// Check for intersections with Magic and destroy them
		if (blastWave) {
			game.children.forEach(function (child) {
				if (child instanceof Magic && blastWave.intersects(child)) {
					child.destroy();
					game.activeMagicCount--;
				}
			});
		}
		barnBackground.y -= 460;
		room1Background.y -= 100;
		boxBackground.y -= 100;
		crateBackground.y -= 100;
		missBackground.y -= 460;
		tireBackground.y -= 100;
		game.children.forEach(function (child) {
			if (child instanceof BadElf && barnMovedDown) {
				child.y -= 100;
			}
		});
		leftClickPressed = false;
		barnMovedDown = false;
		game.children.forEach(function (child) {
			if (child instanceof Magic) {
				child.destroy();
				game.activeMagicCount--;
			}
		});
		// Instantiate BlastWave at Santa's position
		var blastWave = LK.getAsset('BlastWave', {
			anchorX: 0.5,
			anchorY: 0.5
		});
		blastWave.x = santa.x;
		blastWave.y = santa.y;
		blastWave.alpha = 0.5; // Set transparency to 50%
		game.addChild(blastWave);
		// Play the blastwave sound
		LK.getSound('snd_blastwave').play();
		// Scale BlastWave over 2 seconds and then destroy it
		var scaleDuration = 120; // 120 ticks for 2 seconds
		var scaleStep = 200 / scaleDuration; // 10000% over 2 seconds
		var scaleInterval = LK.setInterval(function () {
			blastWave.scale.x += scaleStep;
			blastWave.scale.y += scaleStep;
			if (blastWave.scale.x * blastWave.width >= 2048 && blastWave.scale.y * blastWave.height >= 2732) {
				LK.clearInterval(scaleInterval);
				blastWave.destroy();
			}
		}, 1000 / 60); // 60 FPS
		LK.getSound('snd_reload').play();
		while (hearts.length < playerLives) {
			var heart = new Heart();
			heart.x = 1125 + hearts.length * (heart.width + 5);
			heart.y = santa.y - santa.santaGraphics.height - heart.height / 2 + 350;
			hearts.push(heart);
			LK.gui.topLeft.addChild(heart);
		}
	}
});
 over the shoulder santa firing a revolver Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
 2d transparent christmas crosshair Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
 2d 3rd person front view of a christmas town square with a starry sky Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
 Christmas sparkles png Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
 circular christmas golden star pattern transparent png Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
 2d christmas brick wall Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
 2d opened christmas crate Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
 2d diagonal christmas car or truck in snow Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
 a christmas poster showcasing miss santa clause Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
 a single white snowflake Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
 2d stacked christmas winter tire Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
 2d stacked christmas winter tire Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
 2d christmas magical mistletoe Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
 2d christmas 357 Magnum bullets Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
 2d silhouette of a flying reindeer with a red glowy nose Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
 2d christmas dark sparkles Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
 2d christmas evil robot elf with a gun Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
 2d pile of gray and red nuts and bolts Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
 transparent snow sphere. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
snd_pistol
Sound effect
snd_enemyshot
Sound effect
snd_obstacle
Sound effect
snd_messages
Sound effect
snd_ricochet
Sound effect
snd_reindeer
Sound effect
snd_mistletoe
Sound effect
snd_reindeershot
Sound effect
snd_mistletoeshot
Sound effect
snd_christmasmusic
Music
snd_reload
Sound effect
snd_blastwave
Sound effect