munch
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

174 lignes
3.4 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. "name": "Seedy Bar",
  98. "dir": NORTH,
  99. "desc": "You step into the bar."
  100. }
  101. ],
  102. "objs": [
  103. ]
  104. },
  105. {
  106. "name": "Seedy Bar",
  107. "desc": "God this place is seedy",
  108. "conn": [
  109. {
  110. "name": "Alley",
  111. "dir": SOUTH,
  112. "desc": "You step out of the bar."
  113. }
  114. ],
  115. "objs": [
  116. ]
  117. }
  118. ];
  119. function Location(name="Nowhere",desc="Nada") {
  120. this.name = name;
  121. this.description = desc;
  122. this.exits = [null,null,null,null,null,null,null,null];
  123. this.exitDescs = [null,null,null,null,null,null,null,null];
  124. this.objects = [];
  125. }
  126. function opposite(direction) {
  127. return (direction + 4) % 8;
  128. }
  129. function connectLocations(loc1,loc2,dir,desc) {
  130. if (loc1.exits[dir] != null) {
  131. alert(loc1.name + " is already connected to " + loc1.exits[dir].name);
  132. return;
  133. } else {
  134. if (dir >= 0 && dir <= 7) {
  135. loc1.exits[dir] = loc2;
  136. loc1.exitDescs[dir] = desc;
  137. } else {
  138. alert("Invalid direction given when linking " + loc1.name + " and " + loc2.name + ": " + dir);
  139. }
  140. }
  141. }
  142. function createWorld() {
  143. for (let i = 0; i < locationsSrc.length; i++) {
  144. let src = locationsSrc[i];
  145. let location = new Location(src.name,src.desc);
  146. locations[src.name] = location;
  147. src.objs.forEach(function (obj) {
  148. location.objects.push(new obj());
  149. });
  150. }
  151. for (let i = 0; i < locationsSrc.length; i++) {
  152. let src = locationsSrc[i];
  153. let from = locations[src.name];
  154. for (let j = 0; j < src.conn.length; j++) {
  155. let to = locations[src.conn[j].name];
  156. connectLocations(from, to, src.conn[j].dir, src.conn[j].desc);
  157. }
  158. }
  159. return locations[startLocation];
  160. }