From da3f49389760f4cf59bbe7714abe827b07615644 Mon Sep 17 00:00:00 2001 From: pjht Date: Sat, 6 Oct 2018 11:34:12 -0500 Subject: [PATCH] Add trading and other stuff --- index.html | 5 +++ main.js | 91 ++++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 72 insertions(+), 24 deletions(-) diff --git a/index.html b/index.html index 07a4e7c..9527614 100644 --- a/index.html +++ b/index.html @@ -23,6 +23,9 @@ + @@ -61,6 +64,8 @@

Researches:

+
+
diff --git a/main.js b/main.js index 9efc85d..695bf0c 100644 --- a/main.js +++ b/main.js @@ -7,7 +7,6 @@ var numResources=0; var buildings={}; var unlockedBuildings=[]; var has_shelter=false; -var research_enabled=false; var researches={}; var unlockedResearch=[]; var research_points=0; @@ -41,7 +40,6 @@ var all_buildings={ metal:15 }, build_effects:{ - research_enabled:true, max_research_points:150 } }, @@ -52,6 +50,12 @@ var all_buildings={ build_effects: { max_resources:50 } + }, + "trading post":{ + ings:{ + wood:100, + metal:50 + } } }; var worker_resource={ @@ -59,25 +63,35 @@ var worker_resource={ miner:"metal", scientist:"research_points" }; -var worker_rate=0.5; +var worker_rate=0.1; var all_researches={ - "Faster Workers":{ - maxLevel:3, + "Faster workers":{ + maxLevel:6, cost:100, effects:{ - worker_rate:0.5 + worker_rate:0.2 } } }; var multiplier=1.17; +var trading_rates={ + wood:1, + metal:10 +}; +function tenthRound(number) { + return Math.round(number*10)/10; +} 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"); $(".shelter_required").toggle(has_shelter); $("#link_population").toggle(has_shelter); $(".resources_required").toggle(has_resources); $("#link_buildings").toggle(has_resources); - $("#link_research").toggle(research_enabled); - $(".research_required").toggle(research_enabled); + $("#link_research").toggle(has_lab); + $(".research_required").toggle(has_lab); + $("#link_trading").toggle(has_tpost); } function incResource(name,amount=1) { if (name=="research_points") { @@ -89,10 +103,11 @@ function incResource(name,amount=1) { research_points-=amount; return; } + research_points=tenthRound(research_points); updateResearchPointInfo(); updateResearchButtons(); } else { - if (numResources>maxResources) { + if (numResources>=maxResources) { return; } if (resources[name]) { @@ -105,21 +120,25 @@ function incResource(name,amount=1) { decResource(name,amount); return; } + resources[name]=tenthRound(resources[name]); + numResources=tenthRound(numResources); updateResourceInfo(); - updateCraftButtons(); + updateTradingButtons(); } updateShown(); } -function decResource(name,amount) { +function decResource(name,amount=1) { if (resources[name]0) { + continue; + } var ings=buildingCost(name); var show=true; var disabled=false; @@ -239,9 +257,9 @@ function updateCraftButtons() { i++; } if (disabled) { - $("#craft_buttons").append("
"); + $("#craft_buttons").append("
"); } else { - $("#craft_buttons").append("
"); + $("#craft_buttons").append("
"); } } } @@ -327,7 +345,7 @@ function fire(type) { function autoInc() { for (var worker in workers) { worker_amount=workers[worker]; - amount=worker_rate*worker_amount; + var amount=worker_rate*worker_amount; if (amount>0) { if (worker=="scientist") { var usedMetal=Math.ceil(worker_amount*0.4); @@ -353,7 +371,6 @@ function save() { buildings:buildings, unlockedBuildings:unlockedBuildings, has_shelter:has_shelter, - research_enabled:research_enabled, researches:researches, unlockedResearch:unlockedResearch, research_points:research_points, @@ -376,7 +393,6 @@ function load() { buildings=gamestate.buildings; unlockedBuildings=gamestate.unlockedBuildings; has_shelter=gamestate.has_shelter; - research_enabled=gamestate.research_enabled; researches=gamestate.researches; unlockedResearch=gamestate.unlockedResearch; research_points=gamestate.research_points; @@ -401,7 +417,6 @@ function init() { buildings={}; unlockedBuildings=[]; has_shelter=false; - research_enabled=false; researches={}; unlockedResearch=[]; research_points=0; @@ -421,8 +436,8 @@ function init() { updateResearchPointInfo(); updateResearchButtons(); updateResearchInfo(); - applyResearches(); + updateTradingButtons(); } function set_tab(tab) { if ($("#link_"+tab).is(":visible")) { @@ -450,6 +465,34 @@ function updateResearchInfo() { $("#researches").append("

"+name+": Level "+researches[name]+"

"); } } +function canSell(name,amount=1) { + var goldGotten=trading_rates[name]*amount; + var spacesNeeded=goldGotten-amount; + return maxResources-numResources>=spacesNeeded; +} +function buy(name,amount=1) { + decResource("gold",trading_rates[name]*amount); + incResource(name,amount); +} +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("
"); + $("#tab_trading").append(" "); + $("#tab_trading").append(""); + $("#tab_trading #sell1").prop("disabled",!canSell(name)).removeAttr("id"); + $("#tab_trading #buy1").prop("disabled",resources["gold"]