/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Gem = Container.expand(function (type) {
var self = Container.call(this);
self.gemType = type;
self.gridX = -1;
self.gridY = -1;
self.isAnimating = false;
var gemTypes = ['gemRed', 'gemBlue', 'gemGreen', 'gemYellow', 'gemPurple', 'gemOrange'];
var assetId = gemTypes[type] || 'gemRed';
var gemGraphics = self.attachAsset(assetId, {
anchorX: 0.5,
anchorY: 0.5
});
self.setGridPosition = function (gridX, gridY) {
self.gridX = gridX;
self.gridY = gridY;
self.x = GRID_START_X + gridX * CELL_SIZE + CELL_SIZE / 2;
self.y = GRID_START_Y + gridY * CELL_SIZE + CELL_SIZE / 2;
};
self.animateToPosition = function (targetX, targetY, duration, callback) {
self.isAnimating = true;
tween(self, {
x: targetX,
y: targetY
}, {
duration: duration || 300,
easing: tween.easeOut,
onFinish: function onFinish() {
self.isAnimating = false;
if (callback) callback();
}
});
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x2c1810
});
/****
* Game Code
****/
var GRID_SIZE = 8;
var CELL_SIZE = 200;
var GRID_START_X = (2048 - GRID_SIZE * CELL_SIZE) / 2;
var GRID_START_Y = 400;
var GEM_TYPES = 6;
var grid = [];
var gems = [];
var selectedGem = null;
var isProcessingMatches = false;
var score = 0;
var movesLeft = 30;
// Initialize grid
for (var y = 0; y < GRID_SIZE; y++) {
grid[y] = [];
for (var x = 0; x < GRID_SIZE; x++) {
grid[y][x] = null;
}
}
// Create background grid
for (var y = 0; y < GRID_SIZE; y++) {
for (var x = 0; x < GRID_SIZE; x++) {
var cell = LK.getAsset('gridCell', {
anchorX: 0.5,
anchorY: 0.5
});
cell.x = GRID_START_X + x * CELL_SIZE + CELL_SIZE / 2;
cell.y = GRID_START_Y + y * CELL_SIZE + CELL_SIZE / 2;
cell.alpha = 0.3;
game.addChild(cell);
}
}
// UI Elements
var scoreTxt = new Text2('Score: 0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
scoreTxt.x = 0;
scoreTxt.y = 50;
LK.gui.top.addChild(scoreTxt);
var movesTxt = new Text2('Moves: 30', {
size: 80,
fill: 0xFFFFFF
});
movesTxt.anchor.set(0.5, 0);
movesTxt.x = 0;
movesTxt.y = 150;
LK.gui.top.addChild(movesTxt);
function createRandomGem(x, y) {
var type = Math.floor(Math.random() * GEM_TYPES);
var gem = new Gem(type);
gem.setGridPosition(x, y);
grid[y][x] = gem;
game.addChild(gem);
return gem;
}
function fillGrid() {
for (var y = 0; y < GRID_SIZE; y++) {
for (var x = 0; x < GRID_SIZE; x++) {
if (!grid[y][x]) {
createRandomGem(x, y);
}
}
}
}
function getGemAt(x, y) {
if (x < 0 || x >= GRID_SIZE || y < 0 || y >= GRID_SIZE) {
return null;
}
return grid[y][x];
}
function swapGems(gem1, gem2) {
var tempX = gem1.gridX;
var tempY = gem1.gridY;
// Update grid
grid[gem1.gridY][gem1.gridX] = gem2;
grid[gem2.gridY][gem2.gridX] = gem1;
// Update gem positions
gem1.setGridPosition(gem2.gridX, gem2.gridY);
gem2.setGridPosition(tempX, tempY);
}
function checkMatches() {
var matches = [];
// Check horizontal matches
for (var y = 0; y < GRID_SIZE; y++) {
var count = 1;
var currentType = grid[y][0] ? grid[y][0].gemType : -1;
for (var x = 1; x < GRID_SIZE; x++) {
var gem = grid[y][x];
if (gem && gem.gemType === currentType) {
count++;
} else {
if (count >= 3 && currentType !== -1) {
for (var i = x - count; i < x; i++) {
matches.push({
x: i,
y: y
});
}
}
count = 1;
currentType = gem ? gem.gemType : -1;
}
}
if (count >= 3 && currentType !== -1) {
for (var i = GRID_SIZE - count; i < GRID_SIZE; i++) {
matches.push({
x: i,
y: y
});
}
}
}
// Check vertical matches
for (var x = 0; x < GRID_SIZE; x++) {
var count = 1;
var currentType = grid[0][x] ? grid[0][x].gemType : -1;
for (var y = 1; y < GRID_SIZE; y++) {
var gem = grid[y][x];
if (gem && gem.gemType === currentType) {
count++;
} else {
if (count >= 3 && currentType !== -1) {
for (var i = y - count; i < y; i++) {
matches.push({
x: x,
y: i
});
}
}
count = 1;
currentType = gem ? gem.gemType : -1;
}
}
if (count >= 3 && currentType !== -1) {
for (var i = GRID_SIZE - count; i < GRID_SIZE; i++) {
matches.push({
x: x,
y: i
});
}
}
}
return matches;
}
function removeMatches(matches) {
var gemsToRemove = [];
for (var i = 0; i < matches.length; i++) {
var match = matches[i];
var gem = grid[match.y][match.x];
if (gem && gemsToRemove.indexOf(gem) === -1) {
gemsToRemove.push(gem);
}
}
for (var i = 0; i < gemsToRemove.length; i++) {
var gem = gemsToRemove[i];
grid[gem.gridY][gem.gridX] = null;
gem.destroy();
}
return gemsToRemove.length;
}
function applyGravity() {
var moved = false;
for (var x = 0; x < GRID_SIZE; x++) {
var writeIndex = GRID_SIZE - 1;
for (var y = GRID_SIZE - 1; y >= 0; y--) {
if (grid[y][x]) {
if (y !== writeIndex) {
grid[writeIndex][x] = grid[y][x];
grid[y][x] = null;
grid[writeIndex][x].setGridPosition(x, writeIndex);
moved = true;
}
writeIndex--;
}
}
// Fill empty spaces with new gems
for (var y = writeIndex; y >= 0; y--) {
var gem = createRandomGem(x, y);
gem.y = GRID_START_Y + (y - GRID_SIZE) * CELL_SIZE + CELL_SIZE / 2;
gem.animateToPosition(GRID_START_X + x * CELL_SIZE + CELL_SIZE / 2, GRID_START_Y + y * CELL_SIZE + CELL_SIZE / 2, 300);
moved = true;
}
}
return moved;
}
function processMatches() {
if (isProcessingMatches) return;
isProcessingMatches = true;
var matches = checkMatches();
if (matches.length > 0) {
LK.getSound('match').play();
var removedCount = removeMatches(matches);
score += removedCount * 10;
scoreTxt.setText('Score: ' + score);
LK.setTimeout(function () {
applyGravity();
LK.setTimeout(function () {
isProcessingMatches = false;
processMatches(); // Check for cascade matches
}, 400);
}, 200);
} else {
isProcessingMatches = false;
}
}
function isValidMove(gem1, gem2) {
// Temporarily swap gems
swapGems(gem1, gem2);
// Check if this creates any matches
var matches = checkMatches();
var isValid = matches.length > 0;
// Swap back
swapGems(gem1, gem2);
return isValid;
}
function handleGemSelection(gem) {
if (isProcessingMatches || gem.isAnimating) return;
if (!selectedGem) {
selectedGem = gem;
gem.alpha = 0.7;
} else if (selectedGem === gem) {
selectedGem.alpha = 1.0;
selectedGem = null;
} else {
// Check if gems are adjacent
var dx = Math.abs(selectedGem.gridX - gem.gridX);
var dy = Math.abs(selectedGem.gridY - gem.gridY);
if (dx === 1 && dy === 0 || dx === 0 && dy === 1) {
if (isValidMove(selectedGem, gem)) {
LK.getSound('swap').play();
swapGems(selectedGem, gem);
movesLeft--;
movesTxt.setText('Moves: ' + movesLeft);
selectedGem.alpha = 1.0;
selectedGem = null;
LK.setTimeout(function () {
processMatches();
}, 100);
if (movesLeft <= 0) {
LK.setTimeout(function () {
if (score >= 1000) {
LK.showYouWin();
} else {
LK.showGameOver();
}
}, 1000);
}
} else {
selectedGem.alpha = 1.0;
selectedGem = gem;
gem.alpha = 0.7;
}
} else {
selectedGem.alpha = 1.0;
selectedGem = gem;
gem.alpha = 0.7;
}
}
}
game.down = function (x, y, obj) {
// Find which gem was clicked
for (var gy = 0; gy < GRID_SIZE; gy++) {
for (var gx = 0; gx < GRID_SIZE; gx++) {
var gem = grid[gy][gx];
if (gem && gem.intersects && x >= gem.x - CELL_SIZE / 2 && x <= gem.x + CELL_SIZE / 2 && y >= gem.y - CELL_SIZE / 2 && y <= gem.y + CELL_SIZE / 2) {
handleGemSelection(gem);
return;
}
}
}
// Click outside gems - deselect
if (selectedGem) {
selectedGem.alpha = 1.0;
selectedGem = null;
}
};
// Initial game setup
fillGrid();
LK.setTimeout(function () {
processMatches();
}, 500);
game.update = function () {
// Game update logic handled by event system and timers
}; /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var Gem = Container.expand(function (type) {
var self = Container.call(this);
self.gemType = type;
self.gridX = -1;
self.gridY = -1;
self.isAnimating = false;
var gemTypes = ['gemRed', 'gemBlue', 'gemGreen', 'gemYellow', 'gemPurple', 'gemOrange'];
var assetId = gemTypes[type] || 'gemRed';
var gemGraphics = self.attachAsset(assetId, {
anchorX: 0.5,
anchorY: 0.5
});
self.setGridPosition = function (gridX, gridY) {
self.gridX = gridX;
self.gridY = gridY;
self.x = GRID_START_X + gridX * CELL_SIZE + CELL_SIZE / 2;
self.y = GRID_START_Y + gridY * CELL_SIZE + CELL_SIZE / 2;
};
self.animateToPosition = function (targetX, targetY, duration, callback) {
self.isAnimating = true;
tween(self, {
x: targetX,
y: targetY
}, {
duration: duration || 300,
easing: tween.easeOut,
onFinish: function onFinish() {
self.isAnimating = false;
if (callback) callback();
}
});
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x2c1810
});
/****
* Game Code
****/
var GRID_SIZE = 8;
var CELL_SIZE = 200;
var GRID_START_X = (2048 - GRID_SIZE * CELL_SIZE) / 2;
var GRID_START_Y = 400;
var GEM_TYPES = 6;
var grid = [];
var gems = [];
var selectedGem = null;
var isProcessingMatches = false;
var score = 0;
var movesLeft = 30;
// Initialize grid
for (var y = 0; y < GRID_SIZE; y++) {
grid[y] = [];
for (var x = 0; x < GRID_SIZE; x++) {
grid[y][x] = null;
}
}
// Create background grid
for (var y = 0; y < GRID_SIZE; y++) {
for (var x = 0; x < GRID_SIZE; x++) {
var cell = LK.getAsset('gridCell', {
anchorX: 0.5,
anchorY: 0.5
});
cell.x = GRID_START_X + x * CELL_SIZE + CELL_SIZE / 2;
cell.y = GRID_START_Y + y * CELL_SIZE + CELL_SIZE / 2;
cell.alpha = 0.3;
game.addChild(cell);
}
}
// UI Elements
var scoreTxt = new Text2('Score: 0', {
size: 80,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
scoreTxt.x = 0;
scoreTxt.y = 50;
LK.gui.top.addChild(scoreTxt);
var movesTxt = new Text2('Moves: 30', {
size: 80,
fill: 0xFFFFFF
});
movesTxt.anchor.set(0.5, 0);
movesTxt.x = 0;
movesTxt.y = 150;
LK.gui.top.addChild(movesTxt);
function createRandomGem(x, y) {
var type = Math.floor(Math.random() * GEM_TYPES);
var gem = new Gem(type);
gem.setGridPosition(x, y);
grid[y][x] = gem;
game.addChild(gem);
return gem;
}
function fillGrid() {
for (var y = 0; y < GRID_SIZE; y++) {
for (var x = 0; x < GRID_SIZE; x++) {
if (!grid[y][x]) {
createRandomGem(x, y);
}
}
}
}
function getGemAt(x, y) {
if (x < 0 || x >= GRID_SIZE || y < 0 || y >= GRID_SIZE) {
return null;
}
return grid[y][x];
}
function swapGems(gem1, gem2) {
var tempX = gem1.gridX;
var tempY = gem1.gridY;
// Update grid
grid[gem1.gridY][gem1.gridX] = gem2;
grid[gem2.gridY][gem2.gridX] = gem1;
// Update gem positions
gem1.setGridPosition(gem2.gridX, gem2.gridY);
gem2.setGridPosition(tempX, tempY);
}
function checkMatches() {
var matches = [];
// Check horizontal matches
for (var y = 0; y < GRID_SIZE; y++) {
var count = 1;
var currentType = grid[y][0] ? grid[y][0].gemType : -1;
for (var x = 1; x < GRID_SIZE; x++) {
var gem = grid[y][x];
if (gem && gem.gemType === currentType) {
count++;
} else {
if (count >= 3 && currentType !== -1) {
for (var i = x - count; i < x; i++) {
matches.push({
x: i,
y: y
});
}
}
count = 1;
currentType = gem ? gem.gemType : -1;
}
}
if (count >= 3 && currentType !== -1) {
for (var i = GRID_SIZE - count; i < GRID_SIZE; i++) {
matches.push({
x: i,
y: y
});
}
}
}
// Check vertical matches
for (var x = 0; x < GRID_SIZE; x++) {
var count = 1;
var currentType = grid[0][x] ? grid[0][x].gemType : -1;
for (var y = 1; y < GRID_SIZE; y++) {
var gem = grid[y][x];
if (gem && gem.gemType === currentType) {
count++;
} else {
if (count >= 3 && currentType !== -1) {
for (var i = y - count; i < y; i++) {
matches.push({
x: x,
y: i
});
}
}
count = 1;
currentType = gem ? gem.gemType : -1;
}
}
if (count >= 3 && currentType !== -1) {
for (var i = GRID_SIZE - count; i < GRID_SIZE; i++) {
matches.push({
x: x,
y: i
});
}
}
}
return matches;
}
function removeMatches(matches) {
var gemsToRemove = [];
for (var i = 0; i < matches.length; i++) {
var match = matches[i];
var gem = grid[match.y][match.x];
if (gem && gemsToRemove.indexOf(gem) === -1) {
gemsToRemove.push(gem);
}
}
for (var i = 0; i < gemsToRemove.length; i++) {
var gem = gemsToRemove[i];
grid[gem.gridY][gem.gridX] = null;
gem.destroy();
}
return gemsToRemove.length;
}
function applyGravity() {
var moved = false;
for (var x = 0; x < GRID_SIZE; x++) {
var writeIndex = GRID_SIZE - 1;
for (var y = GRID_SIZE - 1; y >= 0; y--) {
if (grid[y][x]) {
if (y !== writeIndex) {
grid[writeIndex][x] = grid[y][x];
grid[y][x] = null;
grid[writeIndex][x].setGridPosition(x, writeIndex);
moved = true;
}
writeIndex--;
}
}
// Fill empty spaces with new gems
for (var y = writeIndex; y >= 0; y--) {
var gem = createRandomGem(x, y);
gem.y = GRID_START_Y + (y - GRID_SIZE) * CELL_SIZE + CELL_SIZE / 2;
gem.animateToPosition(GRID_START_X + x * CELL_SIZE + CELL_SIZE / 2, GRID_START_Y + y * CELL_SIZE + CELL_SIZE / 2, 300);
moved = true;
}
}
return moved;
}
function processMatches() {
if (isProcessingMatches) return;
isProcessingMatches = true;
var matches = checkMatches();
if (matches.length > 0) {
LK.getSound('match').play();
var removedCount = removeMatches(matches);
score += removedCount * 10;
scoreTxt.setText('Score: ' + score);
LK.setTimeout(function () {
applyGravity();
LK.setTimeout(function () {
isProcessingMatches = false;
processMatches(); // Check for cascade matches
}, 400);
}, 200);
} else {
isProcessingMatches = false;
}
}
function isValidMove(gem1, gem2) {
// Temporarily swap gems
swapGems(gem1, gem2);
// Check if this creates any matches
var matches = checkMatches();
var isValid = matches.length > 0;
// Swap back
swapGems(gem1, gem2);
return isValid;
}
function handleGemSelection(gem) {
if (isProcessingMatches || gem.isAnimating) return;
if (!selectedGem) {
selectedGem = gem;
gem.alpha = 0.7;
} else if (selectedGem === gem) {
selectedGem.alpha = 1.0;
selectedGem = null;
} else {
// Check if gems are adjacent
var dx = Math.abs(selectedGem.gridX - gem.gridX);
var dy = Math.abs(selectedGem.gridY - gem.gridY);
if (dx === 1 && dy === 0 || dx === 0 && dy === 1) {
if (isValidMove(selectedGem, gem)) {
LK.getSound('swap').play();
swapGems(selectedGem, gem);
movesLeft--;
movesTxt.setText('Moves: ' + movesLeft);
selectedGem.alpha = 1.0;
selectedGem = null;
LK.setTimeout(function () {
processMatches();
}, 100);
if (movesLeft <= 0) {
LK.setTimeout(function () {
if (score >= 1000) {
LK.showYouWin();
} else {
LK.showGameOver();
}
}, 1000);
}
} else {
selectedGem.alpha = 1.0;
selectedGem = gem;
gem.alpha = 0.7;
}
} else {
selectedGem.alpha = 1.0;
selectedGem = gem;
gem.alpha = 0.7;
}
}
}
game.down = function (x, y, obj) {
// Find which gem was clicked
for (var gy = 0; gy < GRID_SIZE; gy++) {
for (var gx = 0; gx < GRID_SIZE; gx++) {
var gem = grid[gy][gx];
if (gem && gem.intersects && x >= gem.x - CELL_SIZE / 2 && x <= gem.x + CELL_SIZE / 2 && y >= gem.y - CELL_SIZE / 2 && y <= gem.y + CELL_SIZE / 2) {
handleGemSelection(gem);
return;
}
}
}
// Click outside gems - deselect
if (selectedGem) {
selectedGem.alpha = 1.0;
selectedGem = null;
}
};
// Initial game setup
fillGrid();
LK.setTimeout(function () {
processMatches();
}, 500);
game.update = function () {
// Game update logic handled by event system and timers
};