a munch adventure
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

186 Zeilen
4.5 KiB

  1. "use strict"
  2. let activeModal = null;
  3. const newline = String.fromCharCode(160);
  4. const version = "0.1.2";
  5. let state;
  6. let refreshHook;
  7. function print(lines) {
  8. (lines.concat([newline])).forEach(line => {
  9. const log = document.querySelector("#log");
  10. const div = document.createElement("div");
  11. div.textContent = line;
  12. log.appendChild(div);
  13. });
  14. log.scrollTop = log.scrollHeight;
  15. }
  16. function printRandom(list) {
  17. let choice = Math.floor(Math.random() * list.length)
  18. print(list[choice])
  19. }
  20. function refresh() {
  21. updateRoom(state);
  22. updateStatDisplay(state.info, "world");
  23. updateStatDisplay(state.player.stats, "player");
  24. updateStatDisplay(state.world[state.player.location].data.stats, "area");
  25. if (refreshHook) {
  26. refreshHook(state)
  27. }
  28. }
  29. function switchModal(to) {
  30. closeModal(activeModal);
  31. openModal(to);
  32. }
  33. function closeModal(modal) {
  34. const div = document.querySelector("#" + modal);
  35. div.classList.remove("modal");
  36. div.classList.add("hidden-modal");
  37. }
  38. function openModal(modal) {
  39. const div = document.querySelector("#" + modal);
  40. div.classList.remove("hidden-modal");
  41. div.classList.add("modal");
  42. activeModal = modal;
  43. }
  44. function returnToStart() {
  45. stopAllSound();
  46. stopAllTimers(state);
  47. setBackgroundColor(0, 0, 0);
  48. log.innerHTML = "";
  49. document.querySelector("#game").classList.remove("scene");
  50. document.querySelector("#game").classList.add("hidden-scene");
  51. document.querySelector("#pick").classList.remove("hidden-scene");
  52. document.querySelector("#pick").classList.add("scene");
  53. }
  54. // set up the game
  55. function init(story) {
  56. state = {
  57. player: {
  58. items: {
  59. keys: [
  60. ]
  61. },
  62. rooms: {
  63. },
  64. flags: {
  65. }
  66. },
  67. };
  68. initWorld(story, state);
  69. initGame(story, state);
  70. story.intro.setup(state);
  71. initGamePostSetup(state);
  72. refreshHook = story.refresh;
  73. story.intro.intro(state);
  74. goToRoom(story.intro.start, state);
  75. }
  76. // set up the load screen
  77. function initStart() {
  78. const versionFields = document.querySelectorAll(".version");
  79. const select = document.querySelector("#game-select");
  80. const options = {};
  81. versionFields.forEach(field => {
  82. field.textContent = "Version: " + version;
  83. });
  84. stories.forEach(story => {
  85. const option = document.createElement("option");
  86. option.value = story.id;
  87. option.textContent = story.name;
  88. select.appendChild(option);
  89. options[story.id] = story;
  90. })
  91. select.addEventListener("change", event => {
  92. const holder = document.querySelector("#tags");
  93. holder.innerHTML = "";
  94. const story = stories.filter(s => s.id == [event.target.value])[0];
  95. const tags = story.tags;
  96. initAudio(story);
  97. story.preload.forEach(sound => loadAudio(sound));
  98. tags.forEach(tag => {
  99. const div = document.createElement("div");
  100. div.textContent = tag;
  101. div.classList.add("tag");
  102. holder.appendChild(div);
  103. })
  104. });
  105. const start = document.querySelector("#start-button");
  106. start.addEventListener("click", (event) => {
  107. init(options[select.value]);
  108. document.querySelector("#pick").classList.remove("scene");
  109. document.querySelector("#pick").classList.add("hidden-scene");
  110. document.querySelector("#game").classList.remove("hidden-scene");
  111. document.querySelector("#game").classList.add("scene");
  112. });
  113. const gameMenuButton = document.querySelector("#game-menu-button");
  114. const menuSettings = document.querySelector("#menu-button-settings");
  115. const menuQuit = document.querySelector("#menu-button-quit");
  116. const menuResume = document.querySelector("#menu-button-resume");
  117. const menuSettingsVolume = document.querySelector("#menu-slider-volume");
  118. const menuSettingsClose = document.querySelector("#menu-button-settings-close");
  119. const menuQuitYes = document.querySelector("#menu-button-quit-yes");
  120. const menuQuitNo = document.querySelector("#menu-button-quit-no");
  121. gameMenuButton.addEventListener("click", () => openModal("menu"));
  122. menuSettings.addEventListener("click", () => switchModal("settings"));
  123. menuQuit.addEventListener("click", () => switchModal("quit"));
  124. menuResume.addEventListener("click", () => closeModal("menu"));
  125. menuSettingsVolume.addEventListener("input", () => {
  126. setVolume(parseFloat(menuSettingsVolume.value));
  127. })
  128. menuSettingsClose.addEventListener("click", () => switchModal("menu"));
  129. menuQuitYes.addEventListener("click", () => {
  130. closeModal("quit");
  131. returnToStart();
  132. });
  133. menuQuitNo.addEventListener("click", () => switchModal("menu"));
  134. }
  135. window.addEventListener("load", initStart);