a munch adventure
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

223 líneas
6.0 KiB

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