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

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