/**** 
* Classes
****/ 
// No plugins needed for this game yet.
// Bullet class
var Bullet = Container.expand(function () {
	var self = Container.call(this);
	var graphics = self.attachAsset('bullet', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = -15; // Speed moving upwards
	// Called every tick by the engine if the bullet is attached to the game
	self.update = function () {
		self.y += self.speed;
	};
	return self;
});
// Player class
var Player = Container.expand(function () {
	var self = Container.call(this);
	var graphics = self.attachAsset('player', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// No update needed here as movement is handled by drag in the main game code
	return self;
});
// Target class
var Target = Container.expand(function () {
	var self = Container.call(this);
	var graphics = self.attachAsset('target', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Target is stationary, no update needed unless adding animations later
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x2c3e50 // Dark slate blue background
});
/**** 
* Game Code
****/ 
// Red target circle
// Yellow bullet rectangle
// Blue player rectangle
// Define assets using LK.init.shape for simplicity
// Game constants
var WIN_SCORE = 50;
var FIRE_RATE = 15; // Lower number means faster firing (fire every 15 ticks)
var PLAYER_BOTTOM_MARGIN = 150; // How far from the bottom the player is positioned
// Game variables
var bullets = [];
var dragPlayer = false; // Flag to track if the player is being dragged
var playerLastIntersectingTarget = false; // Track player collision state
// Create and position the target
var target = game.addChild(new Target());
target.x = 2048 / 2; // Center horizontally
target.y = 2732 / 3; // Position vertically (adjust as needed)
// Create and position the player
var player = game.addChild(new Player());
player.x = 2048 / 2; // Start at center horizontally
player.y = 2732 - PLAYER_BOTTOM_MARGIN - player.height / 2; // Position near the bottom
// Score Display
var scoreTxt = new Text2('0', {
	size: 150,
	fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0); // Anchor top-center
LK.gui.top.addChild(scoreTxt); // Add to top-center GUI area
// --- Event Handlers ---
// Handle dragging the player
function handleMove(x, y, obj) {
	if (dragPlayer) {
		// Convert the event x-coordinate (relative to the game stage) to the player's local x
		var localX = game.toLocal(obj.position).x;
		// Clamp player position within screen bounds (consider player width)
		var halfPlayerWidth = player.width / 2;
		player.x = Math.max(halfPlayerWidth, Math.min(localX, 2048 - halfPlayerWidth));
	}
	// Check for player collision with target *during* move for immediate feedback
	checkPlayerTargetCollision();
}
// Start dragging
game.down = function (x, y, obj) {
	// Check if the down event happened on or near the player
	// We allow starting drag anywhere on the screen for simplicity
	dragPlayer = true;
	handleMove(x, y, obj); // Immediately move player to touch position
};
// Stop dragging
game.up = function (x, y, obj) {
	dragPlayer = false;
};
// Assign move handler
game.move = handleMove;
// --- Collision Check Functions ---
function checkPlayerTargetCollision() {
	var currentPlayerIntersecting = player.intersects(target);
	if (!playerLastIntersectingTarget && currentPlayerIntersecting) {
		// Collision just started
		LK.getSound('gameOverSound').play();
		LK.effects.flashObject(player, 0xff0000, 300); // Flash player red
		LK.effects.flashObject(target, 0xff0000, 300); // Flash target red
		LK.showGameOver(); // End the game
	}
	// Update last state for next check
	playerLastIntersectingTarget = currentPlayerIntersecting;
}
// --- Game Update Loop ---
game.update = function () {
	// Check player collision (also checked in handleMove, but good to have here too)
	checkPlayerTargetCollision();
	// Auto-fire bullets
	if (LK.ticks % FIRE_RATE === 0) {
		var newBullet = new Bullet();
		newBullet.x = player.x;
		newBullet.y = player.y - player.height / 2; // Start from top of player
		newBullet.lastY = newBullet.y; // Initialize lastY
		newBullet.lastIntersectingTarget = newBullet.intersects(target); // Initialize intersection state
		game.addChild(newBullet);
		bullets.push(newBullet);
		LK.getSound('shoot').play();
	}
	// Update bullets
	for (var i = bullets.length - 1; i >= 0; i--) {
		var bullet = bullets[i];
		// Update bullet's last states before moving
		if (bullet.lastY === undefined) {
			bullet.lastY = bullet.y;
		}
		if (bullet.lastIntersectingTarget === undefined) {
			bullet.lastIntersectingTarget = false;
		}
		// bullet.update() is called automatically by the engine
		var currentY = bullet.y;
		var currentIntersectingTarget = bullet.intersects(target);
		// 1. Check for collision with target
		if (!bullet.lastIntersectingTarget && currentIntersectingTarget) {
			LK.setScore(LK.getScore() + 1);
			scoreTxt.setText(LK.getScore());
			LK.getSound('hit').play();
			LK.effects.flashObject(target, 0xffffff, 100); // Flash target white on hit
			// Check for win condition
			if (LK.getScore() >= WIN_SCORE) {
				LK.showYouWin();
				// No need to continue processing bullets if game is won
				return;
			}
			// Destroy bullet and remove from array
			bullet.destroy();
			bullets.splice(i, 1);
			continue; // Skip off-screen check for this bullet
		}
		// 2. Check if bullet is off-screen (top)
		// Trigger when crossing the boundary from on-screen to off-screen
		if (bullet.lastY >= -bullet.height / 2 && currentY < -bullet.height / 2) {
			bullet.destroy();
			bullets.splice(i, 1);
			continue; // Bullet is gone, move to the next
		}
		// Update last known states for the next frame
		bullet.lastY = currentY;
		bullet.lastIntersectingTarget = currentIntersectingTarget;
	}
};
// Optionally start background music if one was defined and loaded
// LK.playMusic('musicId'); /**** 
* Classes
****/ 
// No plugins needed for this game yet.
// Bullet class
var Bullet = Container.expand(function () {
	var self = Container.call(this);
	var graphics = self.attachAsset('bullet', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = -15; // Speed moving upwards
	// Called every tick by the engine if the bullet is attached to the game
	self.update = function () {
		self.y += self.speed;
	};
	return self;
});
// Player class
var Player = Container.expand(function () {
	var self = Container.call(this);
	var graphics = self.attachAsset('player', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// No update needed here as movement is handled by drag in the main game code
	return self;
});
// Target class
var Target = Container.expand(function () {
	var self = Container.call(this);
	var graphics = self.attachAsset('target', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Target is stationary, no update needed unless adding animations later
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x2c3e50 // Dark slate blue background
});
/**** 
* Game Code
****/ 
// Red target circle
// Yellow bullet rectangle
// Blue player rectangle
// Define assets using LK.init.shape for simplicity
// Game constants
var WIN_SCORE = 50;
var FIRE_RATE = 15; // Lower number means faster firing (fire every 15 ticks)
var PLAYER_BOTTOM_MARGIN = 150; // How far from the bottom the player is positioned
// Game variables
var bullets = [];
var dragPlayer = false; // Flag to track if the player is being dragged
var playerLastIntersectingTarget = false; // Track player collision state
// Create and position the target
var target = game.addChild(new Target());
target.x = 2048 / 2; // Center horizontally
target.y = 2732 / 3; // Position vertically (adjust as needed)
// Create and position the player
var player = game.addChild(new Player());
player.x = 2048 / 2; // Start at center horizontally
player.y = 2732 - PLAYER_BOTTOM_MARGIN - player.height / 2; // Position near the bottom
// Score Display
var scoreTxt = new Text2('0', {
	size: 150,
	fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0); // Anchor top-center
LK.gui.top.addChild(scoreTxt); // Add to top-center GUI area
// --- Event Handlers ---
// Handle dragging the player
function handleMove(x, y, obj) {
	if (dragPlayer) {
		// Convert the event x-coordinate (relative to the game stage) to the player's local x
		var localX = game.toLocal(obj.position).x;
		// Clamp player position within screen bounds (consider player width)
		var halfPlayerWidth = player.width / 2;
		player.x = Math.max(halfPlayerWidth, Math.min(localX, 2048 - halfPlayerWidth));
	}
	// Check for player collision with target *during* move for immediate feedback
	checkPlayerTargetCollision();
}
// Start dragging
game.down = function (x, y, obj) {
	// Check if the down event happened on or near the player
	// We allow starting drag anywhere on the screen for simplicity
	dragPlayer = true;
	handleMove(x, y, obj); // Immediately move player to touch position
};
// Stop dragging
game.up = function (x, y, obj) {
	dragPlayer = false;
};
// Assign move handler
game.move = handleMove;
// --- Collision Check Functions ---
function checkPlayerTargetCollision() {
	var currentPlayerIntersecting = player.intersects(target);
	if (!playerLastIntersectingTarget && currentPlayerIntersecting) {
		// Collision just started
		LK.getSound('gameOverSound').play();
		LK.effects.flashObject(player, 0xff0000, 300); // Flash player red
		LK.effects.flashObject(target, 0xff0000, 300); // Flash target red
		LK.showGameOver(); // End the game
	}
	// Update last state for next check
	playerLastIntersectingTarget = currentPlayerIntersecting;
}
// --- Game Update Loop ---
game.update = function () {
	// Check player collision (also checked in handleMove, but good to have here too)
	checkPlayerTargetCollision();
	// Auto-fire bullets
	if (LK.ticks % FIRE_RATE === 0) {
		var newBullet = new Bullet();
		newBullet.x = player.x;
		newBullet.y = player.y - player.height / 2; // Start from top of player
		newBullet.lastY = newBullet.y; // Initialize lastY
		newBullet.lastIntersectingTarget = newBullet.intersects(target); // Initialize intersection state
		game.addChild(newBullet);
		bullets.push(newBullet);
		LK.getSound('shoot').play();
	}
	// Update bullets
	for (var i = bullets.length - 1; i >= 0; i--) {
		var bullet = bullets[i];
		// Update bullet's last states before moving
		if (bullet.lastY === undefined) {
			bullet.lastY = bullet.y;
		}
		if (bullet.lastIntersectingTarget === undefined) {
			bullet.lastIntersectingTarget = false;
		}
		// bullet.update() is called automatically by the engine
		var currentY = bullet.y;
		var currentIntersectingTarget = bullet.intersects(target);
		// 1. Check for collision with target
		if (!bullet.lastIntersectingTarget && currentIntersectingTarget) {
			LK.setScore(LK.getScore() + 1);
			scoreTxt.setText(LK.getScore());
			LK.getSound('hit').play();
			LK.effects.flashObject(target, 0xffffff, 100); // Flash target white on hit
			// Check for win condition
			if (LK.getScore() >= WIN_SCORE) {
				LK.showYouWin();
				// No need to continue processing bullets if game is won
				return;
			}
			// Destroy bullet and remove from array
			bullet.destroy();
			bullets.splice(i, 1);
			continue; // Skip off-screen check for this bullet
		}
		// 2. Check if bullet is off-screen (top)
		// Trigger when crossing the boundary from on-screen to off-screen
		if (bullet.lastY >= -bullet.height / 2 && currentY < -bullet.height / 2) {
			bullet.destroy();
			bullets.splice(i, 1);
			continue; // Bullet is gone, move to the next
		}
		// Update last known states for the next frame
		bullet.lastY = currentY;
		bullet.lastIntersectingTarget = currentIntersectingTarget;
	}
};
// Optionally start background music if one was defined and loaded
// LK.playMusic('musicId');