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.
 
 
 
 

596 line
14 KiB

  1. "use strict";
  2. let belongings = {};
  3. let ownedUpgrades = {};
  4. let effects = {};
  5. let remainingUpgrades = [];
  6. let resources = {};
  7. let updateRate = 60;
  8. let currentProductivity = {};
  9. let lastTime = 0;
  10. function calculateProductivity() {
  11. let productivity = 0;
  12. for (const [key, value] of Object.entries(belongings)) {
  13. productivity += productivityOf(key);
  14. }
  15. for (let effect of effects["prod-all"]) {
  16. if (ownedUpgrades[effect.parent]) {
  17. productivity = effect.apply(productivity);
  18. }
  19. }
  20. return productivity;
  21. }
  22. // here's where upgrades will go :3
  23. function productivityMultiplierOf(type) {
  24. let base = 1;
  25. for (let effect of effects["prod"]) {
  26. if (ownedUpgrades[effect.parent] && effect.target == type) {
  27. base = effect.apply(base);
  28. }
  29. }
  30. return base;
  31. }
  32. function productivityOf(type) {
  33. let baseProd = buildings[type].prod;
  34. let prod = baseProd * productivityMultiplierOf(type);
  35. return prod * belongings[type].count;
  36. }
  37. function costOfBuilding(type) {
  38. let baseCost = buildings[type].cost
  39. let countCost = baseCost * Math.pow(1.15, belongings[type].count);
  40. return Math.round(countCost);
  41. }
  42. function buyBuilding(type) {
  43. let cost = costOfBuilding(type);
  44. if (resources.food >= cost) {
  45. belongings[type].count += 1;
  46. resources.food -= cost;
  47. }
  48. updateProductivity();
  49. }
  50. // update stuff
  51. function updateDisplay() {
  52. let newTime = performance.now();
  53. let delta = newTime - lastTime;
  54. lastTime = newTime;
  55. updateProductivity();
  56. addResources(delta);
  57. displayResources();
  58. displayBuildings();
  59. displayUpgrades();
  60. setTimeout(updateDisplay, 1000/updateRate);
  61. }
  62. function updateProductivity() {
  63. currentProductivity["food"] = calculateProductivity();
  64. }
  65. function addResources(delta) {
  66. for (const [resource, amount] of Object.entries(currentProductivity)) {
  67. resources[resource] += amount * delta / 1000;
  68. }
  69. }
  70. function displayResources() {
  71. document.title = "Gorge - " + round(resources.food) + " food";
  72. replaceChildren(document.querySelector("#resource-list"), renderResources());
  73. }
  74. function renderResources() {
  75. let list = [];
  76. for (const [key, value] of Object.entries(resources)) {
  77. let line1 = round(value) + " " + resourceTypes[key].name;
  78. let line2 = round(currentProductivity[key],1) + " " + resourceTypes[key].name + "/sec";
  79. list.push({"text": line1});
  80. list.push({"text": line2});
  81. }
  82. return renderLines(list);
  83. }
  84. function displayBuildings() {
  85. for (const [key, value] of Object.entries(belongings)) {
  86. if (!belongings[key].visible) {
  87. if (resources.food * 10 >= costOfBuilding(key)) {
  88. unlockBuilding(key);
  89. } else {
  90. continue;
  91. }
  92. belongings[key].visible = true;
  93. document.querySelector("#building-" + key).classList.remove("hidden");
  94. }
  95. let button = document.querySelector("#building-" + key);
  96. document.querySelector("#building-" + key + " > .building-button-name").innerText = value.count + " " + (value.count == 1 ? buildings[key].name : buildings[key].plural);
  97. document.querySelector("#building-" + key + " > .building-button-cost").innerText = costOfBuilding(key) + " food";
  98. if (costOfBuilding(key) > resources.food) {
  99. button.classList.add("building-button-disabled");
  100. } else {
  101. button.classList.remove("building-button-disabled");
  102. }
  103. }
  104. }
  105. function canAfford(cost) {
  106. for (const [resource, amount] of Object.entries(cost)) {
  107. if (resources[resource] < amount) {
  108. return false;
  109. }
  110. }
  111. return true;
  112. }
  113. function spend(cost) {
  114. for (const [resource, amount] of Object.entries(cost)) {
  115. resources[resource] -= amount;
  116. }
  117. }
  118. function displayUpgrades() {
  119. for (let id of remainingUpgrades) {
  120. let button = document.querySelector("#upgrade-" + id);
  121. if (ownedUpgrades[id]) {
  122. button.style.display = "none";
  123. continue;
  124. }
  125. if (upgradeReachable(id)) {
  126. button.classList.remove("hidden");
  127. } else {
  128. button.classList.add("hidden");
  129. }
  130. if (upgradeAvailable(id)) {
  131. button.classList.remove("upgrade-button-inactive");
  132. } else {
  133. button.classList.add("upgrade-button-inactive");
  134. }
  135. }
  136. // now we throw out stuff
  137. for (let i = remainingUpgrades.length-1; i >= 0; i--) {
  138. if (ownedUpgrades[remainingUpgrades[i]]) {
  139. remainingUpgrades.splice(i, 1);
  140. }
  141. }
  142. }
  143. function buyUpgrade(id) {
  144. if (ownedUpgrades[id]) {
  145. return;
  146. }
  147. let upgrade = upgrades[id];
  148. if (!upgradeAvailable(id)) {
  149. return;
  150. }
  151. spend(upgrade.cost);
  152. ownedUpgrades[id] = true;
  153. }
  154. function eatMicro() {
  155. resources.food += productivityMultiplierOf("micro");
  156. }
  157. // setup stuff lol
  158. // we'll initialize the dict of buildings we can own
  159. function setup() {
  160. initializeData();
  161. createButtons();
  162. createDisplays();
  163. registerListeners();
  164. load();
  165. unlockAtStart();
  166. }
  167. function unlockAtStart() {
  168. unlockBuilding("micro");
  169. }
  170. function unlockBuilding(id) {
  171. belongings[id].visible = true;
  172. document.querySelector("#building-" + id).classList.remove("hidden");
  173. }
  174. function initializeData() {
  175. for (const [key, value] of Object.entries(buildings)) {
  176. belongings[key] = {};
  177. belongings[key].count = 0;
  178. belongings[key].visible = false;
  179. }
  180. for (const [key, value] of Object.entries(resourceTypes)) {
  181. resources[key] = 0;
  182. currentProductivity[key] = 0;
  183. }
  184. for (const [id, upgrade] of Object.entries(upgrades)) {
  185. ownedUpgrades[id] = false;
  186. for (let effect of upgrade.effects) {
  187. if (effects[effect.type] === undefined) {
  188. effects[effect.type] = [];
  189. }
  190. // copy the data and add an entry for the upgrade id that owns the effect
  191. let newEffect = {};
  192. for (const [key, value] of Object.entries(effect)) {
  193. newEffect[key] = value;
  194. }
  195. newEffect.parent = id;
  196. // unfortunate name collision here
  197. // I'm using apply() to pass on any number of arguments to the
  198. // apply() function of the effect type
  199. newEffect.apply = function(...args) { return effect_types[effect.type].apply.apply(null, [effect].concat(args)); }
  200. effects[effect.type].push(newEffect);
  201. }
  202. }
  203. }
  204. function registerListeners() {
  205. document.querySelector("#tasty-micro").addEventListener("click", eatMicro);
  206. document.querySelector("#save").addEventListener("click", save);
  207. document.querySelector("#reset").addEventListener("click", reset);
  208. }
  209. function createButtons() {
  210. createBuildings();
  211. createUpgrades();
  212. }
  213. function createBuildings() {
  214. let container = document.querySelector("#buildings-area");
  215. for (const [key, value] of Object.entries(buildings)) {
  216. let button = document.createElement("div");
  217. button.classList.add("building-button");
  218. button.classList.add("hidden");
  219. button.id = "building-" + key;
  220. let buttonName = document.createElement("div");
  221. buttonName.classList.add("building-button-name");
  222. let buttonCost = document.createElement("div");
  223. buttonCost.classList.add("building-button-cost");
  224. button.appendChild(buttonName);
  225. button.appendChild(buttonCost);
  226. button.addEventListener("mousemove", function(e) { buildingTooltip(key, e); });
  227. button.addEventListener("mouseleave", function() { buildingTooltipRemove(); });
  228. button.addEventListener("click", function() { buyBuilding(key); });
  229. button.addEventListener("click", function(e) { buildingTooltip(key, e); });
  230. container.appendChild(button);
  231. }
  232. }
  233. // do we have previous techs and at least one of each building?
  234. function upgradeReachable(id) {
  235. if (ownedUpgrades[id]) {
  236. return false;
  237. }
  238. for (const [type, reqs] of Object.entries(upgrades[id].prereqs)) {
  239. if (type == "buildings") {
  240. for (const [building, amount] of Object.entries(reqs)) {
  241. if (belongings[building].count == 0) {
  242. return false;
  243. }
  244. }
  245. }
  246. else if (type == "upgrades") {
  247. for (let upgrade of reqs) {
  248. if (!ownedUpgrades[upgrade]) {
  249. return false;
  250. }
  251. }
  252. }
  253. }
  254. return true;
  255. }
  256. function upgradeAvailable(id) {
  257. if (!upgradeReachable(id)) {
  258. return false;
  259. }
  260. if (!canAfford(upgrades[id].cost)) {
  261. return false;
  262. }
  263. for (const [type, reqs] of Object.entries(upgrades[id].prereqs)) {
  264. if (type == "buildings") {
  265. for (const [building, amount] of Object.entries(upgrades[id].prereqs[type])) {
  266. if (belongings[building].count < amount) {
  267. return false;
  268. }
  269. }
  270. } else if (type == "productivity") {
  271. for (const [key, value] of Object.entries(reqs)) {
  272. if (currentProductivity[key] < value) {
  273. return false;
  274. }
  275. }
  276. }
  277. }
  278. return true;
  279. }
  280. function createUpgrades() {
  281. let container = document.querySelector("#upgrades-list");
  282. for (const [key, value] of Object.entries(upgrades)) {
  283. remainingUpgrades.push(key);
  284. let button = document.createElement("div");
  285. button.classList.add("upgrade-button");
  286. button.classList.add("hidden");
  287. button.id = "upgrade-" + key;
  288. let buttonName = document.createElement("div");
  289. buttonName.classList.add("upgrade-button-name");
  290. buttonName.innerText = value.name;
  291. button.appendChild(buttonName);
  292. button.addEventListener("mousemove", function(e) { upgradeTooltip(key, e); });
  293. button.addEventListener("mouseleave", function() { upgradeTooltipRemove(); });
  294. button.addEventListener("click", function() { buyUpgrade(key); });
  295. container.appendChild(button);
  296. }
  297. }
  298. function createDisplays() {
  299. // nop
  300. }
  301. function renderLine(line) {
  302. let div = document.createElement("div");
  303. div.innerText = line.text;
  304. if (line.valid !== undefined) {
  305. if (line.valid) {
  306. div.classList.add("cost-met");
  307. } else {
  308. div.classList.add("cost-unmet");
  309. }
  310. }
  311. return div;
  312. }
  313. function renderLines(lines) {
  314. let divs = [];
  315. for (let line of lines) {
  316. divs.push(renderLine(line));
  317. }
  318. return divs;
  319. }
  320. function renderCost(cost) {
  321. let list = [];
  322. list.push({
  323. "text": "Cost:"
  324. });
  325. for (const [key, value] of Object.entries(cost)) {
  326. list.push({
  327. "text": value + " " + resourceTypes[key].name,
  328. "valid": resources[key] >= value
  329. });
  330. }
  331. return renderLines(list);
  332. }
  333. function renderPrereqs(prereqs) {
  334. let list = [];
  335. list.push({
  336. "text": "Own:"
  337. });
  338. for (const [key, value] of Object.entries(prereqs)) {
  339. if (key == "buildings") {
  340. for (const [id, amount] of Object.entries(prereqs.buildings)) {
  341. list.push({
  342. "text": buildings[id].name + " x" + amount,
  343. "valid": belongings[id].count >= amount
  344. });
  345. }
  346. } else if (key == "productivity") {
  347. for (const [id, amount] of Object.entries(prereqs.productivity)) {
  348. list.push({
  349. "text": amount + " " + resourceTypes[id].name + "/s",
  350. "valid": currentProductivity[id] >= amount
  351. });
  352. }
  353. }
  354. }
  355. return renderLines(list);
  356. }
  357. function renderEffects(effectList) {
  358. let list = [];
  359. for (let effect of effectList) {
  360. list.push({"text": effect_types[effect.type].desc(effect)});
  361. }
  362. return renderLines(list);
  363. }
  364. function fillTooltip(type, field, content) {
  365. let item = document.querySelector("#" + type + "-tooltip-" + field);
  366. if (typeof(content) === "string") {
  367. item.innerText = content;
  368. } else {
  369. replaceChildren(item, content);
  370. }
  371. }
  372. function upgradeTooltip(id, event) {
  373. let tooltip = document.querySelector("#upgrade-tooltip");
  374. tooltip.style.setProperty("display", "inline-block");
  375. fillTooltip("upgrade", "name", upgrades[id].name);
  376. fillTooltip("upgrade", "desc", upgrades[id].desc);
  377. fillTooltip("upgrade", "effect", renderEffects(upgrades[id].effects));
  378. fillTooltip("upgrade", "cost", renderCost(upgrades[id].cost));
  379. fillTooltip("upgrade", "prereqs", renderPrereqs(upgrades[id].prereqs));
  380. let yOffset = tooltip.parentElement.getBoundingClientRect().y;
  381. let yTrans = Math.round(event.clientY - yOffset);
  382. tooltip.style.setProperty("transform", "translate(-220px, " + yTrans + "px)");
  383. }
  384. function upgradeTooltipRemove() {
  385. let tooltip = document.querySelector("#upgrade-tooltip");
  386. tooltip.style.setProperty("display", "none");
  387. }
  388. function prodSummary(id) {
  389. let list = [];
  390. list.push(
  391. {"text": "Each " + buildings[id].name + " produces " + round(productivityMultiplierOf(id) * buildings[id].prod,1) + " food/sec"}
  392. );
  393. list.push(
  394. {"text": "Your " + belongings[id].count + " " + (belongings[id].count == 1 ? buildings[id].name + " is": buildings[id].plural + " are") + " producing " + round(productivityOf(id),1) + " food/sec"}
  395. );
  396. let percentage = round(100 * productivityOf(id) / currentProductivity["food"], 2);
  397. if (isNaN(percentage)) {
  398. percentage = 0;
  399. }
  400. list.push(
  401. {"text": "(" + percentage + "% of all food)"}
  402. );
  403. return renderLines(list);
  404. }
  405. function buildingTooltip(id, event) {
  406. let tooltip = document.querySelector("#building-tooltip");
  407. tooltip.style.setProperty("display", "inline-block");
  408. fillTooltip("building", "name", buildings[id].name);
  409. fillTooltip("building", "desc", buildings[id].desc);
  410. fillTooltip("building", "cost", costOfBuilding(id) + " food");
  411. fillTooltip("building", "prod", prodSummary(id));
  412. let yOffset = tooltip.parentElement.getBoundingClientRect().y;
  413. let xPos = tooltip.parentElement.getBoundingClientRect().x - 450;
  414. let yPos = event.clientY;
  415. tooltip.style.setProperty("transform", "translate(" + xPos + "px, " + yPos + "px)")
  416. }
  417. function buildingTooltipRemove() {
  418. let tooltip = document.querySelector("#building-tooltip");
  419. tooltip.style.setProperty("display", "none");
  420. }
  421. window.onload = function() {
  422. setup();
  423. lastTime = performance.now();
  424. setTimeout(updateDisplay, 1000/updateRate);
  425. }
  426. function save() {
  427. let storage = window.localStorage;
  428. storage.setItem("save-version", "0.0.1");
  429. storage.setItem("ownedUpgrades", JSON.stringify(ownedUpgrades));
  430. storage.setItem("resources", JSON.stringify(resources));
  431. storage.setItem("belongings", JSON.stringify(belongings));
  432. }
  433. function load() {
  434. let storage = window.localStorage;
  435. if (!storage.getItem("save-version")) {
  436. return;
  437. }
  438. ownedUpgrades = JSON.parse(storage.getItem("ownedUpgrades"));
  439. resources = JSON.parse(storage.getItem("resources"));
  440. belongings = JSON.parse(storage.getItem("belongings"));
  441. }
  442. function reset() {
  443. window.localStorage.clear();
  444. }