a munch adventure
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

117 рядки
3.0 KiB

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