| @@ -47,47 +47,55 @@ deepFreeze(numberModes); | |||
| let numberMode = numberModes["words"]; | |||
| const activePowerups = []; | |||
| const activePowerups = {}; | |||
| function tickPowerups(delta) { | |||
| const powerupList = document.querySelector("#powerup-list"); | |||
| let changed = false; | |||
| // I love mutating arrays as I traverse them. | |||
| for (let i = activePowerups.length - 1; i >= 0; i--) { | |||
| activePowerups[i].lifetime -= delta; | |||
| if (activePowerups[i].lifetime <= 0) { | |||
| const entry = activePowerups[i]; | |||
| Object.entries(activePowerups).filter(x => x[1].life > 0).forEach(([key, data]) => { | |||
| const newLife = data.life - delta; | |||
| if (newLife <= 0) { | |||
| setTimeout(() => { | |||
| powerupList.removeChild(entry.element); | |||
| powerupList.removeChild(data.element); | |||
| }, 1000); | |||
| entry.element.classList.add("powerup-entry-done"); | |||
| activePowerups.splice(i, 1); | |||
| data.element.classList.add("powerup-entry-done"); | |||
| activePowerups[key].life = 0; | |||
| changed = true; | |||
| } else { | |||
| const frac = (activePowerups[i].powerup.duration - activePowerups[i].lifetime) / (activePowerups[i].powerup.duration); | |||
| activePowerups[i].element.style.setProperty("--progress", frac * 100 + "%") | |||
| data.life = newLife; | |||
| const frac = (data.maxLife - data.life) / (data.maxLife); | |||
| data.element.style.setProperty("--progress", frac * 100 + "%") | |||
| } | |||
| } | |||
| }); | |||
| if (changed) { | |||
| updateAll(); | |||
| } | |||
| } | |||
| function addPowerup(powerup) { | |||
| const powerupList = document.querySelector("#powerup-list"); | |||
| const powerupEntry = document.createElement("div"); | |||
| powerupEntry.classList.add("powerup-entry"); | |||
| powerupEntry.innerText = powerup.name; | |||
| powerupList.appendChild(powerupEntry); | |||
| activePowerups.push({ powerup: powerup, lifetime: powerup.duration, element: powerupEntry }); | |||
| updateAll(); | |||
| function addPowerup(key, powerup) { | |||
| console.log(key, powerup) | |||
| // powerup already exists | |||
| if (activePowerups[key].life > 0) { | |||
| activePowerups[key].life += powerup.duration; | |||
| activePowerups[key].maxLife = activePowerups[key].life; | |||
| } else { | |||
| const powerupList = document.querySelector("#powerup-list"); | |||
| const powerupEntry = document.createElement("div"); | |||
| powerupEntry.classList.add("powerup-entry"); | |||
| powerupEntry.innerText = powerup.name; | |||
| powerupList.appendChild(powerupEntry); | |||
| activePowerups[key] = {powerup: powerup, life: powerup.duration, maxLife: powerup.duration, element: powerupEntry }; | |||
| updateAll(); | |||
| } | |||
| } | |||
| function applyGlobalProdBonus(cost) { | |||
| @@ -226,18 +234,21 @@ function updateDisplay() { | |||
| function updateProductivity() { | |||
| Object.assign(currentProductivity, calculateProductivity()); | |||
| activePowerups.forEach(entry => { | |||
| const powerup = entry.powerup; | |||
| const state = { | |||
| ownedUpgrades: ownedUpgrades, | |||
| resources: resources, | |||
| currentProductivity: currentProductivity, | |||
| belongings: belongings | |||
| }; | |||
| powerup.effect(state); | |||
| }) | |||
| Object.entries(activePowerups).forEach(([key, entry]) => { | |||
| if (entry.time > 0) { | |||
| const powerup = entry.powerup; | |||
| const state = { | |||
| ownedUpgrades: ownedUpgrades, | |||
| resources: resources, | |||
| currentProductivity: currentProductivity, | |||
| belongings: belongings | |||
| }; | |||
| powerup.effect(state); | |||
| } | |||
| }); | |||
| } | |||
| function addResources(delta) { | |||
| @@ -622,6 +633,10 @@ function initializeData() { | |||
| } | |||
| } | |||
| Object.keys(powerups).filter(x => powerups[x].duration !== undefined).forEach(key => activePowerups[key] = { | |||
| life: 0 | |||
| }); | |||
| } | |||
| function registerListeners() { | |||
| @@ -1025,7 +1040,7 @@ function doPowerup() { | |||
| button.addEventListener("mousedown", e => { | |||
| if (powerup.duration !== undefined) { | |||
| addPowerup(powerup); | |||
| addPowerup(choices[choice], powerup); | |||
| } else { | |||
| powerup.effect(state); | |||
| } | |||