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.
 
 
 
 

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