cookie clicker but bigger
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

106 行
2.2 KiB

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