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.
 
 
 
 

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