a munch adventure
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

181 linhas
4.8 KiB

  1. stories = [];
  2. function initGame(story) {
  3. state.info = {};
  4. state.info.time = {
  5. id: "time",
  6. name: "Time",
  7. type: "counter",
  8. value: 60*60*9,
  9. get render() {
  10. return renderTime(this.value);
  11. }
  12. }
  13. state.player.stats = {};
  14. state.timers = [];
  15. }
  16. function createStatDisplays(stats, statType) {
  17. const holder = document.querySelector("#" + statType + "-info");
  18. holder.innerHTML = "";
  19. Object.entries(stats).forEach(([key, val]) => {
  20. if (val.type == "meter") {
  21. const field = document.createElement("div");
  22. field.id = statType + "-info-" + key;
  23. field.setAttribute("max", val.max);
  24. field.setAttribute("value", val.value);
  25. field.classList.add("stat-bar-holder");
  26. const label = document.createElement("div");
  27. label.classList.add("stat-bar-label");
  28. label.textContent = val.name;
  29. const bar = document.createElement("div");
  30. bar.classList.add("stat-bar");
  31. bar.style["background-color"] = val.color;
  32. field.appendChild(label);
  33. field.appendChild(bar);
  34. holder.appendChild(field);
  35. } else if (val.type == "counter") {
  36. const field = document.createElement("div");
  37. field.id = statType + "-info-" + key;
  38. holder.appendChild(field);
  39. }
  40. });
  41. }
  42. function initGamePostSetup() {
  43. createStatDisplays(state.info, "world");
  44. createStatDisplays(state.player.stats, "player");
  45. }
  46. function getStat(stat) {
  47. return state.player.stats[stat].value;
  48. }
  49. function changeStat(stat, amount) {
  50. let value = state.player.stats[stat].value;
  51. value += amount;
  52. value = Math.max(value, state.player.stats[stat].min);
  53. value = Math.min(value, state.player.stats[stat].max);
  54. state.player.stats[stat].value = value;
  55. }
  56. // TODO: format string this lol
  57. function renderTime(time) {
  58. let hours = Math.floor(time / 3600) % 12;
  59. const ampm = Math.floor(time / 3600) % 24 < 12 ? "AM" : "PM";
  60. let minutes = Math.floor(time / 60) % 60;
  61. let seconds = time % 60;
  62. if (minutes <= 9)
  63. minutes = "0" + minutes;
  64. if (seconds <= 9)
  65. seconds = "0" + seconds;
  66. return hours + ":" + minutes + ":" + seconds + " " + ampm;
  67. }
  68. function updateStatDisplay(stats, statType) {
  69. Object.entries(stats).forEach(([key, val]) => {
  70. if (val.hidden === true) {
  71. const field = document.querySelector("#" + statType + "-info-" + key);
  72. field.style.display = "none";
  73. } else {
  74. const field = document.querySelector("#" + statType + "-info-" + key);
  75. field.style.display = "block";
  76. if (val.type == "meter") {
  77. const bar = document.querySelector("#" + statType + "-info-" + key + " > .stat-bar");
  78. bar.style["background-color"] = val.color;
  79. }
  80. }
  81. if (val.type == "meter") {
  82. const field = document.querySelector("#" + statType + "-info-" + key + " > .stat-bar");
  83. field.style.width = (val.value / val.max * 100) + "%";
  84. } else if (val.type == "counter") {
  85. const field = document.querySelector("#" + statType + "-info-" + key);
  86. field.innerText = val.name + ": " + (val.render !== undefined ? val.render : val.value);
  87. }
  88. });
  89. }
  90. /*
  91. {
  92. id: an optional name; needed to manually kill a timer
  93. func: the function to invoke
  94. delay: how long to wait between invocations
  95. loop: false = no looping, true = loop forever
  96. }
  97. Returns the timeout id - but you still need to cancel it through stopTimer!
  98. */
  99. function startTimer(config) {
  100. const timeout = setTimeout(() => {
  101. const result = config.func(config);
  102. refresh();
  103. // the timer may have terminated itself!
  104. // we have to make sure it still exists
  105. if (state.timers.some(x => x.timeout == timeout)){
  106. state.timers = state.timers.filter(x => x.timeout != timeout);
  107. if (typeof(result) === "number") {
  108. config.delay = result;
  109. }
  110. // you shouldn't use a delay of 0 anyway
  111. if (result && config.loop) {
  112. startTimer(config);
  113. }
  114. }
  115. }, config.delay);
  116. state.timers.push({id: config.id, timeout: timeout, classes: config.classes || []});
  117. return timeout;
  118. }
  119. function stopTimer(id) {
  120. const matches = state.timers.filter(timer => timer.id == id);
  121. matches.forEach(timer => clearTimeout(timer.timeout));
  122. state.timers = state.timers.filter(timer => timer.id != id);
  123. }
  124. function stopClassTimers(timerClass, inverse) {
  125. const matches = state.timers.filter(timer => timer.classes.includes(timerClass));
  126. const others = state.timers.filter(timer => !timer.classes.includes(timerClass));
  127. if (inverse) {
  128. others.forEach(timer => clearTimeout(timer.timeout));
  129. state.timers = matches;
  130. } else {
  131. matches.forEach(timer => clearTimeout(timer.timeout));
  132. state.timers = others;
  133. }
  134. }
  135. function stopAllTimers() {
  136. state.timers.forEach(x => clearTimeout(x.timeout));
  137. state.timers = [];
  138. }
  139. function setBackgroundColor(r, g, b) {
  140. document.querySelector(".scene").style["background-color"] = "rgb(" + r + "," + g + "," + b + ")";
  141. }