cookie clicker but bigger
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

1136 řádky
27 KiB

  1. "use strict";
  2. const belongings = {};
  3. const ownedUpgrades = {};
  4. const remainingUpgrades = [];
  5. let showOwnedUpgrades = false;
  6. const effects = {};
  7. const resources = {};
  8. let updateRate = 30;
  9. const currentProductivity = {};
  10. const contributions = {};
  11. let clickBonus = 0;
  12. let clickVictim = "micro";
  13. let lastTime = 0;
  14. let controlHeld = false;
  15. let shiftHeld = false;
  16. let mouseTarget = undefined;
  17. const activePowerups = [];
  18. function tickPowerups(delta) {
  19. const powerupList = document.querySelector("#powerup-list");
  20. let changed = false;
  21. // I love mutating arrays as I traverse them.
  22. for (let i = activePowerups.length - 1; i >= 0; i--) {
  23. activePowerups[i].lifetime -= delta;
  24. if (activePowerups[i].lifetime <= 0) {
  25. const entry = activePowerups[i];
  26. setTimeout(() => {
  27. powerupList.removeChild(entry.element);
  28. }, 1000);
  29. entry.element.classList.add("powerup-entry-done");
  30. activePowerups.splice(i, 1);
  31. changed = true;
  32. } else {
  33. const frac = (activePowerups[i].powerup.duration - activePowerups[i].lifetime) / (activePowerups[i].powerup.duration);
  34. activePowerups[i].element.style.setProperty("--progress", frac * 100 + "%")
  35. }
  36. }
  37. if (changed) {
  38. updateAll();
  39. }
  40. }
  41. function addPowerup(powerup) {
  42. const powerupList = document.querySelector("#powerup-list");
  43. const powerupEntry = document.createElement("div");
  44. powerupEntry.classList.add("powerup-entry");
  45. powerupEntry.innerText = powerup.name;
  46. powerupList.appendChild(powerupEntry);
  47. activePowerups.push({ powerup: powerup, lifetime: powerup.duration, element: powerupEntry });
  48. updateAll();
  49. }
  50. function applyGlobalProdBonus(cost) {
  51. for (let effect of effects["prod-all"]) {
  52. if (ownedUpgrades[effect.parent]) {
  53. cost = effect.apply(cost);
  54. }
  55. }
  56. return cost;
  57. }
  58. function calculateProductivity() {
  59. let productivity = makeCost();
  60. for (const [key, value] of Object.entries(belongings)) {
  61. const provided = productivityOf(key);
  62. productivity = addCost(productivity, provided);
  63. contributions[key] = provided;
  64. }
  65. return productivity;
  66. }
  67. // here's where upgrades will go :3
  68. function applyProductivityMultipliers(type, cost) {
  69. for (let effect of effects["prod"]) {
  70. if (ownedUpgrades[effect.parent] && effect.target == type) {
  71. cost = effect.apply(cost);
  72. }
  73. }
  74. for (let effect of effects["helper"]) {
  75. if (ownedUpgrades[effect.parent] && effect.helped == type) {
  76. cost = effect.apply(cost, belongings[effect.helper].count);
  77. }
  78. }
  79. return cost;
  80. }
  81. function productivityOf(type) {
  82. let baseProd = makeCost(buildings[type].prod);
  83. baseProd = applyProductivityMultipliers(type, baseProd);
  84. baseProd = applyGlobalProdBonus(baseProd);
  85. baseProd = scaleCost(baseProd, belongings[type].count);
  86. return baseProd;
  87. }
  88. function makeCost(source) {
  89. const empty = mapObject(resourceTypes, () => 0);
  90. return deepFreeze({...empty, ...source});
  91. }
  92. function addCost(cost1, cost2) {
  93. return deepFreeze(Object.keys(resourceTypes).reduce((o, k) => ({ ...o, [k]: cost1[k] + cost2[k]}), {}));
  94. }
  95. function scaleCost(cost, scale) {
  96. if (typeof(scale) != "number") console.log(scale)
  97. return deepFreeze(Object.keys(resourceTypes).reduce((o, k) => ({ ...o, [k]: cost[k] * scale}), {}));
  98. }
  99. function costOfBuilding(type, count = 1) {
  100. let total = makeCost();
  101. while (count > 0) {
  102. let baseCost = makeCost(buildings[type].cost);
  103. baseCost = scaleCost(baseCost, Math.pow(1.15, belongings[type].count + count - 1));
  104. total = addCost(total, baseCost);
  105. count--;
  106. }
  107. return mapObject(total, round);
  108. }
  109. function buildingCount() {
  110. if (controlHeld) {
  111. return 10;
  112. } else if (shiftHeld) {
  113. return 5;
  114. } else {
  115. return 1;
  116. }
  117. }
  118. function buyBuilding(type, e) {
  119. const count = buildingCount();
  120. let cost = costOfBuilding(type, count);
  121. if (canAfford(cost)) {
  122. spend(cost);
  123. belongings[type].count += count;
  124. }
  125. updateProductivity();
  126. updateClickBonus();
  127. }
  128. function updateAll() {
  129. updateProductivity();
  130. updateClickBonus();
  131. updateClickVictim();
  132. }
  133. // update stuff
  134. function updateDisplay() {
  135. let newTime = performance.now();
  136. let delta = newTime - lastTime;
  137. lastTime = newTime;
  138. addResources(delta);
  139. displayResources();
  140. displayBuildings();
  141. displayUpgrades(showOwnedUpgrades);
  142. tickPowerups(delta);
  143. setTimeout(updateDisplay, 1000 / updateRate);
  144. }
  145. function updateProductivity() {
  146. Object.assign(currentProductivity, calculateProductivity());
  147. activePowerups.forEach(entry => {
  148. const powerup = entry.powerup;
  149. const state = {
  150. ownedUpgrades: ownedUpgrades,
  151. resources: resources,
  152. currentProductivity: currentProductivity,
  153. belongings: belongings
  154. };
  155. powerup.effect(state);
  156. })
  157. }
  158. function addResources(delta) {
  159. for (const [resource, amount] of Object.entries(currentProductivity)) {
  160. resources[resource] += amount * delta / 1000;
  161. }
  162. }
  163. function displayResources() {
  164. document.title = "Gorge - " + round(resources.food) + " food";
  165. replaceChildren(document.querySelector("#resource-list"), renderResources());
  166. }
  167. function renderResources() {
  168. let list = [];
  169. for (const [key, value] of Object.entries(resources)) {
  170. let line1 = render(value, 3, 0) + " " + resourceTypes[key].name;
  171. let line2 = render(currentProductivity[key], 1, 1) + " " + resourceTypes[key].name + "/sec";
  172. list.push({ "text": line1, "class": "resource-quantity" });
  173. list.push({ "text": line2, "class": "resource-rate" });
  174. }
  175. return renderLines(list);
  176. }
  177. function displayBuildings() {
  178. const count = buildingCount();
  179. for (const [key, value] of Object.entries(belongings)) {
  180. let available = states.buildings[key].available;
  181. if (!belongings[key].visible) {
  182. if (resources.food * 10 >= costOfBuilding(key).food) {
  183. unlockBuilding(key);
  184. } if (belongings[key].count > 0) {
  185. unlockBuilding(key);
  186. } else {
  187. continue;
  188. }
  189. belongings[key].visible = true;
  190. let button = cache.buildingButtons[key].button;
  191. button.classList.remove("hidden");
  192. }
  193. let button = cache.buildingButtons[key].button;
  194. let name = cache.buildingButtons[key].name;
  195. let cost = cache.buildingButtons[key].cost;
  196. const buildingCost = costOfBuilding(key, count);
  197. name.innerText = value.count + " " + (value.count == 1 ? buildings[key].name : buildings[key].plural);
  198. cost.innerText = render(buildingCost.food) + " food";
  199. if (canAfford(buildingCost) && available !== true) {
  200. button.classList.remove("building-button-disabled");
  201. cost.classList.add("building-button-cost-valid");
  202. states.buildings[key].available = true;
  203. } else if (!canAfford(buildingCost) && available !== false) {
  204. button.classList.add("building-button-disabled");
  205. cost.classList.add("building-button-cost-invalid");
  206. states.buildings[key].available = false;
  207. }
  208. }
  209. }
  210. function canAfford(cost) {
  211. for (const [resource, amount] of Object.entries(cost)) {
  212. if (resources[resource] < amount) {
  213. return false;
  214. }
  215. }
  216. return true;
  217. }
  218. function spend(cost) {
  219. for (const [resource, amount] of Object.entries(cost)) {
  220. resources[resource] -= amount;
  221. }
  222. }
  223. function switchShowOwnedUpgrades() {
  224. if (showOwnedUpgrades) {
  225. document.querySelector("#upgrades").innerText = "Upgrades";
  226. } else {
  227. document.querySelector("#upgrades").innerText = "Owned Upgrades";
  228. }
  229. showOwnedUpgrades = !showOwnedUpgrades;
  230. }
  231. function displayUpgrades(owned) {
  232. if (owned) {
  233. Object.entries(ownedUpgrades).forEach(([key, val]) => {
  234. let button = cache.upgradeButtons[key];
  235. if (val) {
  236. button.classList.remove("hidden");
  237. } else {
  238. button.classList.add("hidden");
  239. }
  240. });
  241. }
  242. else {
  243. for (let id of remainingUpgrades) {
  244. let button = cache.upgradeButtons[id];
  245. let visible = states.upgrades[id].visible;
  246. let available = states.upgrades[id].available;
  247. if (ownedUpgrades[id] && visible !== false) {
  248. button.classList.add("hidden");
  249. states.upgrades[id].visible = false;
  250. continue;
  251. }
  252. if (upgradeReachable(id) && visible !== true) {
  253. button.classList.remove("hidden");
  254. states.upgrades[id].visible = true;
  255. } else if (!upgradeReachable(id) && visible !== false) {
  256. button.classList.add("hidden");
  257. states.upgrades[id].visible = false;
  258. }
  259. if (upgradeAvailable(id) && available !== true) {
  260. button.classList.remove("upgrade-button-inactive");
  261. states.upgrades[id].available = true;
  262. } else if (!upgradeAvailable(id) && available !== false) {
  263. button.classList.add("upgrade-button-inactive");
  264. states.upgrades[id].available = false;
  265. }
  266. }
  267. // we aren't trimming the list of upgrades now
  268. // because we need to switch between owned and unowned upgrades
  269. // - thus we need to be able to show or hide anything
  270. /*
  271. for (let i = remainingUpgrades.length-1; i >= 0; i--) {
  272. if (ownedUpgrades[remainingUpgrades[i]]) {
  273. remainingUpgrades.splice(i, 1);
  274. }
  275. }*/
  276. }
  277. }
  278. function updateClickBonus() {
  279. let bonus = 0;
  280. for (let effect of effects["click"]) {
  281. if (ownedUpgrades[effect.parent]) {
  282. bonus = effect.apply(bonus, currentProductivity["food"]);
  283. }
  284. }
  285. clickBonus = bonus;
  286. }
  287. function updateClickVictim() {
  288. for (let effect of effects["click-victim"]) {
  289. if (ownedUpgrades[effect.parent]) {
  290. clickVictim = effect.id;
  291. document.querySelector("#tasty-micro").innerText = "Eat " + buildings[effect.id].name;
  292. }
  293. }
  294. }
  295. function buyUpgrade(id, e) {
  296. if (ownedUpgrades[id]) {
  297. return;
  298. }
  299. let upgrade = upgrades[id];
  300. if (!upgradeAvailable(id)) {
  301. return;
  302. }
  303. spend(upgrade.cost);
  304. ownedUpgrades[id] = true;
  305. let text = "Bought " + upgrade.name + "!";
  306. clickPopup(text, "upgrade", [e.clientX, e.clientY]);
  307. updateProductivity();
  308. updateClickBonus();
  309. updateClickVictim();
  310. }
  311. function eatPrey() {
  312. const add = buildings[clickVictim]["prod"].food * 10 + clickBonus;
  313. resources.food += add;
  314. return add;
  315. }
  316. // setup stuff lol
  317. // we'll initialize the dict of buildings we can own
  318. function setup() {
  319. // create static data
  320. createTemplateUpgrades();
  321. // prepare dynamic stuff
  322. initializeData();
  323. createButtons();
  324. createDisplays();
  325. registerListeners();
  326. load();
  327. unlockAtStart();
  328. initializeCaches();
  329. initializeStates();
  330. updateAll();
  331. }
  332. const cache = {};
  333. function initializeCaches() {
  334. const buildingButtons = {};
  335. for (const [key, value] of Object.entries(belongings)) {
  336. let button = document.querySelector("#building-" + key);
  337. let name = document.querySelector("#building-" + key + " > .building-button-name");
  338. let cost = document.querySelector("#building-" + key + " > .building-button-cost");
  339. buildingButtons[key] = {
  340. button: button,
  341. name: name,
  342. cost: cost
  343. }
  344. }
  345. cache.buildingButtons = buildingButtons;
  346. const upgradeButtons = {};
  347. Object.keys(upgrades).forEach(key => {
  348. upgradeButtons[key] = document.querySelector("#upgrade-" + key);
  349. });
  350. cache.upgradeButtons = upgradeButtons;
  351. }
  352. const states = {};
  353. // we can keep track of some things, like whether
  354. // specific upgrades are currently visible. this
  355. // way, we don't have to set them visible every tick;
  356. // we can just check if they've been handled already
  357. function initializeStates() {
  358. const buildingStates = {};
  359. Object.keys(buildings).forEach(key => {
  360. buildingStates[key] = {
  361. visible: undefined,
  362. available: undefined
  363. }
  364. });
  365. states.buildings = buildingStates;
  366. const upgradeStates = {};
  367. Object.keys(upgrades).forEach(key => {
  368. upgradeStates[key] = {
  369. visible: undefined,
  370. available: undefined
  371. }
  372. });
  373. states.upgrades = upgradeStates;
  374. }
  375. function unlockAtStart() {
  376. unlockBuilding("micro");
  377. for (const [key, value] of Object.entries(belongings)) {
  378. if (belongings[key].visible) {
  379. unlockBuilding(key);
  380. }
  381. }
  382. }
  383. function unlockBuilding(id) {
  384. belongings[id].visible = true;
  385. document.querySelector("#building-" + id).classList.remove("hidden");
  386. }
  387. function initializeData() {
  388. for (const [key, value] of Object.entries(buildings)) {
  389. belongings[key] = {};
  390. belongings[key].count = 0;
  391. belongings[key].visible = false;
  392. contributions[key] = makeCost();
  393. }
  394. for (const [key, value] of Object.entries(resourceTypes)) {
  395. resources[key] = 0;
  396. currentProductivity[key] = 0;
  397. }
  398. for (const [id, upgrade] of Object.entries(upgrades)) {
  399. ownedUpgrades[id] = false;
  400. for (let effect of upgrade.effects) {
  401. if (effects[effect.type] === undefined) {
  402. effects[effect.type] = [];
  403. }
  404. // copy the data and add an entry for the upgrade id that owns the effect
  405. let newEffect = {};
  406. for (const [key, value] of Object.entries(effect)) {
  407. newEffect[key] = value;
  408. }
  409. newEffect.parent = id;
  410. // unfortunate name collision here
  411. // I'm using apply() to pass on any number of arguments to the
  412. // apply() function of the effect type
  413. newEffect.apply = function (...args) { return effect_types[effect.type].apply.apply(null, [effect].concat(args)); }
  414. effects[effect.type].push(newEffect);
  415. }
  416. }
  417. }
  418. function registerListeners() {
  419. document.querySelector("#tasty-micro").addEventListener("click", (e) => {
  420. const add = eatPrey();
  421. const text = "+" + round(add, 1) + " food";
  422. const gulp = "*glp*";
  423. clickPopup(text, "food", [e.clientX, e.clientY]);
  424. clickPopup(gulp, "gulp", [e.clientX, e.clientY]);
  425. });
  426. document.querySelector("#save").addEventListener("click", save);
  427. document.querySelector("#reset").addEventListener("click", reset);
  428. document.querySelector("#upgrades").addEventListener("click", switchShowOwnedUpgrades);
  429. document.addEventListener("keydown", e => {
  430. shiftHeld = e.shiftKey;
  431. controlHeld = e.ctrlKey;
  432. if (mouseTarget)
  433. mouseTarget.dispatchEvent(new Event("mousemove"));
  434. return true;
  435. });
  436. document.addEventListener("keyup", e => {
  437. shiftHeld = e.shiftKey;
  438. controlHeld = e.ctrlKey;
  439. if (mouseTarget)
  440. mouseTarget.dispatchEvent(new Event("mousemove"));
  441. return true;
  442. });
  443. }
  444. function createButtons() {
  445. createBuildings();
  446. createUpgrades();
  447. }
  448. function createBuildings() {
  449. let container = document.querySelector("#buildings-list");
  450. for (const [key, value] of Object.entries(buildings)) {
  451. let button = document.createElement("div");
  452. button.classList.add("building-button");
  453. button.classList.add("hidden");
  454. button.id = "building-" + key;
  455. let buttonName = document.createElement("div");
  456. buttonName.classList.add("building-button-name");
  457. let buttonCost = document.createElement("div");
  458. buttonCost.classList.add("building-button-cost");
  459. let buildingIcon = document.createElement("i");
  460. buildingIcon.classList.add("fas");
  461. buildingIcon.classList.add(value.icon);
  462. button.appendChild(buttonName);
  463. button.appendChild(buttonCost);
  464. button.appendChild(buildingIcon);
  465. button.addEventListener("mousemove", function (e) { mouseTarget = button; buildingTooltip(key, e); });
  466. button.addEventListener("mouseleave", function () { mouseTarget = undefined; buildingTooltipRemove(); });
  467. button.addEventListener("click", function (e) { buyBuilding(key, e); });
  468. button.addEventListener("click", function (e) { buildingTooltip(key, e); });
  469. container.appendChild(button);
  470. }
  471. }
  472. // do we have previous techs and at least one of each building?
  473. function upgradeReachable(id) {
  474. if (ownedUpgrades[id]) {
  475. return false;
  476. }
  477. if (upgrades[id].prereqs !== undefined) {
  478. for (const [type, reqs] of Object.entries(upgrades[id].prereqs)) {
  479. if (type == "buildings") {
  480. for (const [building, amount] of Object.entries(reqs)) {
  481. if (belongings[building].count == 0) {
  482. return false;
  483. }
  484. }
  485. }
  486. else if (type == "upgrades") {
  487. for (let upgrade of reqs) {
  488. if (!ownedUpgrades[upgrade]) {
  489. return false;
  490. }
  491. }
  492. }
  493. }
  494. }
  495. return true;
  496. }
  497. function upgradeAvailable(id) {
  498. if (!upgradeReachable(id)) {
  499. return false;
  500. }
  501. if (!canAfford(upgrades[id].cost)) {
  502. return false;
  503. }
  504. if (upgrades[id].prereqs !== undefined) {
  505. for (const [type, reqs] of Object.entries(upgrades[id].prereqs)) {
  506. if (type == "buildings") {
  507. for (const [building, amount] of Object.entries(upgrades[id].prereqs[type])) {
  508. if (belongings[building].count < amount) {
  509. return false;
  510. }
  511. }
  512. } else if (type == "productivity") {
  513. for (const [key, value] of Object.entries(reqs)) {
  514. if (currentProductivity[key] < value) {
  515. return false;
  516. }
  517. }
  518. }
  519. }
  520. }
  521. return true;
  522. }
  523. function createUpgrades() {
  524. let container = document.querySelector("#upgrades-list");
  525. for (const [key, value] of Object.entries(upgrades)) {
  526. remainingUpgrades.push(key);
  527. let button = document.createElement("div");
  528. button.classList.add("upgrade-button");
  529. button.classList.add("hidden");
  530. button.id = "upgrade-" + key;
  531. let buttonName = document.createElement("div");
  532. buttonName.classList.add("upgrade-button-name");
  533. buttonName.innerText = value.name;
  534. let upgradeIcon = document.createElement("i");
  535. upgradeIcon.classList.add("fas");
  536. upgradeIcon.classList.add(value.icon);
  537. button.appendChild(buttonName);
  538. button.appendChild(upgradeIcon);
  539. button.addEventListener("mouseenter", function (e) { mouseTarget = button; upgradeTooltip(key, e); });
  540. button.addEventListener("mousemove", function (e) { mouseTarget = button; upgradeTooltip(key, e); });
  541. button.addEventListener("mouseleave", function () { mouseTarget = undefined; upgradeTooltipRemove(); });
  542. button.addEventListener("click", function (e) { buyUpgrade(key, e); });
  543. container.appendChild(button);
  544. }
  545. }
  546. function createDisplays() {
  547. // nop
  548. }
  549. function renderLine(line) {
  550. let div = document.createElement("div");
  551. div.innerText = line.text;
  552. if (line.valid !== undefined) {
  553. if (line.valid) {
  554. div.classList.add("cost-met");
  555. } else {
  556. div.classList.add("cost-unmet");
  557. }
  558. }
  559. if (line.class !== undefined) {
  560. for (let entry of line.class.split(",")) {
  561. div.classList.add(entry);
  562. }
  563. }
  564. return div;
  565. }
  566. function renderLines(lines) {
  567. let divs = [];
  568. for (let line of lines) {
  569. divs.push(renderLine(line));
  570. }
  571. return divs;
  572. }
  573. function renderCost(cost) {
  574. let list = [];
  575. list.push({
  576. "text": "Cost:"
  577. });
  578. for (const [key, value] of Object.entries(cost)) {
  579. list.push({
  580. "text": render(value, 0) + " " + resourceTypes[key].name,
  581. "valid": resources[key] >= value
  582. });
  583. }
  584. return renderLines(list);
  585. }
  586. function renderPrereqs(prereqs) {
  587. let list = [];
  588. if (prereqs === undefined) {
  589. return renderLines(list);
  590. }
  591. list.push({
  592. "text": "Own:"
  593. });
  594. for (const [key, value] of Object.entries(prereqs)) {
  595. if (key == "buildings") {
  596. for (const [id, amount] of Object.entries(prereqs.buildings)) {
  597. list.push({
  598. "text": buildings[id].name + " x" + render(amount, 0),
  599. "valid": belongings[id].count >= amount
  600. });
  601. }
  602. } else if (key == "productivity") {
  603. for (const [id, amount] of Object.entries(prereqs.productivity)) {
  604. list.push({
  605. "text": render(amount, 0) + " " + resourceTypes[id].name + "/s",
  606. "valid": currentProductivity[id] >= amount
  607. });
  608. }
  609. }
  610. }
  611. return renderLines(list);
  612. }
  613. function renderEffects(effectList) {
  614. let list = [];
  615. for (let effect of effectList) {
  616. list.push({ "text": effect_types[effect.type].desc(effect) });
  617. }
  618. return renderLines(list);
  619. }
  620. function clickPopup(text, type, location) {
  621. const div = document.createElement("div");
  622. div.textContent = text;
  623. div.classList.add("click-popup-" + type);
  624. var direction;
  625. if (type == "food") {
  626. direction = -150;
  627. } else if (type == "gulp") {
  628. direction = -150;
  629. } else if (type == "upgrade") {
  630. direction = -50;
  631. } else if (type == "info") {
  632. direction = 0;
  633. }
  634. direction *= Math.random() * 0.5 + 1;
  635. direction = Math.round(direction) + "px"
  636. div.style.setProperty("--target", direction)
  637. div.style.left = location[0] + "px";
  638. div.style.top = location[1] + "px";
  639. const body = document.querySelector("body");
  640. body.appendChild(div);
  641. setTimeout(() => {
  642. body.removeChild(div);
  643. }, 2000);
  644. }
  645. function doNews() {
  646. const state = {
  647. ownedUpgrades: ownedUpgrades,
  648. resources: resources,
  649. currentProductivity: currentProductivity,
  650. belongings: belongings
  651. };
  652. let options = [];
  653. news.forEach(entry => {
  654. if (entry.condition(state)) {
  655. options = options.concat(entry.lines);
  656. }
  657. });
  658. const choice = Math.floor(Math.random() * options.length);
  659. showNews(options[choice](state));
  660. setTimeout(() => {
  661. doNews();
  662. }, 15000 + Math.random() * 2500);
  663. }
  664. function showNews(text) {
  665. const div = document.createElement("div");
  666. div.textContent = text;
  667. div.classList.add("news-text");
  668. const body = document.querySelector("body");
  669. body.appendChild(div);
  670. setTimeout(() => {
  671. body.removeChild(div);
  672. }, 10000);
  673. }
  674. function doPowerup() {
  675. const lifetime = 10000;
  676. const button = document.createElement("div");
  677. const left = Math.round(Math.random() * 50 + 25) + "%";
  678. const top = Math.round(Math.random() * 50 + 25) + "%";
  679. button.classList.add("powerup");
  680. button.style.setProperty("--lifetime", lifetime / 1000 + "s");
  681. button.style.setProperty("--leftpos", left);
  682. button.style.setProperty("--toppos", top);
  683. const body = document.querySelector("body");
  684. body.appendChild(button);
  685. const choices = [];
  686. Object.entries(powerups).forEach(([key, val]) => {
  687. choices.push(key);
  688. });
  689. const choice = Math.floor(Math.random() * choices.length);
  690. const powerup = powerups[choices[choice]];
  691. const icon = document.createElement("div");
  692. icon.classList.add("fas");
  693. icon.classList.add(powerup.icon);
  694. button.appendChild(icon);
  695. const remove = setTimeout(() => {
  696. body.removeChild(button);
  697. }, lifetime);
  698. let delay = 60000 + Math.random() * 30000;
  699. for (let effect of effects["powerup-freq"]) {
  700. if (ownedUpgrades[effect.parent]) {
  701. delay = effect.apply(delay);
  702. }
  703. }
  704. setTimeout(() => {
  705. doPowerup();
  706. }, delay);
  707. const state = {
  708. ownedUpgrades: ownedUpgrades,
  709. resources: resources,
  710. currentProductivity: currentProductivity,
  711. belongings: belongings
  712. };
  713. button.addEventListener("mousedown", e => {
  714. if (powerup.duration !== undefined) {
  715. addPowerup(powerup);
  716. } else {
  717. powerup.effect(state);
  718. }
  719. powerup.popup(powerup, e);
  720. button.classList.add("powerup-clicked");
  721. clearTimeout(remove);
  722. setTimeout(() => {
  723. body.removeChild(button);
  724. }, 500);
  725. });
  726. }
  727. function fillTooltip(type, field, content) {
  728. let item = document.querySelector("#" + type + "-tooltip-" + field);
  729. if (typeof (content) === "string") {
  730. item.innerText = content;
  731. } else {
  732. replaceChildren(item, content);
  733. }
  734. }
  735. function upgradeTooltip(id, event) {
  736. let tooltip = document.querySelector("#upgrade-tooltip");
  737. tooltip.style.setProperty("display", "inline-block");
  738. fillTooltip("upgrade", "name", upgrades[id].name);
  739. fillTooltip("upgrade", "desc", upgrades[id].desc);
  740. fillTooltip("upgrade", "effect", renderEffects(upgrades[id].effects));
  741. fillTooltip("upgrade", "cost", renderCost(upgrades[id].cost));
  742. fillTooltip("upgrade", "prereqs", renderPrereqs(upgrades[id].prereqs));
  743. let yOffset = tooltip.parentElement.getBoundingClientRect().y;
  744. let tooltipSize = tooltip.getBoundingClientRect().height;
  745. let yTrans = Math.round(event.clientY - yOffset);
  746. var body = document.body,
  747. html = document.documentElement;
  748. var height = Math.max(window.innerHeight);
  749. yTrans = Math.min(yTrans, height - tooltipSize - 150);
  750. tooltip.style.setProperty("transform", "translate(-220px, " + yTrans + "px)");
  751. }
  752. function upgradeTooltipRemove() {
  753. let tooltip = document.querySelector("#upgrade-tooltip");
  754. tooltip.style.setProperty("display", "none");
  755. }
  756. function prodSummary(id) {
  757. let list = [];
  758. list.push(
  759. { "text": "Each " + buildings[id].name + " produces " + contributions[id].food + " food/sec" }
  760. );
  761. list.push(
  762. { "text": "Your " + render(belongings[id].count) + " " + (belongings[id].count == 1 ? buildings[id].name + " is" : buildings[id].plural + " are") + " producing " + round(productivityOf(id).food, 1) + " food/sec" }
  763. );
  764. let percentage = round(100 * productivityOf(id).food / currentProductivity["food"], 2);
  765. if (isNaN(percentage)) {
  766. percentage = 0;
  767. }
  768. list.push(
  769. { "text": "(" + percentage + "% of all food)" }
  770. );
  771. return renderLines(list);
  772. }
  773. function buildingTooltip(id, event) {
  774. let tooltip = document.querySelector("#building-tooltip");
  775. tooltip.style.setProperty("display", "inline-block");
  776. const count = buildingCount();
  777. fillTooltip("building", "name", (count != 1 ? count + "x " : "") + buildings[id].name);
  778. fillTooltip("building", "desc", buildings[id].desc);
  779. fillTooltip("building", "cost", render(costOfBuilding(id, count).food) + " food");
  780. fillTooltip("building", "prod", prodSummary(id));
  781. let xPos = tooltip.parentElement.getBoundingClientRect().x - 450;
  782. // wow browsers are bad
  783. var body = document.body,
  784. html = document.documentElement;
  785. var height = Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight);
  786. let yPos = Math.min(event.clientY, height - 200);
  787. tooltip.style.setProperty("transform", "translate(" + xPos + "px, " + yPos + "px)")
  788. }
  789. function buildingTooltipRemove() {
  790. let tooltip = document.querySelector("#building-tooltip");
  791. tooltip.style.setProperty("display", "none");
  792. }
  793. window.onload = function () {
  794. setup();
  795. lastTime = performance.now();
  796. doNews();
  797. doPowerup();
  798. setTimeout(updateDisplay, 1000 / updateRate);
  799. setTimeout(autosave, 60000);
  800. }
  801. function autosave() {
  802. saveGame();
  803. let x = window.innerWidth / 2;
  804. let y = window.innerHeight * 9 / 10;
  805. clickPopup("Autosaving...", "info", [x, y]);
  806. setTimeout(autosave, 60000);
  807. }
  808. function save(e) {
  809. saveGame();
  810. clickPopup("Saved!", "info", [e.clientX, e.clientY]);
  811. }
  812. function saveGame() {
  813. try {
  814. let storage = window.localStorage;
  815. storage.setItem("save-version", 1);
  816. storage.setItem("ownedUpgrades", JSON.stringify(ownedUpgrades));
  817. storage.setItem("resources", JSON.stringify(resources));
  818. storage.setItem("belongings", JSON.stringify(belongings));
  819. } catch (e) {
  820. clickPopup("Can't save - no access to local storage.", "info", [window.innerWidth / 2, window.innerHeight / 5]);
  821. }
  822. }
  823. function load() {
  824. try {
  825. let storage = window.localStorage;
  826. if (!storage.getItem("save-version")) {
  827. return;
  828. }
  829. let newOwnedUpgrades = JSON.parse(storage.getItem("ownedUpgrades"));
  830. for (const [key, value] of Object.entries(newOwnedUpgrades)) {
  831. ownedUpgrades[key] = value;
  832. }
  833. let newResources = JSON.parse(storage.getItem("resources"));
  834. for (const [key, value] of Object.entries(newResources)) {
  835. resources[key] = value;
  836. }
  837. let newBelongings = JSON.parse(storage.getItem("belongings"));
  838. for (const [key, value] of Object.entries(newBelongings)) {
  839. belongings[key] = value;
  840. }
  841. } catch (e) {
  842. clickPopup("Can't load - no access to local storage.", "info", [window.innerWidth / 2, window.innerHeight / 5]);
  843. }
  844. }
  845. function reset() {
  846. window.localStorage.clear();
  847. }