From a9bd5d1590a7e01d7a967cc6a47bf67d8e812017 Mon Sep 17 00:00:00 2001 From: Fen Dweller Date: Sun, 22 Jul 2018 09:31:32 -0500 Subject: [PATCH] Upgrades can have many effects. Effects are grouped by type for ease of use. --- constants.js | 123 ++++++++++++++++++++++++++++++--------------------- gorge.js | 66 ++++++++++++++++++--------- util.js | 2 +- 3 files changed, 119 insertions(+), 72 deletions(-) diff --git a/constants.js b/constants.js index ea1514e..2df72f2 100644 --- a/constants.js +++ b/constants.js @@ -113,13 +113,12 @@ const buildings = { } } -const upgrade_types = { +const effect_types = { "prod": { "apply": function(effect, productivity) { - return productivity * 2; + return productivity * effect.amount; }, "desc": function(effect) { - console.log(effect); return round(effect.amount, 2) + "x food production from " + buildings[effect.target].plural; } }, @@ -140,11 +139,13 @@ const upgrades = { "cost": { "food": buildings.micro.cost * 10 }, - "effect": { - "type": "prod", - "target": "micro", - "amount": 2, - }, + "effects": [ + { + "type": "prod", + "target": "micro", + "amount": 2, + } + ], "prereqs": { "buildings": { "micro": 1 @@ -157,11 +158,13 @@ const upgrades = { "cost": { "food": buildings.micro.cost * 50 }, - "effect": { - "type": "prod", - "target": "micro", - "amount": 2.25, - }, + "effects": [ + { + "type": "prod", + "target": "micro", + "amount": 2.25, + } + ], "prereqs": { "buildings": { "micro": 5 @@ -177,11 +180,13 @@ const upgrades = { "cost": { "food": buildings.anthro.cost * 5 }, - "effect": { - "type": "prod", - "target": "anthro", - "amount": 2, - }, + "effects": [ + { + "type": "prod", + "target": "anthro", + "amount": 2, + } + ], "prereqs": { "buildings": { "anthro": 1 @@ -194,11 +199,13 @@ const upgrades = { "cost": { "food": buildings.anthro.cost * 50 }, - "effect": { - "type": "prod", - "target": "anthro", - "amount": 2.25, - }, + "effects": [ + { + "type": "prod", + "target": "anthro", + "amount": 2.25, + } + ], "prereqs": { "buildings": { "anthro": 10 @@ -214,10 +221,12 @@ const upgrades = { "cost": { "food": 5e2 }, - "effect": { - "type": "prod-all", - "amount": 1.05 - }, + "effects": [ + { + "type": "prod-all", + "amount": 1.05 + } + ], "prereqs": { "productivity": { "food": 1e1 @@ -230,10 +239,12 @@ const upgrades = { "cost": { "food": 5e3 }, - "effect": { - "type": "prod-all", - "amount": 1.05 - }, + "effects": [ + { + "type": "prod-all", + "amount": 1.05 + } + ], "prereqs": { "productivity": { "food": 1e2 @@ -249,10 +260,12 @@ const upgrades = { "cost": { "food": 5e4 }, - "effect": { - "type": "prod-all", - "amount": 1.05 - }, + "effects": [ + { + "type": "prod-all", + "amount": 1.05 + } + ], "prereqs": { "productivity": { "food": 1e3 @@ -268,10 +281,12 @@ const upgrades = { "cost": { "food": 5e5 }, - "effect": { - "type": "prod-all", - "amount": 1.05 - }, + "effects": [ + { + "type": "prod-all", + "amount": 1.05 + } + ], "prereqs": { "productivity": { "food": 1e4 @@ -287,10 +302,12 @@ const upgrades = { "cost": { "food": 5e6 }, - "effect": { - "type": "prod-all", - "amount": 1.05 - }, + "effects": [ + { + "type": "prod-all", + "amount": 1.05 + } + ], "prereqs": { "productivity": { "food": 1e5 @@ -306,10 +323,12 @@ const upgrades = { "cost": { "food": 5e7 }, - "effect": { - "type": "prod-all", - "amount": 1.05 - }, + "effects": [ + { + "type": "prod-all", + "amount": 1.05 + } + ], "prereqs": { "productivity": { "food": 1e6 @@ -325,10 +344,12 @@ const upgrades = { "cost": { "food": 5e8 }, - "effect": { - "type": "prod-all", - "amount": 1.05 - }, + "effects": [ + { + "type": "prod-all", + "amount": 1.05 + } + ], "prereqs": { "productivity": { "food": 1e7 diff --git a/gorge.js b/gorge.js index 8669ae0..32a7d3b 100644 --- a/gorge.js +++ b/gorge.js @@ -4,6 +4,8 @@ let belongings = {}; let ownedUpgrades = {}; +let effects = {}; + let remainingUpgrades = []; let resources = {}; @@ -18,16 +20,12 @@ function calculateProductivity() { productivity += productivityOf(key); } - for (const [key, value] of Object.entries(upgrades)) { - if (!ownedUpgrades[key]) { - continue; - } + for (let effect of effects["prod-all"]) { - if (value.effect.type == "prod-all") { - productivity *= value.effect.amount; + if (ownedUpgrades[effect.parent]) { + productivity = effect.apply(productivity); } } - return productivity; } @@ -36,16 +34,10 @@ function calculateProductivity() { function productivityMultiplierOf(type) { let base = 1; - for (const [key, value] of Object.entries(upgrades)) { - if (!ownedUpgrades[key]) { - continue; - } + for (let effect of effects["prod"]) { - - if (value.effect.type == "prod") { - if (value.effect.target == type) { - base *= value.effect.amount; - } + if (ownedUpgrades[effect.parent] && effect.target == type) { + base = effect.apply(base); } } @@ -226,8 +218,33 @@ function initializeData() { currentProductivity[key] = 0; } - for (const [key, value] of Object.entries(upgrades)) { - ownedUpgrades[key] = false; + for (const [id, upgrade] of Object.entries(upgrades)) { + ownedUpgrades[id] = false; + + for (let effect of upgrade.effects) { + if (effects[effect.type] === undefined) { + effects[effect.type] = []; + } + + // copy the data and add an entry for the upgrade id that owns the effect + + let newEffect = {}; + + for (const [key, value] of Object.entries(effect)) { + newEffect[key] = value; + } + + newEffect.parent = id; + + // unfortunate name collision here + // I'm using apply() to pass on any number of arguments to the + // apply() function of the effect type + + newEffect.apply = function(...args) { return effect_types[effect.type].apply.apply(null, [effect].concat(args)); } + + effects[effect.type].push(newEffect); + } + } } @@ -309,7 +326,6 @@ function upgradeAvailable(id) { } } } else if (type == "productivity") { - console.log(type, reqs); for (const [key, value] of Object.entries(reqs)) { if (currentProductivity[key] < value) { return false; @@ -429,6 +445,16 @@ function renderPrereqs(prereqs) { return renderLines(list); } +function renderEffects(effectList) { + let list = []; + + for (let effect of effectList) { + list.push({"text": effect_types[effect.type].desc(effect)}); + } + + return renderLines(list); +} + function fillTooltip(type, field, content) { let item = document.querySelector("#" + type + "-tooltip-" + field); if (typeof(content) === "string") { @@ -445,7 +471,7 @@ function upgradeTooltip(id, event) { fillTooltip("upgrade", "name", upgrades[id].name); fillTooltip("upgrade", "desc", upgrades[id].desc); - fillTooltip("upgrade", "effect", upgrade_types[upgrades[id].effect.type].desc(upgrades[id].effect)); + fillTooltip("upgrade", "effect", renderEffects(upgrades[id].effects)); fillTooltip("upgrade", "cost", renderCost(upgrades[id].cost)); fillTooltip("upgrade", "prereqs", renderPrereqs(upgrades[id].prereqs)); diff --git a/util.js b/util.js index fd8d11a..fe4e471 100644 --- a/util.js +++ b/util.js @@ -15,6 +15,6 @@ function removeChildren(element) { } } -function round(val, places) { +function round(val, places = 0) { return Math.round(val * Math.pow(10, places)) / Math.pow(10, places); }