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

253 řádky
6.9 KiB

  1. let currentRoom = null;
  2. let currentDialog = null;
  3. let dirButtons = [];
  4. let actionButtons = [];
  5. let dialogButtons = [];
  6. let mode = "explore";
  7. let actions = [];
  8. let time = 9*60;
  9. let newline = " ";
  10. let player = new Player();
  11. function Object(name="Potato") {
  12. this.name = name;
  13. this.actions = [];
  14. }
  15. function Burger() {
  16. Object.call(this, "Burger");
  17. this.actions.push({
  18. "name": "Punch Burger",
  19. "action": function() {
  20. player.health += 10;
  21. update(["You punch the hamburger."]);
  22. }
  23. });
  24. }
  25. function Nerd() {
  26. Object.call(this, "Nerd");
  27. this.actions.push({
  28. "name": "Eat Nerd",
  29. "action": function() {
  30. startDialog(new EatDude());
  31. }
  32. });
  33. }
  34. function startDialog(dialog) {
  35. mode = "dialog";
  36. currentDialog = dialog;
  37. updateDisplay();
  38. }
  39. function updateExploreCompass() {
  40. for (let i = 0; i < dirButtons.length; i++) {
  41. let button = dirButtons[i];
  42. if (currentRoom.exits[i] == null) {
  43. button.disabled = true;
  44. button.classList.remove("active-compass-button");
  45. button.classList.add("inactive-button");
  46. button.innerHTML = "";
  47. } else {
  48. button.disabled = false;
  49. button.classList.remove("inactive-button");
  50. button.classList.add("active-compass-button");
  51. button.innerHTML = currentRoom.exits[i].name;
  52. }
  53. }
  54. }
  55. function updateExploreActions() {
  56. for (let i = 0; i < actionButtons.length; i++) {
  57. if (i < actions.length) {
  58. actionButtons[i].disabled = false;
  59. actionButtons[i].innerHTML = actions[i].name;
  60. actionButtons[i].classList.remove("inactive-button");
  61. actionButtons[i].classList.add("active-button");
  62. }
  63. else {
  64. actionButtons[i].disabled = true;
  65. actionButtons[i].innerHTML = "";
  66. actionButtons[i].classList.remove("active-button");
  67. actionButtons[i].classList.add("inactive-button");
  68. }
  69. }
  70. }
  71. function updateExplore() {
  72. updateExploreCompass();
  73. updateExploreActions();
  74. }
  75. function updateCombat() {
  76. }
  77. function updateDialog() {
  78. for (let i = 0; i < dialogButtons.length; i++) {
  79. if (i < currentDialog.choices.length) {
  80. dialogButtons[i].disabled = false;
  81. dialogButtons[i].innerHTML = currentDialog.choices[i].text;
  82. dialogButtons[i].classList.remove("inactive-button");
  83. dialogButtons[i].classList.add("active-button");
  84. } else {
  85. dialogButtons[i].disabled = true;
  86. dialogButtons[i].innerHTML = "";
  87. dialogButtons[i].classList.remove("active-button");
  88. dialogButtons[i].classList.add("inactive-button");
  89. }
  90. }
  91. }
  92. function updateDisplay() {
  93. switch(mode) {
  94. case "explore":
  95. document.getElementById("selector-explore").style.display = "flex";
  96. document.getElementById("selector-combat").style.display = "none";
  97. document.getElementById("selector-dialog").style.display = "none";
  98. updateExplore();
  99. break;
  100. case "combat":
  101. document.getElementById("selector-explore").style.display = "none";
  102. document.getElementById("selector-combat").style.display = "flex";
  103. document.getElementById("selector-dialog").style.display = "none";
  104. updateCombat();
  105. case "dialog":
  106. document.getElementById("selector-explore").style.display = "none";
  107. document.getElementById("selector-combat").style.display = "none";
  108. document.getElementById("selector-dialog").style.display = "flex";
  109. updateDialog();
  110. break;
  111. }
  112. document.getElementById("time").innerHTML = "Time: " + renderTime(time);
  113. document.getElementById("stat-name").innerHTML = "Name: " + player.name;
  114. document.getElementById("stat-health").innerHTML = "Health: " + player.health + "/" + player.maxHealth;
  115. document.getElementById("stat-fullness").innerHTML = "Fullness: " + player.fullness + "/" + player.maxFullness;
  116. }
  117. function advanceTime(amount) {
  118. time = (time + amount) % 1440;
  119. }
  120. function renderTime(time) {
  121. let suffix = (time < 720) ? "AM" : "PM";
  122. let hour = Math.floor((time % 720) / 60);
  123. if (hour == 0)
  124. hour = 12;
  125. let minute = time % 60;
  126. if (minute < 9)
  127. minute = "0" + minute;
  128. return hour + ":" + minute + " " + suffix;
  129. }
  130. function move(direction) {
  131. let target = currentRoom.exits[direction];
  132. if (target == null) {
  133. alert("Tried to move to an empty room!");
  134. return;
  135. }
  136. moveTo(target);
  137. }
  138. function moveTo(room) {
  139. actions = [];
  140. currentRoom = room;
  141. advanceTime(1);
  142. currentRoom.objects.forEach(function (object) {
  143. object.actions.forEach(function (action) {
  144. actions.push(action);
  145. })
  146. })
  147. update(["You move to " + currentRoom.name,currentRoom.description,newline]);
  148. }
  149. window.addEventListener('load', function(event) {
  150. loadActions();
  151. loadCompass();
  152. loadDialog();
  153. currentRoom = createWorld();
  154. currentRoom.objects.push(new Burger());
  155. currentRoom.objects.push(new Nerd());
  156. moveTo(currentRoom);
  157. updateDisplay();
  158. });
  159. function update(lines=[]) {
  160. let log = document.getElementById("log");
  161. for (let i=0; i<lines.length; i++) {
  162. let div = document.createElement("div");
  163. div.innerHTML = lines[i];
  164. log.appendChild(div);
  165. }
  166. updateDisplay();
  167. }
  168. function dialogClicked(index) {
  169. currentDialog = currentDialog.choices[index].node;
  170. update([currentDialog.visit()]);
  171. if (currentDialog.choices.length == 0) {
  172. mode = "explore";
  173. updateDisplay();
  174. }
  175. }
  176. function loadDialog() {
  177. dialogButtons = Array.from( document.querySelectorAll(".dialog-button"));
  178. for (let i = 0; i < dialogButtons.length; i++) {
  179. dialogButtons[i].addEventListener("click", function() { dialogClicked(i); });
  180. }
  181. }
  182. function actionClicked(index) {
  183. actions[index].action();
  184. }
  185. function loadActions() {
  186. actionButtons = Array.from( document.querySelectorAll(".action-button"));
  187. for (let i = 0; i < actionButtons.length; i++) {
  188. actionButtons[i].addEventListener("click", function() { actionClicked(i); });
  189. }
  190. }
  191. function loadCompass() {
  192. dirButtons[NORTH_WEST] = document.getElementById("compass-north-west");
  193. dirButtons[NORTH_WEST].addEventListener("click", function() {
  194. move(NORTH_WEST);
  195. });
  196. dirButtons[NORTH] = document.getElementById("compass-north");
  197. dirButtons[NORTH].addEventListener("click", function() {
  198. move(NORTH);
  199. });
  200. dirButtons[NORTH_EAST] = document.getElementById("compass-north-east");
  201. dirButtons[NORTH_EAST].addEventListener("click", function() {
  202. move(NORTH_EAST);
  203. });
  204. dirButtons[WEST] = document.getElementById("compass-west");
  205. dirButtons[WEST].addEventListener("click", function() {
  206. move(WEST);
  207. });
  208. dirButtons[EAST] = document.getElementById("compass-east");
  209. dirButtons[EAST].addEventListener("click", function() {
  210. move(EAST);
  211. });
  212. dirButtons[SOUTH_WEST] = document.getElementById("compass-south-west");
  213. dirButtons[SOUTH_WEST].addEventListener("click", function() {
  214. move(SOUTH_WEST);
  215. });
  216. dirButtons[SOUTH] = document.getElementById("compass-south");
  217. dirButtons[SOUTH].addEventListener("click", function() {
  218. move(SOUTH);
  219. });
  220. dirButtons[SOUTH_EAST] = document.getElementById("compass-south-east");
  221. dirButtons[SOUTH_EAST].addEventListener("click", function() {
  222. move(SOUTH_EAST);
  223. });
  224. }