munch
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

114 řádky
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. this.objects = [];
  67. }
  68. function opposite(direction) {
  69. return (direction + 4) % 8;
  70. }
  71. function connectLocations(loc1,loc2,loc1Exit) {
  72. if (loc1.exits[loc1Exit] != null) {
  73. alert(loc1.name + " is already connected to " + loc1.exits[loc1Exit].name);
  74. return;
  75. } else if (loc2.exits[opposite(loc1Exit)] != null) {
  76. alert(loc2.name + " is already connected to " + loc2.exits[opposite(loc1Exit)].name);
  77. return;
  78. } else {
  79. if (loc1Exit >= 0 && loc1Exit <= 7) {
  80. loc1.exits[loc1Exit] = loc2;
  81. loc2.exits[opposite(loc1Exit)] = loc1;
  82. }
  83. }
  84. }
  85. function createWorld() {
  86. for (let i = 0; i < locationsSrc.length; i++) {
  87. let src = locationsSrc[i];
  88. let location = new Location(src.name,src.desc);
  89. locations[src.name] = location;
  90. }
  91. for (let i = 0; i < locationsSrc.length; i++) {
  92. let src = locationsSrc[i];
  93. let from = locations[src.name];
  94. for (let j = 0; j < src.conn.length; j++) {
  95. let to = locations[src.conn[j].name];
  96. connectLocations(from, to, src.conn[j].dir);
  97. }
  98. }
  99. return locations[startLocation];
  100. }