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.
 
 
 
 

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