a munch adventure
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

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