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ů.
 
 
 
 

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