cookie clicker but bigger
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

799 linhas
19 KiB

  1. "use strict";
  2. let belongings = {};
  3. let ownedUpgrades = {};
  4. let remainingUpgrades = [];
  5. let showOwnedUpgrades = false;
  6. let effects = {};
  7. let resources = {};
  8. let updateRate = 60;
  9. let currentProductivity = {};
  10. let clickBonus = 0;
  11. let clickVictim = "micro";
  12. let lastTime = 0;
  13. function applyGlobalProdBonuses(productivity) {
  14. for (let effect of effects["prod-all"]) {
  15. if (ownedUpgrades[effect.parent]) {
  16. productivity = effect.apply(productivity);
  17. }
  18. }
  19. return productivity;
  20. }
  21. function calculateProductivity() {
  22. let productivity = 0;
  23. for (const [key, value] of Object.entries(belongings)) {
  24. productivity += productivityOf(key);
  25. }
  26. return productivity;
  27. }
  28. // here's where upgrades will go :3
  29. function productivityMultiplierOf(type) {
  30. let base = 1;
  31. for (let effect of effects["prod"]) {
  32. if (ownedUpgrades[effect.parent] && effect.target == type) {
  33. base = effect.apply(base);
  34. }
  35. }
  36. for (let effect of effects["helper"]) {
  37. if (ownedUpgrades[effect.parent] && effect.helped == type) {
  38. base = effect.apply(base, belongings[effect.helper].count);
  39. }
  40. }
  41. return base;
  42. }
  43. function productivityOf(type) {
  44. let baseProd = buildings[type].prod;
  45. let prod = baseProd * productivityMultiplierOf(type);
  46. prod = applyGlobalProdBonuses(prod);
  47. return prod * belongings[type].count;
  48. }
  49. function costOfBuilding(type) {
  50. let baseCost = buildings[type].cost
  51. let countCost = baseCost * Math.pow(1.15, belongings[type].count);
  52. return Math.round(countCost);
  53. }
  54. function buyBuilding(type) {
  55. let cost = costOfBuilding(type);
  56. if (resources.food >= cost) {
  57. belongings[type].count += 1;
  58. resources.food -= cost;
  59. }
  60. updateProductivity();
  61. updateClickBonus();
  62. }
  63. // update stuff
  64. function updateDisplay() {
  65. let newTime = performance.now();
  66. let delta = newTime - lastTime;
  67. lastTime = newTime;
  68. addResources(delta);
  69. displayResources();
  70. displayBuildings();
  71. displayUpgrades(showOwnedUpgrades);
  72. setTimeout(updateDisplay, 1000/updateRate);
  73. }
  74. function updateProductivity() {
  75. currentProductivity["food"] = calculateProductivity();
  76. }
  77. function addResources(delta) {
  78. for (const [resource, amount] of Object.entries(currentProductivity)) {
  79. resources[resource] += amount * delta / 1000;
  80. }
  81. }
  82. function displayResources() {
  83. document.title = "Gorge - " + round(resources.food) + " food";
  84. replaceChildren(document.querySelector("#resource-list"), renderResources());
  85. }
  86. function renderResources() {
  87. let list = [];
  88. for (const [key, value] of Object.entries(resources)) {
  89. let line1 = render(value, 3, 0) + " " + resourceTypes[key].name;
  90. let line2 = render(currentProductivity[key], 1, 1) + " " + resourceTypes[key].name + "/sec";
  91. list.push({"text": line1, "class": "resource-quantity"});
  92. list.push({"text": line2, "class": "resource-rate"});
  93. }
  94. return renderLines(list);
  95. }
  96. function displayBuildings() {
  97. for (const [key, value] of Object.entries(belongings)) {
  98. if (!belongings[key].visible) {
  99. if (resources.food * 10 >= costOfBuilding(key)) {
  100. unlockBuilding(key);
  101. } else {
  102. continue;
  103. }
  104. belongings[key].visible = true;
  105. document.querySelector("#building-" + key).classList.remove("hidden");
  106. }
  107. let button = document.querySelector("#building-" + key);
  108. let name = document.querySelector("#building-" + key + " > .building-button-name");
  109. let cost = document.querySelector("#building-" + key + " > .building-button-cost");
  110. name.innerText = value.count + " " + (value.count == 1 ? buildings[key].name : buildings[key].plural);
  111. cost.innerText = render(costOfBuilding(key)) + " food";
  112. if (costOfBuilding(key) > resources.food) {
  113. button.classList.add("building-button-disabled");
  114. cost.classList.add("building-button-cost-invalid");
  115. } else {
  116. button.classList.remove("building-button-disabled");
  117. cost.classList.add("building-button-cost-valid");
  118. }
  119. }
  120. }
  121. function canAfford(cost) {
  122. for (const [resource, amount] of Object.entries(cost)) {
  123. if (resources[resource] < amount) {
  124. return false;
  125. }
  126. }
  127. return true;
  128. }
  129. function spend(cost) {
  130. for (const [resource, amount] of Object.entries(cost)) {
  131. resources[resource] -= amount;
  132. }
  133. }
  134. function switchShowOwnedUpgrades() {
  135. if (showOwnedUpgrades) {
  136. document.querySelector("#upgrades").innerText = "Upgrades";
  137. } else {
  138. document.querySelector("#upgrades").innerText = "Owned Upgrades";
  139. }
  140. showOwnedUpgrades = !showOwnedUpgrades;
  141. }
  142. function displayUpgrades(owned) {
  143. if (owned) {
  144. Object.entries(ownedUpgrades).forEach(([key, val]) => {
  145. let button = document.querySelector("#upgrade-" + key);
  146. if (val) {
  147. button.classList.remove("hidden");
  148. } else {
  149. button.classList.add("hidden");
  150. }
  151. });
  152. }
  153. else {
  154. for (let id of remainingUpgrades) {
  155. let button = document.querySelector("#upgrade-" + id);
  156. if (ownedUpgrades[id]) {
  157. button.classList.add("hidden");
  158. continue;
  159. }
  160. if (upgradeReachable(id)) {
  161. button.classList.remove("hidden");
  162. } else {
  163. button.classList.add("hidden");
  164. }
  165. if (upgradeAvailable(id)) {
  166. button.classList.remove("upgrade-button-inactive");
  167. } else {
  168. button.classList.add("upgrade-button-inactive");
  169. }
  170. }
  171. // we aren't trimming the list of upgrades now
  172. // because we need to switch between owned and unowned upgrades
  173. // - thus we need to be able to show or hide anything
  174. /*
  175. for (let i = remainingUpgrades.length-1; i >= 0; i--) {
  176. if (ownedUpgrades[remainingUpgrades[i]]) {
  177. remainingUpgrades.splice(i, 1);
  178. }
  179. }*/
  180. }
  181. }
  182. function updateClickBonus() {
  183. let bonus = 0;
  184. for (let effect of effects["click"]) {
  185. if (ownedUpgrades[effect.parent]) {
  186. bonus = effect.apply(bonus, currentProductivity["food"]);
  187. }
  188. }
  189. clickBonus = bonus;
  190. }
  191. function updateClickVictim() {
  192. for (let effect of effects["click-victim"]) {
  193. if (ownedUpgrades[effect.parent]) {
  194. clickVictim = effect.id;
  195. }
  196. }
  197. }
  198. function buyUpgrade(id, e) {
  199. if (ownedUpgrades[id]) {
  200. return;
  201. }
  202. let upgrade = upgrades[id];
  203. if (!upgradeAvailable(id)) {
  204. return;
  205. }
  206. spend(upgrade.cost);
  207. ownedUpgrades[id] = true;
  208. let text = "Bought " + upgrade.name + "!";
  209. clickPopup(text, "upgrade", [e.clientX, e.clientY]);
  210. updateProductivity();
  211. updateClickBonus();
  212. updateClickVictim();
  213. }
  214. function eatPrey() {
  215. const add = buildings[clickVictim]["prod"] * 10 * productivityMultiplierOf(clickVictim) + clickBonus;
  216. resources.food += add;
  217. return add;
  218. }
  219. // setup stuff lol
  220. // we'll initialize the dict of buildings we can own
  221. function setup() {
  222. // create static data
  223. createTemplateUpgrades();
  224. // prepare dynamic stuff
  225. initializeData();
  226. createButtons();
  227. createDisplays();
  228. registerListeners();
  229. load();
  230. unlockAtStart();
  231. updateProductivity();
  232. }
  233. function unlockAtStart() {
  234. unlockBuilding("micro");
  235. for (const [key, value] of Object.entries(belongings)) {
  236. if (belongings[key].visible) {
  237. unlockBuilding(key);
  238. }
  239. }
  240. }
  241. function unlockBuilding(id) {
  242. belongings[id].visible = true;
  243. document.querySelector("#building-" + id).classList.remove("hidden");
  244. }
  245. function initializeData() {
  246. for (const [key, value] of Object.entries(buildings)) {
  247. belongings[key] = {};
  248. belongings[key].count = 0;
  249. belongings[key].visible = false;
  250. }
  251. for (const [key, value] of Object.entries(resourceTypes)) {
  252. resources[key] = 0;
  253. currentProductivity[key] = 0;
  254. }
  255. for (const [id, upgrade] of Object.entries(upgrades)) {
  256. ownedUpgrades[id] = false;
  257. for (let effect of upgrade.effects) {
  258. if (effects[effect.type] === undefined) {
  259. effects[effect.type] = [];
  260. }
  261. // copy the data and add an entry for the upgrade id that owns the effect
  262. let newEffect = {};
  263. for (const [key, value] of Object.entries(effect)) {
  264. newEffect[key] = value;
  265. }
  266. newEffect.parent = id;
  267. // unfortunate name collision here
  268. // I'm using apply() to pass on any number of arguments to the
  269. // apply() function of the effect type
  270. newEffect.apply = function(...args) { return effect_types[effect.type].apply.apply(null, [effect].concat(args)); }
  271. effects[effect.type].push(newEffect);
  272. }
  273. }
  274. }
  275. function registerListeners() {
  276. document.querySelector("#tasty-micro").addEventListener("click", (e) => {
  277. const add = eatPrey();
  278. const text = "+" + round(add, 1) + " food";
  279. const gulp = "*glp*";
  280. clickPopup(text, "food", [e.clientX, e.clientY]);
  281. clickPopup(gulp, "gulp", [e.clientX, e.clientY]);
  282. });
  283. document.querySelector("#save").addEventListener("click", save);
  284. document.querySelector("#reset").addEventListener("click", reset);
  285. document.querySelector("#upgrades").addEventListener("click", switchShowOwnedUpgrades);
  286. }
  287. function createButtons() {
  288. createBuildings();
  289. createUpgrades();
  290. }
  291. function createBuildings() {
  292. let container = document.querySelector("#buildings-list");
  293. for (const [key, value] of Object.entries(buildings)) {
  294. let button = document.createElement("div");
  295. button.classList.add("building-button");
  296. button.classList.add("hidden");
  297. button.id = "building-" + key;
  298. let buttonName = document.createElement("div");
  299. buttonName.classList.add("building-button-name");
  300. let buttonCost = document.createElement("div");
  301. buttonCost.classList.add("building-button-cost");
  302. let buildingIcon = document.createElement("i");
  303. buildingIcon.classList.add("fas");
  304. buildingIcon.classList.add(value.icon);
  305. button.appendChild(buttonName);
  306. button.appendChild(buttonCost);
  307. button.appendChild(buildingIcon);
  308. button.addEventListener("mousemove", function(e) { buildingTooltip(key, e); });
  309. button.addEventListener("mouseleave", function() { buildingTooltipRemove(); });
  310. button.addEventListener("click", function() { buyBuilding(key); });
  311. button.addEventListener("click", function(e) { buildingTooltip(key, e); });
  312. container.appendChild(button);
  313. }
  314. }
  315. // do we have previous techs and at least one of each building?
  316. function upgradeReachable(id) {
  317. if (ownedUpgrades[id]) {
  318. return false;
  319. }
  320. if (upgrades[id].prereqs !== undefined ){
  321. for (const [type, reqs] of Object.entries(upgrades[id].prereqs)) {
  322. if (type == "buildings") {
  323. for (const [building, amount] of Object.entries(reqs)) {
  324. if (belongings[building].count == 0) {
  325. return false;
  326. }
  327. }
  328. }
  329. else if (type == "upgrades") {
  330. for (let upgrade of reqs) {
  331. if (!ownedUpgrades[upgrade]) {
  332. return false;
  333. }
  334. }
  335. }
  336. }
  337. }
  338. return true;
  339. }
  340. function upgradeAvailable(id) {
  341. if (!upgradeReachable(id)) {
  342. return false;
  343. }
  344. if (!canAfford(upgrades[id].cost)) {
  345. return false;
  346. }
  347. if (upgrades[id].prereqs !== undefined) {
  348. for (const [type, reqs] of Object.entries(upgrades[id].prereqs)) {
  349. if (type == "buildings") {
  350. for (const [building, amount] of Object.entries(upgrades[id].prereqs[type])) {
  351. if (belongings[building].count < amount) {
  352. return false;
  353. }
  354. }
  355. } else if (type == "productivity") {
  356. for (const [key, value] of Object.entries(reqs)) {
  357. if (currentProductivity[key] < value) {
  358. return false;
  359. }
  360. }
  361. }
  362. }
  363. }
  364. return true;
  365. }
  366. function createUpgrades() {
  367. let container = document.querySelector("#upgrades-list");
  368. for (const [key, value] of Object.entries(upgrades)) {
  369. remainingUpgrades.push(key);
  370. let button = document.createElement("div");
  371. button.classList.add("upgrade-button");
  372. button.classList.add("hidden");
  373. button.id = "upgrade-" + key;
  374. let buttonName = document.createElement("div");
  375. buttonName.classList.add("upgrade-button-name");
  376. buttonName.innerText = value.name;
  377. let upgradeIcon = document.createElement("i");
  378. upgradeIcon.classList.add("fas");
  379. upgradeIcon.classList.add(value.icon);
  380. button.appendChild(buttonName);
  381. button.appendChild(upgradeIcon);
  382. button.addEventListener("mousemove", function(e) { upgradeTooltip(key, e); });
  383. button.addEventListener("mouseleave", function() { upgradeTooltipRemove(); });
  384. button.addEventListener("click", function(e) { buyUpgrade(key, e); });
  385. container.appendChild(button);
  386. }
  387. }
  388. function createDisplays() {
  389. // nop
  390. }
  391. function renderLine(line) {
  392. let div = document.createElement("div");
  393. div.innerText = line.text;
  394. if (line.valid !== undefined) {
  395. if (line.valid) {
  396. div.classList.add("cost-met");
  397. } else {
  398. div.classList.add("cost-unmet");
  399. }
  400. }
  401. if (line.class !== undefined) {
  402. for (let entry of line.class.split(",")) {
  403. div.classList.add(entry);
  404. }
  405. }
  406. return div;
  407. }
  408. function renderLines(lines) {
  409. let divs = [];
  410. for (let line of lines) {
  411. divs.push(renderLine(line));
  412. }
  413. return divs;
  414. }
  415. function renderCost(cost) {
  416. let list = [];
  417. list.push({
  418. "text": "Cost:"
  419. });
  420. for (const [key, value] of Object.entries(cost)) {
  421. list.push({
  422. "text": render(value,0) + " " + resourceTypes[key].name,
  423. "valid": resources[key] >= value
  424. });
  425. }
  426. return renderLines(list);
  427. }
  428. function renderPrereqs(prereqs) {
  429. let list = [];
  430. if (prereqs === undefined) {
  431. return renderLines(list);
  432. }
  433. list.push({
  434. "text": "Own:"
  435. });
  436. for (const [key, value] of Object.entries(prereqs)) {
  437. if (key == "buildings") {
  438. for (const [id, amount] of Object.entries(prereqs.buildings)) {
  439. list.push({
  440. "text": buildings[id].name + " x" + render(amount,0),
  441. "valid": belongings[id].count >= amount
  442. });
  443. }
  444. } else if (key == "productivity") {
  445. for (const [id, amount] of Object.entries(prereqs.productivity)) {
  446. list.push({
  447. "text": render(amount,0) + " " + resourceTypes[id].name + "/s",
  448. "valid": currentProductivity[id] >= amount
  449. });
  450. }
  451. }
  452. }
  453. return renderLines(list);
  454. }
  455. function renderEffects(effectList) {
  456. let list = [];
  457. for (let effect of effectList) {
  458. list.push({"text": effect_types[effect.type].desc(effect)});
  459. }
  460. return renderLines(list);
  461. }
  462. function clickPopup(text, type, location) {
  463. const div = document.createElement("div");
  464. div.textContent = text;
  465. div.classList.add("click-popup-" + type);
  466. var direction;
  467. if (type == "food") {
  468. direction = -150;
  469. } else if (type == "gulp") {
  470. direction = -150;
  471. } else if (type == "upgrade") {
  472. direction = -50;
  473. } else if (type == "info") {
  474. direction = 0;
  475. }
  476. direction *= Math.random() * 0.5 + 1;
  477. direction = Math.round(direction) + "px"
  478. div.style.setProperty("--target", direction)
  479. div.style.left = location[0] + "px";
  480. div.style.top = location[1] + "px";
  481. const body = document.querySelector("body");
  482. body.appendChild(div);
  483. setTimeout(() => {
  484. body.removeChild(div);
  485. }, 2000);
  486. }
  487. function fillTooltip(type, field, content) {
  488. let item = document.querySelector("#" + type + "-tooltip-" + field);
  489. if (typeof(content) === "string") {
  490. item.innerText = content;
  491. } else {
  492. replaceChildren(item, content);
  493. }
  494. }
  495. function upgradeTooltip(id, event) {
  496. let tooltip = document.querySelector("#upgrade-tooltip");
  497. tooltip.style.setProperty("display", "inline-block");
  498. fillTooltip("upgrade", "name", upgrades[id].name);
  499. fillTooltip("upgrade", "desc", upgrades[id].desc);
  500. fillTooltip("upgrade", "effect", renderEffects(upgrades[id].effects));
  501. fillTooltip("upgrade", "cost", renderCost(upgrades[id].cost));
  502. fillTooltip("upgrade", "prereqs", renderPrereqs(upgrades[id].prereqs));
  503. let yOffset = tooltip.parentElement.getBoundingClientRect().y;
  504. let tooltipSize = tooltip.getBoundingClientRect().height;
  505. let yTrans = Math.round(event.clientY - yOffset);
  506. var body = document.body,
  507. html = document.documentElement;
  508. var height = Math.max(window.innerHeight);
  509. yTrans = Math.min(yTrans, height - tooltipSize - 150);
  510. tooltip.style.setProperty("transform", "translate(-220px, " + yTrans + "px)");
  511. }
  512. function upgradeTooltipRemove() {
  513. let tooltip = document.querySelector("#upgrade-tooltip");
  514. tooltip.style.setProperty("display", "none");
  515. }
  516. function prodSummary(id) {
  517. let list = [];
  518. list.push(
  519. {"text": "Each " + buildings[id].name + " produces " + round(productivityMultiplierOf(id) * buildings[id].prod,1) + " food/sec"}
  520. );
  521. list.push(
  522. {"text": "Your " + render(belongings[id].count) + " " + (belongings[id].count == 1 ? buildings[id].name + " is": buildings[id].plural + " are") + " producing " + round(productivityOf(id),1) + " food/sec"}
  523. );
  524. let percentage = round(100 * productivityOf(id) / currentProductivity["food"], 2);
  525. if (isNaN(percentage)) {
  526. percentage = 0;
  527. }
  528. list.push(
  529. {"text": "(" + percentage + "% of all food)"}
  530. );
  531. return renderLines(list);
  532. }
  533. function buildingTooltip(id, event) {
  534. let tooltip = document.querySelector("#building-tooltip");
  535. tooltip.style.setProperty("display", "inline-block");
  536. fillTooltip("building", "name", buildings[id].name);
  537. fillTooltip("building", "desc", buildings[id].desc);
  538. fillTooltip("building", "cost", render(costOfBuilding(id)) + " food");
  539. fillTooltip("building", "prod", prodSummary(id));
  540. let xPos = tooltip.parentElement.getBoundingClientRect().x - 450;
  541. // wow browsers are bad
  542. var body = document.body,
  543. html = document.documentElement;
  544. var height = Math.max( body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight );
  545. let yPos = Math.min(event.clientY, height - 200);
  546. tooltip.style.setProperty("transform", "translate(" + xPos + "px, " + yPos + "px)")
  547. }
  548. function buildingTooltipRemove() {
  549. let tooltip = document.querySelector("#building-tooltip");
  550. tooltip.style.setProperty("display", "none");
  551. }
  552. window.onload = function() {
  553. setup();
  554. lastTime = performance.now();
  555. setTimeout(updateDisplay, 1000/updateRate);
  556. setTimeout(autosave, 60000);
  557. }
  558. function autosave() {
  559. saveGame();
  560. let x = window.innerWidth / 2;
  561. let y = window.innerHeight * 9 / 10;
  562. clickPopup("Autosaving...", "info", [x, y]);
  563. setTimeout(autosave, 60000);
  564. }
  565. function save(e) {
  566. saveGame();
  567. clickPopup("Saved!", "info", [e.clientX, e.clientY]);
  568. }
  569. function saveGame() {
  570. let storage = window.localStorage;
  571. storage.setItem("save-version", "0.0.1");
  572. storage.setItem("ownedUpgrades", JSON.stringify(ownedUpgrades));
  573. storage.setItem("resources", JSON.stringify(resources));
  574. storage.setItem("belongings", JSON.stringify(belongings));
  575. }
  576. function load() {
  577. let storage = window.localStorage;
  578. if (!storage.getItem("save-version")) {
  579. return;
  580. }
  581. let newOwnedUpgrades = JSON.parse(storage.getItem("ownedUpgrades"));
  582. for (const [key, value] of Object.entries(newOwnedUpgrades)) {
  583. ownedUpgrades[key] = value;
  584. }
  585. let newResources = JSON.parse(storage.getItem("resources"));
  586. for (const [key, value] of Object.entries(newResources)) {
  587. resources[key] = value;
  588. }
  589. let newBelongings = JSON.parse(storage.getItem("belongings"));
  590. for (const [key, value] of Object.entries(newBelongings)) {
  591. belongings[key] = value;
  592. }
  593. }
  594. function reset() {
  595. window.localStorage.clear();
  596. }