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.
 
 
 
 

228 line
5.1 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(state) {
  14. initRoomState(state);
  15. }
  16. function initRoomState(state) {
  17. state.player.rooms = {};
  18. Object.entries(world).forEach(([key, val]) => {
  19. state.player.rooms[key] = {};
  20. });
  21. }
  22. function showActionDescription(desc) {
  23. const descHolder = document.querySelector("#desc");
  24. descHolder.textContent = desc;
  25. }
  26. function removeActionDescription() {
  27. const descHolder = document.querySelector("#desc");
  28. descHolder.textContent = "";
  29. }
  30. function moveToRoom(dest, state) {
  31. const room = world[dest];
  32. console.log(room);
  33. if (room.hooks) {
  34. for (let hook of room.hooks) {
  35. if (!hook(room, state)) {
  36. return;
  37. }
  38. }
  39. }
  40. updateRoom(dest, state);
  41. }
  42. function updateRoom(dest, state) {
  43. const room = world[dest];
  44. if (!state.player.rooms[dest.id]) {
  45. state.player.rooms[dest.id] = {};
  46. }
  47. const areaName = document.querySelector("#area-name");
  48. const areaDesc = document.querySelector("#area-desc");
  49. areaName.textContent = room.name;
  50. areaDesc.textContent = room.desc;
  51. const moveHolder = document.querySelector("#move-holder");
  52. moveHolder.innerHTML = "";
  53. Object.entries(dirs).forEach(([dir, name]) => {
  54. const button = document.createElement("button");
  55. button.classList.add("move-button")
  56. button.id = "move-" + dir;
  57. button.classList.add("disabled");
  58. button.setAttribute("disabled", "true");
  59. button.textContent = dirs[dir];
  60. moveHolder.appendChild(button);
  61. });
  62. if (room.exits) {
  63. Object.entries(room.exits).forEach(([dir, exit]) => {
  64. const button = document.querySelector("#move-" + dir);
  65. const dest = world[exit.target];
  66. // don't even show an exit if this fails!
  67. if (exit.show) {
  68. if (!exit.show.every(cond => cond(room, state))) {
  69. return;
  70. }
  71. }
  72. button.textContent = dest.name;
  73. // if any condition fails, don't enable/add a listener
  74. if (exit.conditions) {
  75. if (!exit.conditions.every(cond => cond(room,state))) {
  76. return;
  77. }
  78. }
  79. button.classList.remove("disabled");
  80. button.removeAttribute("disabled");
  81. button.addEventListener("click", () => {
  82. // todo: log
  83. moveToRoom(exit.target, state);
  84. })
  85. });
  86. }
  87. const actionHolder = document.querySelector("#actions");
  88. actionHolder.innerHTML = "";
  89. if (room.actions) {
  90. room.actions.forEach(action => {
  91. const button = document.createElement("button");
  92. button.classList.add("action-button");
  93. if (action.show) {
  94. if (!action.show.every(cond => cond(room, state))) {
  95. return;
  96. }
  97. }
  98. button.textContent = action.name;
  99. actionHolder.appendChild(button);
  100. if (action.conditions) {
  101. if (!action.conditions.every(cond => cond(room, state))) {
  102. button.classList.add("disabled");
  103. button.setAttribute("disabled", "true");
  104. return;
  105. }
  106. }
  107. button.addEventListener("click", () => {
  108. action.execute(room, state);
  109. updateRoom(room.id, state);
  110. });
  111. button.addEventListener("mouseenter", () => {
  112. showActionDescription(action.desc);
  113. });
  114. button.addEventListener("mouseleave", () => {
  115. removeActionDescription();
  116. });
  117. });
  118. }
  119. }
  120. world = {
  121. "Home": {
  122. "id": "Home",
  123. "name": "Home",
  124. "desc": "Where the wifi autoconnects",
  125. "actions": [
  126. {
  127. "name": "Squint",
  128. "desc": "Squint in a very aggressive manner",
  129. "execute": (self, state) => {
  130. state.player.rooms[self.id].squinted = true;
  131. print(["You stare at the wall and notice a secret door. But where is the key?"]);
  132. }
  133. },
  134. {
  135. "name": "Find Keys",
  136. "desc": "Find your keys",
  137. "execute": (self, state) => {
  138. state.player.items.keys.push("Locked Room");
  139. print(["You found your keys under the couch cushions"]);
  140. },
  141. "show": [
  142. (self, state) => {
  143. return state.player.rooms[self.id].squinted;
  144. },
  145. (self, state) => {
  146. return !state.player.items.keys.includes("Locked Room");
  147. }
  148. ]
  149. }
  150. ],
  151. "exits": {
  152. "up": {
  153. "target": "Locked Room",
  154. "desc": "It's locked!",
  155. "move": "You enter the secret locked room",
  156. "conditions": [
  157. (self, state) => {
  158. return state.player.items.keys.includes("Locked Room");
  159. }
  160. ],
  161. "show": [
  162. (self, state) => {
  163. console.log(self);
  164. return state.player.rooms[self.id].squinted;
  165. }
  166. ]
  167. }
  168. },
  169. "hooks": [
  170. (self, state) => {
  171. print(["This is a test of the hooks"]);
  172. return true;
  173. }
  174. ]
  175. },
  176. "Locked Room": {
  177. "id": "Locked Room",
  178. "name": "Locked Room",
  179. "desc": "Super seecret",
  180. "exits": {
  181. "down": {
  182. "target": "Home",
  183. "desc": "Back to home",
  184. "move": "You dab"
  185. }
  186. }
  187. }
  188. }