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.
 
 
 
 

163 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 changeStat(stat, amount, state) {
  47. let value = state.player.stats[stat].value;
  48. value += amount;
  49. value = Math.max(value, state.player.stats[stat].min);
  50. value = Math.min(value, state.player.stats[stat].max);
  51. state.player.stats[stat].value = value;
  52. }
  53. // TODO: format string this lol
  54. function renderTime(time) {
  55. let hours = Math.floor(time / 3600) % 12;
  56. const ampm = Math.floor(time / 3600) % 24 < 12 ? "AM" : "PM";
  57. let minutes = Math.floor(time / 60) % 60;
  58. let seconds = time % 60;
  59. if (minutes <= 9)
  60. minutes = "0" + minutes;
  61. if (seconds <= 9)
  62. seconds = "0" + seconds;
  63. return hours + ":" + minutes + ":" + seconds + " " + ampm;
  64. }
  65. function updateStatDisplay(stats, statType) {
  66. Object.entries(stats).forEach(([key, val]) => {
  67. if (val.type == "meter") {
  68. const field = document.querySelector("#" + statType + "-info-" + key + " > .stat-bar");
  69. field.style.width = (val.value / val.max * 100) + "%";
  70. } else if (val.type == "counter") {
  71. const field = document.querySelector("#" + statType + "-info-" + key);
  72. field.innerText = val.name + ": " + (val.render !== undefined ? val.render : val.value);
  73. }
  74. });
  75. }
  76. /*
  77. {
  78. id: an optional name; needed to manually kill a timer
  79. func: the function to invoke
  80. delay: how long to wait between invocations
  81. loop: false = no looping, true = loop forever
  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, 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 stopClassTimers(timerClass, state, inverse) {
  111. const matches = state.timers.filter(timer => timer.classes.includes(timerClass));
  112. const others = state.timers.filter(timer => !timer.classes.includes(timerClass));
  113. if (inverse) {
  114. others.forEach(timer => clearTimeout(timer.timeout));
  115. state.timers = matches;
  116. } else {
  117. matches.forEach(timer => clearTimeout(timer.timeout));
  118. state.timers = others;
  119. }
  120. }
  121. function stopAllTimers(state) {
  122. state.timers.forEach(x => clearTimeout(x.timeout));
  123. state.timers = [];
  124. }
  125. function setBackgroundColor(r, g, b) {
  126. document.querySelector(".scene").style["background-color"] = "rgb(" + r + "," + g + "," + b + ")";
  127. }