a munch adventure
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

160 lines
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. const timeout = setTimeout(() => {
  73. const result = config.func(state, config);
  74. refresh();
  75. // the timer may have terminated itself!
  76. // we have to make sure it still exists
  77. if (state.timers.some(x => x.timeout == timeout)){
  78. state.timers = state.timers.filter(x => x.timeout != timeout);
  79. if (typeof(result) === "number") {
  80. config.delay = result;
  81. }
  82. // you shouldn't use a delay of 0 anyway
  83. if (result && config.loop) {
  84. startTimer(config, state);
  85. }
  86. }
  87. }, config.delay);
  88. state.timers.push({id: config.id, timeout: timeout, room: config.room, classes: config.classes || []});
  89. return timeout;
  90. }
  91. function stopTimer(id, state) {
  92. const matches = state.timers.filter(timer => timer.id == id);
  93. matches.forEach(timer => clearTimeout(timer.timeout));
  94. state.timers = state.timers.filter(timer => timer.id != id);
  95. }
  96. function stopRoomTimers(room, state) {
  97. const matches = state.timers.filter(timer => timer.room == room);
  98. matches.forEach(timer => clearTimeout(timer.timeout));
  99. state.timers = state.timers.filter(timer => timer.room != room);
  100. }
  101. function stopClassTimers(timerClass, state, inverse) {
  102. const matches = state.timers.filter(timer => timer.classes.includes(timerClass));
  103. const others = state.timers.filter(timer => !timer.classes.includes(timerClass));
  104. if (inverse) {
  105. others.forEach(timer => clearTimeout(timer.timeout));
  106. state.timers = matches;
  107. } else {
  108. matches.forEach(timer => clearTimeout(timer.timeout));
  109. state.timers = others;
  110. }
  111. }
  112. function stopAllTimers(state) {
  113. state.timers.forEach(x => clearTimeout(x.timeout));
  114. state.timers = [];
  115. }
  116. function setBackgroundColor(r, g, b) {
  117. document.querySelector(".scene").style["background-color"] = "rgb(" + r + "," + g + "," + b + ")";
  118. }