cookie clicker but bigger
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

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