a munch adventure
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 

169 rindas
4.6 KiB

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