/**** * Classes ****/ var CoinButton = Container.expand(function () { var self = Container.call(this); self.buttonGraphic = self.attachAsset('coinButton', { anchorX: 0.5, anchorY: 0.5 }); self.on('down', function () { playerCoins += 50; // Add 50 coins to the player's total LK.effects.flashScreen(0x00ff00, 300); }); }); var ShopItem = Container.expand(function (id, price, maxPurchase) { var self = Container.call(this); self.id = id; self.price = price; self.maxPurchase = maxPurchase; self.purchaseCount = 0; self.itemGraphic = self.attachAsset(id, { anchorX: 0.5, anchorY: 0.5 }); self.priceText = new Text2(self.price.toString(), { size: 50, fill: '#ffffff' }); self.addChild(self.itemGraphic); self.addChild(self.priceText); self.updateAffordability = function (playerCoins) { if (self.purchaseCount >= self.maxPurchase) { self.priceText.setText('N.A.'); self.priceText.fill = '#ff0000'; } else if (playerCoins >= self.price) { self.priceText.setText(self.price.toString()); self.priceText.fill = '#1eff00'; } else { self.priceText.setText(self.price.toString()); self.priceText.fill = '#ff0000'; } }; self.itemFunction = function () { // Add the item to the activated items display activatedItems.push(self.id); activatedItemDisplay.updateItems(activatedItems); }; self.on('down', function () { if (playerCoins >= self.price && self.purchaseCount < self.maxPurchase) { playerCoins -= self.price; // Subtract item cost from player's coins self.purchaseCount++; LK.effects.flashScreen(0x00ff00, 300); // Execute the function that belongs to this item // This is a placeholder for the actual function self.itemFunction(); // Update the price text to N.A. if the item has been bought if (self.purchaseCount >= self.maxPurchase) { self.priceText.setText('N.A.'); } } else { LK.effects.flashScreen(0xff0000, 300); } }); }); var Item1 = ShopItem.expand(function () { var self = ShopItem.call(this, 'item1', 100, 1); }); var Item2 = ShopItem.expand(function () { var self = ShopItem.call(this, 'item2', 200, 1); }); var Item3 = ShopItem.expand(function () { var self = ShopItem.call(this, 'item3', 300, 1); }); var Item4 = ShopItem.expand(function () { var self = ShopItem.call(this, 'item4', 400, 1); }); var Item5 = ShopItem.expand(function () { var self = ShopItem.call(this, 'item5', 500, 1); }); var Item6 = ShopItem.expand(function () { var self = ShopItem.call(this, 'item6', 600, 1); }); var Item7 = ShopItem.expand(function () { var self = ShopItem.call(this, 'item7', 700, 1); }); var Item8 = ShopItem.expand(function () { var self = ShopItem.call(this, 'item8', 800, 1); }); var ShopMenu = Container.expand(function () { var self = Container.call(this); self.visible = false; self.items = []; self.background = self.attachAsset('shopBackground', { anchorX: 0.5, anchorY: 0.5 }); self.toggleVisibility = function () { self.visible = !self.visible; self.background.visible = self.visible; // Show/hide background with the menu }; self.addItem = function (item) { self.items.push(item); self.addChild(item); }; self.updateItemsAffordability = function (playerCoins) { self.items.forEach(function (item) { item.updateAffordability(playerCoins); }); }; }); var ActivatedItemDisplay = Container.expand(function () { var self = Container.call(this); self.items = []; self.updateItems = function (activatedItems) { // Remove all current item graphics self.items.forEach(function (item) { item.destroy(); }); self.items = []; // Add new activated item graphics activatedItems.forEach(function (itemId, index) { var itemGraphic = self.attachAsset(itemId, { anchorX: 0.5, anchorY: 0.5, x: index * 150 + 75, // Position items with a fixed offset y: 50 // Position items at a fixed height from the bottom }); self.items.push(itemGraphic); }); }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 // Init game with black background }); /**** * Game Code ****/ var activatedItemDisplay = game.addChild(new ActivatedItemDisplay()); activatedItemDisplay.y = 2732 - 100; // Position at the bottom of the screen var activatedItems = []; // Array to keep track of activated items var shopButton = game.addChild(LK.getAsset('shopIcon', { anchorX: 1, anchorY: 0, x: 2048, y: 0 })); var shopMenu = game.addChild(new ShopMenu()); shopMenu.x = 1024; // Center of the screen shopMenu.y = 1366; // Middle of the screen // Sample items for the shop shopMenu.addItem(new Item1(100)); shopMenu.addItem(new Item2(200)); shopMenu.addItem(new Item3(300)); shopMenu.addItem(new Item4(400)); shopMenu.addItem(new Item5(500)); shopMenu.addItem(new Item6(600)); shopMenu.addItem(new Item7(700)); shopMenu.addItem(new Item8(800)); // Position shop items in rows of 3 var itemOffsetX = 200; var itemOffsetY = 200; var itemsPerRow = 3; shopMenu.items.forEach(function (item, index) { item.x = index % itemsPerRow * itemOffsetX - (itemsPerRow - 1) * itemOffsetX / 2; item.y = Math.floor(index / itemsPerRow) * itemOffsetY - (shopMenu.items.length / itemsPerRow - 1) * itemOffsetY / 2; }); var coinButton = game.addChild(new CoinButton()); coinButton.x = 1024; // Center of the screen horizontally coinButton.y = 2732 - 150; // Near the bottom of the screen // Player's coins var playerCoins = 150; // Example starting coins // Display player's coins var coinDisplay = new Text2(playerCoins.toString(), { size: 100, fill: '#ffffff' }); LK.gui.top.addChild(coinDisplay); // Shop button event listener shopButton.on('down', function () { shopMenu.toggleVisibility(); shopMenu.updateItemsAffordability(playerCoins); }); // Main game loop LK.on('tick', function () { // Game logic goes here // For example, earning coins or spending them in the shop // This is just a placeholder for the actual game logic // Update coin display coinDisplay.setText(playerCoins.toString()); }); // Hide the shop menu initially shopMenu.visible = false; // Update the shop menu's visibility based on the shop button game.on('tick', function () { shopMenu.visible = shopMenu.visible; });
/****
* Classes
****/
var CoinButton = Container.expand(function () {
var self = Container.call(this);
self.buttonGraphic = self.attachAsset('coinButton', {
anchorX: 0.5,
anchorY: 0.5
});
self.on('down', function () {
playerCoins += 50; // Add 50 coins to the player's total
LK.effects.flashScreen(0x00ff00, 300);
});
});
var ShopItem = Container.expand(function (id, price, maxPurchase) {
var self = Container.call(this);
self.id = id;
self.price = price;
self.maxPurchase = maxPurchase;
self.purchaseCount = 0;
self.itemGraphic = self.attachAsset(id, {
anchorX: 0.5,
anchorY: 0.5
});
self.priceText = new Text2(self.price.toString(), {
size: 50,
fill: '#ffffff'
});
self.addChild(self.itemGraphic);
self.addChild(self.priceText);
self.updateAffordability = function (playerCoins) {
if (self.purchaseCount >= self.maxPurchase) {
self.priceText.setText('N.A.');
self.priceText.fill = '#ff0000';
} else if (playerCoins >= self.price) {
self.priceText.setText(self.price.toString());
self.priceText.fill = '#1eff00';
} else {
self.priceText.setText(self.price.toString());
self.priceText.fill = '#ff0000';
}
};
self.itemFunction = function () {
// Add the item to the activated items display
activatedItems.push(self.id);
activatedItemDisplay.updateItems(activatedItems);
};
self.on('down', function () {
if (playerCoins >= self.price && self.purchaseCount < self.maxPurchase) {
playerCoins -= self.price; // Subtract item cost from player's coins
self.purchaseCount++;
LK.effects.flashScreen(0x00ff00, 300);
// Execute the function that belongs to this item
// This is a placeholder for the actual function
self.itemFunction();
// Update the price text to N.A. if the item has been bought
if (self.purchaseCount >= self.maxPurchase) {
self.priceText.setText('N.A.');
}
} else {
LK.effects.flashScreen(0xff0000, 300);
}
});
});
var Item1 = ShopItem.expand(function () {
var self = ShopItem.call(this, 'item1', 100, 1);
});
var Item2 = ShopItem.expand(function () {
var self = ShopItem.call(this, 'item2', 200, 1);
});
var Item3 = ShopItem.expand(function () {
var self = ShopItem.call(this, 'item3', 300, 1);
});
var Item4 = ShopItem.expand(function () {
var self = ShopItem.call(this, 'item4', 400, 1);
});
var Item5 = ShopItem.expand(function () {
var self = ShopItem.call(this, 'item5', 500, 1);
});
var Item6 = ShopItem.expand(function () {
var self = ShopItem.call(this, 'item6', 600, 1);
});
var Item7 = ShopItem.expand(function () {
var self = ShopItem.call(this, 'item7', 700, 1);
});
var Item8 = ShopItem.expand(function () {
var self = ShopItem.call(this, 'item8', 800, 1);
});
var ShopMenu = Container.expand(function () {
var self = Container.call(this);
self.visible = false;
self.items = [];
self.background = self.attachAsset('shopBackground', {
anchorX: 0.5,
anchorY: 0.5
});
self.toggleVisibility = function () {
self.visible = !self.visible;
self.background.visible = self.visible; // Show/hide background with the menu
};
self.addItem = function (item) {
self.items.push(item);
self.addChild(item);
};
self.updateItemsAffordability = function (playerCoins) {
self.items.forEach(function (item) {
item.updateAffordability(playerCoins);
});
};
});
var ActivatedItemDisplay = Container.expand(function () {
var self = Container.call(this);
self.items = [];
self.updateItems = function (activatedItems) {
// Remove all current item graphics
self.items.forEach(function (item) {
item.destroy();
});
self.items = [];
// Add new activated item graphics
activatedItems.forEach(function (itemId, index) {
var itemGraphic = self.attachAsset(itemId, {
anchorX: 0.5,
anchorY: 0.5,
x: index * 150 + 75,
// Position items with a fixed offset
y: 50 // Position items at a fixed height from the bottom
});
self.items.push(itemGraphic);
});
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 // Init game with black background
});
/****
* Game Code
****/
var activatedItemDisplay = game.addChild(new ActivatedItemDisplay());
activatedItemDisplay.y = 2732 - 100; // Position at the bottom of the screen
var activatedItems = []; // Array to keep track of activated items
var shopButton = game.addChild(LK.getAsset('shopIcon', {
anchorX: 1,
anchorY: 0,
x: 2048,
y: 0
}));
var shopMenu = game.addChild(new ShopMenu());
shopMenu.x = 1024; // Center of the screen
shopMenu.y = 1366; // Middle of the screen
// Sample items for the shop
shopMenu.addItem(new Item1(100));
shopMenu.addItem(new Item2(200));
shopMenu.addItem(new Item3(300));
shopMenu.addItem(new Item4(400));
shopMenu.addItem(new Item5(500));
shopMenu.addItem(new Item6(600));
shopMenu.addItem(new Item7(700));
shopMenu.addItem(new Item8(800));
// Position shop items in rows of 3
var itemOffsetX = 200;
var itemOffsetY = 200;
var itemsPerRow = 3;
shopMenu.items.forEach(function (item, index) {
item.x = index % itemsPerRow * itemOffsetX - (itemsPerRow - 1) * itemOffsetX / 2;
item.y = Math.floor(index / itemsPerRow) * itemOffsetY - (shopMenu.items.length / itemsPerRow - 1) * itemOffsetY / 2;
});
var coinButton = game.addChild(new CoinButton());
coinButton.x = 1024; // Center of the screen horizontally
coinButton.y = 2732 - 150; // Near the bottom of the screen
// Player's coins
var playerCoins = 150; // Example starting coins
// Display player's coins
var coinDisplay = new Text2(playerCoins.toString(), {
size: 100,
fill: '#ffffff'
});
LK.gui.top.addChild(coinDisplay);
// Shop button event listener
shopButton.on('down', function () {
shopMenu.toggleVisibility();
shopMenu.updateItemsAffordability(playerCoins);
});
// Main game loop
LK.on('tick', function () {
// Game logic goes here
// For example, earning coins or spending them in the shop
// This is just a placeholder for the actual game logic
// Update coin display
coinDisplay.setText(playerCoins.toString());
});
// Hide the shop menu initially
shopMenu.visible = false;
// Update the shop menu's visibility based on the shop button
game.on('tick', function () {
shopMenu.visible = shopMenu.visible;
});