cookie clicker but bigger
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

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