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.
 
 
 
 

93 lines
2.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 moveToRoom(dest, state) {
  14. updateRoom(dest, state);
  15. }
  16. function updateRoom(dest, state) {
  17. const room = world[dest];
  18. const areaName = document.querySelector("#area-name");
  19. const areaDesc = document.querySelector("#area-desc");
  20. areaName.innerText = room.name;
  21. areaDesc.innerText = room.desc;
  22. const holder = document.querySelector("#move-holder");
  23. holder.innerHTML = "";
  24. Object.entries(dirs).forEach(([dir, name]) => {
  25. const button = document.createElement("button");
  26. button.classList.add("move-button")
  27. button.id = "move-" + dir;
  28. button.classList.add("disabled");
  29. button.setAttribute("disabled", "true");
  30. button.innerText = dirs[dir];
  31. holder.appendChild(button);
  32. });
  33. Object.entries(room.exits).forEach(([dir, exit]) => {
  34. const button = document.querySelector("#move-" + dir);
  35. const dest = world[exit.target];
  36. // if any condition fails, don't enable/add a listener
  37. if (exit.conditions) {
  38. console.log(exit.conditions);
  39. if (!exit.conditions.every(cond => cond(state))) {
  40. return;
  41. }
  42. }
  43. button.classList.remove("disabled");
  44. button.removeAttribute("disabled");
  45. button.innerText = dest.name;
  46. button.addEventListener("click", () => {
  47. // todo: log
  48. moveToRoom(exit.target, state);
  49. })
  50. });
  51. }
  52. world = {
  53. "Home": {
  54. "name": "Home",
  55. "desc": "Where the wifi autoconnects",
  56. "exits": {
  57. "up": {
  58. "target": "Locked Room",
  59. "desc": "It's locked!",
  60. "move": "You enter the secret locked room",
  61. "conditions": [
  62. state => {
  63. return state.player.items.keys.includes("Locked Room");
  64. }
  65. ]
  66. }
  67. }
  68. },
  69. "Locked Room": {
  70. "name": "Locked Room",
  71. "desc": "Super seecret",
  72. "exits": {
  73. "down": {
  74. "target": "Home",
  75. "desc": "Back to home",
  76. "move": "You dab"
  77. }
  78. }
  79. }
  80. }