Fix a couple bugs. Also no more underscores.

This commit is contained in:
pjht 2018-10-15 14:19:42 -05:00
parent ef7eca07bf
commit bf68fc7ea5
2 changed files with 119 additions and 117 deletions

View File

@ -12,61 +12,61 @@
<body> <body>
<ul class="nav nav-tabs"> <ul class="nav nav-tabs">
<li class="nav-item"> <li class="nav-item">
<a class="nav-link active" id="link_main" onclick="set_tab('main')">Main</a> <a class="nav-link active" id="linkMain" onclick="setTab('main')">Main</a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" id="link_buildings" onclick="set_tab('buildings')">Buildings</a> <a class="nav-link" id="linkBuildings" onclick="setTab('buildings')">Buildings</a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" id="link_population" onclick="set_tab('population')">Population</a> <a class="nav-link" id="linkPopulation" onclick="setTab('population')">Population</a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" id="link_research" onclick="set_tab('research')">Research</a> <a class="nav-link" id="linkResearch" onclick="setTab('research')">Research</a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" id="link_trading" onclick="set_tab('trading')">Trading</a> <a class="nav-link" id="linkTrading" onclick="setTab('trading')">Trading</a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" id="link_options" onclick="set_tab('options')">Options</a> <a class="nav-link" id="linkOptions" onclick="setTab('options')">Options</a>
</li> </li>
</ul> </ul>
<div id="tab_main"> <div id="tabMain">
<button onclick="incResource('wood')">Gather wood</button> <button onclick="incResource('wood')">Gather wood</button>
<div class="resources_required"> <div class="resourcesRequired">
<br> <br>
<p id="max_resources">Resources:</p> <p id="maxResources">Resources:</p>
<p id="resources"></p> <p id="resources"></p>
</div> </div>
</div> </div>
<div id="tab_buildings"> <div id="tabBuildings">
<div id="craft_buttons"></div> <div id="craftButtons"></div>
<p>Buildings:</p> <p>Buildings:</p>
<p id="buildings"></p> <p id="buildings"></p>
</div> </div>
<div id="tab_population"> <div id="tabPopulation">
<p id="pop">Population uninitialized!</p> <p id="pop">Population uninitialized!</p>
<p id="work_pop">Working population uninitialized!</p> <p id="workPop">Working population uninitialized!</p>
<p id="num_lumberjacks">Number of lumberjacks uninitialized!</p> <p id="numLumberjacks">Number of lumberjacks uninitialized!</p>
<button class="hire" onclick="hire('lumberjack')">Hire</button> <button class="hire" onclick="hire('lumberjack')">Hire</button>
<button id="fire_lumberjack" onclick="fire('lumberjack')">Fire</button> <button id="fireLumberjack" onclick="fire('lumberjack')">Fire</button>
<p id="num_miners">Number of miners uninitialized!</p> <p id="numMiners">Number of miners uninitialized!</p>
<button class="hire" onclick="hire('miner')">Hire</button> <button class="hire" onclick="hire('miner')">Hire</button>
<button id="fire_miner" onclick="fire('miner')">Fire</button> <button id="fireMiner" onclick="fire('miner')">Fire</button>
<div class="research_required"> <div class="researchRequired">
<p id="num_scientists">Number of scientists uninitialized!</p> <p id="numScientists">Number of scientists uninitialized!</p>
<button class="hire" onclick="hire('scientist')">Hire</button> <button class="hire" onclick="hire('scientist')">Hire</button>
<button id="fire_scientist" onclick="fire('scientist')">Fire</button> <button id="fireScientist" onclick="fire('scientist')">Fire</button>
</div> </div>
</div> </div>
<div id="tab_research"> <div id="tabResearch">
<p id="research_points">Research points uninitialized!</p> <p id="researchPoints">Research points uninitialized!</p>
<div id="research_buttons"></div> <div id="researchButtons"></div>
<p>Researches:</p> <p>Researches:</p>
<div id="researches"></div> <div id="researches"></div>
</div> </div>
<div id="tab_trading"> <div id="tabTrading">
</div> </div>
<div id="tab_options"> <div id="tabOptions">
<button onclick="reset()">Reset</button> <button onclick="reset()">Reset</button>
</div> </div>
</body> </body>

186
main.js
View File

@ -5,9 +5,9 @@ var buildings;
var unlockedBuildings; var unlockedBuildings;
var researches; var researches;
var unlockedResearch; var unlockedResearch;
var research_points; var researchPoints;
var workers; var workers;
var all_buildings={ var allBuildings={
hut:{ hut:{
ings:{ ings:{
wood:15 wood:15
@ -31,7 +31,7 @@ var all_buildings={
metal:15 metal:15
}, },
attributes:{ attributes:{
max_research_points:150 maxResearchPoints:100
} }
}, },
storehouse:{ storehouse:{
@ -39,7 +39,7 @@ var all_buildings={
wood:60 wood:60
}, },
attributes: { attributes: {
max_resources:100 maxResources:100
} }
}, },
"trading post":{ "trading post":{
@ -52,28 +52,28 @@ var all_buildings={
var allWorkers={ var allWorkers={
lumberjack:"wood", lumberjack:"wood",
miner:"metal", miner:"metal",
scientist:"research_points" scientist:"researchPoints"
}; };
var worker_rate=0.1; var workerRate=0.1;
var all_researches={ var allResearches={
"Faster workers":{ "Faster workers":{
maxLevel:6, maxLevel:6,
cost:100, cost:100,
effects:{ effects:{
worker_rate:0.2 workerRate:0.2
} }
} }
}; };
var multiplier=1.17; var multiplier=1.17;
var trading_rates={ var tradingRates={
wood:1, wood:1,
metal:10 metal:10
}; };
var startingStorage=100; var startingStorage=100;
var shelters=[]; var shelters=[];
function initializeShelterArray() { function initializeShelterArray() {
for (var name in all_buildings) { for (var name in allBuildings) {
var building=all_buildings[name]; var building=allBuildings[name];
var attrbs=building.attributes; var attrbs=building.attributes;
if (attrbs) { if (attrbs) {
var maxpop=attrbs.maxPop; var maxpop=attrbs.maxPop;
@ -84,7 +84,7 @@ function initializeShelterArray() {
} }
} }
function getBuildingAttribute(name,attrname) { function getBuildingAttribute(name,attrname) {
var attr=all_buildings[name].attributes[attrname]; var attr=allBuildings[name].attributes[attrname];
if (attr==undefined) { if (attr==undefined) {
attr=0; attr=0;
} }
@ -102,13 +102,13 @@ function tenthRound(number) {
} }
function maxResources() { function maxResources() {
var numStorehouses=numOfBuilding("storehouse"); var numStorehouses=numOfBuilding("storehouse");
var storehouseResources=getBuildingAttribute("storehouse","max_resources"); var storehouseResources=getBuildingAttribute("storehouse","maxResources");
var extraStorage=numStorehouses*storehouseResources; var extraStorage=numStorehouses*storehouseResources;
return startingStorage+extraStorage; return startingStorage+extraStorage;
} }
function max_research_points() { function maxResearchPoints() {
var numLabs=numOfBuilding("labs"); var numLabs=numOfBuilding("lab");
var labRpoints=getBuildingAttribute("lab","max_research_points"); var labRpoints=getBuildingAttribute("lab","maxResearchPoints");
return numLabs*labRpoints; return numLabs*labRpoints;
} }
function maxPop() { function maxPop() {
@ -122,36 +122,36 @@ function maxPop() {
return maxpop; return maxpop;
} }
function updateShown() { function updateShown() {
var has_resources=Object.keys(resources).length>0; var hasResources=Object.keys(resources).length>0;
var has_lab=Object.keys(buildings).includes("lab"); var hasLab=Object.keys(buildings).includes("lab");
var has_tpost=Object.keys(buildings).includes("trading post"); var hasTpost=Object.keys(buildings).includes("trading post");
var has_shelter=false var hasShelter=false
for (var i in shelters) { for (var i in shelters) {
var shelter=shelters[i]; var shelter=shelters[i];
if (Object.keys(buildings).includes(shelter)) { if (Object.keys(buildings).includes(shelter)) {
has_shelter=true; hasShelter=true;
break; break;
} }
} }
$(".shelter_required").toggle(has_shelter); $(".shelterRequired").toggle(hasShelter);
$("#link_population").toggle(has_shelter); $("#linkPopulation").toggle(hasShelter);
$(".resources_required").toggle(has_resources); $(".resourcesRequired").toggle(hasResources);
$("#link_buildings").toggle(has_resources); $("#linkBuildings").toggle(hasResources);
$("#link_research").toggle(has_lab); $("#linkResearch").toggle(hasLab);
$(".research_required").toggle(has_lab); $(".researchRequired").toggle(hasLab);
$("#link_trading").toggle(has_tpost); $("#linkTrading").toggle(hasTpost);
} }
function incResource(name,amount=1) { function incResource(name,amount=1) {
if (name=="research_points") { if (name=="researchPoints") {
if (research_points>max_research_points()) { if (researchPoints>maxResearchPoints()) {
return; return;
} }
research_points+=amount; researchPoints+=amount;
if (research_points>max_research_points()) { if (researchPoints>maxResearchPoints()) {
research_points-=amount; researchPoints-=amount;
return; return;
} }
research_points=tenthRound(research_points); researchPoints=tenthRound(researchPoints);
updateResearchPointInfo(); updateResearchPointInfo();
updateResearchButtons(); updateResearchButtons();
} else { } else {
@ -203,15 +203,15 @@ function incBuilding(name) {
} }
function buildingCost(name) { function buildingCost(name) {
var newIngs={}; var newIngs={};
var ings=all_buildings[name].ings; var ings=allBuildings[name].ings;
var building_amount=buildings[name]; var buildingAmount=buildings[name];
if (building_amount==undefined) { if (buildingAmount==undefined) {
building_amount=0; buildingAmount=0;
} }
for (var ing in ings) { for (var ing in ings) {
var ing_amount=ings[ing]; var ingAmount=ings[ing];
ing_amount=Math.floor(ing_amount*(multiplier**building_amount)); ingAmount=Math.floor(ingAmount*(multiplier**buildingAmount));
newIngs[ing]=ing_amount; newIngs[ing]=ingAmount;
} }
return newIngs; return newIngs;
} }
@ -243,7 +243,7 @@ function updateResourceInfo() {
var capName=capitalizeFirst(name); var capName=capitalizeFirst(name);
$("#resources").append("<p>"+capName+": "+resources[name]+"</p>"); $("#resources").append("<p>"+capName+": "+resources[name]+"</p>");
} }
$("#max_resources").text("Resources: ("+numResources+"/"+maxResources()+")"); $("#maxResources").text("Resources: ("+numResources+"/"+maxResources()+")");
} }
function updateBuildingInfo() { function updateBuildingInfo() {
$("#buildings").html(""); $("#buildings").html("");
@ -253,8 +253,8 @@ function updateBuildingInfo() {
} }
} }
function updateCraftButtons() { function updateCraftButtons() {
$("#craft_buttons").html(""); $("#craftButtons").html("");
for (var name in all_buildings) { for (var name in allBuildings) {
if (name=="trading post" && buildings["trading post"]>0) { if (name=="trading post" && buildings["trading post"]>0) {
continue; continue;
} }
@ -294,9 +294,9 @@ function updateCraftButtons() {
i++; i++;
} }
if (disabled) { if (disabled) {
$("#craft_buttons").append("<button disabled onclick=\"craft('"+name+"')\">Craft a "+btext+"</button><br>"); $("#craftButtons").append("<button disabled onclick=\"craft('"+name+"')\">Craft a "+btext+"</button><br>");
} else { } else {
$("#craft_buttons").append("<button onclick=\"craft('"+name+"')\">Craft a "+btext+"</button><br>"); $("#craftButtons").append("<button onclick=\"craft('"+name+"')\">Craft a "+btext+"</button><br>");
} }
} }
} }
@ -318,52 +318,53 @@ function updateWorkerInfo() {
var working=0; var working=0;
for (var worker in allWorkers) { for (var worker in allWorkers) {
var workerAmount=workers[worker]; var workerAmount=workers[worker];
if (workerAmount) { if (!workerAmount) {
workerAmount=0; workerAmount=0;
} }
working+=workerAmount; working+=workerAmount;
$("#fire_"+worker).attr("disabled",workerAmount==0); worker=capitalizeFirst(worker);
$("#num_"+worker+"s").text(capitalizeFirst(worker)+"s: "+workerAmount); $("#fire"+worker).attr("disabled",workerAmount==0);
$("#num"+worker+"s").text(worker+"s: "+workerAmount);
} }
$("#work_pop").text("Working: "+working+"/"+pop); $("#workPop").text("Working: "+working+"/"+pop);
$(".hire").attr("disabled",working==pop); $(".hire").attr("disabled",working==pop);
} }
function updateTradingButtons() { function updateTradingButtons() {
$("#tab_trading").html(""); $("#tabTrading").html("");
for (var name in trading_rates) { for (var name in tradingRates) {
var rate=trading_rates[name]; var rate=tradingRates[name];
$("#tab_trading").append("<p>"+capitalizeFirst(name)+": "+rate+" gold"+"</p>"); $("#tabTrading").append("<p>"+capitalizeFirst(name)+": "+rate+" gold"+"</p>");
$("#tab_trading").append("<button id=sell1 onclick=\"sell('"+name+"')\">Sell 1</button>&nbsp"); $("#tabTrading").append("<button id=sell1 onclick=\"sell('"+name+"')\">Sell 1</button>&nbsp");
$("#tab_trading").append("<button id=buy1 onclick=\"buy('"+name+"')\">Buy 1</button><br>"); $("#tabTrading").append("<button id=buy1 onclick=\"buy('"+name+"')\">Buy 1</button><br>");
$("#tab_trading").append("<button id=sell10 onclick=\"sell('"+name+"',10)\">Sell 10</button>&nbsp"); $("#tabTrading").append("<button id=sell10 onclick=\"sell('"+name+"',10)\">Sell 10</button>&nbsp");
$("#tab_trading").append("<button id=buy10 onclick=\"buy('"+name+"',10)\">Buy 10</button>"); $("#tabTrading").append("<button id=buy10 onclick=\"buy('"+name+"',10)\">Buy 10</button>");
$("#tab_trading #sell1").prop("disabled",!canSell(name)).removeAttr("id"); $("#tabTrading #sell1").prop("disabled",!canSell(name)).removeAttr("id");
$("#tab_trading #buy1").prop("disabled",resources["gold"]<trading_rates[name]).removeAttr("id"); $("#tabTrading #buy1").prop("disabled",resources["gold"]<tradingRates[name]).removeAttr("id");
$("#tab_trading #sell10").prop("disabled",!canSell(name,10)).removeAttr("id"); $("#tabTrading #sell10").prop("disabled",!canSell(name,10)).removeAttr("id");
$("#tab_trading #buy10").prop("disabled",resources["gold"]<trading_rates[name]*10).removeAttr("id"); $("#tabTrading #buy10").prop("disabled",resources["gold"]<tradingRates[name]*10).removeAttr("id");
} }
} }
function updateResearchPointInfo() { function updateResearchPointInfo() {
$("#research_points").text("Research points: "+research_points+"/"+max_research_points()); $("#researchPoints").text("Research points: "+researchPoints+"/"+maxResearchPoints());
} }
function updateResearchButtons() { function updateResearchButtons() {
$("#research_buttons").html(""); $("#researchButtons").html("");
for (var name in all_researches) { for (var name in allResearches) {
var research=all_researches[name]; var research=allResearches[name];
var cost=research.cost; var cost=research.cost;
var maxLevel=research.maxLevel; var maxLevel=research.maxLevel;
var show=true; var show=true;
var disabled=false; var disabled=false;
if (!unlockedResearch.includes(name)) { if (!unlockedResearch.includes(name)) {
if (research_points<cost/2) { if (researchPoints<cost/2) {
show=false; show=false;
} else if (research_points<cost) { } else if (researchPoints<cost) {
disabled=true; disabled=true;
} }
} else { } else {
if (researches[name]==maxLevel) { if (researches[name]==maxLevel) {
show=false show=false
} else if (research_points<cost) { } else if (researchPoints<cost) {
disabled=true; disabled=true;
} }
} }
@ -372,9 +373,9 @@ function updateResearchButtons() {
unlockedResearch.push(name); unlockedResearch.push(name);
} }
if (disabled) { if (disabled) {
$("#research_buttons").append("<button disabled onclick=\"research('"+name+"')\">"+name+" ("+cost+" points)</button><br>"); $("#researchButtons").append("<button disabled onclick=\"research('"+name+"')\">"+name+" ("+cost+" points)</button><br>");
} else { } else {
$("#research_buttons").append("<button onclick=\"research('"+name+"')\">"+name+" ("+cost+" points)</button><br>"); $("#researchButtons").append("<button onclick=\"research('"+name+"')\">"+name+" ("+cost+" points)</button><br>");
} }
} }
} }
@ -387,18 +388,18 @@ function updateResearchInfo() {
} }
function applyResearches() { function applyResearches() {
for (var research in researches) { for (var research in researches) {
var effects=all_researches[research].effects; var effects=allResearches[research].effects;
var level=researches[research]; var level=researches[research];
for (var effect in effects) { for (var effect in effects) {
if (effect=="worker_rate") { if (effect=="workerRate") {
worker_rate=0.5; workerRate=0.5;
worker_rate+=effects[effect]*level; workerRate+=effects[effect]*level;
} }
} }
} }
} }
function research(name) { function research(name) {
research_points-=all_researches[name].cost; researchPoints-=allResearches[name].cost;
if (researches[name]) { if (researches[name]) {
researches[name]+=1; researches[name]+=1;
} else { } else {
@ -426,11 +427,11 @@ function fire(type) {
} }
function autoInc() { function autoInc() {
for (var worker in allWorkers) { for (var worker in allWorkers) {
worker_amount=workers[worker]; workerAmount=workers[worker];
var amount=worker_rate*worker_amount; var amount=workerRate*workerAmount;
if (amount>0) { if (amount>0) {
if (worker=="scientist") { if (worker=="scientist") {
var usedMetal=Math.ceil(worker_amount*0.4); var usedMetal=Math.ceil(workerAmount*0.4);
if (resources["metal"]>=usedMetal) { if (resources["metal"]>=usedMetal) {
incResource(allWorkers[worker],amount); incResource(allWorkers[worker],amount);
decResource("metal",usedMetal); decResource("metal",usedMetal);
@ -451,7 +452,7 @@ function save() {
unlockedBuildings:unlockedBuildings, unlockedBuildings:unlockedBuildings,
researches:researches, researches:researches,
unlockedResearch:unlockedResearch, unlockedResearch:unlockedResearch,
research_points:research_points, researchPoints:researchPoints,
} }
localStorage.setItem("game",JSON.stringify(gamestate)); localStorage.setItem("game",JSON.stringify(gamestate));
} }
@ -468,7 +469,7 @@ function load() {
unlockedBuildings=gamestate.unlockedBuildings; unlockedBuildings=gamestate.unlockedBuildings;
researches=gamestate.researches; researches=gamestate.researches;
unlockedResearch=gamestate.unlockedResearch; unlockedResearch=gamestate.unlockedResearch;
research_points=gamestate.research_points; researchPoints=gamestate.researchPoints;
return true; return true;
} }
function reset() { function reset() {
@ -487,14 +488,14 @@ function init() {
unlockedBuildings=[]; unlockedBuildings=[];
researches={}; researches={};
unlockedResearch=[]; unlockedResearch=[];
research_points=0; researchPoints=0;
workers={ workers={
lumberjack:0, lumberjack:0,
miner:0, miner:0,
scientist:0 scientist:0
}; };
} }
set_tab("main"); setTab("main");
updateShown(); updateShown();
updateResourceInfo(); updateResourceInfo();
updateBuildingInfo(); updateBuildingInfo();
@ -507,26 +508,27 @@ function init() {
applyResearches(); applyResearches();
updateTradingButtons(); updateTradingButtons();
} }
function set_tab(tab) { function setTab(tab) {
if ($("#link_"+tab).is(":visible")) { tab=capitalizeFirst(tab);
$("[id^='tab_']").hide(); if ($("#link"+tab).is(":visible")) {
$("[id^='link_']").removeClass("active"); $("[id^='tab']").hide();
$("#tab_"+tab).show(); $("[id^='link']").removeClass("active");
$("#link_"+tab).addClass("active"); $("#tab"+tab).show();
$("#link"+tab).addClass("active");
} }
} }
function canSell(name,amount=1) { function canSell(name,amount=1) {
var goldGotten=trading_rates[name]*amount; var goldGotten=tradingRates[name]*amount;
var spacesNeeded=goldGotten-amount; var spacesNeeded=goldGotten-amount;
return maxResources()-numResources>=spacesNeeded; return maxResources()-numResources>=spacesNeeded;
} }
function buy(name,amount=1) { function buy(name,amount=1) {
decResource("gold",trading_rates[name]*amount); decResource("gold",tradingRates[name]*amount);
incResource(name,amount); incResource(name,amount);
} }
function sell(name,amount=1) { function sell(name,amount=1) {
decResource(name,amount); decResource(name,amount);
incResource("gold",trading_rates[name]*amount); incResource("gold",tradingRates[name]*amount);
} }
$(document).ready(function() { $(document).ready(function() {
initializeShelterArray(); initializeShelterArray();