munch
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.
 
 
 

61 lines
1.6 KiB

  1. let currentRoom = null;
  2. function updateDisplay() {
  3. document.getElementById("location").innerHTML = currentRoom.name;
  4. }
  5. function move(direction) {
  6. let target = currentRoom.exits[direction];
  7. if (target == null) {
  8. alert("Tried to move to an empty room!");
  9. return;
  10. } else {
  11. currentRoom = target;
  12. update(["You move to " + currentRoom.name]);
  13. updateDisplay();
  14. }
  15. }
  16. window.addEventListener('load', function(event) {
  17. loadCompass();
  18. currentRoom = createWorld();
  19. updateDisplay();
  20. });
  21. function update(lines=[]) {
  22. let log = document.getElementById("log");
  23. for (let i=0; i<lines.length; i++) {
  24. let div = document.createElement("div");
  25. div.innerHTML = lines[i];
  26. log.appendChild(div);
  27. }
  28. }
  29. function loadCompass() {
  30. document.getElementById("compass-north-west").addEventListener("click", function() {
  31. move(NORTH_WEST);
  32. });
  33. document.getElementById("compass-north").addEventListener("click", function() {
  34. move(NORTH);
  35. });
  36. document.getElementById("compass-north-east").addEventListener("click", function() {
  37. move(NORTH_EAST);
  38. });
  39. document.getElementById("compass-west").addEventListener("click", function() {
  40. move(WEST);
  41. });
  42. document.getElementById("compass-east").addEventListener("click", function() {
  43. move(EAST);
  44. });
  45. document.getElementById("compass-south-west").addEventListener("click", function() {
  46. move(SOUTH_WEST);
  47. });
  48. document.getElementById("compass-south").addEventListener("click", function() {
  49. move(SOUTH);
  50. });
  51. document.getElementById("compass-south-east").addEventListener("click", function() {
  52. move(SOUTH_EAST);
  53. });
  54. }