a munch adventure
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

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