munch
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

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