a munch adventure
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

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