cookie clicker but bigger
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

1079 satır
26 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. const buildingButtons = {}
  178. function displayBuildings() {
  179. const count = buildingCount();
  180. for (const [key, value] of Object.entries(belongings)) {
  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 = buildingButtons[key].button;
  191. button.classList.remove("hidden");
  192. }
  193. let button = buildingButtons[key].button;
  194. let name = buildingButtons[key].name;
  195. let cost = 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) && button.classList.contains("building-button-disabled")) {
  200. button.classList.remove("building-button-disabled");
  201. cost.classList.add("building-button-cost-valid");
  202. } else if (!canAfford(buildingCost) && !button.classList.contains("building-button-disabled")) {
  203. button.classList.add("building-button-disabled");
  204. cost.classList.add("building-button-cost-invalid");
  205. }
  206. }
  207. }
  208. function canAfford(cost) {
  209. for (const [resource, amount] of Object.entries(cost)) {
  210. if (resources[resource] < amount) {
  211. return false;
  212. }
  213. }
  214. return true;
  215. }
  216. function spend(cost) {
  217. for (const [resource, amount] of Object.entries(cost)) {
  218. resources[resource] -= amount;
  219. }
  220. }
  221. function switchShowOwnedUpgrades() {
  222. if (showOwnedUpgrades) {
  223. document.querySelector("#upgrades").innerText = "Upgrades";
  224. } else {
  225. document.querySelector("#upgrades").innerText = "Owned Upgrades";
  226. }
  227. showOwnedUpgrades = !showOwnedUpgrades;
  228. }
  229. function displayUpgrades(owned) {
  230. if (owned) {
  231. Object.entries(ownedUpgrades).forEach(([key, val]) => {
  232. let button = document.querySelector("#upgrade-" + key);
  233. if (val) {
  234. button.classList.remove("hidden");
  235. } else {
  236. button.classList.add("hidden");
  237. }
  238. });
  239. }
  240. else {
  241. for (let id of remainingUpgrades) {
  242. let button = document.querySelector("#upgrade-" + id);
  243. if (ownedUpgrades[id]) {
  244. button.classList.add("hidden");
  245. continue;
  246. }
  247. if (upgradeReachable(id)) {
  248. button.classList.remove("hidden");
  249. } else {
  250. button.classList.add("hidden");
  251. }
  252. if (upgradeAvailable(id)) {
  253. button.classList.remove("upgrade-button-inactive");
  254. } else {
  255. button.classList.add("upgrade-button-inactive");
  256. }
  257. }
  258. // we aren't trimming the list of upgrades now
  259. // because we need to switch between owned and unowned upgrades
  260. // - thus we need to be able to show or hide anything
  261. /*
  262. for (let i = remainingUpgrades.length-1; i >= 0; i--) {
  263. if (ownedUpgrades[remainingUpgrades[i]]) {
  264. remainingUpgrades.splice(i, 1);
  265. }
  266. }*/
  267. }
  268. }
  269. function updateClickBonus() {
  270. let bonus = 0;
  271. for (let effect of effects["click"]) {
  272. if (ownedUpgrades[effect.parent]) {
  273. bonus = effect.apply(bonus, currentProductivity["food"]);
  274. }
  275. }
  276. clickBonus = bonus;
  277. }
  278. function updateClickVictim() {
  279. for (let effect of effects["click-victim"]) {
  280. if (ownedUpgrades[effect.parent]) {
  281. clickVictim = effect.id;
  282. document.querySelector("#tasty-micro").innerText = "Eat " + buildings[effect.id].name;
  283. }
  284. }
  285. }
  286. function buyUpgrade(id, e) {
  287. if (ownedUpgrades[id]) {
  288. return;
  289. }
  290. let upgrade = upgrades[id];
  291. if (!upgradeAvailable(id)) {
  292. return;
  293. }
  294. spend(upgrade.cost);
  295. ownedUpgrades[id] = true;
  296. let text = "Bought " + upgrade.name + "!";
  297. clickPopup(text, "upgrade", [e.clientX, e.clientY]);
  298. updateProductivity();
  299. updateClickBonus();
  300. updateClickVictim();
  301. }
  302. function eatPrey() {
  303. const add = buildings[clickVictim]["prod"].food * 10 + clickBonus;
  304. resources.food += add;
  305. return add;
  306. }
  307. // setup stuff lol
  308. // we'll initialize the dict of buildings we can own
  309. function setup() {
  310. // create static data
  311. createTemplateUpgrades();
  312. // prepare dynamic stuff
  313. initializeData();
  314. createButtons();
  315. createDisplays();
  316. registerListeners();
  317. load();
  318. unlockAtStart();
  319. initializeCaches();
  320. updateAll();
  321. }
  322. function initializeCaches() {
  323. for (const [key, value] of Object.entries(belongings)) {
  324. let button = document.querySelector("#building-" + key);
  325. let name = document.querySelector("#building-" + key + " > .building-button-name");
  326. let cost = document.querySelector("#building-" + key + " > .building-button-cost");
  327. buildingButtons[key] = {
  328. button: button,
  329. name: name,
  330. cost: cost
  331. }
  332. }
  333. }
  334. function unlockAtStart() {
  335. unlockBuilding("micro");
  336. for (const [key, value] of Object.entries(belongings)) {
  337. if (belongings[key].visible) {
  338. unlockBuilding(key);
  339. }
  340. }
  341. }
  342. function unlockBuilding(id) {
  343. belongings[id].visible = true;
  344. document.querySelector("#building-" + id).classList.remove("hidden");
  345. }
  346. function initializeData() {
  347. for (const [key, value] of Object.entries(buildings)) {
  348. belongings[key] = {};
  349. belongings[key].count = 0;
  350. belongings[key].visible = false;
  351. contributions[key] = makeCost();
  352. }
  353. for (const [key, value] of Object.entries(resourceTypes)) {
  354. resources[key] = 0;
  355. currentProductivity[key] = 0;
  356. }
  357. for (const [id, upgrade] of Object.entries(upgrades)) {
  358. ownedUpgrades[id] = false;
  359. for (let effect of upgrade.effects) {
  360. if (effects[effect.type] === undefined) {
  361. effects[effect.type] = [];
  362. }
  363. // copy the data and add an entry for the upgrade id that owns the effect
  364. let newEffect = {};
  365. for (const [key, value] of Object.entries(effect)) {
  366. newEffect[key] = value;
  367. }
  368. newEffect.parent = id;
  369. // unfortunate name collision here
  370. // I'm using apply() to pass on any number of arguments to the
  371. // apply() function of the effect type
  372. newEffect.apply = function (...args) { return effect_types[effect.type].apply.apply(null, [effect].concat(args)); }
  373. effects[effect.type].push(newEffect);
  374. }
  375. }
  376. }
  377. function registerListeners() {
  378. document.querySelector("#tasty-micro").addEventListener("click", (e) => {
  379. const add = eatPrey();
  380. const text = "+" + round(add, 1) + " food";
  381. const gulp = "*glp*";
  382. clickPopup(text, "food", [e.clientX, e.clientY]);
  383. clickPopup(gulp, "gulp", [e.clientX, e.clientY]);
  384. });
  385. document.querySelector("#save").addEventListener("click", save);
  386. document.querySelector("#reset").addEventListener("click", reset);
  387. document.querySelector("#upgrades").addEventListener("click", switchShowOwnedUpgrades);
  388. document.addEventListener("keydown", e => {
  389. shiftHeld = e.shiftKey;
  390. controlHeld = e.ctrlKey;
  391. if (mouseTarget)
  392. mouseTarget.dispatchEvent(new Event("mousemove"));
  393. return true;
  394. });
  395. document.addEventListener("keyup", e => {
  396. shiftHeld = e.shiftKey;
  397. controlHeld = e.ctrlKey;
  398. if (mouseTarget)
  399. mouseTarget.dispatchEvent(new Event("mousemove"));
  400. return true;
  401. });
  402. }
  403. function createButtons() {
  404. createBuildings();
  405. createUpgrades();
  406. }
  407. function createBuildings() {
  408. let container = document.querySelector("#buildings-list");
  409. for (const [key, value] of Object.entries(buildings)) {
  410. let button = document.createElement("div");
  411. button.classList.add("building-button");
  412. button.classList.add("hidden");
  413. button.id = "building-" + key;
  414. let buttonName = document.createElement("div");
  415. buttonName.classList.add("building-button-name");
  416. let buttonCost = document.createElement("div");
  417. buttonCost.classList.add("building-button-cost");
  418. let buildingIcon = document.createElement("i");
  419. buildingIcon.classList.add("fas");
  420. buildingIcon.classList.add(value.icon);
  421. button.appendChild(buttonName);
  422. button.appendChild(buttonCost);
  423. button.appendChild(buildingIcon);
  424. button.addEventListener("mousemove", function (e) { mouseTarget = button; buildingTooltip(key, e); });
  425. button.addEventListener("mouseleave", function () { mouseTarget = undefined; buildingTooltipRemove(); });
  426. button.addEventListener("click", function (e) { buyBuilding(key, e); });
  427. button.addEventListener("click", function (e) { buildingTooltip(key, e); });
  428. container.appendChild(button);
  429. }
  430. }
  431. // do we have previous techs and at least one of each building?
  432. function upgradeReachable(id) {
  433. if (ownedUpgrades[id]) {
  434. return false;
  435. }
  436. if (upgrades[id].prereqs !== undefined) {
  437. for (const [type, reqs] of Object.entries(upgrades[id].prereqs)) {
  438. if (type == "buildings") {
  439. for (const [building, amount] of Object.entries(reqs)) {
  440. if (belongings[building].count == 0) {
  441. return false;
  442. }
  443. }
  444. }
  445. else if (type == "upgrades") {
  446. for (let upgrade of reqs) {
  447. if (!ownedUpgrades[upgrade]) {
  448. return false;
  449. }
  450. }
  451. }
  452. }
  453. }
  454. return true;
  455. }
  456. function upgradeAvailable(id) {
  457. if (!upgradeReachable(id)) {
  458. return false;
  459. }
  460. if (!canAfford(upgrades[id].cost)) {
  461. return false;
  462. }
  463. if (upgrades[id].prereqs !== undefined) {
  464. for (const [type, reqs] of Object.entries(upgrades[id].prereqs)) {
  465. if (type == "buildings") {
  466. for (const [building, amount] of Object.entries(upgrades[id].prereqs[type])) {
  467. if (belongings[building].count < amount) {
  468. return false;
  469. }
  470. }
  471. } else if (type == "productivity") {
  472. for (const [key, value] of Object.entries(reqs)) {
  473. if (currentProductivity[key] < value) {
  474. return false;
  475. }
  476. }
  477. }
  478. }
  479. }
  480. return true;
  481. }
  482. function createUpgrades() {
  483. let container = document.querySelector("#upgrades-list");
  484. for (const [key, value] of Object.entries(upgrades)) {
  485. remainingUpgrades.push(key);
  486. let button = document.createElement("div");
  487. button.classList.add("upgrade-button");
  488. button.classList.add("hidden");
  489. button.id = "upgrade-" + key;
  490. let buttonName = document.createElement("div");
  491. buttonName.classList.add("upgrade-button-name");
  492. buttonName.innerText = value.name;
  493. let upgradeIcon = document.createElement("i");
  494. upgradeIcon.classList.add("fas");
  495. upgradeIcon.classList.add(value.icon);
  496. button.appendChild(buttonName);
  497. button.appendChild(upgradeIcon);
  498. button.addEventListener("mouseenter", function (e) { mouseTarget = button; upgradeTooltip(key, e); });
  499. button.addEventListener("mousemove", function (e) { mouseTarget = button; upgradeTooltip(key, e); });
  500. button.addEventListener("mouseleave", function () { mouseTarget = undefined; upgradeTooltipRemove(); });
  501. button.addEventListener("click", function (e) { buyUpgrade(key, e); });
  502. container.appendChild(button);
  503. }
  504. }
  505. function createDisplays() {
  506. // nop
  507. }
  508. function renderLine(line) {
  509. let div = document.createElement("div");
  510. div.innerText = line.text;
  511. if (line.valid !== undefined) {
  512. if (line.valid) {
  513. div.classList.add("cost-met");
  514. } else {
  515. div.classList.add("cost-unmet");
  516. }
  517. }
  518. if (line.class !== undefined) {
  519. for (let entry of line.class.split(",")) {
  520. div.classList.add(entry);
  521. }
  522. }
  523. return div;
  524. }
  525. function renderLines(lines) {
  526. let divs = [];
  527. for (let line of lines) {
  528. divs.push(renderLine(line));
  529. }
  530. return divs;
  531. }
  532. function renderCost(cost) {
  533. let list = [];
  534. list.push({
  535. "text": "Cost:"
  536. });
  537. for (const [key, value] of Object.entries(cost)) {
  538. list.push({
  539. "text": render(value, 0) + " " + resourceTypes[key].name,
  540. "valid": resources[key] >= value
  541. });
  542. }
  543. return renderLines(list);
  544. }
  545. function renderPrereqs(prereqs) {
  546. let list = [];
  547. if (prereqs === undefined) {
  548. return renderLines(list);
  549. }
  550. list.push({
  551. "text": "Own:"
  552. });
  553. for (const [key, value] of Object.entries(prereqs)) {
  554. if (key == "buildings") {
  555. for (const [id, amount] of Object.entries(prereqs.buildings)) {
  556. list.push({
  557. "text": buildings[id].name + " x" + render(amount, 0),
  558. "valid": belongings[id].count >= amount
  559. });
  560. }
  561. } else if (key == "productivity") {
  562. for (const [id, amount] of Object.entries(prereqs.productivity)) {
  563. list.push({
  564. "text": render(amount, 0) + " " + resourceTypes[id].name + "/s",
  565. "valid": currentProductivity[id] >= amount
  566. });
  567. }
  568. }
  569. }
  570. return renderLines(list);
  571. }
  572. function renderEffects(effectList) {
  573. let list = [];
  574. for (let effect of effectList) {
  575. list.push({ "text": effect_types[effect.type].desc(effect) });
  576. }
  577. return renderLines(list);
  578. }
  579. function clickPopup(text, type, location) {
  580. const div = document.createElement("div");
  581. div.textContent = text;
  582. div.classList.add("click-popup-" + type);
  583. var direction;
  584. if (type == "food") {
  585. direction = -150;
  586. } else if (type == "gulp") {
  587. direction = -150;
  588. } else if (type == "upgrade") {
  589. direction = -50;
  590. } else if (type == "info") {
  591. direction = 0;
  592. }
  593. direction *= Math.random() * 0.5 + 1;
  594. direction = Math.round(direction) + "px"
  595. div.style.setProperty("--target", direction)
  596. div.style.left = location[0] + "px";
  597. div.style.top = location[1] + "px";
  598. const body = document.querySelector("body");
  599. body.appendChild(div);
  600. setTimeout(() => {
  601. body.removeChild(div);
  602. }, 2000);
  603. }
  604. function doNews() {
  605. const state = {
  606. ownedUpgrades: ownedUpgrades,
  607. resources: resources,
  608. currentProductivity: currentProductivity,
  609. belongings: belongings
  610. };
  611. let options = [];
  612. news.forEach(entry => {
  613. if (entry.condition(state)) {
  614. options = options.concat(entry.lines);
  615. }
  616. });
  617. const choice = Math.floor(Math.random() * options.length);
  618. showNews(options[choice](state));
  619. setTimeout(() => {
  620. doNews();
  621. }, 15000 + Math.random() * 2500);
  622. }
  623. function showNews(text) {
  624. const div = document.createElement("div");
  625. div.textContent = text;
  626. div.classList.add("news-text");
  627. const body = document.querySelector("body");
  628. body.appendChild(div);
  629. setTimeout(() => {
  630. body.removeChild(div);
  631. }, 10000);
  632. }
  633. function doPowerup() {
  634. const lifetime = 10000;
  635. const button = document.createElement("div");
  636. const left = Math.round(Math.random() * 50 + 25) + "%";
  637. const top = Math.round(Math.random() * 50 + 25) + "%";
  638. button.classList.add("powerup");
  639. button.style.setProperty("--lifetime", lifetime / 1000 + "s");
  640. button.style.setProperty("--leftpos", left);
  641. button.style.setProperty("--toppos", top);
  642. const body = document.querySelector("body");
  643. body.appendChild(button);
  644. const choices = [];
  645. Object.entries(powerups).forEach(([key, val]) => {
  646. choices.push(key);
  647. });
  648. const choice = Math.floor(Math.random() * choices.length);
  649. const powerup = powerups[choices[choice]];
  650. const icon = document.createElement("div");
  651. icon.classList.add("fas");
  652. icon.classList.add(powerup.icon);
  653. button.appendChild(icon);
  654. const remove = setTimeout(() => {
  655. body.removeChild(button);
  656. }, lifetime);
  657. let delay = 60000 + Math.random() * 30000;
  658. for (let effect of effects["powerup-freq"]) {
  659. if (ownedUpgrades[effect.parent]) {
  660. delay = effect.apply(delay);
  661. }
  662. }
  663. setTimeout(() => {
  664. doPowerup();
  665. }, delay);
  666. const state = {
  667. ownedUpgrades: ownedUpgrades,
  668. resources: resources,
  669. currentProductivity: currentProductivity,
  670. belongings: belongings
  671. };
  672. button.addEventListener("mousedown", e => {
  673. if (powerup.duration !== undefined) {
  674. addPowerup(powerup);
  675. } else {
  676. powerup.effect(state);
  677. }
  678. powerup.popup(powerup, e);
  679. button.classList.add("powerup-clicked");
  680. clearTimeout(remove);
  681. setTimeout(() => {
  682. body.removeChild(button);
  683. }, 500);
  684. });
  685. }
  686. function fillTooltip(type, field, content) {
  687. let item = document.querySelector("#" + type + "-tooltip-" + field);
  688. if (typeof (content) === "string") {
  689. item.innerText = content;
  690. } else {
  691. replaceChildren(item, content);
  692. }
  693. }
  694. function upgradeTooltip(id, event) {
  695. let tooltip = document.querySelector("#upgrade-tooltip");
  696. tooltip.style.setProperty("display", "inline-block");
  697. fillTooltip("upgrade", "name", upgrades[id].name);
  698. fillTooltip("upgrade", "desc", upgrades[id].desc);
  699. fillTooltip("upgrade", "effect", renderEffects(upgrades[id].effects));
  700. fillTooltip("upgrade", "cost", renderCost(upgrades[id].cost));
  701. fillTooltip("upgrade", "prereqs", renderPrereqs(upgrades[id].prereqs));
  702. let yOffset = tooltip.parentElement.getBoundingClientRect().y;
  703. let tooltipSize = tooltip.getBoundingClientRect().height;
  704. let yTrans = Math.round(event.clientY - yOffset);
  705. var body = document.body,
  706. html = document.documentElement;
  707. var height = Math.max(window.innerHeight);
  708. yTrans = Math.min(yTrans, height - tooltipSize - 150);
  709. tooltip.style.setProperty("transform", "translate(-220px, " + yTrans + "px)");
  710. }
  711. function upgradeTooltipRemove() {
  712. let tooltip = document.querySelector("#upgrade-tooltip");
  713. tooltip.style.setProperty("display", "none");
  714. }
  715. function prodSummary(id) {
  716. let list = [];
  717. list.push(
  718. { "text": "Each " + buildings[id].name + " produces " + contributions[id].food + " food/sec" }
  719. );
  720. list.push(
  721. { "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" }
  722. );
  723. let percentage = round(100 * productivityOf(id).food / currentProductivity["food"], 2);
  724. if (isNaN(percentage)) {
  725. percentage = 0;
  726. }
  727. list.push(
  728. { "text": "(" + percentage + "% of all food)" }
  729. );
  730. return renderLines(list);
  731. }
  732. function buildingTooltip(id, event) {
  733. let tooltip = document.querySelector("#building-tooltip");
  734. tooltip.style.setProperty("display", "inline-block");
  735. const count = buildingCount();
  736. fillTooltip("building", "name", (count != 1 ? count + "x " : "") + buildings[id].name);
  737. fillTooltip("building", "desc", buildings[id].desc);
  738. fillTooltip("building", "cost", render(costOfBuilding(id, count).food) + " food");
  739. fillTooltip("building", "prod", prodSummary(id));
  740. let xPos = tooltip.parentElement.getBoundingClientRect().x - 450;
  741. // wow browsers are bad
  742. var body = document.body,
  743. html = document.documentElement;
  744. var height = Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight);
  745. let yPos = Math.min(event.clientY, height - 200);
  746. tooltip.style.setProperty("transform", "translate(" + xPos + "px, " + yPos + "px)")
  747. }
  748. function buildingTooltipRemove() {
  749. let tooltip = document.querySelector("#building-tooltip");
  750. tooltip.style.setProperty("display", "none");
  751. }
  752. window.onload = function () {
  753. setup();
  754. lastTime = performance.now();
  755. doNews();
  756. doPowerup();
  757. setTimeout(updateDisplay, 1000 / updateRate);
  758. setTimeout(autosave, 60000);
  759. }
  760. function autosave() {
  761. saveGame();
  762. let x = window.innerWidth / 2;
  763. let y = window.innerHeight * 9 / 10;
  764. clickPopup("Autosaving...", "info", [x, y]);
  765. setTimeout(autosave, 60000);
  766. }
  767. function save(e) {
  768. saveGame();
  769. clickPopup("Saved!", "info", [e.clientX, e.clientY]);
  770. }
  771. function saveGame() {
  772. try {
  773. let storage = window.localStorage;
  774. storage.setItem("save-version", 1);
  775. storage.setItem("ownedUpgrades", JSON.stringify(ownedUpgrades));
  776. storage.setItem("resources", JSON.stringify(resources));
  777. storage.setItem("belongings", JSON.stringify(belongings));
  778. } catch (e) {
  779. clickPopup("Can't save - no access to local storage.", "info", [window.innerWidth / 2, window.innerHeight / 5]);
  780. }
  781. }
  782. function load() {
  783. try {
  784. let storage = window.localStorage;
  785. if (!storage.getItem("save-version")) {
  786. return;
  787. }
  788. let newOwnedUpgrades = JSON.parse(storage.getItem("ownedUpgrades"));
  789. for (const [key, value] of Object.entries(newOwnedUpgrades)) {
  790. ownedUpgrades[key] = value;
  791. }
  792. let newResources = JSON.parse(storage.getItem("resources"));
  793. for (const [key, value] of Object.entries(newResources)) {
  794. resources[key] = value;
  795. }
  796. let newBelongings = JSON.parse(storage.getItem("belongings"));
  797. for (const [key, value] of Object.entries(newBelongings)) {
  798. belongings[key] = value;
  799. }
  800. } catch (e) {
  801. clickPopup("Can't load - no access to local storage.", "info", [window.innerWidth / 2, window.innerHeight / 5]);
  802. }
  803. }
  804. function reset() {
  805. window.localStorage.clear();
  806. }