@ -1,25 +1,68 @@
let game = new Vue ( {
let game = new Vue ( {
el : '#root' ,
el : '#root' ,
data : {
data : {
version : 0.5 ,
version : 0.6 ,
debug : {
levelSandbox : false ,
buildingsSandbox : false
} ,
resources : {
resources : {
gold : 0 ,
gold : {
wood : 0 ,
amount : 500 ,
stone : 0 ,
name : 'Gold' ,
iron : 0 ,
worth : 100 ,
bricks : 0 ,
icon : 'img/gold.png' ,
corn : 0 ,
unlocked : true
coal : 0 ,
} ,
planks : 0
wood : {
} ,
amount : 0 ,
hadResource : {
name : 'Wood' ,
wood : false ,
worth : 10 ,
stone : false ,
icon : 'img/wood.png' ,
iron : false ,
unlocked : false
bricks : false ,
} ,
corn : false ,
stone : {
coal : false ,
amount : 0 ,
planks : false
name : 'Stone' ,
worth : 25 ,
icon : 'img/stone.png' ,
unlocked : false
} ,
iron : {
amount : 0 ,
name : 'Iron' ,
worth : 100 ,
icon : 'img/iron.png' ,
unlocked : false
} ,
bricks : {
amount : 0 ,
name : 'Bricks' ,
worth : 200 ,
icon : 'img/bricks.png' ,
unlocked : false
} ,
corn : {
amount : 0 ,
name : 'Corn' ,
worth : 75 ,
icon : 'img/corn.png' ,
unlocked : false
} ,
coal : {
amount : 0 ,
name : 'Coal' ,
worth : 25 ,
icon : 'img/coal.png' ,
unlocked : false
} ,
planks : {
amount : 0 ,
name : 'Planks' ,
worth : 100 ,
icon : 'img/planks.png' ,
unlocked : false
}
} ,
} ,
storageNames : null ,
storageNames : null ,
buildings : [
buildings : [
@ -145,7 +188,6 @@ let game = new Vue({
this . storageNames = {
this . storageNames = {
lastVersion : 'lastVersion' ,
lastVersion : 'lastVersion' ,
resources : 'resources' + this . version ,
resources : 'resources' + this . version ,
hadResource : 'hadResource' + this . version ,
buildings : 'buildings' + this . version ,
buildings : 'buildings' + this . version ,
currentQuest : 'currentQuest' + this . version
currentQuest : 'currentQuest' + this . version
}
}
@ -153,7 +195,6 @@ let game = new Vue({
this . checkVersion ( ) ;
this . checkVersion ( ) ;
this . checkBuildings ( ) ;
this . checkBuildings ( ) ;
this . loadHadResourceFromStorage ( ) ;
this . loadResourcesFromStorage ( ) ;
this . loadResourcesFromStorage ( ) ;
this . currentQuest = JSON . parse ( localStorage . getItem ( this . storageNames . currentQuest ) ) ;
this . currentQuest = JSON . parse ( localStorage . getItem ( this . storageNames . currentQuest ) ) ;
@ -221,70 +262,58 @@ let game = new Vue({
}
}
} ,
} ,
// Save resources a user ever had to generate quests
saveHadResourceToStorage ( ) {
localStorage . setItem ( this . storageNames . hadResource , JSON . stringify ( this . hadResource ) ) ;
} ,
loadHadResourceFromStorage : function ( ) {
let savedHadResource = JSON . parse ( localStorage . getItem ( this . storageNames . hadResource ) ) ;
if ( savedHadResource ) {
this . hadResource = savedHadResource ;
}
} ,
// Resource-Management
// Resource-Management
add ( amount = 0 , resource = 'gold' ) {
add ( amount = 0 , resource = 'gold' ) {
switch ( resource ) {
switch ( resource ) {
case 'wood' :
case 'wood' :
this . sendResourceMessage ( amount , 'wood' ) ;
this . sendResourceMessage ( amount , 'wood' ) ;
this . resources . wood += amount ;
console . log ( this . resources . wood . amount , amount ) ;
this . hadResource . wood = true ;
this . resources . wood . amount += amount ;
this . resources . wood . unlocked = true ;
break ;
break ;
case 'planks' :
case 'planks' :
this . sendResourceMessage ( amount , 'planks' ) ;
this . sendResourceMessage ( amount , 'planks' ) ;
this . resources . planks += amount ;
this . resources . planks . amount += amount ;
this . hadResource . planks = true ;
this . resources . planks . unlocked = true ;
break ;
break ;
case 'stone' :
case 'stone' :
this . sendResourceMessage ( amount , 'stone' ) ;
this . sendResourceMessage ( amount , 'stone' ) ;
this . resources . stone += amount ;
this . resources . stone . amount += amount ;
this . hadResource . stone = true ;
this . resources . stone . unlocked = true ;
break ;
break ;
case 'bricks' :
case 'bricks' :
this . sendResourceMessage ( amount , 'bricks' ) ;
this . sendResourceMessage ( amount , 'bricks' ) ;
this . resources . bricks += amount ;
this . resources . bricks . amount += amount ;
this . hadResource . bricks = true ;
this . resources . bricks . unlocked = true ;
break ;
break ;
case 'coal' :
case 'coal' :
this . sendResourceMessage ( amount , 'coal' ) ;
this . sendResourceMessage ( amount , 'coal' ) ;
this . resources . coal += amount ;
this . resources . coal . amount += amount ;
this . hadResource . coal = true ;
this . resources . coal . unlocked = true ;
break ;
break ;
case 'iron' :
case 'iron' :
this . sendResourceMessage ( amount , 'iron' ) ;
this . sendResourceMessage ( amount , 'iron' ) ;
this . resources . iron += amount ;
this . resources . iron . amount += amount ;
this . hadResource . iron = true ;
this . resources . iron . unlocked = true ;
break ;
break ;
case 'corn' :
case 'corn' :
this . sendResourceMessage ( amount , 'corn' ) ;
this . sendResourceMessage ( amount , 'corn' ) ;
this . resources . corn += amount ;
this . resources . corn . amount += amount ;
this . hadResource . corn = true ;
this . resources . corn . unlocked = true ;
break ;
break ;
case 'gold' :
case 'gold' :
this . sendResourceMessage ( amount , 'gold' ) ;
this . sendResourceMessage ( amount , 'gold' ) ;
this . resources . gold += amount ;
this . resources . gold . amount += amount ;
this . hadResource . gold = true ;
this . resources . gold . unlocke d = true ;
break ;
break ;
}
}
this . saveHadResourceToStorag e( ) ;
this . $forceUpdat e( ) ;
this . saveResourcesToStorage ( ) ;
this . saveResourcesToStorage ( ) ;
} ,
} ,
sub ( amount = 0 , resource = false ) {
sub ( amount = 0 , resource = false ) {
this . resources . gold -= amount ;
this . resources . gold . amount -= amount ;
return this . saveResourcesToStorage ( ) ;
return this . saveResourcesToStorage ( ) ;
} ,
} ,
@ -294,6 +323,9 @@ let game = new Vue({
this . buildings . forEach ( ( building ) => {
this . buildings . forEach ( ( building ) => {
if ( building . isOwned && ! building . hasMissingResources ) {
if ( building . isOwned && ! building . hasMissingResources ) {
building . price = building . pricePerLevel [ building . level - 1 ] ;
building . amount = building . amountPerLevel [ building . level - 1 ] ;
building . interval = building . intervalPerLevel [ building . level - 1 ] ;
vue . initiateIntervals ( building ) ;
vue . initiateIntervals ( building ) ;
}
}
} ) ;
} ) ;
@ -337,9 +369,12 @@ let game = new Vue({
} ,
} ,
buyBuilding ( building ) {
buyBuilding ( building ) {
if ( this . resources . gold >= building . price ) {
if ( this . resources . gold . amount >= building . price ) {
this . sub ( building . price ) ;
this . sub ( building . price ) ;
building . isOwned = true ;
building . isOwned = true ;
building . price = building . pricePerLevel [ 0 ] ;
building . amount = building . amountPerLevel [ 0 ] ;
building . interval = building . intervalPerLevel [ 0 ] ;
this . upgradeBuilding ( building , true ) ;
this . upgradeBuilding ( building , true ) ;
this . saveBuildingsToStorage ( ) ;
this . saveBuildingsToStorage ( ) ;
@ -349,12 +384,12 @@ let game = new Vue({
} ,
} ,
buyUpgrade ( building ) {
buyUpgrade ( building ) {
if ( building . level === 'X' ) {
if ( building . level === building . maxLevel ) {
this . sendWarning ( 'Already at MAX-Level' ) ;
this . sendWarning ( 'Already at MAX-Level' ) ;
return false ;
return false ;
}
}
if ( this . resources . gold >= building . price ) {
if ( this . resources . gold . amount >= building . price ) {
this . sub ( building . price ) ;
this . sub ( building . price ) ;
this . upgradeBuilding ( building ) ;
this . upgradeBuilding ( building ) ;
} else {
} else {
@ -376,9 +411,9 @@ let game = new Vue({
}
}
building . price = building . pricePerLevel [ building . level - 1 ] ;
building . price = building . pricePerLevel [ building . level - 1 ] ;
building . amount = building . amountPerLevel [ building . level - 1 ] ;
if ( first === false ) {
if ( first === false ) {
building . amount = building . amountPerLevel [ building . level - 1 ] ;
building . intervalInSeconds = building . intervalPerLevel [ building . level - 1 ] ;
building . intervalInSeconds = building . intervalPerLevel [ building . level - 1 ] ;
}
}
@ -397,12 +432,12 @@ let game = new Vue({
buildingHasEnoughResourcesToStart ( building ) {
buildingHasEnoughResourcesToStart ( building ) {
if ( building . requires ) {
if ( building . requires ) {
return (
return (
building . requires . wood > this . resources . wood ||
building . requires . wood > this . resources . wood . amount ||
building . requires . stone > this . resources . stone ||
building . requires . stone > this . resources . stone . amount ||
building . requires . iron > this . resources . iron ||
building . requires . iron > this . resources . iron . amount ||
building . requires . bricks > this . resources . bricks ||
building . requires . bricks > this . resources . bricks . amount ||
building . requires . coal > this . resources . coal ||
building . requires . coal > this . resources . coal . amount ||
building . requires . corn > this . resources . corn
building . requires . corn > this . resources . corn . amount
) ;
) ;
} else {
} else {
return true ;
return true ;
@ -426,39 +461,39 @@ let game = new Vue({
let rewardSum = 0 ;
let rewardSum = 0 ;
if ( this . hadResource . wood > 0 ) {
if ( this . resources . wood . unlocke d > 0 ) {
randomWood = this . getRandomElementForQuestResource ( possibleAmountsForQuest ) ;
randomWood = this . getRandomElementForQuestResource ( possibleAmountsForQuest ) ;
rewardSum += ( randomWood * 5 ) ;
rewardSum += ( randomWood * this . resources . wood . worth ) ;
}
}
if ( this . hadResource . stone > 0 ) {
if ( this . resources . stone . unlocked > 0 ) {
randomStone = this . getRandomElementForQuestResource ( possibleAmountsForQuest ) ;
randomStone = this . getRandomElementForQuestResource ( possibleAmountsForQuest ) ;
rewardSum += ( randomStone * 7 ) ;
rewardSum += ( randomStone * this . resources . stone . worth ) ;
}
}
if ( this . hadResource . iron > 0 ) {
if ( this . resources . iron . unlocked > 0 ) {
randomIron = this . getRandomElementForQuestResource ( possibleAmountsForQuest ) ;
randomIron = this . getRandomElementForQuestResource ( possibleAmountsForQuest ) ;
rewardSum += ( randomIron * 200 ) ;
rewardSum += ( randomIron * this . resources . iron . worth ) ;
}
}
if ( this . hadResource . bricks > 0 ) {
if ( this . resources . bricks . unlocked > 0 ) {
randomBricks = this . getRandomElementForQuestResource ( possibleAmountsForQuest ) ;
randomBricks = this . getRandomElementForQuestResource ( possibleAmountsForQuest ) ;
rewardSum += ( randomBricks * 25 ) ;
rewardSum += ( randomBricks * this . resources . bricks . worth ) ;
}
}
if ( this . hadResource . corn > 0 ) {
if ( this . resources . corn . unlocked > 0 ) {
randomCorn = this . getRandomElementForQuestResource ( possibleAmountsForQuest ) ;
randomCorn = this . getRandomElementForQuestResource ( possibleAmountsForQuest ) ;
rewardSum += ( randomCorn * 30 ) ;
rewardSum += ( randomCorn * this . resources . corn . worth ) ;
}
}
if ( this . hadResource . coal > 0 ) {
if ( this . resources . coal . unlocked > 0 ) {
randomCoal = this . getRandomElementForQuestResource ( possibleAmountsForQuest ) ;
randomCoal = this . getRandomElementForQuestResource ( possibleAmountsForQuest ) ;
rewardSum += ( randomCoal * 10 ) ;
rewardSum += ( randomCoal * this . resources . coal . worth ) ;
}
}
if ( this . hadResource . planks > 0 ) {
if ( this . resources . planks . unlocked > 0 ) {
randomPlanks = this . getRandomElementForQuestResource ( possibleAmountsForQuest ) ;
randomPlanks = this . getRandomElementForQuestResource ( possibleAmountsForQuest ) ;
rewardSum += ( randomPlanks * 5 ) ;
rewardSum += ( randomPlanks * this . resources . planks . worth ) ;
}
}
if ( rewardSum > 0 ) {
if ( rewardSum > 0 ) {
@ -486,12 +521,12 @@ let game = new Vue({
} ,
} ,
isQuestRedeemable ( ) {
isQuestRedeemable ( ) {
let enoughWood = this . resources . wood >= this . currentQuest . wood ;
let enoughWood = this . resources . wood . amount >= this . currentQuest . wood ;
let enoughStone = this . resources . stone >= this . currentQuest . stone ;
let enoughStone = this . resources . stone . amount >= this . currentQuest . stone ;
let enoughIron = this . resources . iron >= this . currentQuest . iron ;
let enoughIron = this . resources . iron . amount >= this . currentQuest . iron ;
let enoughBricks = this . resources . bricks >= this . currentQuest . bricks ;
let enoughBricks = this . resources . bricks . amount >= this . currentQuest . bricks ;
let enoughCorn = this . resources . corn >= this . currentQuest . corn ;
let enoughCorn = this . resources . corn . amount >= this . currentQuest . corn ;
let enoughCoal = this . resources . coal >= this . currentQuest . coal ;
let enoughCoal = this . resources . coal . amount >= this . currentQuest . coal ;
if ( enoughWood && enoughStone && enoughIron && enoughBricks && enoughCorn && enoughCoal ) {
if ( enoughWood && enoughStone && enoughIron && enoughBricks && enoughCorn && enoughCoal ) {
return true ;
return true ;
@ -502,13 +537,13 @@ let game = new Vue({
redeemReward ( ) {
redeemReward ( ) {
if ( this . isQuestRedeemable ( ) ) {
if ( this . isQuestRedeemable ( ) ) {
this . resources . gold += this . currentQuest . reward ;
this . resources . gold . amount += this . currentQuest . reward ;
this . resources . wood -= this . currentQuest . wood ;
this . resources . wood . amount -= this . currentQuest . wood ;
this . resources . stone -= this . currentQuest . stone ;
this . resources . stone . amount -= this . currentQuest . stone ;
this . resources . iron -= this . currentQuest . iron ;
this . resources . iron . amount -= this . currentQuest . iron ;
this . resources . bricks -= this . currentQuest . bricks ;
this . resources . bricks . amount -= this . currentQuest . bricks ;
this . resources . corn -= this . currentQuest . corn ;
this . resources . corn . amount -= this . currentQuest . corn ;
this . resources . coal -= this . currentQuest . coal ;
this . resources . coal . amount -= this . currentQuest . coal ;
this . sendRewardMessage ( this . currentQuest . reward ) ;
this . sendRewardMessage ( this . currentQuest . reward ) ;
this . generateQuestWithRandomItems ( ) ;
this . generateQuestWithRandomItems ( ) ;
@ -519,12 +554,12 @@ let game = new Vue({
} ,
} ,
useRequiredResources ( building ) {
useRequiredResources ( building ) {
this . resources . wood -= building . requires . wood ;
this . resources . wood . amount -= ( building . requires . wood ? building . requires . wood : 0 ) ;
this . resources . stone -= building . requires . stone ;
this . resources . stone . amount -= ( building . requires . stone ? building . requires . stone : 0 ) ;
this . resources . iron -= building . requires . iron ;
this . resources . iron . amount -= ( building . requires . iron ? building . requires . iron : 0 ) ;
this . resources . bricks -= building . requires . bricks ;
this . resources . bricks . amount -= ( building . requires . bricks ? building . requires . bricks : 0 ) ;
this . resources . corn -= building . requires . corn ;
this . resources . corn . amount -= ( building . requires . corn ? building . requires . corn : 0 ) ;
this . resources . coal -= building . requires . coal ;
this . resources . coal . amount -= ( building . requires . coal ? building . requires . coal : 0 ) ;
} ,
} ,
// Templating
// Templating
@ -623,16 +658,8 @@ let game = new Vue({
} ) ;
} ) ;
} ,
} ,
// Debug & Testing
removeBlackSmith ( ) {
this . buildings [ 5 ] . isOwned = false ;
this . saveBuildingsToStorage ( ) ;
location . reload ( ) ;
} ,
reset ( hard = false ) {
reset ( hard = false ) {
localStorage . removeItem ( 'buildings' + this . version ) ;
localStorage . removeItem ( 'buildings' + this . version ) ;
localStorage . removeItem ( 'hadResource' + this . version ) ;
localStorage . removeItem ( 'currentQuest' + this . version ) ;
localStorage . removeItem ( 'currentQuest' + this . version ) ;
if ( hard ) {
if ( hard ) {