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.
 
 
 

212 line
4.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": "North Street",
  54. "dir": WEST,
  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": "North 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": EAST,
  80. "desc": "You step back into your apartment"
  81. },
  82. {
  83. "name": "Crossroads",
  84. "dir": SOUTH,
  85. "desc": "You walk south"
  86. }
  87. ],
  88. "objs": [
  89. Nerd
  90. ]
  91. },
  92. {
  93. "name": "Alley",
  94. "desc": "A suspicious alley",
  95. "conn": [
  96. {
  97. "name": "North Street",
  98. "dir": EAST,
  99. "desc": "You hurry back into the open street."
  100. },
  101. {
  102. "name": "Seedy Bar",
  103. "dir": NORTH,
  104. "desc": "You step into the bar."
  105. }
  106. ],
  107. "objs": [
  108. ]
  109. },
  110. {
  111. "name": "Seedy Bar",
  112. "desc": "God this place is seedy",
  113. "conn": [
  114. {
  115. "name": "Alley",
  116. "dir": SOUTH,
  117. "desc": "You step out of the bar"
  118. }
  119. ],
  120. "objs": [
  121. ]
  122. },
  123. {
  124. "name": "Crossroads",
  125. "desc": "Where the roads cross",
  126. "conn": [
  127. {
  128. "name": "North Street",
  129. "dir": NORTH,
  130. "desc": "You walk north"
  131. },
  132. {
  133. "name": "South Street",
  134. "dir": SOUTH,
  135. "desc": "You walk south"
  136. }
  137. ],
  138. "objs": [
  139. ]
  140. },
  141. {
  142. "name": "South Street",
  143. "desc": "This street is in the south",
  144. "conn": [
  145. {
  146. "name": "Crossroads",
  147. "dir": NORTH,
  148. "desc": "You walk to the crossroads"
  149. }
  150. ],
  151. "objs": [
  152. ]
  153. }
  154. ];
  155. function Location(name="Nowhere",desc="Nada") {
  156. this.name = name;
  157. this.description = desc;
  158. this.exits = [null,null,null,null,null,null,null,null];
  159. this.exitDescs = [null,null,null,null,null,null,null,null];
  160. this.objects = [];
  161. }
  162. function opposite(direction) {
  163. return (direction + 4) % 8;
  164. }
  165. function connectLocations(loc1,loc2,dir,desc) {
  166. if (loc1.exits[dir] != null) {
  167. alert(loc1.name + " is already connected to " + loc1.exits[dir].name);
  168. return;
  169. } else {
  170. if (dir >= 0 && dir <= 7) {
  171. loc1.exits[dir] = loc2;
  172. loc1.exitDescs[dir] = desc;
  173. } else {
  174. alert("Invalid direction given when linking " + loc1.name + " and " + loc2.name + ": " + dir);
  175. }
  176. }
  177. }
  178. function createWorld() {
  179. for (let i = 0; i < locationsSrc.length; i++) {
  180. let src = locationsSrc[i];
  181. let location = new Location(src.name,src.desc);
  182. locations[src.name] = location;
  183. src.objs.forEach(function (obj) {
  184. location.objects.push(new obj());
  185. });
  186. }
  187. for (let i = 0; i < locationsSrc.length; i++) {
  188. let src = locationsSrc[i];
  189. let from = locations[src.name];
  190. for (let j = 0; j < src.conn.length; j++) {
  191. let to = locations[src.conn[j].name];
  192. connectLocations(from, to, src.conn[j].dir, src.conn[j].desc);
  193. }
  194. }
  195. return locations[startLocation];
  196. }