/****
* Plugins
****/
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
// Button class to represent a clickable button
var Button = Container.expand(function (text, callback) {
var self = Container.call(this);
var buttonGraphics = self.attachAsset('upgrade', {
anchorX: 0.5,
anchorY: 0.5
});
var buttonText = new Text2(text, {
size: 30,
fill: 0xFFFFFF
});
buttonText.anchor.set(0.5, 0.5);
self.addChild(buttonText);
self.down = function (x, y, obj) {
callback();
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Cookie class to represent the clickable cookie
var Cookie = Container.expand(function () {
var self = Container.call(this);
var cookieGraphics = self.attachAsset('cookie', {
anchorX: 0.5,
anchorY: 0.5
});
// Event handler for clicking the cookie
self.down = function (x, y, obj) {
// Increase the cookie count
game.cookieCount += game.cookiesPerClick;
game.updateScore();
};
});
// Upgrade class to represent purchasable upgrades
var Upgrade = Container.expand(function (name, cost, cpsIncrease) {
var self = Container.call(this);
self.name = name;
self.cost = cost;
self.cpsIncrease = cpsIncrease;
var upgradeGraphics = self.attachAsset(self.name, {
anchorX: 0.5,
anchorY: 0.5
});
// Add price and name below the image
var priceText = new Text2(self.cost.toString(), {
size: 30,
fill: 0xFFFFFF
});
priceText.anchor.set(0.5, 0);
priceText.y = upgradeGraphics.height / 2 + 10;
self.addChild(priceText);
var nameText = new Text2(self.name, {
size: 30,
fill: 0xFFFFFF
});
nameText.anchor.set(0.5, 0);
nameText.y = priceText.y + 40;
self.addChild(nameText);
// Event handler for purchasing the upgrade
self.down = function (x, y, obj) {
if (game.cookieCount >= self.cost) {
game.cookieCount -= self.cost;
game.cookiesPerSecond += self.cpsIncrease;
self.cost *= 1.2; // Increase the cost by 20%
priceText.setText(self.cost.toString()); // Update price text
game.updateScore();
// Do not destroy the upgrade after purchase
}
};
});
var Temple = Upgrade.expand(function () {
var self = Upgrade.call(this, 'Temple', 20000, 200);
});
var Mine = Upgrade.expand(function () {
var self = Upgrade.call(this, 'Mine', 5000, 50);
});
var Farm = Upgrade.expand(function () {
var self = Upgrade.call(this, 'Farm', 500, 10);
});
var Factory = Upgrade.expand(function () {
var self = Upgrade.call(this, 'Factory', 2000, 20);
});
var Bank = Upgrade.expand(function () {
var self = Upgrade.call(this, 'Bank', 10000, 100);
});
/****
* Initialize Game
****/
var game = new LK.Game();
/****
* Game Code
****/
var background = game.attachAsset('background', {
anchorX: 0.0,
anchorY: 0.0
});
// Initialize game variables
game.cookieCount = storage.cookieCount || 0;
game.cookiesPerClick = 1;
game.cookiesPerSecond = 0;
// Create and position the cookie
var cookie = new Cookie();
cookie.x = 2048 / 2;
cookie.y = 2732 / 2;
game.addChild(cookie);
// Create and position upgrades
var upgrades = [];
var upgrade1 = new Upgrade('Grandma', 100, 1);
upgrade1.x = 2048 - upgrade1.width;
upgrade1.y = 500;
upgrades.push(upgrade1);
game.addChild(upgrade1);
upgrade1.visible = false;
var farm = new Farm();
farm.x = 2048 - farm.width;
farm.y = 700 + 100; // Increase the gap to 100 pixels
upgrades.push(farm);
game.addChild(farm);
farm.visible = false;
var factory = new Factory();
factory.x = 2048 - factory.width;
factory.y = 900 + 200; // Increase the gap to 100 pixels
upgrades.push(factory);
game.addChild(factory);
factory.visible = false;
var mine = new Mine();
mine.x = 2048 - mine.width;
mine.y = 1100 + 300; // Increase the gap to 100 pixels
upgrades.push(mine);
game.addChild(mine);
mine.visible = false;
var bank = new Bank();
bank.x = 2048 - bank.width;
bank.y = 1300 + 400; // Increase the gap to 100 pixels
upgrades.push(bank);
game.addChild(bank);
bank.visible = false;
var temple = new Temple();
temple.x = 2048 - temple.width;
temple.y = 1500 + 500; // Increase the gap to 100 pixels
upgrades.push(temple);
game.addChild(temple);
temple.visible = false;
// Create and display the score text
var scoreTxt = new Text2('Soda: 0\nPer Second: 0', {
size: 100,
fill: 0xFFFFFF
});
var menuButton = new Button('Menu', function () {
// Open the upgrades menu
upgrades.forEach(function (upgrade) {
upgrade.visible = !upgrade.visible;
});
});
menuButton.x = 2048 - menuButton.width;
menuButton.y = 100;
game.addChild(menuButton);
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Function to update the score display
game.updateScore = function () {
scoreTxt.setText('Cookie: ' + game.cookieCount + '\nPer Second: ' + game.cookiesPerSecond);
storage.cookieCount = game.cookieCount;
storage.cookiesPerSecond = game.cookiesPerSecond;
};
// Update function to handle cookies per second
game.update = function () {
if (LK.ticks % 60 == 0) {
// Every second
game.cookieCount += game.cookiesPerSecond;
game.updateScore();
storage.cookiesPerSecond = game.cookiesPerSecond;
}
}; /****
* Plugins
****/
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
// Button class to represent a clickable button
var Button = Container.expand(function (text, callback) {
var self = Container.call(this);
var buttonGraphics = self.attachAsset('upgrade', {
anchorX: 0.5,
anchorY: 0.5
});
var buttonText = new Text2(text, {
size: 30,
fill: 0xFFFFFF
});
buttonText.anchor.set(0.5, 0.5);
self.addChild(buttonText);
self.down = function (x, y, obj) {
callback();
};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Cookie class to represent the clickable cookie
var Cookie = Container.expand(function () {
var self = Container.call(this);
var cookieGraphics = self.attachAsset('cookie', {
anchorX: 0.5,
anchorY: 0.5
});
// Event handler for clicking the cookie
self.down = function (x, y, obj) {
// Increase the cookie count
game.cookieCount += game.cookiesPerClick;
game.updateScore();
};
});
// Upgrade class to represent purchasable upgrades
var Upgrade = Container.expand(function (name, cost, cpsIncrease) {
var self = Container.call(this);
self.name = name;
self.cost = cost;
self.cpsIncrease = cpsIncrease;
var upgradeGraphics = self.attachAsset(self.name, {
anchorX: 0.5,
anchorY: 0.5
});
// Add price and name below the image
var priceText = new Text2(self.cost.toString(), {
size: 30,
fill: 0xFFFFFF
});
priceText.anchor.set(0.5, 0);
priceText.y = upgradeGraphics.height / 2 + 10;
self.addChild(priceText);
var nameText = new Text2(self.name, {
size: 30,
fill: 0xFFFFFF
});
nameText.anchor.set(0.5, 0);
nameText.y = priceText.y + 40;
self.addChild(nameText);
// Event handler for purchasing the upgrade
self.down = function (x, y, obj) {
if (game.cookieCount >= self.cost) {
game.cookieCount -= self.cost;
game.cookiesPerSecond += self.cpsIncrease;
self.cost *= 1.2; // Increase the cost by 20%
priceText.setText(self.cost.toString()); // Update price text
game.updateScore();
// Do not destroy the upgrade after purchase
}
};
});
var Temple = Upgrade.expand(function () {
var self = Upgrade.call(this, 'Temple', 20000, 200);
});
var Mine = Upgrade.expand(function () {
var self = Upgrade.call(this, 'Mine', 5000, 50);
});
var Farm = Upgrade.expand(function () {
var self = Upgrade.call(this, 'Farm', 500, 10);
});
var Factory = Upgrade.expand(function () {
var self = Upgrade.call(this, 'Factory', 2000, 20);
});
var Bank = Upgrade.expand(function () {
var self = Upgrade.call(this, 'Bank', 10000, 100);
});
/****
* Initialize Game
****/
var game = new LK.Game();
/****
* Game Code
****/
var background = game.attachAsset('background', {
anchorX: 0.0,
anchorY: 0.0
});
// Initialize game variables
game.cookieCount = storage.cookieCount || 0;
game.cookiesPerClick = 1;
game.cookiesPerSecond = 0;
// Create and position the cookie
var cookie = new Cookie();
cookie.x = 2048 / 2;
cookie.y = 2732 / 2;
game.addChild(cookie);
// Create and position upgrades
var upgrades = [];
var upgrade1 = new Upgrade('Grandma', 100, 1);
upgrade1.x = 2048 - upgrade1.width;
upgrade1.y = 500;
upgrades.push(upgrade1);
game.addChild(upgrade1);
upgrade1.visible = false;
var farm = new Farm();
farm.x = 2048 - farm.width;
farm.y = 700 + 100; // Increase the gap to 100 pixels
upgrades.push(farm);
game.addChild(farm);
farm.visible = false;
var factory = new Factory();
factory.x = 2048 - factory.width;
factory.y = 900 + 200; // Increase the gap to 100 pixels
upgrades.push(factory);
game.addChild(factory);
factory.visible = false;
var mine = new Mine();
mine.x = 2048 - mine.width;
mine.y = 1100 + 300; // Increase the gap to 100 pixels
upgrades.push(mine);
game.addChild(mine);
mine.visible = false;
var bank = new Bank();
bank.x = 2048 - bank.width;
bank.y = 1300 + 400; // Increase the gap to 100 pixels
upgrades.push(bank);
game.addChild(bank);
bank.visible = false;
var temple = new Temple();
temple.x = 2048 - temple.width;
temple.y = 1500 + 500; // Increase the gap to 100 pixels
upgrades.push(temple);
game.addChild(temple);
temple.visible = false;
// Create and display the score text
var scoreTxt = new Text2('Soda: 0\nPer Second: 0', {
size: 100,
fill: 0xFFFFFF
});
var menuButton = new Button('Menu', function () {
// Open the upgrades menu
upgrades.forEach(function (upgrade) {
upgrade.visible = !upgrade.visible;
});
});
menuButton.x = 2048 - menuButton.width;
menuButton.y = 100;
game.addChild(menuButton);
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Function to update the score display
game.updateScore = function () {
scoreTxt.setText('Cookie: ' + game.cookieCount + '\nPer Second: ' + game.cookiesPerSecond);
storage.cookieCount = game.cookieCount;
storage.cookiesPerSecond = game.cookiesPerSecond;
};
// Update function to handle cookies per second
game.update = function () {
if (LK.ticks % 60 == 0) {
// Every second
game.cookieCount += game.cookiesPerSecond;
game.updateScore();
storage.cookiesPerSecond = game.cookiesPerSecond;
}
};