You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
539 lines
18 KiB
539 lines
18 KiB
const storage = { |
|
resources: 'resources', |
|
buildings: 'buildings', |
|
currentQuest: 'currentQuest' |
|
}; |
|
|
|
let game = new Vue({ |
|
el: '#root', |
|
data: { |
|
resources: { |
|
gold: 0, |
|
wood: 0, |
|
stone: 0, |
|
iron: 0, |
|
bricks: 0, |
|
corn: 0, |
|
coal: 0, |
|
planks: 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: 'Carpenter', |
|
resource: 'planks', |
|
icon: 'medieval_lumber', |
|
level: 0, |
|
maxLevel: 15, |
|
isOwned: false, |
|
isUpgradeable: true, |
|
amount: 5, |
|
amountMultiplicator: 1, |
|
intervalInSeconds: 25, |
|
intervalMultiplicator: 0.80, |
|
price: 15000, |
|
priceMultiplicator: 2, |
|
hasRequirements: true, |
|
requires: { |
|
wood: 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: 'Coal Mine', |
|
resource: 'coal', |
|
icon: 'medieval_mine', |
|
level: 0, |
|
maxLevel: 15, |
|
isOwned: false, |
|
isUpgradeable: true, |
|
amount: 2, |
|
amountMultiplicator: 1, |
|
intervalInSeconds: 20, |
|
intervalMultiplicator: 0.95, |
|
price: 600, |
|
priceMultiplicator: 2 |
|
}, |
|
{ |
|
name: 'Blacksmith', |
|
resource: 'iron', |
|
icon: 'medieval_blacksmith', |
|
level: 0, |
|
maxLevel: 15, |
|
isOwned: false, |
|
isUpgradeable: true, |
|
amount: 2, |
|
amountMultiplicator: 3, |
|
intervalInSeconds: 20, |
|
intervalMultiplicator: 0.95, |
|
price: 1000, |
|
priceMultiplicator: 2.5, |
|
hasRequirements: true, |
|
requires: { |
|
stone: 2, |
|
coal: 1 |
|
} |
|
}, |
|
{ |
|
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.checkBuildings(); |
|
this.loadResourcesFromStorage(); |
|
this.currentQuest = JSON.parse(localStorage.getItem('currentQuest')); |
|
|
|
this.loadBuildingsFromStorage(); |
|
this.reloadBuildings(); |
|
}, |
|
methods: { |
|
checkBuildings() { |
|
let savedBuildings = JSON.parse(localStorage.getItem('buildings')); |
|
|
|
if (!savedBuildings) { |
|
localStorage.setItem('buildings', JSON.stringify(this.buildings)); |
|
} else if (savedBuildings.length !== this.buildings.length) { |
|
localStorage.setItem('buildings', JSON.stringify(this.buildings)); |
|
this.sendInfo('Buildings have been resetted due to an important update.'); |
|
} |
|
}, |
|
|
|
saveBuildingsToStorage() { |
|
localStorage.setItem('buildings', JSON.stringify(this.buildings)); |
|
}, |
|
|
|
loadBuildingsFromStorage() { |
|
let savedBuildings = JSON.parse(localStorage.getItem('buildings')); |
|
|
|
if (savedBuildings) { |
|
savedBuildings.forEach((building) => { |
|
if (building.isOwned) { |
|
building.loader = 10; |
|
} |
|
}); |
|
|
|
this.buildings = savedBuildings; |
|
} |
|
}, |
|
|
|
saveResourcesToStorage() { |
|
localStorage.setItem('resources', JSON.stringify(this.resources)); |
|
}, |
|
|
|
loadResourcesFromStorage: function () { |
|
let savedResources = JSON.parse(localStorage.getItem('resources')); |
|
|
|
if (savedResources) { |
|
this.resources = savedResources; |
|
} |
|
}, |
|
|
|
add(amount = 0, resource = 'gold') { |
|
switch (resource) { |
|
case 'wood': |
|
this.sendResourceMessage(amount, 'wood'); |
|
this.resources.wood += amount; |
|
break; |
|
case 'planks': |
|
this.sendResourceMessage(amount, 'planks'); |
|
this.resources.planks += amount; |
|
break; |
|
case 'stone': |
|
this.sendResourceMessage(amount, 'stone'); |
|
this.resources.stone += amount; |
|
break; |
|
case 'bricks': |
|
this.sendResourceMessage(amount, 'bricks'); |
|
this.resources.bricks += amount; |
|
break; |
|
case 'coal': |
|
this.sendResourceMessage(amount, 'coal'); |
|
this.resources.coal += amount; |
|
break; |
|
case 'iron': |
|
this.sendResourceMessage(amount, 'iron'); |
|
this.resources.iron = Number(this.resources.iron + amount); |
|
break; |
|
case 'corn': |
|
this.sendResourceMessage(amount, 'corn'); |
|
this.resources.corn += amount; |
|
break; |
|
case 'gold': |
|
this.sendResourceMessage(amount, 'gold'); |
|
this.resources.gold += amount; |
|
break; |
|
} |
|
|
|
this.saveResourcesToStorage(); |
|
}, |
|
|
|
sub(amount = 0, resource = false) { |
|
this.resources.gold -= amount; |
|
return this.saveResourcesToStorage(); |
|
}, |
|
|
|
getFormattedNumber(value) { |
|
return value.toLocaleString('de-DE'); |
|
}, |
|
|
|
reloadBuildings() { |
|
let game = this; |
|
this.buildings.forEach((building) => { |
|
if (building.isOwned) { |
|
game.initiateIntervals(building); |
|
} |
|
}); |
|
}, |
|
|
|
initiateIntervals(building) { |
|
building.intervalEarnID = setInterval(() => { |
|
if (building.requires) { |
|
if ( |
|
building.requires.wood > game.resources.wood || |
|
building.requires.stone > game.resources.stone || |
|
building.requires.iron > game.resources.iron || |
|
building.requires.bricks > game.resources.bricks || |
|
building.requires.coal > game.resources.coal || |
|
building.requires.corn > game.resources.corn |
|
) { |
|
building.hasMissingResources = true; |
|
clearInterval(building.intervalLoadingID); |
|
} else { |
|
building.hasMissingResources = false; |
|
game.useRequiredResources(building); |
|
game.add(building.amount, building.resource); |
|
game.reloadSingleBuilding(building); |
|
} |
|
} else { |
|
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); |
|
}, |
|
|
|
buyBuilding(building) { |
|
if (this.resources.gold >= building.price) { |
|
this.sub(building.price); |
|
building.isOwned = true; |
|
|
|
this.upgradeBuilding(building, true); |
|
this.saveBuildingsToStorage(); |
|
} else { |
|
this.sendWarning('Not enough gold'); |
|
} |
|
}, |
|
|
|
buyUpgrade(building) { |
|
if (building.level === 'MAX') { |
|
this.sendWarning('Already at MAX-Level'); |
|
return false; |
|
} |
|
|
|
if (this.resources.gold >= building.price) { |
|
this.sub(building.price); |
|
this.upgradeBuilding(building); |
|
} else { |
|
this.sendWarning('Not enough gold'); |
|
} |
|
}, |
|
|
|
upgradeBuilding(building, first = false) { |
|
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.amount = building.amount * building.amountMultiplicator; |
|
building.intervalInSeconds = Number(building.intervalInSeconds * building.intervalMultiplicator).toFixed(2); |
|
} |
|
|
|
this.reloadSingleBuilding(building); |
|
this.saveBuildingsToStorage(); |
|
}, |
|
|
|
reloadSingleBuilding(building) { |
|
clearInterval(building.intervalEarnID); |
|
clearInterval(building.intervalLoadingID); |
|
building.loader = 10; |
|
|
|
this.initiateIntervals(building); |
|
}, |
|
|
|
generateQuestWithRandomItems() { |
|
let possibleAmountsForQuest = [ |
|
0, 0, 0, 5, 10, 15, 20, 25, 30, 35 |
|
]; |
|
|
|
let randomWood = 0; |
|
let randomStone = 0; |
|
let randomIron = 0; |
|
let randomBricks = 0; |
|
let randomCorn = 0; |
|
let randomCoal = 0; |
|
let randomPlanks = 0; |
|
|
|
let rewardSum = 0; |
|
|
|
|
|
if (this.resources.wood > 0) { |
|
randomWood = this.getRandomElementForQuestResource(possibleAmountsForQuest); |
|
rewardSum += (randomWood * 5); |
|
} |
|
|
|
if (this.resources.stone > 0) { |
|
randomStone = this.getRandomElementForQuestResource(possibleAmountsForQuest); |
|
rewardSum += (randomStone * 7); |
|
} |
|
|
|
if (this.resources.iron > 0) { |
|
randomIron = this.getRandomElementForQuestResource(possibleAmountsForQuest); |
|
rewardSum += (randomIron * 200); |
|
} |
|
|
|
if (this.resources.bricks > 0) { |
|
randomBricks = this.getRandomElementForQuestResource(possibleAmountsForQuest); |
|
rewardSum += (randomBricks * 25); |
|
} |
|
|
|
if (this.resources.corn > 0) { |
|
randomCorn = this.getRandomElementForQuestResource(possibleAmountsForQuest); |
|
rewardSum += (randomCorn * 30); |
|
} |
|
|
|
if (this.resources.coal > 0) { |
|
randomCoal = this.getRandomElementForQuestResource(possibleAmountsForQuest); |
|
rewardSum += (randomCoal * 10); |
|
} |
|
|
|
if (this.resources.planks > 0) { |
|
randomPlanks = this.getRandomElementForQuestResource(possibleAmountsForQuest); |
|
rewardSum += (randomPlanks * 5); |
|
} |
|
|
|
if (rewardSum > 0) { |
|
this.currentQuest = { |
|
wood: randomWood, |
|
stone: randomStone, |
|
iron: randomIron, |
|
bricks: randomBricks, |
|
corn: randomCorn, |
|
coal: randomCoal, |
|
planks: randomPlanks, |
|
reward: rewardSum |
|
}; |
|
} else { |
|
this.currentQuest = null; |
|
} |
|
|
|
localStorage.setItem('currentQuest', JSON.stringify(this.currentQuest)); |
|
console.log('New Quest:', this.currentQuest); |
|
return this.currentQuest; |
|
}, |
|
|
|
getRandomElementForQuestResource(possibleValues) { |
|
return possibleValues[Math.floor(Math.random() * possibleValues.length)]; |
|
}, |
|
|
|
isQuestRedeemable() { |
|
let enoughWood = this.resources.wood >= this.currentQuest.wood; |
|
let enoughStone = this.resources.stone >= this.currentQuest.stone; |
|
let enoughIron = this.resources.iron >= this.currentQuest.iron; |
|
let enoughBricks = this.resources.bricks >= this.currentQuest.bricks; |
|
let enoughCorn = this.resources.corn >= this.currentQuest.corn; |
|
let enoughCoal = this.resources.coal >= this.currentQuest.coal; |
|
|
|
if (enoughWood && enoughStone && enoughIron && enoughBricks && enoughCorn && enoughCoal) { |
|
return true; |
|
} else { |
|
return false; |
|
} |
|
}, |
|
|
|
redeemReward() { |
|
if (this.isQuestRedeemable()) { |
|
this.resources.gold += 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.resources.coal -= this.currentQuest.coal; |
|
|
|
this.sendRewardMessage(this.currentQuest.reward); |
|
this.generateQuestWithRandomItems(); |
|
} else { |
|
this.sendWarning('Not enough resources to redeem reward.'); |
|
return false; |
|
} |
|
}, |
|
|
|
useRequiredResources(building) { |
|
this.resources.wood -= building.requires.wood; |
|
this.resources.stone -= building.requires.stone; |
|
this.resources.iron -= building.requires.iron; |
|
this.resources.bricks -= building.requires.bricks; |
|
this.resources.corn -= building.requires.corn; |
|
this.resources.coal -= building.requires.coal; |
|
}, |
|
|
|
// Templating |
|
getResourceIconForBuilding(building) { |
|
return this.getResourceIcon(building.resource); |
|
}, |
|
|
|
getResourceIcon(resource) { |
|
return '<img class="resource-icon" src="img/' + resource + '.png" title="' + resource + '" alt="' + resource + '">'; |
|
}, |
|
|
|
getRequirementsText(building) { |
|
let requirementList = '<br/>Uses'; |
|
|
|
if (building.requires.wood) { |
|
requirementList += ' ' + building.requires.wood + ' ' + this.getResourceIcon('wood'); |
|
} |
|
|
|
if (building.requires.stone) { |
|
requirementList += ' ' + building.requires.stone + ' ' + this.getResourceIcon('stone'); |
|
} |
|
|
|
if (building.requires.iron) { |
|
requirementList += ' ' + building.requires.iron + ' ' + this.getResourceIcon('iron'); |
|
} |
|
|
|
if (building.requires.bricks) { |
|
requirementList += ' ' + building.requires.bricks + ' ' + this.getResourceIcon('bricks'); |
|
} |
|
|
|
if (building.requires.coal) { |
|
requirementList += ' ' + building.requires.coal + ' ' + this.getResourceIcon('coal'); |
|
} |
|
|
|
if (building.requires.corn) { |
|
requirementList += ' ' + building.requires.corn + ' ' + this.getResourceIcon('corn'); |
|
} |
|
|
|
return requirementList + ' for ' + building.amount + ' ' + this.getResourceIconForBuilding(building); |
|
}, |
|
|
|
// Alerts |
|
sendRewardMessage(reward) { |
|
iziToast.show({ |
|
image: 'img/gold.png', |
|
color: 'green', |
|
message: 'You got a reward of ' + reward + ' gold!', |
|
position: 'bottomCenter', |
|
timeout: 1500, |
|
}); |
|
}, |
|
|
|
sendResourceMessage(amount, resource) { |
|
iziToast.show({ |
|
image: 'img/' + resource + '.png', |
|
position: 'bottomCenter', |
|
message: 'Produced ' + amount + ' ' + resource + '!', |
|
timeout: 1000, |
|
}); |
|
}, |
|
|
|
sendNotification(message) { |
|
iziToast.show({ |
|
color: 'green', |
|
message: message, |
|
position: 'bottomCenter', |
|
}); |
|
}, |
|
|
|
sendWarning(message) { |
|
iziToast.show({ |
|
color: 'red', |
|
message: message, |
|
position: 'bottomCenter', |
|
}); |
|
}, |
|
|
|
sendInfo(message) { |
|
iziToast.show({ |
|
color: 'blue', |
|
message: message, |
|
position: 'bottomCenter', |
|
}); |
|
} |
|
} |
|
});
|
|
|