let game = new Vue({ el: '#root', data: { money: 0, resources: { wood: 50, stone: 0, iron: 0, bricks: 0, corn: 0 }, buildings: [ { name: 'Bank', resource: 'gold', icon: 'medieval_largeCastle', level: 1, maxLevel: 20, isOwned: true, isUpgradeable: true, amount: 100, amountMultiplicator: 1.5, intervalInSeconds: 30, intervalMultiplicator: 1, price: 1000, priceMultiplicator: 10, }, { name: 'Lumberjack', resource: 'wood', icon: 'medieval_lumber', level: 0, maxLevel: 15, isOwned: false, isUpgradeable: true, amount: 2, amountMultiplicator: 1, intervalInSeconds: 10, intervalMultiplicator: 0.95, price: 100, priceMultiplicator: 2 }, { name: 'Quarry', resource: 'stone', icon: 'medieval_mine', level: 0, maxLevel: 15, isOwned: false, isUpgradeable: true, amount: 2, amountMultiplicator: 1, intervalInSeconds: 20, intervalMultiplicator: 0.95, price: 250, priceMultiplicator: 2 }, { name: 'Farm', resource: 'corn', icon: 'medieval_farm', level: 0, maxLevel: 25, isOwned: false, isUpgradeable: true, amount: 5, amountMultiplicator: 2, price: 500, priceMultiplicator: 2, intervalInSeconds: 60, intervalMultiplicator: 1 }, ], currentQuest: null, loadedIntervals: [] }, created() { this.money = this.getSavedMoney(); this.saveMoney(); this.loadResources(); this.loadBuildings(); this.reloadBuildings(); }, methods: { saveMoney() { localStorage.setItem('money', String(this.money)); return this.getSavedMoney(); }, getSavedMoney() { this.money = Number(localStorage.getItem('money') ?? 0); return this.money; }, add(amount = 0, resource = 'gold') { switch (resource) { case 'wood': this.resources.wood += amount; break; case 'stone': this.resources.stone += amount; break; case 'corn': this.resources.corn += amount; break; case 'gold': this.money += amount; break; } this.saveResources(); return this.saveMoney(); }, sub(amount = 0, resource = false) { this.money -= amount; return this.saveMoney(); }, getAmount() { return this.money.toFixed(0); }, getAmountFormatted() { return Intl.NumberFormat('de-DE', {style: 'currency', currency: 'EUR'}).format(this.money); }, reloadBuildings() { this.killIntervals(); let game = this; this.buildings.forEach((building) => { if (building.isOwned) { game.initiateIntervals(building); } }); }, initiateIntervals(building) { building.intervalEarnID = setInterval(() => { game.add(building.amount, building.resource); }, building.intervalInSeconds * 1000); building.intervalLoadingID = setInterval(() => { if (building.loader < 100) { building.loader += 10; } else { building.loader = 10; } game.$forceUpdate() }, building.intervalInSeconds / 10 * 1000) this.loadedIntervals.push(building.intervalEarnID, building.intervalLoadingID); }, killIntervals() { this.buildings.forEach((building) => { building.loader = 0; }); this.loadedIntervals.forEach((interval) => { clearInterval(interval) }); }, buyBuilding(building) { if (this.money >= building.price) { this.sub(building.price); building.isOwned = true; this.upgradeBuilding(building, true); this.saveBuildings(); } else { alert('Not enough money'); } }, buyUpgrade(building) { if (building.level === 'MAX') { alert('Already at MAX-Level'); return false; } if (this.money >= building.price) { this.sub(building.price); this.upgradeBuilding(building); } else { alert('Not enough money'); } }, upgradeBuilding(building, first = false) { // this.killIntervals(); if (building.level < (building.maxLevel - 1)) { building.level++; } else { building.level = 'MAX'; building.isUpgradeable = false; } building.price = Number(building.price * building.priceMultiplicator).toFixed(2); if (first === false) { building.intervalInSeconds = Number(building.intervalInSeconds * building.intervalMultiplicator).toFixed(2); } // this.reloadBuildings(); this.reloadSingleBuilding(building); this.saveBuildings(); }, reloadSingleBuilding(building) { clearInterval(building.intervalEarnID); clearInterval(building.intervalLoadingID); building.loader = 10; this.initiateIntervals(building); }, saveBuildings() { localStorage.setItem('buildings', JSON.stringify(this.buildings)); }, loadBuildings: function () { let savedBuildings = JSON.parse(localStorage.getItem('buildings')); if (savedBuildings) { this.buildings = savedBuildings; } }, saveResources() { localStorage.setItem('resources', JSON.stringify(this.resources)); }, loadResources: function () { let savedResources = JSON.parse(localStorage.getItem('resources')); if (savedResources) { this.resources = savedResources; } }, generateQuest() { let possibleAmountsForQuest = [ 0, 5, 10, 15, 20, 25, 30, 35 ]; let randomWood = 0; let randomStone = 0; let randomIron = 0; let randomBricks = 0; let randomCorn = 0; let rewardSum = 0; if (this.resources.wood > 0) { randomWood = this.getRandomElement(possibleAmountsForQuest); rewardSum += (randomWood * 5); } if (this.resources.stone > 0) { randomStone = this.getRandomElement(possibleAmountsForQuest); rewardSum += (randomStone * 7); } if (this.resources.iron > 0) { randomIron = this.getRandomElement(possibleAmountsForQuest); rewardSum += (randomIron * 15); } if (this.resources.bricks > 0) { randomBricks = this.getRandomElement(possibleAmountsForQuest); rewardSum += (randomBricks * 25); } if (this.resources.corn > 0) { randomCorn = this.getRandomElement(possibleAmountsForQuest); rewardSum += (randomCorn * 30); } if (rewardSum > 0) { this.currentQuest = { wood: randomWood, stone: randomStone, iron: randomIron, bricks: randomBricks, corn: randomCorn, reward: rewardSum }; } else { this.currentQuest = null; } return this.currentQuest; }, getRandomElement(possibleValues) { return possibleValues[Math.floor(Math.random() * possibleValues.length)]; }, isQuestRedeemable() { return ( this.resources.wood >= this.currentQuest.wood && this.resources.stone >= this.currentQuest.stone && this.resources.iron >= this.currentQuest.iron && this.resources.bricks >= this.currentQuest.bricks && this.resources.corn >= this.currentQuest.corn ); }, redeemReward() { if (this.isQuestRedeemable()) { this.money += this.currentQuest.reward; this.resources.wood -= this.currentQuest.wood; this.resources.stone -= this.currentQuest.stone; this.resources.iron -= this.currentQuest.iron; this.resources.bricks -= this.currentQuest.bricks; this.resources.corn -= this.currentQuest.corn; this.generateQuest(); } else { return false; } } }, });