/****
* Classes
****/
var OverhangPlate = Container.expand(function (width, direction, color) {
var self = Container.call(this);
var plateGraphics = self.attachAsset('plate', {
anchorX: 0.5,
anchorY: 0.5
});
plateGraphics.width = width;
plateGraphics.tint = color;
self.speedX = direction === 'right' ? 5 : -5;
self.speedY = -20;
self.rotationSpeed = direction === 'right' ? 0.01 : -0.01;
self.move = function () {
self.x += self.speedX;
self.y += self.speedY;
self.speedY += 0.5;
self.speedX *= 0.99;
self.rotation += self.rotationSpeed;
};
});
var Brick = Container.expand(function (targetWidth) {
var self = Container.call(this);
var brickGraphics = self.attachAsset('brick', {
anchorX: 0.5,
anchorY: 0.5
});
brickGraphics.width = targetWidth;
self.speed = 22.5;
self.move = function () {
self.x -= self.speed;
};
self.stack = function () {
self.speed = 0;
};
self.slide = function () {
self.x = 2048 + self.width / 2;
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
var scoreTxt = new Text2('0', {
size: 150,
fill: '#ffffff',
font: 'Impact'
});
scoreTxt.anchor.set(.5, 0);
LK.gui.topCenter.addChild(scoreTxt);
var plateContainer = new Container();
plateContainer.targetY = 0;
game.addChild(plateContainer);
var currentPlateWidth = 1000;
var plates = [];
var plateColorIndex = 0;
var firstBrick = new Brick(currentPlateWidth);
firstBrick.x = 2048 / 2;
firstBrick.y = 2732 - firstBrick.height / 2;
firstBrick.speed = 0;
plates.push(firstBrick);
plateContainer.addChild(firstBrick);
var newBrick = new Brick(currentPlateWidth);
newBrick.slide();
newBrick.y = 2732 - plates.length * newBrick.height - newBrick.height / 2;
plates.push(newBrick);
plateContainer.addChild(newBrick);
game.on('down', function (obj) {
var topBrick = plates[plates.length - 2];
if (newBrick.x < topBrick.x + topBrick.width && newBrick.x + newBrick.width > topBrick.x) {
var overhang = newBrick.x + newBrick.width / 2 - (topBrick.x + topBrick.width / 2);
var overhangSide = overhang > 0 ? 'right' : 'left';
overhang = Math.abs(overhang);
newBrick.stack();
currentPlateWidth -= overhang;
var overhangBrick = new OverhangPlate(overhang, overhangSide, newBrick.color);
overhangBrick.x = overhangSide === 'right' ? newBrick.x + newBrick.width / 2 : newBrick.x - newBrick.width / 2;
overhangBrick.y = newBrick.y;
plateContainer.addChildAt(overhangBrick, 0);
newBrick.width -= overhang;
if (overhangSide === 'right') {
newBrick.x -= overhang / 2;
overhangBrick.x -= overhang / 2;
} else {
newBrick.x += overhang / 2;
overhangBrick.x += overhang / 2;
}
newBrick = new Brick(currentPlateWidth);
newBrick.slide();
newBrick.y = 2732 - plates.length * newBrick.height - newBrick.height / 2;
plates.push(newBrick);
plateContainer.addChild(newBrick);
if (plates.length > 17) {
plateContainer.targetY += newBrick.height;
}
scoreTxt.setText(plates.length - 2);
} else {
LK.showGameOver();
}
});
LK.on('tick', function () {
for (var i = 0; i < plateContainer.children.length; i++) {
var plate = plateContainer.children[i];
plate.move();
if (plate instanceof OverhangPlate && (plate.x < -plate.width / 2 || plate.x > 2048 + plate.width / 2 || plate.y > 2732 + plate.height / 2)) {
plate.destroy();
}
if (plate instanceof Brick && plate.x < -plate.width / 2) {
LK.showGameOver();
}
}
if (plateContainer.y !== plateContainer.targetY) {
plateContainer.y += (plateContainer.targetY - plateContainer.y) * 0.1;
}
});