var OverhangPlate = Container.expand(function (width, direction, color) {
var self = Container.call(this);
var plateGraphics = self.createAsset('plate', 'Overhang Plate Graphics', .5, .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 Plate = Container.expand(function (targetWidth) {
var self = Container.call(this);
var plateGraphics = self.createAsset('plate', 'Plate Graphics', .5, .5);
plateGraphics.width = targetWidth;
self.colorIndex = 0;
self.hsvToRgb = function (h, s, v) {
var r, g, b;
var i = Math.floor(h * 6);
var f = h * 6 - i;
var p = v * (1 - s);
var q = v * (1 - f * s);
var t = v * (1 - (1 - f) * s);
switch (i % 6) {
case 0:
(r = v, g = t, b = p);
break;
case 1:
(r = q, g = v, b = p);
break;
case 2:
(r = p, g = v, b = t);
break;
case 3:
(r = p, g = q, b = v);
break;
case 4:
(r = t, g = p, b = v);
break;
case 5:
(r = v, g = p, b = q);
break;
}
return (r * 255 << 16) + (g * 255 << 8) + b * 255;
};
self.updateColor = function () {
var h = self.colorIndex / 32;
var s = 1;
var v = 1;
self.color = self.hsvToRgb(h, s, v);
plateGraphics.tint = self.color;
self.colorIndex = (self.colorIndex + 1) % 32;
var bgColor = self.hsvToRgb(h, s, v / 3);
LK.stageContainer.setBackgroundColor(bgColor);
};
self.updateColor();
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;
};
});
var Game = Container.expand(function () {
var self = Container.call(this);
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;
self.addChild(plateContainer);
var currentPlateWidth = 1000;
var plates = [];
var plateColorIndex = 0;
var firstPlate = new Plate(currentPlateWidth);
firstPlate.x = 2048 / 2;
firstPlate.y = 2732 - firstPlate.height / 2;
firstPlate.speed = 0;
firstPlate.colorIndex = plateColorIndex;
firstPlate.updateColor();
plates.push(firstPlate);
plateContainer.addChild(firstPlate);
var newPlate = new Plate(currentPlateWidth);
newPlate.slide();
newPlate.y = 2732 - plates.length * newPlate.height - newPlate.height / 2;
plateColorIndex++;
newPlate.colorIndex = plateColorIndex;
newPlate.updateColor();
plates.push(newPlate);
plateContainer.addChild(newPlate);
stage.on('down', function (obj) {
var topPlate = plates[plates.length - 2];
if (newPlate.x < topPlate.x + topPlate.width && newPlate.x + newPlate.width > topPlate.x) {
var overhang = newPlate.x + newPlate.width / 2 - (topPlate.x + topPlate.width / 2);
var overhangSide = overhang > 0 ? 'right' : 'left';
overhang = Math.abs(overhang);
newPlate.stack();
currentPlateWidth -= overhang;
var overhangPlate = new OverhangPlate(overhang, overhangSide, newPlate.color);
overhangPlate.x = overhangSide === 'right' ? newPlate.x + newPlate.width / 2 : newPlate.x - newPlate.width / 2;
overhangPlate.y = newPlate.y;
plateContainer.addChildAt(overhangPlate, 0);
newPlate.width -= overhang;
if (overhangSide === 'right') {
newPlate.x -= overhang / 2;
overhangPlate.x -= overhang / 2;
} else {
newPlate.x += overhang / 2;
overhangPlate.x += overhang / 2;
}
newPlate = new Plate(currentPlateWidth);
newPlate.slide();
newPlate.y = 2732 - plates.length * newPlate.height - newPlate.height / 2;
plateColorIndex++;
newPlate.colorIndex = plateColorIndex;
newPlate.updateColor();
plates.push(newPlate);
plateContainer.addChild(newPlate);
if (plates.length > 17) {
plateContainer.targetY += newPlate.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 Plate && plate.x < -plate.width / 2) {
LK.showGameOver();
}
}
if (plateContainer.y !== plateContainer.targetY) {
plateContainer.y += (plateContainer.targetY - plateContainer.y) * 0.1;
}
});
});