a munch adventure
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

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