munch
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

113 rindas
2.1 KiB

  1. "use strict";
  2. /*jshint browser: true*/
  3. /*jshint devel: true*/
  4. let NORTH = 0;
  5. let NORTH_EAST = 1;
  6. let EAST = 2;
  7. let SOUTH_EAST = 3;
  8. let SOUTH = 4;
  9. let SOUTH_WEST = 5;
  10. let WEST = 6;
  11. let NORTH_WEST = 7;
  12. let startLocation = "Bedroom";
  13. let locations = {};
  14. let locationsSrc = [
  15. {
  16. "name": "Bedroom",
  17. "desc": "A bedroom",
  18. "conn": [
  19. {
  20. "name": "Bathroom",
  21. "dir": EAST
  22. },
  23. {
  24. "name": "Living Room",
  25. "dir": NORTH
  26. }
  27. ]
  28. },
  29. {
  30. "name": "Bathroom",
  31. "desc": "The bathroom",
  32. "conn": [
  33. ]
  34. },
  35. {
  36. "name": "Living Room",
  37. "desc": "A bare living room",
  38. "conn": [
  39. {
  40. "name": "Street",
  41. "dir": NORTH
  42. }
  43. ]
  44. },
  45. {
  46. "name": "Street",
  47. "desc": "It's a street",
  48. "conn": [
  49. {
  50. "name": "Alley",
  51. "dir": WEST
  52. }
  53. ]
  54. },
  55. {
  56. "name": "Alley",
  57. "desc": "A suspicious alley",
  58. "conn": [
  59. ]
  60. }
  61. ]
  62. function Location(name="Nowhere",desc="Nada") {
  63. this.name = name;
  64. this.description = desc;
  65. this.exits = [null,null,null,null,null,null,null,null];
  66. }
  67. function opposite(direction) {
  68. return (direction + 4) % 8;
  69. }
  70. function connectLocations(loc1,loc2,loc1Exit) {
  71. if (loc1.exits[loc1Exit] != null) {
  72. alert(loc1.name + " is already connected to " + loc1.exits[loc1Exit].name);
  73. return;
  74. } else if (loc2.exits[opposite(loc1Exit)] != null) {
  75. alert(loc2.name + " is already connected to " + loc2.exits[opposite(loc1Exit)].name);
  76. return;
  77. } else {
  78. if (loc1Exit >= 0 && loc1Exit <= 7) {
  79. loc1.exits[loc1Exit] = loc2;
  80. loc2.exits[opposite(loc1Exit)] = loc1;
  81. }
  82. }
  83. }
  84. function createWorld() {
  85. for (let i = 0; i < locationsSrc.length; i++) {
  86. let src = locationsSrc[i];
  87. let location = new Location(src.name,src.desc);
  88. locations[src.name] = location;
  89. }
  90. for (let i = 0; i < locationsSrc.length; i++) {
  91. let src = locationsSrc[i];
  92. let from = locations[src.name];
  93. for (let j = 0; j < src.conn.length; j++) {
  94. let to = locations[src.conn[j].name];
  95. connectLocations(from, to, src.conn[j].dir);
  96. }
  97. }
  98. return locations[startLocation];
  99. }