cookie clicker but bigger
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

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