a munch adventure
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

182 satır
3.9 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(story, state) {
  14. state.world = story["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. }