a munch adventure
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

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