/**** 
* Classes
****/ 
// FlyingObject class
var FlyingObject = Container.expand(function () {
	var self = Container.call(this);
	this.hasBeenSliced = false; // Track if the object has been sliced
	var objectGraphics = self.attachAsset('flyingObject', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 5;
	self._move_migrated = function () {
		self.y += self.speed;
		// Remove object if it moves off screen
		if (self.y > 2732) {
			self.destroy();
		}
	};
});
// Line class
var Line = Container.expand(function () {
	var self = Container.call(this);
	self.drawLine = function (start, end) {
		var lineLength = Math.sqrt(Math.pow(end.x - start.x, 2) + Math.pow(end.y - start.y, 2));
		var angle = Math.atan2(end.y - start.y, end.x - start.x);
		var lineGraphics = self.attachAsset('line', {
			anchorX: 0.5,
			anchorY: 0.5,
			x: start.x,
			y: start.y,
			width: lineLength,
			height: 150,
			// Fixed thickness
			rotation: angle,
			// Rotate line to match start and end points
			color: 0xFF0000
		});
		self.addChild(lineGraphics);
		// Set a timer to remove the line after 0.5 second
		LK.setTimeout(function () {
			self.removeChild(lineGraphics);
		}, 500);
	};
});
// SlicedObject class
var SlicedObject = Container.expand(function () {
	var self = Container.call(this);
	var objectGraphics;
	if (Math.random() < 0.5) {
		objectGraphics = self.attachAsset('Storma1', {
			anchorX: 0.5,
			anchorY: 0.5
		});
	} else {
		objectGraphics = self.attachAsset('Storma2', {
			anchorX: 0.5,
			anchorY: 0.5
		});
	}
	self.speed = 5;
	self.direction = Math.random() < 0.5 ? -1 : 1; // Random direction
	self._move_migrated = function () {
		self.y += self.speed;
		self.x += self.speed * self.direction;
		// Remove object if it moves off screen
		if (self.y > 2732 || self.x < 0 || self.x > 2048) {
			self.destroy();
		}
	};
});
/**** 
* Initialize Game
****/ 
// Assets will be automatically created based on usage in the code.
var game = new LK.Game({
	backgroundColor: 0x000000
	// Init game with black background
	// This property does not need to be set as the engine automatically centers and scales the background image to fit the screen.
});
/**** 
* Game Code
****/ 
var comboCounter = 0;
var comboTxt = new Text2(comboCounter.toString(), {
	size: 100,
	fill: "#ff0000"
});
LK.gui.topRight.addChild(comboTxt);
var flyingObjects = [];
var score = 0;
var scoreTxt = new Text2(score.toString(), {
	size: 150,
	fill: "#ffffff"
});
LK.gui.top.addChild(scoreTxt);
// Function to spawn flying objects
function spawnFlyingObject() {
	var flyingObject = new FlyingObject();
	flyingObject.x = Math.random() * 2048; // Spawn at a random x position
	flyingObject.y = 2732; // Spawn at the bottom
	flyingObject.speed = 5; // Set a consistent speed for simplicity
	flyingObject._move_migrated = function () {
		// Override the move function for diagonal movement
		this.y -= this.speed; // Move up
		this.x += this.speed * (Math.random() < 0.5 ? -1 : 1); // Move left or right randomly
		// Remove object if it moves off screen
		if (this.y < 0 || this.x < 0 || this.x > 2048) {
			this.destroy();
			if (comboCounter > 0) {
				score *= 2; // Double the score when combo ends
				scoreTxt.setText(score.toString()); // Update score display
				comboCounter = 0; // Reset combo counter on miss
				comboTxt.setText("Combo: " + comboCounter.toString()); // Update combo display
			}
		}
	};
	flyingObjects.push(flyingObject);
	game.addChild(flyingObject);
}
var line = new Line();
game.addChild(line);
var startPos = null;
// Handle drawing action
game.on('down', function (x, y, obj) {
	startPos = game.toLocal(obj.global);
});
game.on('move', function (x, y, obj) {
	if (startPos) {
		var endPos = game.toLocal(obj.global);
		line.drawLine(startPos, endPos);
		flyingObjects.forEach(function (object, index) {
			if (line.intersects(object) && !object.hasBeenSliced && object.assetId !== 'Storma1' && object.assetId !== 'Storma2') {
				object.hasBeenSliced = true; // Mark the object as sliced
				object.destroy(); // Destroy the object
				flyingObjects.splice(index, 1); // Remove from array
				score++; // Increase score
				scoreTxt.setText(score.toString()); // Update score display
				comboCounter++; // Increment combo counter
				comboTxt.setText("Combo: " + comboCounter.toString()); // Update combo display
			}
		});
	}
});
game.on('up', function (x, y, obj) {
	startPos = null;
	if (line.graphics) {
		line.graphics.clear();
	}
});
// Game tick function
LK.on('tick', function () {
	// Move flying objects
	flyingObjects.forEach(function (object) {
		object._move_migrated();
	});
	// Spawn a new flying object every 60 frames (about 1 second)
	if (LK.ticks % 60 === 0) {
		spawnFlyingObject();
	}
});
// Initialize the game with a flying object
spawnFlyingObject(); /**** 
* Classes
****/ 
// FlyingObject class
var FlyingObject = Container.expand(function () {
	var self = Container.call(this);
	this.hasBeenSliced = false; // Track if the object has been sliced
	var objectGraphics = self.attachAsset('flyingObject', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 5;
	self._move_migrated = function () {
		self.y += self.speed;
		// Remove object if it moves off screen
		if (self.y > 2732) {
			self.destroy();
		}
	};
});
// Line class
var Line = Container.expand(function () {
	var self = Container.call(this);
	self.drawLine = function (start, end) {
		var lineLength = Math.sqrt(Math.pow(end.x - start.x, 2) + Math.pow(end.y - start.y, 2));
		var angle = Math.atan2(end.y - start.y, end.x - start.x);
		var lineGraphics = self.attachAsset('line', {
			anchorX: 0.5,
			anchorY: 0.5,
			x: start.x,
			y: start.y,
			width: lineLength,
			height: 150,
			// Fixed thickness
			rotation: angle,
			// Rotate line to match start and end points
			color: 0xFF0000
		});
		self.addChild(lineGraphics);
		// Set a timer to remove the line after 0.5 second
		LK.setTimeout(function () {
			self.removeChild(lineGraphics);
		}, 500);
	};
});
// SlicedObject class
var SlicedObject = Container.expand(function () {
	var self = Container.call(this);
	var objectGraphics;
	if (Math.random() < 0.5) {
		objectGraphics = self.attachAsset('Storma1', {
			anchorX: 0.5,
			anchorY: 0.5
		});
	} else {
		objectGraphics = self.attachAsset('Storma2', {
			anchorX: 0.5,
			anchorY: 0.5
		});
	}
	self.speed = 5;
	self.direction = Math.random() < 0.5 ? -1 : 1; // Random direction
	self._move_migrated = function () {
		self.y += self.speed;
		self.x += self.speed * self.direction;
		// Remove object if it moves off screen
		if (self.y > 2732 || self.x < 0 || self.x > 2048) {
			self.destroy();
		}
	};
});
/**** 
* Initialize Game
****/ 
// Assets will be automatically created based on usage in the code.
var game = new LK.Game({
	backgroundColor: 0x000000
	// Init game with black background
	// This property does not need to be set as the engine automatically centers and scales the background image to fit the screen.
});
/**** 
* Game Code
****/ 
var comboCounter = 0;
var comboTxt = new Text2(comboCounter.toString(), {
	size: 100,
	fill: "#ff0000"
});
LK.gui.topRight.addChild(comboTxt);
var flyingObjects = [];
var score = 0;
var scoreTxt = new Text2(score.toString(), {
	size: 150,
	fill: "#ffffff"
});
LK.gui.top.addChild(scoreTxt);
// Function to spawn flying objects
function spawnFlyingObject() {
	var flyingObject = new FlyingObject();
	flyingObject.x = Math.random() * 2048; // Spawn at a random x position
	flyingObject.y = 2732; // Spawn at the bottom
	flyingObject.speed = 5; // Set a consistent speed for simplicity
	flyingObject._move_migrated = function () {
		// Override the move function for diagonal movement
		this.y -= this.speed; // Move up
		this.x += this.speed * (Math.random() < 0.5 ? -1 : 1); // Move left or right randomly
		// Remove object if it moves off screen
		if (this.y < 0 || this.x < 0 || this.x > 2048) {
			this.destroy();
			if (comboCounter > 0) {
				score *= 2; // Double the score when combo ends
				scoreTxt.setText(score.toString()); // Update score display
				comboCounter = 0; // Reset combo counter on miss
				comboTxt.setText("Combo: " + comboCounter.toString()); // Update combo display
			}
		}
	};
	flyingObjects.push(flyingObject);
	game.addChild(flyingObject);
}
var line = new Line();
game.addChild(line);
var startPos = null;
// Handle drawing action
game.on('down', function (x, y, obj) {
	startPos = game.toLocal(obj.global);
});
game.on('move', function (x, y, obj) {
	if (startPos) {
		var endPos = game.toLocal(obj.global);
		line.drawLine(startPos, endPos);
		flyingObjects.forEach(function (object, index) {
			if (line.intersects(object) && !object.hasBeenSliced && object.assetId !== 'Storma1' && object.assetId !== 'Storma2') {
				object.hasBeenSliced = true; // Mark the object as sliced
				object.destroy(); // Destroy the object
				flyingObjects.splice(index, 1); // Remove from array
				score++; // Increase score
				scoreTxt.setText(score.toString()); // Update score display
				comboCounter++; // Increment combo counter
				comboTxt.setText("Combo: " + comboCounter.toString()); // Update combo display
			}
		});
	}
});
game.on('up', function (x, y, obj) {
	startPos = null;
	if (line.graphics) {
		line.graphics.clear();
	}
});
// Game tick function
LK.on('tick', function () {
	// Move flying objects
	flyingObjects.forEach(function (object) {
		object._move_migrated();
	});
	// Spawn a new flying object every 60 frames (about 1 second)
	if (LK.ticks % 60 === 0) {
		spawnFlyingObject();
	}
});
// Initialize the game with a flying object
spawnFlyingObject();