/****
* Classes
****/
// Customer class representing a customer entity
var Customer = Container.expand(function () {
var self = Container.call(this);
var customerGraphics = self.attachAsset('customer', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 2; // Speed of the customer
self.targetX = Math.random() * 2048; // Random target position
self.targetY = Math.random() * 2732; // Random target position
// Method to update the customer's position
self.update = function () {
var dx = self.targetX - self.x;
var dy = self.targetY - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance > self.speed) {
self.x += dx / distance * self.speed;
self.y += dy / distance * self.speed;
} else {
self.targetX = Math.random() * 2048;
self.targetY = Math.random() * 2732;
}
};
});
// House class representing a house entity
var House = Container.expand(function () {
var self = Container.call(this);
var houseGraphics = self.attachAsset('house', {
anchorX: 0.5,
anchorY: 0.5
});
self.price = 500; // Initial price of the house
self.upgradeLevel = 0; // Initial upgrade level
// Method to upgrade the house
self.upgrade = function () {
self.upgradeLevel++;
self.price += 200; // Increase price with each upgrade
};
// Method to sell the house
self.sell = function () {
var sellPrice = self.price;
self.destroy();
return sellPrice;
};
// Method to check if a point is within the house's bounds
self.containsPoint = function (point) {
var bounds = houseGraphics.getBounds();
return point.x >= bounds.x && point.x <= bounds.x + bounds.width && point.y >= bounds.y && point.y <= bounds.y + bounds.height;
};
});
//<Assets used in the game will automatically appear here>
// Phone class representing a phone entity
var Phone = Container.expand(function () {
var self = Container.call(this);
var phoneGraphics = self.attachAsset('phone', {
anchorX: 0.5,
anchorY: 0.5
});
self.price = 100; // Initial price of the phone
self.upgradeLevel = 0; // Initial upgrade level
// Method to upgrade the phone
self.upgrade = function () {
self.upgradeLevel++;
self.price += 50; // Increase price with each upgrade
};
// Method to sell the phone
self.sell = function () {
var sellPrice = self.price;
self.destroy();
return sellPrice;
};
// Method to check if a point is within the phone's bounds
self.containsPoint = function (point) {
var bounds = phoneGraphics.getBounds();
return point.x >= bounds.x && point.x <= bounds.x + bounds.width && point.y >= bounds.y && point.y <= bounds.y + bounds.height;
};
});
// Player class representing the player entity
var Player = Container.expand(function () {
var self = Container.call(this);
self.money = 500; // Initial money
self.phones = []; // Array to hold player's phones
// Method to buy a phone
self.buyPhone = function () {
if (self.money >= 100) {
var newPhone = new Phone();
newPhone.x = Math.random() * 2048;
newPhone.y = Math.random() * 2732;
self.phones.push(newPhone);
game.addChild(newPhone);
self.money -= 100;
}
};
// Method to upgrade a phone
self.upgradePhone = function (phone) {
if (self.money >= 50) {
phone.upgrade();
self.money -= 50;
}
};
// Method to sell a phone
self.sellPhone = function (phone) {
var sellPrice = phone.sell();
self.money += sellPrice;
var index = self.phones.indexOf(phone);
if (index > -1) {
self.phones.splice(index, 1);
}
};
});
// Shop class representing the shop entity
var Shop = Container.expand(function () {
var self = Container.call(this);
self.buyPhone = function (player) {
if (player.money >= 100) {
var newPhone = new Phone();
newPhone.x = Math.random() * 2048;
newPhone.y = Math.random() * 2732;
player.phones.push(newPhone);
game.addChild(newPhone);
player.money -= 100;
}
};
self.upgradePhone = function (player, phone) {
if (player.money >= 50) {
phone.upgrade();
player.money -= 50;
}
};
self.sellPhone = function (player, phone) {
var sellPrice = phone.sell();
player.money += sellPrice;
var index = player.phones.indexOf(phone);
if (index > -1) {
player.phones.splice(index, 1);
}
};
// Method to buy a house
self.buyHouse = function (player) {
if (player.money >= 500) {
var newHouse = new House();
newHouse.x = Math.random() * 2048;
newHouse.y = Math.random() * 2732;
houses.push(newHouse);
game.addChild(newHouse);
player.money -= 500;
}
};
// Method to upgrade a house
self.upgradeHouse = function (player, house) {
if (player.money >= 200) {
house.upgrade();
player.money -= 200;
}
};
// Method to sell a house
self.sellHouse = function (player, house) {
var sellPrice = house.sell();
player.money += sellPrice;
var index = houses.indexOf(house);
if (index > -1) {
houses.splice(index, 1);
}
};
// Method to buy an old phone
self.buyOldPhone = function (player) {
if (player.money >= 50) {
// Old phones cost less
var oldPhone = new Phone();
oldPhone.x = Math.random() * 2048;
oldPhone.y = Math.random() * 2732;
oldPhone.price = 50; // Set the price of the old phone
player.phones.push(oldPhone);
game.addChild(oldPhone);
player.money -= 50;
}
};
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000 //Init game with black background
});
/****
* Game Code
****/
// Initialize player
var player = new Player();
game.addChild(player);
// Add phone to the shop
var phone = new Phone();
phone.x = 1024; // Center of the screen horizontally
phone.y = 1366; // Center of the screen vertically
game.addChild(phone);
// Initialize customers
var customers = [];
for (var i = 0; i < 5; i++) {
var customer = new Customer();
customer.x = Math.random() * 2048;
customer.y = Math.random() * 2732;
customers.push(customer);
game.addChild(customer);
}
// Initialize houses
var houses = [];
for (var i = 0; i < 3; i++) {
var house = new House();
house.x = Math.random() * 2048;
house.y = Math.random() * 2732;
houses.push(house);
game.addChild(house);
}
// Initialize customers
var customers = [];
for (var i = 0; i < 5; i++) {
var customer = new Customer();
customer.x = Math.random() * 2048;
customer.y = Math.random() * 2732;
customers.push(customer);
game.addChild(customer);
}
// Initialize shop
var shop = new Shop();
// Display player's money
var moneyTxt = new Text2('Money: $' + player.money, {
size: 50,
fill: "#ffffff"
});
moneyTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(moneyTxt);
// Update money display
function updateMoneyDisplay() {
moneyTxt.setText('Money: $' + player.money);
}
// Handle game updates
game.update = function () {
// Update money display every tick
updateMoneyDisplay();
// Update customers' positions
for (var i = 0; i < customers.length; i++) {
customers[i].update();
}
// Update houses' positions
for (var i = 0; i < houses.length; i++) {
houses[i].update();
}
// Update customers' positions
for (var i = 0; i < customers.length; i++) {
customers[i].update();
}
};
// Handle touch events to buy, upgrade, or sell phones
game.down = function (x, y, obj) {
var localPos = game.toLocal(obj.global);
var touchedPhone = null;
// Check if a phone was touched
for (var i = 0; i < player.phones.length; i++) {
if (player.phones[i].containsPoint(localPos)) {
touchedPhone = player.phones[i];
break;
}
}
if (touchedPhone) {
// Upgrade or sell the touched phone
if (obj.event.shiftKey) {
shop.sellPhone(player, touchedPhone);
} else {
shop.upgradePhone(player, touchedPhone);
}
} else {
// Check if a house was touched
var touchedHouse = null;
for (var i = 0; i < houses.length; i++) {
if (houses[i].containsPoint(localPos)) {
touchedHouse = houses[i];
break;
}
}
if (touchedHouse) {
// Upgrade or sell the touched house
if (obj.event.shiftKey) {
shop.sellHouse(player, touchedHouse);
} else {
shop.upgradeHouse(player, touchedHouse);
}
} else {
// Buy a new phone if no phone or house was touched
if (obj.event && obj.event.detail === 2) {
// Check for double click
shop.buyOldPhone(player);
} else {
shop.buyPhone(player);
}
}
}
};