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.
 
 
 
 

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