/****
* Classes
****/
//<Assets used in the game will automatically appear here>
// PuzzlePiece class to represent each piece of the puzzle
var PuzzlePiece = Container.expand(function () {
var self = Container.call(this);
var pieceGraphics = self.attachAsset('piece', {
anchorX: 0.5,
anchorY: 0.5
});
self.correctPosition = {
x: 0,
y: 0
};
self.isCorrect = false;
self.setCorrectPosition = function (x, y) {
self.correctPosition.x = x;
self.correctPosition.y = y;
};
self.checkPosition = function () {
if (Math.abs(self.x - self.correctPosition.x) < 10 && Math.abs(self.y - self.correctPosition.y) < 10) {
self.isCorrect = true;
self.x = self.correctPosition.x;
self.y = self.correctPosition.y;
} else {
self.isCorrect = false;
}
};
self.down = function (x, y, obj) {
dragNode = self;
};
self.up = function (x, y, obj) {
dragNode = null;
self.checkPosition();
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
var pieces = [];
var dragNode = null;
var level = 1;
var maxLevels = 80;
var scoreTxt = new Text2('Level: 1', {
size: 100,
fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
function createPuzzle(level) {
// Clear previous pieces
pieces.forEach(piece => piece.destroy());
pieces = [];
// Create new pieces for the current level
for (var i = 0; i < level; i++) {
var piece = new PuzzlePiece();
piece.x = Math.random() * 2048;
piece.y = Math.random() * 2732;
piece.setCorrectPosition(100 + i * 150, 100 + i * 150);
pieces.push(piece);
game.addChild(piece);
}
}
function handleMove(x, y, obj) {
if (dragNode) {
dragNode.x = x;
dragNode.y = y;
}
}
game.move = handleMove;
game.down = function (x, y, obj) {
handleMove(x, y, obj);
};
game.up = function (x, y, obj) {
dragNode = null;
};
game.update = function () {
if (pieces.every(piece => piece.isCorrect)) {
if (level < maxLevels) {
level++;
scoreTxt.setText('Level: ' + level);
createPuzzle(level);
} else {
LK.showGameOver();
}
}
};
// Initialize the first level
createPuzzle(level);