Worker hash will start empty

This commit is contained in:
pjht 2018-10-14 19:03:54 -05:00
parent 7c6be15b1b
commit f83769c9ab

26
main.js
View File

@ -54,7 +54,7 @@ var all_buildings={
} }
} }
}; };
var worker_resource={ var allWorkers={
lumberjack:"wood", lumberjack:"wood",
miner:"metal", miner:"metal",
scientist:"research_points" scientist:"research_points"
@ -276,9 +276,13 @@ function updatePopulationInfo() {
function updateWorkerInfo() { function updateWorkerInfo() {
$("#work_pop").text("Working: "+working+"/"+pop); $("#work_pop").text("Working: "+working+"/"+pop);
$(".hire").attr("disabled",working==pop); $(".hire").attr("disabled",working==pop);
for (var worker in workers) { for (var worker in Object.keys(allWorkers)) {
$("#fire_"+worker).attr("disabled",workers[worker]==0); var workerAmount=workers[worker];
$("#num_"+worker+"s").text(capitalizeFirst(worker)+"s: "+workers[worker]); if (workerAmount) {
workerAmount=0;
}
$("#fire_"+worker).attr("disabled",workerAmount==0);
$("#num_"+worker+"s").text(capitalizeFirst(worker)+"s: "+workerAmount);
} }
} }
function research(name) { function research(name) {
@ -329,28 +333,36 @@ function updateResearchButtons() {
} }
} }
function hire(type) { function hire(type) {
if (workers[type]) {
workers[type]+=1; workers[type]+=1;
} else {
workers[type]=1;
}
working+=1; working+=1;
updateWorkerInfo(); updateWorkerInfo();
} }
function fire(type) { function fire(type) {
if (workers[type]) {
workers[type]-=1; workers[type]-=1;
} else {
throw new Error("Cannot fire a never hired employee");
}
working-=1; working-=1;
updateWorkerInfo(); updateWorkerInfo();
} }
function autoInc() { function autoInc() {
for (var worker in workers) { for (var worker in Object.keys(allWorkers)) {
worker_amount=workers[worker]; worker_amount=workers[worker];
var amount=worker_rate*worker_amount; var amount=worker_rate*worker_amount;
if (amount>0) { if (amount>0) {
if (worker=="scientist") { if (worker=="scientist") {
var usedMetal=Math.ceil(worker_amount*0.4); var usedMetal=Math.ceil(worker_amount*0.4);
if (resources["metal"]>=usedMetal) { if (resources["metal"]>=usedMetal) {
incResource(worker_resource[worker],amount); incResource(allWorkers[worker],amount);
decResource("metal",usedMetal); decResource("metal",usedMetal);
} }
} else { } else {
incResource(worker_resource[worker],amount); incResource(allWorkers[worker],amount);
} }
} }
} }