/**** 
* Classes
****/ 
// Background image covering the entire screen
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Block class representing each block in the game
var Block = Container.expand(function () {
	var self = Container.call(this);
	var blockGraphics = self.attachAsset('block', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.update = function () {
		// Update logic for blocks if needed
	};
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000000 //Init game with black background 
});
/**** 
* Game Code
****/ 
var background = LK.getAsset('background', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: 2048 / 2,
	y: 2732 / 2
});
game.addChild(background);
// Function to check and remove blocks when 10 are aligned horizontally 
function checkAndRemoveAlignedBlocks() {
	for (var j = 0; j < boardGrid[0].length; j++) {
		var consecutiveBlocks = 0;
		for (var i = 0; i < boardGrid.length; i++) {
			if (boardGrid[i][j] !== null) {
				consecutiveBlocks++;
				if (consecutiveBlocks >= 10) {
					// Remove the aligned blocks
					for (var k = i; k > i - 10; k--) {
						if (boardGrid[k][j] !== null) {
							boardGrid[k][j].destroy();
							boardGrid[k][j] = null;
						}
					}
					// Shift blocks above down
					for (var k = i - 10; k >= 0; k--) {
						if (boardGrid[k][j] !== null) {
							boardGrid[k + 10][j] = boardGrid[k][j];
							boardGrid[k][j] = null;
							boardGrid[k + 10][j].y += BLOCK_SIZE * 10;
						}
					}
					consecutiveBlocks = 0; // Reset counter after removal
				}
			} else {
				consecutiveBlocks = 0; // Reset counter if a gap is found
			}
		}
	}
}
// Constants
var BOARD_WIDTH = 2048;
var BOARD_HEIGHT = 2000; // Area above where blocks are placed
var BLOCK_SIZE = 100; // Size of each block
var MAX_BLOCKS = Math.floor(BOARD_WIDTH / BLOCK_SIZE * (BOARD_HEIGHT / BLOCK_SIZE) * 0.85); // 85% of the board
// Variables
var blocks = [];
var currentBlock = null;
var isGameOver = false;
// Create a grid for the board
var boardGrid = [];
for (var i = 0; i < BOARD_WIDTH / BLOCK_SIZE; i++) {
	boardGrid[i] = [];
	for (var j = 0; j < BOARD_HEIGHT / BLOCK_SIZE; j++) {
		boardGrid[i][j] = null;
	}
}
// Function to check if the board is filled beyond the limit
function checkBoardFilled() {
	var filledCount = 0;
	for (var i = 0; i < boardGrid.length; i++) {
		for (var j = 0; j < boardGrid[i].length; j++) {
			if (boardGrid[i][j] !== null) {
				filledCount++;
			}
		}
	}
	return filledCount >= MAX_BLOCKS;
}
// Function to place a block on the board
function placeBlock(x, y) {
	var gridX = Math.floor(x / BLOCK_SIZE);
	var gridY = Math.floor(y / BLOCK_SIZE);
	if (boardGrid[gridX][gridY] === null) {
		boardGrid[gridX][gridY] = currentBlock;
		currentBlock.x = gridX * BLOCK_SIZE + BLOCK_SIZE / 2;
		currentBlock.y = gridY * BLOCK_SIZE + BLOCK_SIZE / 2;
		currentBlock = null;
	}
}
// Function to create a new block at the bottom
function createNewBlock() {
	currentBlock = new Block();
	if (currentBlock) {
		currentBlock.x = Math.random() * (BOARD_WIDTH - BLOCK_SIZE) + BLOCK_SIZE / 2;
		currentBlock.y = BLOCK_SIZE / 2;
		game.addChild(currentBlock);
	}
}
// Add a button to the top-right corner
var button = LK.getAsset('button', {
	anchorX: 1.0,
	// Right edge
	anchorY: 0.0,
	// Top edge
	x: 2048,
	// Rightmost x-coordinate
	y: 0 // Topmost y-coordinate
});
game.addChild(button);
// Set a timeout to end the game after 90 seconds
var gameDuration = 90 * 1000; // 90 seconds in milliseconds
var gameEndTimeout = LK.setTimeout(function () {
	LK.showGameOver();
}, gameDuration);
// Handle game updates
game.update = function () {
	if (isGameOver) {
		return;
	}
	// Move the current block upwards
	if (currentBlock) {
		currentBlock.y -= 5; // Move speed
		if (currentBlock.y <= BOARD_HEIGHT) {
			placeBlock(currentBlock.x, currentBlock.y);
			checkAndRemoveAlignedBlocks(); // Check and remove aligned blocks
			createNewBlock();
		}
	} else {
		createNewBlock(); // Ensure a new block is created if none exists
	}
};
// Handle touch/mouse down events
game.down = function (x, y, obj) {
	if (obj.event && obj.event.target === button) {
		LK.showGameOver();
	} else if (currentBlock) {
		placeBlock(x, y);
		createNewBlock();
	}
};
// Handle touch/mouse move events
game.move = function (x, y, obj) {
	if (currentBlock) {
		currentBlock.x = x;
	}
};
// Handle touch/mouse up events
game.up = function (x, y, obj) {
	// No specific action needed on up
}; /**** 
* Classes
****/ 
// Background image covering the entire screen
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Block class representing each block in the game
var Block = Container.expand(function () {
	var self = Container.call(this);
	var blockGraphics = self.attachAsset('block', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.update = function () {
		// Update logic for blocks if needed
	};
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000000 //Init game with black background 
});
/**** 
* Game Code
****/ 
var background = LK.getAsset('background', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: 2048 / 2,
	y: 2732 / 2
});
game.addChild(background);
// Function to check and remove blocks when 10 are aligned horizontally 
function checkAndRemoveAlignedBlocks() {
	for (var j = 0; j < boardGrid[0].length; j++) {
		var consecutiveBlocks = 0;
		for (var i = 0; i < boardGrid.length; i++) {
			if (boardGrid[i][j] !== null) {
				consecutiveBlocks++;
				if (consecutiveBlocks >= 10) {
					// Remove the aligned blocks
					for (var k = i; k > i - 10; k--) {
						if (boardGrid[k][j] !== null) {
							boardGrid[k][j].destroy();
							boardGrid[k][j] = null;
						}
					}
					// Shift blocks above down
					for (var k = i - 10; k >= 0; k--) {
						if (boardGrid[k][j] !== null) {
							boardGrid[k + 10][j] = boardGrid[k][j];
							boardGrid[k][j] = null;
							boardGrid[k + 10][j].y += BLOCK_SIZE * 10;
						}
					}
					consecutiveBlocks = 0; // Reset counter after removal
				}
			} else {
				consecutiveBlocks = 0; // Reset counter if a gap is found
			}
		}
	}
}
// Constants
var BOARD_WIDTH = 2048;
var BOARD_HEIGHT = 2000; // Area above where blocks are placed
var BLOCK_SIZE = 100; // Size of each block
var MAX_BLOCKS = Math.floor(BOARD_WIDTH / BLOCK_SIZE * (BOARD_HEIGHT / BLOCK_SIZE) * 0.85); // 85% of the board
// Variables
var blocks = [];
var currentBlock = null;
var isGameOver = false;
// Create a grid for the board
var boardGrid = [];
for (var i = 0; i < BOARD_WIDTH / BLOCK_SIZE; i++) {
	boardGrid[i] = [];
	for (var j = 0; j < BOARD_HEIGHT / BLOCK_SIZE; j++) {
		boardGrid[i][j] = null;
	}
}
// Function to check if the board is filled beyond the limit
function checkBoardFilled() {
	var filledCount = 0;
	for (var i = 0; i < boardGrid.length; i++) {
		for (var j = 0; j < boardGrid[i].length; j++) {
			if (boardGrid[i][j] !== null) {
				filledCount++;
			}
		}
	}
	return filledCount >= MAX_BLOCKS;
}
// Function to place a block on the board
function placeBlock(x, y) {
	var gridX = Math.floor(x / BLOCK_SIZE);
	var gridY = Math.floor(y / BLOCK_SIZE);
	if (boardGrid[gridX][gridY] === null) {
		boardGrid[gridX][gridY] = currentBlock;
		currentBlock.x = gridX * BLOCK_SIZE + BLOCK_SIZE / 2;
		currentBlock.y = gridY * BLOCK_SIZE + BLOCK_SIZE / 2;
		currentBlock = null;
	}
}
// Function to create a new block at the bottom
function createNewBlock() {
	currentBlock = new Block();
	if (currentBlock) {
		currentBlock.x = Math.random() * (BOARD_WIDTH - BLOCK_SIZE) + BLOCK_SIZE / 2;
		currentBlock.y = BLOCK_SIZE / 2;
		game.addChild(currentBlock);
	}
}
// Add a button to the top-right corner
var button = LK.getAsset('button', {
	anchorX: 1.0,
	// Right edge
	anchorY: 0.0,
	// Top edge
	x: 2048,
	// Rightmost x-coordinate
	y: 0 // Topmost y-coordinate
});
game.addChild(button);
// Set a timeout to end the game after 90 seconds
var gameDuration = 90 * 1000; // 90 seconds in milliseconds
var gameEndTimeout = LK.setTimeout(function () {
	LK.showGameOver();
}, gameDuration);
// Handle game updates
game.update = function () {
	if (isGameOver) {
		return;
	}
	// Move the current block upwards
	if (currentBlock) {
		currentBlock.y -= 5; // Move speed
		if (currentBlock.y <= BOARD_HEIGHT) {
			placeBlock(currentBlock.x, currentBlock.y);
			checkAndRemoveAlignedBlocks(); // Check and remove aligned blocks
			createNewBlock();
		}
	} else {
		createNewBlock(); // Ensure a new block is created if none exists
	}
};
// Handle touch/mouse down events
game.down = function (x, y, obj) {
	if (obj.event && obj.event.target === button) {
		LK.showGameOver();
	} else if (currentBlock) {
		placeBlock(x, y);
		createNewBlock();
	}
};
// Handle touch/mouse move events
game.move = function (x, y, obj) {
	if (currentBlock) {
		currentBlock.x = x;
	}
};
// Handle touch/mouse up events
game.up = function (x, y, obj) {
	// No specific action needed on up
};