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.
 
 
 
 

103 line
2.1 KiB

  1. "use strict";
  2. let belongings = {}
  3. let resources = {
  4. "food": 0
  5. }
  6. let updateRate = 60;
  7. function calculateProductivity() {
  8. let productivity = 0;
  9. for (const [key, value] of Object.entries(belongings)) {
  10. productivity += productivityOf(key);
  11. }
  12. return productivity;
  13. }
  14. // here's where upgrades will go :3
  15. function productivityOf(type) {
  16. let baseProd = buildings[type].prod;
  17. return baseProd * belongings[type].count;
  18. }
  19. function costOf(type) {
  20. let baseCost = buildings[type].cost
  21. let countCost = baseCost * Math.pow(1.02, belongings[type].count);
  22. return countCost;
  23. }
  24. function buyBuilding(type) {
  25. let cost = costOf(type);
  26. if (resources.food > cost) {
  27. belongings[type].count += 1;
  28. resources.food -= cost;
  29. }
  30. }
  31. // update stuff
  32. function updateResources() {
  33. addResources();
  34. displayResources();
  35. displayBuildings();
  36. setTimeout(updateResources, 1000/updateRate);
  37. }
  38. function addResources() {
  39. resources.food += calculateProductivity() * 1 / updateRate;
  40. }
  41. function displayResources() {
  42. document.getElementById("resource-food").innerText = "Food: " + render(resources.food);
  43. }
  44. function displayBuildings() {
  45. for (const [key, value] of Object.entries(belongings)) {
  46. document.getElementById("building-" + key).innerText = buildings[key].name + ": " + belongings[key].count;
  47. }
  48. }
  49. function eatMicro() {
  50. resources.food += 1;
  51. }
  52. // setup stuff lol
  53. // we'll initialize the dict of buildings we can own
  54. function setup() {
  55. initializeData();
  56. registerListeners();
  57. console.log(belongings)
  58. }
  59. function initializeData() {
  60. for (const [key, value] of Object.entries(buildings)) {
  61. belongings[key] = {};
  62. belongings[key].count = 0;
  63. }
  64. }
  65. function registerListeners() {
  66. document.querySelectorAll(".building-button").forEach(function(button) {
  67. let id = button.id.replace("building-", "");
  68. button.addEventListener("click", function() { buyBuilding(id); });
  69. });
  70. document.querySelector("#tasty-micro").addEventListener("click", eatMicro);
  71. }
  72. window.onload = function() {
  73. setup();
  74. setTimeout(updateResources, 1000/updateRate);
  75. }