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.
 
 
 
 

209 lines
4.5 KiB

  1. dirs = {
  2. "up-left": "Northwest",
  3. "up": "North",
  4. "up-right": "Northeast",
  5. "left": "West",
  6. "right": "East",
  7. "down-left": "Southwest",
  8. "down": "South",
  9. "down-right": "Southeast",
  10. "ascend": "Up",
  11. "descend": "Down"
  12. }
  13. function initWorld(story, state) {
  14. state.world = story["world"];
  15. initRoomState(state);
  16. }
  17. function initRoomState(state) {
  18. state.player.rooms = {};
  19. Object.entries(state.world).forEach(([key, val]) => {
  20. state.player.rooms[key] = {};
  21. });
  22. }
  23. function showActionDescription(desc) {
  24. const descHolder = document.querySelector("#desc");
  25. descHolder.textContent = desc;
  26. }
  27. function removeActionDescription() {
  28. const descHolder = document.querySelector("#desc");
  29. descHolder.textContent = "";
  30. }
  31. function moveToRoom(src, exit, dest, state) {
  32. const from = state.world[state.player.location];
  33. const room = state.world[dest];
  34. if (exit.hooks) {
  35. for (let hook of exit.hooks) {
  36. if (!hook(room, exit, state)) {
  37. return;
  38. }
  39. }
  40. }
  41. if (room.hooks) {
  42. for (let hook of room.hooks) {
  43. if (!hook(room, state)) {
  44. return;
  45. }
  46. }
  47. }
  48. if (exit.move)
  49. exit.move(from, state);
  50. if (from && from.exit)
  51. from.exit(from, state);
  52. if (room.move)
  53. room.move(room, state);
  54. if (room.enter)
  55. room.enter(room, state);
  56. state.player.location = dest;
  57. refresh();
  58. }
  59. function goToRoom(dest, state) {
  60. const from = state.world[state.player.location];
  61. const room = state.world[dest];
  62. if (room.hooks) {
  63. for (let hook of room.hooks) {
  64. if (!hook(room, state)) {
  65. return;
  66. }
  67. }
  68. }
  69. if (from && from.exit)
  70. from.exit(from, state);
  71. if (room.enter)
  72. room.enter(state.world[dest], state);
  73. state.player.location = dest;
  74. refresh();
  75. }
  76. function updateRoom(state) {
  77. const name = state.player.location;
  78. const room = state.world[name];
  79. if (!state.player.rooms[room.id]) {
  80. state.player.rooms[room.id] = {};
  81. }
  82. const areaName = document.querySelector("#area-name");
  83. const areaDesc = document.querySelector("#area-desc");
  84. areaName.textContent = room.name;
  85. areaDesc.textContent = room.desc;
  86. const moveHolder = document.querySelector("#move-holder");
  87. moveHolder.innerHTML = "";
  88. Object.entries(dirs).forEach(([dir, name]) => {
  89. const button = document.createElement("button");
  90. button.classList.add("move-button")
  91. button.id = "move-" + dir;
  92. button.classList.add("disabled");
  93. button.setAttribute("disabled", "true");
  94. button.textContent = dirs[dir];
  95. moveHolder.appendChild(button);
  96. });
  97. if (room.exits) {
  98. Object.entries(room.exits).forEach(([dir, exit]) => {
  99. const button = document.querySelector("#move-" + dir);
  100. const dest = state.world[exit.target];
  101. // don't even show an exit if this fails!
  102. if (exit.show) {
  103. if (!exit.show.every(cond => cond(room, state))) {
  104. return;
  105. }
  106. }
  107. button.textContent = dest.name;
  108. // if any condition fails, don't enable/add a listener
  109. if (exit.conditions) {
  110. if (!exit.conditions.every(cond => cond(room,state))) {
  111. return;
  112. }
  113. }
  114. button.classList.remove("disabled");
  115. button.removeAttribute("disabled");
  116. button.addEventListener("click", () => {
  117. // todo: log
  118. moveToRoom(room, exit, exit.target, state);
  119. })
  120. button.addEventListener("mouseenter", () => {
  121. showActionDescription(exit.desc);
  122. });
  123. button.addEventListener("mouseleave", () => {
  124. removeActionDescription();
  125. });
  126. });
  127. }
  128. const actionHolder = document.querySelector("#actions");
  129. actionHolder.innerHTML = "";
  130. if (room.actions) {
  131. room.actions.forEach(action => {
  132. const button = document.createElement("button");
  133. button.classList.add("action-button");
  134. if (action.show) {
  135. if (!action.show.every(cond => cond(room, state))) {
  136. return;
  137. }
  138. }
  139. button.textContent = action.name;
  140. actionHolder.appendChild(button);
  141. if (action.conditions) {
  142. if (!action.conditions.every(cond => cond(room, state))) {
  143. button.classList.add("disabled");
  144. button.setAttribute("disabled", "true");
  145. return;
  146. }
  147. }
  148. button.addEventListener("click", () => {
  149. action.execute(room, state);
  150. refresh();
  151. });
  152. button.addEventListener("mouseenter", () => {
  153. showActionDescription(action.desc);
  154. });
  155. button.addEventListener("mouseleave", () => {
  156. removeActionDescription();
  157. });
  158. });
  159. }
  160. }