diff --git a/main.js b/main.js index 6ce4ed1..dc89250 100644 --- a/main.js +++ b/main.js @@ -1,23 +1,18 @@ -var maxPop; var pop; -var working; var resources; -var maxResources; var numResources; var buildings; var unlockedBuildings; -var has_shelter; var researches; var unlockedResearch; var research_points; -var max_research_points; var workers; var all_buildings={ hut:{ ings:{ wood:15 }, - build_effects:{ + attributes:{ maxPop:2 } }, @@ -26,7 +21,7 @@ var all_buildings={ wood:30, metal:5 }, - build_effects:{ + attributes:{ maxPop:4 } }, @@ -35,7 +30,7 @@ var all_buildings={ wood:30, metal:15 }, - build_effects:{ + attributes:{ max_research_points:150 } }, @@ -43,7 +38,7 @@ var all_buildings={ ings:{ wood:60 }, - build_effects: { + attributes: { max_resources:100 } }, @@ -74,13 +69,70 @@ var trading_rates={ wood:1, metal:10 }; +var startingStorage=100; +var shelters=[]; +function initializeShelterArray() { + for (var name in all_buildings) { + var building=all_buildings[name]; + var attrbs=building.attributes; + if (attrbs) { + var maxpop=attrbs.maxPop; + if (maxpop) { + shelters.push(name); + } + } + } +} +function getBuildingAttribute(name,attrname) { + var attr=all_buildings[name].attributes[attrname]; + if (attr==undefined) { + attr=0; + } + return attr; +} +function numOfBuilding(name) { + var count=buildings[name] + if (count==undefined) { + count=0; + } + return count; +} function tenthRound(number) { return Math.round(number*10)/10; } +function maxResources() { + var numStorehouses=numOfBuilding("storehouse"); + var storehouseResources=getBuildingAttribute("storehouse","max_resources"); + var extraStorage=numStorehouses*storehouseResources; + return startingStorage+extraStorage; +} +function max_research_points() { + var numLabs=numOfBuilding("labs"); + var labRpoints=getBuildingAttribute("lab","max_research_points"); + return numLabs*labRpoints; +} +function maxPop() { + var maxpop=0; + for (var i in shelters) { + var shelter=shelters[i]; + if (numOfBuilding(shelter)>0) { + maxpop+=numOfBuilding(shelter)*getBuildingAttribute(shelter,"maxPop"); + } + } + return maxpop; +} function updateShown() { var has_resources=Object.keys(resources).length>0; var has_lab=Object.keys(buildings).includes("lab"); var has_tpost=Object.keys(buildings).includes("trading post"); + var has_shelter=false + for (var i in shelters) { + var shelter=shelters[i]; + if (Object.keys(buildings).includes(shelter)) { + has_shelter=true; + break; + } + } $(".shelter_required").toggle(has_shelter); $("#link_population").toggle(has_shelter); $(".resources_required").toggle(has_resources); @@ -91,11 +143,11 @@ function updateShown() { } function incResource(name,amount=1) { if (name=="research_points") { - if (research_points>max_research_points) { + if (research_points>max_research_points()) { return; } research_points+=amount; - if (research_points>max_research_points) { + if (research_points>max_research_points()) { research_points-=amount; return; } @@ -103,7 +155,7 @@ function incResource(name,amount=1) { updateResearchPointInfo(); updateResearchButtons(); } else { - if (numResources>=maxResources) { + if (numResources>=maxResources()) { return; } if (resources[name]) { @@ -112,7 +164,7 @@ function incResource(name,amount=1) { resources[name]=amount; } numResources+=amount; - if (numResources>maxResources) { + if (numResources>maxResources()) { decResource(name,amount); return; } @@ -142,20 +194,9 @@ function incBuilding(name) { } else { buildings[name]=1; } - var effects=all_buildings[name]["build_effects"]; - for (effect in effects) { - if (effect=="maxPop") { - maxPop+=effects[effect]; - has_shelter=true; - updatePopulationInfo(); - updateWorkerInfo(); - } - if (effect=="max_resources") { - maxResources+=effects[effect]; - } - if (effect=="max_research_points") { - max_research_points+=effects[effect]; - } + if (shelters.includes(name)) { + updatePopulationInfo(); + updateWorkerInfo(); } updateShown(); updateBuildingInfo(); @@ -202,7 +243,7 @@ function updateResourceInfo() { var capName=capitalizeFirst(name); $("#resources").append("
"+capName+": "+resources[name]+"
"); } - $("#max_resources").text("Resources: ("+numResources+"/"+maxResources+")"); + $("#max_resources").text("Resources: ("+numResources+"/"+maxResources()+")"); } function updateBuildingInfo() { $("#buildings").html(""); @@ -264,40 +305,46 @@ function updatePopulation() { if(pop>0) { updateWorkerInfo(); } - if (pop"+name+": Level "+researches[name]+"
"); - } -} function canSell(name,amount=1) { var goldGotten=trading_rates[name]*amount; var spacesNeeded=goldGotten-amount; - return maxResources-numResources>=spacesNeeded; + return maxResources()-numResources>=spacesNeeded; } function buy(name,amount=1) { decResource("gold",trading_rates[name]*amount); @@ -488,22 +528,8 @@ function sell(name,amount=1) { decResource(name,amount); incResource("gold",trading_rates[name]*amount); } -function updateTradingButtons() { - $("#tab_trading").html(""); - for (var name in trading_rates) { - var rate=trading_rates[name]; - $("#tab_trading").append(""+capitalizeFirst(name)+": "+rate+" gold"+"
"); - $("#tab_trading").append(" "); - $("#tab_trading").append("