/****
* Classes
****/
var BasicUpgradeButton = Container.expand(function () {
var self = Container.call(this);
var buttonGraphics = self.attachAsset('upgradeButton', {
anchorX: 0.5,
anchorY: 0.5
});
buttonGraphics.width = 900;
buttonGraphics.height = 300;
buttonGraphics.tint = 0x00ff00;
self.upgradeLevel = 1;
self.buttonText = new Text2('Upgrade: +' + self.upgradeLevel, {
size: 100,
fill: '#ffffff',
anchor: {
x: 0.5,
y: 0.5
},
pivot: {
x: 0.5,
y: 0.5
}
});
self.addChild(self.buttonText);
self.buttonText.x = -350;
self.buttonText.y = -100;
var costText = new Text2('', {
size: 100,
fill: '#ffffff',
anchor: {
x: 0.5,
y: 0.5
},
pivot: {
x: 0.5,
y: 0.5
}
});
self.addChild(costText);
costText.x = -350;
costText.y = 0;
self.setCost = function (cost, affordable) {
costText.setText('Cost: 🍬' + formatNumber(cost));
costText.fill = affordable ? '#00ff00' : '#ff0000';
buttonGraphics.tint = affordable ? 0x00ff00 : 0xff0000;
};
self.click = function () {
buttonGraphics.width = buttonGraphics.width * 1.1;
buttonGraphics.height = buttonGraphics.height * 1.1;
LK.setTimeout(function () {
buttonGraphics.width = buttonGraphics.width / 1.1;
buttonGraphics.height = buttonGraphics.height / 1.1;
}, 100);
};
});
var Candy = Container.expand(function () {
var self = Container.call(this);
var candyGraphics = self.attachAsset('candy', {
anchorX: 0.5,
anchorY: 0.5
});
candyGraphics.scale.set(3);
self.click = function () {
self.scale.x = 1.1;
self.scale.y = 1.1;
LK.setTimeout(function () {
self.scale.x = 1;
self.scale.y = 1;
}, 100);
};
});
var PopUpNumber = Container.expand(function () {
var self = Container.call(this);
self.incomeText = new Text2('Income', {
size: 300,
fill: '#ffffff',
anchor: {
x: 0.5,
y: 0.5
}
});
self.addChild(self.incomeText);
self.incomeText.x = 0;
self.incomeText.y = 0;
self.setText = function (text) {
self.incomeText.setText(text);
};
self.fly = function (direction) {
var speed = 20;
var deceleration = 0.98;
var flyInterval = LK.setInterval(function () {
speed *= deceleration;
self.x += Math.cos(direction) * speed;
self.y += Math.sin(direction) * speed;
self.alpha -= 0.005;
if (self.incomeText) {
self.incomeText.alpha = self.alpha;
}
if (self.alpha <= 0 || speed < 0.1) {
LK.clearInterval(flyInterval);
if (self.incomeText) {
self.incomeText.destroy();
}
self.destroy();
}
}, 16);
};
});
var RebirthButton = Container.expand(function () {
var self = Container.call(this);
var buttonGraphics = self.attachAsset('rebirthButton', {
anchorX: 0.5,
anchorY: 0.5
});
buttonGraphics.width = 900;
buttonGraphics.height = 300;
buttonGraphics.tint = 0x00ff00;
self.rebirthLevel = 0;
self.buttonText = new Text2('Rebirth: x' + (self.rebirthLevel + 1), {
size: 100,
fill: '#ffffff',
anchor: {
x: 0.5,
y: 0.5
},
pivot: {
x: 0.5,
y: 0.5
}
});
self.addChild(self.buttonText);
self.buttonText.x = -350;
self.buttonText.y = -100;
var costText = new Text2('', {
size: 100,
fill: '#ffffff',
anchor: {
x: 0.5,
y: 0.5
},
pivot: {
x: 0.5,
y: 0.5
}
});
self.addChild(costText);
costText.x = -350;
costText.y = 0;
self.setCost = function (cost, affordable) {
costText.setText('Cost: 🍬' + formatNumber(cost));
costText.fill = affordable ? '#00ff00' : '#ff0000';
buttonGraphics.tint = affordable ? 0x00ff00 : 0xff0000;
};
self.click = function () {
buttonGraphics.width = buttonGraphics.width * 1.1;
buttonGraphics.height = buttonGraphics.height * 1.1;
LK.setTimeout(function () {
buttonGraphics.width = buttonGraphics.width / 1.1;
buttonGraphics.height = buttonGraphics.height / 1.1;
}, 100);
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
function formatNumber(num) {
if (num >= 1000000) {
return (num / 1000000).toFixed(2) + 'M';
} else if (num >= 1000) {
return (num / 1000).toFixed(2) + 'K';
} else {
return Math.floor(num);
}
}
var background = game.attachAsset('background3', {
anchorX: 0.5,
anchorY: 0.5
});
background.width *= 2;
background.height *= 2;
background.x = 2048 / 2;
background.y = 2732 / 2;
game.addChild(background);
var blackLayer = game.attachAsset('blackLayer', {
anchorX: 0.5,
anchorY: 0.5
});
blackLayer.width = background.width;
blackLayer.height = background.height;
blackLayer.x = 2048 / 2;
blackLayer.y = 2732 / 2;
blackLayer.alpha = 1;
game.addChild(blackLayer);
var clickIntensity = 0;
var score = 0;
var clickPower = 1;
var candy = game.addChild(new Candy());
var upgrade = game.addChild(new BasicUpgradeButton());
var rebirth = game.addChild(new RebirthButton());
candy.x = 2048 / 2;
candy.y = 2732 / 2;
upgrade.x = 2048 / 2;
upgrade.y = 2732 - 850;
rebirth.x = 2048 / 2;
rebirth.y = 2732 - 450;
var scoreTxt = new Text2('🍬0', {
size: 150,
fill: "#ffffff"
});
scoreTxt.anchor.set(.5, 0);
LK.gui.top.addChild(scoreTxt);
game.scoreTxt = scoreTxt;
var startText = new Text2('Tap Candy to Start!', {
size: 200,
fill: "#ffffff",
stroke: '#000000',
strokeThickness: 25
});
startText.anchor.set(.5, 0);
startText.x = 2048 / 2;
startText.y = 2732 / 2 - 600;
game.addChild(startText);
game.startText = startText;
var gameStarted = false;
var countdown = 120;
var countdownInterval;
candy.on('down', function (x, y, obj) {
if (!gameStarted) {
gameStarted = true;
game.startText.setText('Time Left: ' + countdown + 's');
countdownInterval = LK.setInterval(function () {
countdown--;
if (countdown <= 0) {
LK.clearInterval(countdownInterval);
game.startText.setText('Your Score: ' + score);
LK.showGameOver();
} else {
game.startText.setText('Time Left: ' + countdown + 's');
}
}, 1000);
}
candy.click();
score += clickPower * Math.pow(2, rebirth.rebirthLevel);
scoreTxt.setText('🍬' + formatNumber(score));
var popUpNumber = game.addChild(new PopUpNumber());
popUpNumber.x = candy.x - candy.width / 2;
popUpNumber.y = candy.y - candy.height / 2;
var direction = Math.random() * Math.PI * 2;
popUpNumber.fly(direction);
popUpNumber.setText('🍬' + formatNumber(clickPower * Math.pow(2, rebirth.rebirthLevel)));
clickIntensity += (1 - clickIntensity) * 0.05;
if (clickIntensity > 1) {
clickIntensity = 1;
}
blackLayer.alpha = 1 - clickIntensity;
var upgradeCost = Math.floor(5 * Math.pow(1.13, upgrade.upgradeLevel));
upgrade.setCost(upgradeCost, score >= upgradeCost);
var rebirthCost = Math.pow(10, rebirth.rebirthLevel) * 100;
rebirth.setCost(rebirthCost, score >= rebirthCost);
});
LK.on('tick', function () {
clickIntensity -= 0.002;
if (clickIntensity < 0) {
clickIntensity = 0;
}
blackLayer.alpha = 1 - clickIntensity;
});
LK.on('tick', function () {
background.rotation += clickIntensity * 0.02;
});
upgrade.on('down', function (x, y, obj) {
var upgradeCost = Math.floor(5 * Math.pow(1.13, upgrade.upgradeLevel));
if (score >= upgradeCost) {
score -= upgradeCost;
clickPower += 1;
upgrade.upgradeLevel++;
scoreTxt.setText('🍬' + score);
upgrade.buttonText.setText('Upgrade: +' + upgrade.upgradeLevel);
upgradeCost = Math.floor(5 * Math.pow(1.13, upgrade.upgradeLevel));
}
upgrade.setCost(upgradeCost, score >= upgradeCost);
});
rebirth.on('down', function (x, y, obj) {
var rebirthCost = Math.pow(10, rebirth.rebirthLevel) * 100;
if (score >= rebirthCost) {
score = 0;
rebirth.rebirthLevel++;
upgrade.upgradeLevel = 1;
clickPower = 1;
scoreTxt.setText('🍬' + score);
rebirth.buttonText.setText('Rebirth: x' + Math.pow(2, rebirth.rebirthLevel));
rebirthCost = Math.pow(10, rebirth.rebirthLevel) * 100;
clickIntensity = 0;
}
rebirth.setCost(rebirthCost, score >= rebirthCost);
var upgradeCost = Math.floor(5 * Math.pow(1.13, upgrade.upgradeLevel));
upgrade.setCost(upgradeCost, score >= upgradeCost);
upgrade.buttonText.setText('Upgrade: +' + upgrade.upgradeLevel);
});
var initialCost = 5 * Math.pow(1.13, upgrade.upgradeLevel);
upgrade.setCost(initialCost, score >= initialCost);
var initialRebirthCost = Math.pow(10, rebirth.rebirthLevel) * 100;
rebirth.setCost(initialRebirthCost, score >= initialRebirthCost);