a munch adventure
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

154 wiersze
4.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. holder.innerHTML = "";
  11. Object.entries(state.player.stats).forEach(([key, val]) => {
  12. if (val.type == "meter") {
  13. const field = document.createElement("div");
  14. field.id = "player-info-" + key;
  15. field.setAttribute("max", val.max);
  16. field.setAttribute("value", val.value);
  17. field.classList.add("stat-bar-holder");
  18. const label = document.createElement("div");
  19. label.classList.add("stat-bar-label");
  20. label.textContent = val.name;
  21. const bar = document.createElement("div");
  22. bar.classList.add("stat-bar");
  23. bar.style["background-color"] = val.color;
  24. field.appendChild(label);
  25. field.appendChild(bar);
  26. holder.appendChild(field);
  27. }
  28. });
  29. }
  30. // TODO: format string this lol
  31. function renderTime(time) {
  32. let hours = Math.floor(time / 3600) % 12;
  33. const ampm = Math.floor(time / 3600) % 24 < 12 ? "AM" : "PM";
  34. let minutes = Math.floor(time / 60) % 60;
  35. let seconds = time % 60;
  36. if (minutes <= 9)
  37. minutes = "0" + minutes;
  38. if (seconds <= 9)
  39. seconds = "0" + seconds;
  40. return hours + ":" + minutes + ":" + seconds + " " + ampm;
  41. }
  42. function updateWorldInfo(state) {
  43. const timeInfo = document.querySelector("#world-info-time");
  44. timeInfo.textContent = "Time: " + renderTime(state.info.time);
  45. }
  46. function updatePlayerInfo(state) {
  47. Object.entries(state.player.stats).forEach(([key, val]) => {
  48. if (val.type == "meter") {
  49. const field = document.querySelector("#player-info-" + key + " > .stat-bar");
  50. field.style.width = (val.value / val.max * 100) + "%";
  51. }
  52. });
  53. }
  54. /*
  55. {
  56. id: an optional name; needed to manually kill a timer
  57. func: the function to invoke
  58. delay: how long to wait between invocations
  59. loop: false = no looping, true = loop forever
  60. room: the room associated with the timer
  61. }
  62. Returns the timeout id - but you still need to cancel it through stopTimer!
  63. */
  64. function startTimer(config, state) {
  65. if (config.loop) {
  66. const timeout = setTimeout(() => {
  67. const result = config.func(state, config);
  68. refresh();
  69. // the timer may have terminated itself!
  70. // we have to make sure it still exists
  71. if (state.timers.some(x => x.timeout == timeout)){
  72. state.timers = state.timers.filter(x => x.timeout != timeout);
  73. if (typeof(result) === "number") {
  74. config.delay = result;
  75. }
  76. // you shouldn't use a delay of 0 anyway
  77. if (result) {
  78. startTimer(config, state);
  79. }
  80. }
  81. }, config.delay);
  82. state.timers.push({id: config.id, timeout: timeout, room: config.room, classes: config.classes || []});
  83. return timeout;
  84. }
  85. }
  86. function stopTimer(id, state) {
  87. const matches = state.timers.filter(timer => timer.id == id);
  88. matches.forEach(timer => clearTimeout(timer.timeout));
  89. state.timers = state.timers.filter(timer => timer.id != id);
  90. }
  91. function stopRoomTimers(room, state) {
  92. const matches = state.timers.filter(timer => timer.room == room);
  93. matches.forEach(timer => clearTimeout(timer.timeout));
  94. state.timers = state.timers.filter(timer => timer.room != room);
  95. }
  96. function stopClassTimers(timerClass, state, inverse) {
  97. const matches = state.timers.filter(timer => timer.classes.includes(timerClass));
  98. const others = state.timers.filter(timer => !timer.classes.includes(timerClass));
  99. if (inverse) {
  100. others.forEach(timer => clearTimeout(timer.timeout));
  101. state.timers = matches;
  102. } else {
  103. matches.forEach(timer => clearTimeout(timer.timeout));
  104. state.timers = others;
  105. }
  106. }
  107. function stopAllTimers(state) {
  108. state.timers.forEach(x => clearTimeout(x.timeout));
  109. state.timers = [];
  110. }
  111. function setBackgroundColor(r, g, b) {
  112. document.querySelector(".scene").style["background-color"] = "rgb(" + r + "," + g + "," + b + ")";
  113. }