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.
 
 
 
 

107 líneas
2.7 KiB

  1. stories = [];
  2. function initGame(story, state) {
  3. state.info = {};
  4. state.info.time = 60 * 60 * 9;
  5. state.player.stats = {};
  6. state.player.stats.health = 100;
  7. state.timers = [];
  8. }
  9. // TODO: format string this lol
  10. function renderTime(time) {
  11. let hours = Math.floor(time / 3600) % 12;
  12. const ampm = Math.floor(time / 3600) % 24 < 12 ? "AM" : "PM";
  13. let minutes = Math.floor(time / 60) % 60;
  14. let seconds = time % 60;
  15. if (minutes <= 9)
  16. minutes = "0" + minutes;
  17. if (seconds <= 9)
  18. seconds = "0" + seconds;
  19. return hours + ":" + minutes + ":" + seconds + " " + ampm;
  20. }
  21. function updateWorldInfo(state) {
  22. const timeInfo = document.querySelector("#world-info-time");
  23. timeInfo.textContent = "Time: " + renderTime(state.info.time);
  24. }
  25. function updatePlayerInfo(state) {
  26. const health = document.querySelector("#player-info-health");
  27. health.textContent = "Health: " + state.player.stats.health;
  28. }
  29. /*
  30. {
  31. id: an optional name; needed to manually kill a timer
  32. func: the function to invoke
  33. delay: how long to wait between invocations
  34. loop: false = no looping, true = loop forever
  35. room: the room associated with the timer
  36. }
  37. Returns the timeout id - but you still need to cancel it through stopTimer!
  38. */
  39. function startTimer(config, state) {
  40. if (config.loop) {
  41. const timeout = setTimeout(() => {
  42. const result = config.func(state);
  43. state.timers = state.timers.filter(x => x.timeout != timeout);
  44. refresh();
  45. if (result)
  46. startTimer(config, state);
  47. }, config.delay);
  48. state.timers.push({id: config.id, timeout: timeout, room: config.room, classes: config.classes || []});
  49. return timeout;
  50. }
  51. }
  52. function stopTimer(id, state) {
  53. const matches = state.timers.filter(timer => timer.id == id);
  54. matches.forEach(timer => clearTimeout(timer.timeout));
  55. state.timers = state.timers.filter(timer => timer.id != id);
  56. }
  57. function stopRoomTimers(room, state) {
  58. const matches = state.timers.filter(timer => timer.room == room);
  59. matches.forEach(timer => clearTimeout(timer.timeout));
  60. state.timers = state.timers.filter(timer => timer.room != room);
  61. }
  62. function stopClassTimers(timerClass, state, inverse) {
  63. const matches = state.timers.filter(timer => timer.classes.includes(timerClass));
  64. const others = state.timers.filter(timer => !timer.classes.includes(timerClass));
  65. if (inverse) {
  66. others.forEach(timer => clearTimeout(timer.timeout));
  67. state.timers = matches;
  68. } else {
  69. matches.forEach(timer => clearTimeout(timer.timeout));
  70. state.timers = others;
  71. }
  72. }
  73. function stopAllTimers(state) {
  74. state.timers.forEach(x => clearTimeout(x.timeout));
  75. state.timers = [];
  76. }
  77. function setBackgroundColor(r, g, b) {
  78. document.querySelector(".scene").style["background-color"] = "rgb(" + r + "," + g + "," + b + ")";
  79. }