/****
* Classes
****/
// Class for the game board
var Board = Container.expand(function () {
var self = Container.call(this);
self.grid = [];
self.rows = 8;
self.cols = 8;
self.spacing = 10;
var mesureGem = new Gem();
self.gemSize = mesureGem.width + self.spacing;
self.init = function () {
for (var row = 0; row < self.rows; row++) {
self.grid[row] = [];
for (var col = 0; col < self.cols; col++) {
var gem = new Gem();
gem.x = gem.targetX = col * self.gemSize;
gem.y = gem.targetY = row * self.gemSize;
gem.col = col;
gem.row = row;
self.grid[row][col] = gem;
self.addChild(gem);
}
}
};
// Method to swap two gems
self.swapGems = function (gem1, gem2) {
gem1.swapTarget = gem2;
gem2.swapTarget = gem1;
var temp = self.grid[gem1.row][gem1.col];
self.grid[gem1.row][gem1.col] = self.grid[gem2.row][gem2.col];
self.grid[gem2.row][gem2.col] = temp;
var tempX = gem1.targetX;
var tempY = gem1.targetY;
var tempRow = gem1.row;
var tempCol = gem1.col;
gem1.targetX = gem2.targetX;
gem1.targetY = gem2.targetY;
gem1.row = gem2.row;
gem1.col = gem2.col;
gem2.targetX = tempX;
gem2.targetY = tempY;
gem2.row = tempRow;
gem2.col = tempCol;
gem1.atRest = gem2.atRest = false;
};
self.findMatches = function () {
var matches = [];
for (var row = 0; row < self.rows; row++) {
for (var col = 0; col < self.cols; col++) {
var match = self.checkMatch(row, col);
if (match) {
matches = matches.concat(match);
}
}
}
return matches;
};
self.resolve = function () {
var matches = board.findMatches();
var offsetYs = [];
for (var a = 0; a < self.cols; a++) {
offsetYs[a] = 0;
}
// Loop over all the gems on the board
for (var row = 0; row < board.rows; row++) {
for (var col = 0; col < board.cols; col++) {
var gem = board.grid[row][col];
// Check if the gem is part of a match
if (matches.some(function (match) {
return match.includes(gem);
})) {
// If it is, set its alpha to half opacity
self.grid[gem.row][gem.col] = undefined;
gem.destroy();
gem.isDestroyed = true;
offsetYs[gem.col]++;
score += 10;
scoreTxt.setText(score);
} else {
// If it's not, set its alpha to full opacity
//gem.alpha = 1;
}
}
}
//Move down all gems into available spaces here.
//Implement that the below code is repeated until no changes occur
var changes;
do {
changes = false;
for (var row = self.rows - 1; row >= 0; row--) {
for (var col = 0; col < self.cols; col++) {
if (self.grid[row][col] === undefined) {
for (var aboveRow = row - 1; aboveRow >= 0; aboveRow--) {
if (self.grid[aboveRow][col] !== undefined) {
self.grid[row][col] = self.grid[aboveRow][col];
self.grid[aboveRow][col] = undefined;
self.grid[row][col].row = row;
self.grid[row][col].setTargetPosition();
changes = true;
break;
}
}
}
}
}
} while (changes);
//Code to update to make sure gems are inserted correctly.
for (var row = 0; row < self.rows; row++) {
for (var col = 0; col < self.cols; col++) {
if (self.grid[row][col] === undefined) {
var newGem = new Gem();
newGem.atRest = false;
newGem.x = col * self.gemSize;
newGem.y = -offsetYs[col] * self.gemSize;
offsetYs[col]--;
newGem.col = col;
newGem.row = row;
self.grid[row][col] = newGem;
self.addChild(newGem);
newGem.setTargetPosition();
}
}
}
};
self.checkMatch = function (row, col) {
var gem = self.grid[row][col];
// Ignore gems that are not resting on the board
if (!gem.atRest) {
return null;
}
var horizontalMatch = [gem];
var verticalMatch = [gem];
// Check for horizontal match
for (var i = col + 1; i < self.cols; i++) {
if (self.grid[row][i].type === gem.type && self.grid[row][i].atRest) {
horizontalMatch.push(self.grid[row][i]);
} else {
break;
}
}
// Check for vertical match
for (var i = row + 1; i < self.rows; i++) {
if (self.grid[i][col].type === gem.type && self.grid[i][col].atRest) {
verticalMatch.push(self.grid[i][col]);
} else {
break;
}
}
// Return match if 3 or more gems of the same type are in a row
if (horizontalMatch.length >= 3 || verticalMatch.length >= 3) {
var toReturn = [];
if (horizontalMatch.length >= 3) {
toReturn.push(horizontalMatch);
}
if (verticalMatch.length >= 3) {
toReturn.push(verticalMatch);
}
return toReturn;
} else {
return null;
}
};
});
//<Assets used in the game will automatically appear here>
// Class for a single gem
var Gem = Container.expand(function () {
var self = Container.call(this);
self.type = Math.floor(Math.random() * 5); // Random type for the gem
self.row = 0;
self.col = 0;
self.swapTarget = undefined;
self.isDestroyed = false;
var gemGraphics;
switch (self.type) {
case 0:
gemGraphics = self.attachAsset('gem1', {
anchorX: 0.5,
anchorY: 0.5
});
break;
case 1:
gemGraphics = self.attachAsset('gem2', {
anchorX: 0.5,
anchorY: 0.5
});
break;
case 2:
gemGraphics = self.attachAsset('gem3', {
anchorX: 0.5,
anchorY: 0.5
});
break;
case 3:
gemGraphics = self.attachAsset('gem4', {
anchorX: 0.5,
anchorY: 0.5
});
break;
case 4:
gemGraphics = self.attachAsset('gem5', {
anchorX: 0.5,
anchorY: 0.5
});
break;
}
self.targetX = 0;
self.targetY = 0;
self.update = function () {
// Update logic for the gem if needed
};
self.down = function (x, y, obj) {
// Logic for selecting the gem
game.selectGem(self, x, y);
};
self.atRest = false;
self.velocityX = 0;
self.velocityY = 0;
var swapCount = 0;
self.update = function () {
if (currentDragNode != self) {
var orgXoffset = self.x - self.targetX;
var orgYoffset = self.y - self.targetY;
self.velocityX += -orgXoffset / 100;
self.velocityY += -orgYoffset / 100;
self.x += self.velocityX;
self.y += self.velocityY;
var newYoffset = self.y - self.targetY;
var newXoffset = self.x - self.targetX;
if (orgYoffset < 0 && newYoffset > 0 || orgYoffset > 0 && newYoffset < 0) {
self.velocityY = 0;
self.y = self.targetY;
}
if (orgXoffset < 0 && newXoffset > 0 || orgXoffset > 0 && newXoffset < 0) {
self.velocityX = 0;
self.x = self.targetX;
}
}
self.atRest = Math.abs(self.x - self.targetX) < 1 && Math.abs(self.y - self.targetY) < 1;
if (self.atRest && self.swapTarget && self.swapTarget.atRest && !self.isDestroyed && !self.swapTarget.isDestroyed) {
if (swapCount == 0) {
swapCount++;
return;
}
board.swapGems(self.swapTarget, self);
self.swapTarget.swapTarget = undefined;
self.swapTarget = undefined;
} else {
swapCount = 0;
}
};
self.setTargetPosition = function () {
self.targetX = self.col * board.gemSize;
self.targetY = self.row * board.gemSize;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
var selectedGem = null;
var score = 0;
var scoreLabel = new Text2('Score:', {
size: 50,
fill: "#fff"
});
scoreLabel.x = 180;
scoreLabel.y = 20;
LK.gui.addChild(scoreLabel);
var scoreTxt = new Text2('0', {
size: 50,
fill: "#fff"
});
scoreTxt.x = scoreLabel.x + scoreLabel.width + 20;
scoreTxt.y = 20;
LK.gui.addChild(scoreTxt);
var board = new Board();
board.x = (2048 - board.cols * board.gemSize + board.gemSize + board.spacing) / 2;
board.y = (2732 - board.rows * board.gemSize + board.gemSize + board.spacing) / 2;
board.init();
game.addChild(board);
var currentDragNode = undefined;
var currentDragNodeX = 0;
var currentDragNodeY = 0;
game.selectGem = function (gem, offsetX, offsetY) {
currentDragNode = gem;
currentDragNodeX = offsetX;
currentDragNodeY = offsetY;
board.addChild(gem);
};
game.move = function (x, y) {
if (currentDragNode) {
currentDragNode.x = x - board.x - currentDragNodeX;
currentDragNode.y = y - board.y - currentDragNodeY;
var offsetX = currentDragNode.targetX - currentDragNode.x;
var offsetY = currentDragNode.targetY - currentDragNode.y;
if (Math.abs(offsetX) > 100) {
console.log("Should swap X");
if (offsetX > 0) {
if (currentDragNode.col > 0) {
var leftGem = board.grid[currentDragNode.row][currentDragNode.col - 1];
board.swapGems(currentDragNode, leftGem);
currentDragNode = undefined;
return;
}
} else {
if (currentDragNode.col < board.cols - 1) {
var rightGem = board.grid[currentDragNode.row][currentDragNode.col + 1];
board.swapGems(currentDragNode, rightGem);
currentDragNode = undefined;
return;
}
}
}
if (Math.abs(offsetY) > 100) {
console.log("Should swap Y");
if (offsetY > 0) {
if (currentDragNode.row > 0) {
var upGem = board.grid[currentDragNode.row - 1][currentDragNode.col];
board.swapGems(currentDragNode, upGem);
currentDragNode = undefined;
return;
}
} else {
if (currentDragNode.row < board.rows - 1) {
var downGem = board.grid[currentDragNode.row + 1][currentDragNode.col];
board.swapGems(currentDragNode, downGem);
currentDragNode = undefined;
return;
}
}
}
}
};
game.up = function () {
currentDragNode = undefined;
};
//Debug method
game.update = function () {
board.resolve();
}; ===================================================================
--- original.js
+++ change.js
@@ -268,16 +268,16 @@
****/
var selectedGem = null;
var score = 0;
var scoreLabel = new Text2('Score:', {
- size: 100,
+ size: 50,
fill: "#fff"
});
-scoreLabel.x = 120;
+scoreLabel.x = 180;
scoreLabel.y = 20;
LK.gui.addChild(scoreLabel);
var scoreTxt = new Text2('0', {
- size: 100,
+ size: 50,
fill: "#fff"
});
scoreTxt.x = scoreLabel.x + scoreLabel.width + 20;
scoreTxt.y = 20;
Can you draw a smiling face with rounded edges, square red and shiny effect?. In-Game asset. 2d. High contrast. No shadows
Can you draw a smiling face with rounded edges, square blue and shiny effect?. In-Game asset. 2d. High contrast. No shadows
Can you draw a smiling face with rounded edges, square frame, yellow and shiny effect?. In-Game asset. 2d. High contrast. No shadows
Can you draw a smiling face with rounded edges, green and shiny effect?. In-Game asset. 2d. High contrast. No shadows
Can you draw the same drawing in purple?