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

155 lines
3.0 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. It has a bed in it.",
  18. "conn": [
  19. {
  20. "name": "Bathroom",
  21. "dir": EAST,
  22. "desc": "You step into your bathroom."
  23. },
  24. {
  25. "name": "Living Room",
  26. "dir": NORTH,
  27. "desc": "You walk into the living room."
  28. }
  29. ],
  30. "objs": [
  31. Bed
  32. ]
  33. },
  34. {
  35. "name": "Bathroom",
  36. "desc": "Your modest bathroom.",
  37. "conn": [
  38. {
  39. "name": "Bedroom",
  40. "dir": WEST,
  41. "desc": "You walk back into your bedroom."
  42. }
  43. ],
  44. "objs": [
  45. Toilet
  46. ]
  47. },
  48. {
  49. "name": "Living Room",
  50. "desc": "A bare living room",
  51. "conn": [
  52. {
  53. "name": "Street",
  54. "dir": NORTH,
  55. "desc": "You step outside."
  56. },
  57. {
  58. "name": "Bedroom",
  59. "dir": SOUTH,
  60. "desc": "You walk into your bedroom."
  61. }
  62. ],
  63. "objs": [
  64. TV,
  65. Phone
  66. ]
  67. },
  68. {
  69. "name": "Street",
  70. "desc": "It's a street",
  71. "conn": [
  72. {
  73. "name": "Alley",
  74. "dir": WEST,
  75. "desc": "You wander into the dark alley."
  76. },
  77. {
  78. "name": "Living Room",
  79. "dir": SOUTH,
  80. "desc": "You step back into your apartment."
  81. }
  82. ],
  83. "objs": [
  84. Nerd
  85. ]
  86. },
  87. {
  88. "name": "Alley",
  89. "desc": "A suspicious alley",
  90. "conn": [
  91. {
  92. "name": "Street",
  93. "dir": EAST,
  94. "desc": "You hurry back into the open street."
  95. }
  96. ],
  97. "objs": [
  98. ]
  99. }
  100. ]
  101. function Location(name="Nowhere",desc="Nada") {
  102. this.name = name;
  103. this.description = desc;
  104. this.exits = [null,null,null,null,null,null,null,null];
  105. this.exitDescs = [null,null,null,null,null,null,null,null];
  106. this.objects = [];
  107. }
  108. function opposite(direction) {
  109. return (direction + 4) % 8;
  110. }
  111. function connectLocations(loc1,loc2,dir,desc) {
  112. if (loc1.exits[dir] != null) {
  113. alert(loc1.name + " is already connected to " + loc1.exits[dir].name);
  114. return;
  115. } else {
  116. if (dir >= 0 && dir <= 7) {
  117. loc1.exits[dir] = loc2;
  118. loc1.exitDescs[dir] = desc;
  119. } else {
  120. alert("Invalid direction given when linking " + loc1.name + " and " + loc2.name + ": " + dir);
  121. }
  122. }
  123. }
  124. function createWorld() {
  125. for (let i = 0; i < locationsSrc.length; i++) {
  126. let src = locationsSrc[i];
  127. let location = new Location(src.name,src.desc);
  128. locations[src.name] = location;
  129. src.objs.forEach(function (obj) {
  130. location.objects.push(new obj());
  131. });
  132. }
  133. for (let i = 0; i < locationsSrc.length; i++) {
  134. let src = locationsSrc[i];
  135. let from = locations[src.name];
  136. for (let j = 0; j < src.conn.length; j++) {
  137. let to = locations[src.conn[j].name];
  138. connectLocations(from, to, src.conn[j].dir, src.conn[j].desc);
  139. }
  140. }
  141. return locations[startLocation];
  142. }