munch
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

354 satır
9.3 KiB

  1. let currentRoom = null;
  2. let currentDialog = null;
  3. let dirButtons = [];
  4. let actionButtons = [];
  5. let mode = "explore";
  6. let actions = [];
  7. let time = 9*60*60;
  8. let newline = " ";
  9. let player = new Player();
  10. let respawnRoom;
  11. function round(number, digits) {
  12. return Math.round(number * Math.pow(10,digits)) / Math.pow(10,digits);
  13. }
  14. function updateExploreCompass() {
  15. for (let i = 0; i < dirButtons.length; i++) {
  16. let button = dirButtons[i];
  17. if (currentRoom.exits[i] == null) {
  18. button.disabled = true;
  19. button.classList.remove("active-compass-button");
  20. button.classList.add("inactive-button");
  21. button.innerHTML = "";
  22. } else {
  23. button.disabled = false;
  24. button.classList.remove("inactive-button");
  25. button.classList.add("active-compass-button");
  26. button.innerHTML = currentRoom.exits[i].name;
  27. }
  28. }
  29. }
  30. function updateExploreActions() {
  31. for (let i = 0; i < actionButtons.length; i++) {
  32. if (i < actions.length) {
  33. actionButtons[i].disabled = false;
  34. actionButtons[i].innerHTML = actions[i].name;
  35. actionButtons[i].classList.remove("inactive-button");
  36. actionButtons[i].classList.add("active-button");
  37. }
  38. else {
  39. actionButtons[i].disabled = true;
  40. actionButtons[i].innerHTML = "";
  41. actionButtons[i].classList.remove("active-button");
  42. actionButtons[i].classList.add("inactive-button");
  43. }
  44. }
  45. }
  46. function updateExplore() {
  47. updateExploreCompass();
  48. updateExploreActions();
  49. }
  50. function updateEaten() {
  51. let list = document.getElementById("eaten");
  52. while(list.firstChild) {
  53. list.removeChild(list.firstChild);
  54. }
  55. for (let i = 0; i < currentFoe.struggles.length; i++) {
  56. let li = document.createElement("li");
  57. let button = document.createElement("button");
  58. button.classList.add("eaten-button");
  59. button.innerHTML = currentFoe.struggles[i].name;
  60. button.addEventListener("click", function() { struggleClicked(i); } );
  61. button.addEventListener("mouseover", function() { struggleHovered(i); } );
  62. li.appendChild(button);
  63. list.appendChild(li);
  64. }
  65. }
  66. function updateCombat() {
  67. let list = document.getElementById("combat");
  68. while(list.firstChild) {
  69. list.removeChild(list.firstChild);
  70. }
  71. for (let i = 0; i < player.attacks.length; i++) {
  72. let li = document.createElement("li");
  73. let button = document.createElement("button");
  74. button.classList.add("combat-button");
  75. button.innerHTML = player.attacks[i].name;
  76. button.addEventListener("click", function() { attackClicked(i); } );
  77. button.addEventListener("mouseover", function() { attackHovered(i); } );
  78. li.appendChild(button);
  79. list.appendChild(li);
  80. }
  81. }
  82. function updateDialog() {
  83. let list = document.getElementById("dialog");
  84. while(list.firstChild) {
  85. list.removeChild(list.firstChild);
  86. }
  87. for (let i = 0; i < currentDialog.choices.length; i++) {
  88. let li = document.createElement("li");
  89. let button = document.createElement("button");
  90. button.classList.add("dialog-button");
  91. button.innerHTML = currentDialog.choices[i].text;
  92. button.addEventListener("click", function() { dialogClicked(i); });
  93. li.appendChild(button);
  94. list.appendChild(li);
  95. }
  96. }
  97. function updateDisplay() {
  98. document.querySelectorAll(".selector").forEach(function (x) {
  99. x.style.display = "none";
  100. });
  101. switch(mode) {
  102. case "explore":
  103. document.getElementById("selector-explore").style.display = "flex";
  104. updateExplore();
  105. break;
  106. case "combat":
  107. document.getElementById("selector-combat").style.display = "flex";
  108. updateCombat();
  109. break;
  110. case "dialog":
  111. document.getElementById("selector-dialog").style.display = "flex";
  112. updateDialog();
  113. break;
  114. case "eaten":
  115. document.getElementById("selector-eaten").style.display = "flex";
  116. updateEaten();
  117. break;
  118. }
  119. document.getElementById("time").innerHTML = "Time: " + renderTime(time);
  120. document.getElementById("stat-name").innerHTML = "Name: " + player.name;
  121. document.getElementById("stat-health").innerHTML = "Health: " + player.health + "/" + player.maxHealth;
  122. document.getElementById("stat-fullness").innerHTML = "Fullness: " + round(player.fullness(),0);
  123. }
  124. function advanceTime(amount) {
  125. time = (time + amount) % 86400;
  126. update(player.stomach.digest(amount));
  127. }
  128. function renderTime(time) {
  129. let suffix = (time < 43200) ? "AM" : "PM";
  130. let hour = Math.floor((time % 43200) / 3600);
  131. if (hour == 0)
  132. hour = 12;
  133. let minute = Math.floor(time / 60) % 60;
  134. if (minute < 9)
  135. minute = "0" + minute;
  136. return hour + ":" + minute + " " + suffix;
  137. }
  138. function move(direction) {
  139. let target = currentRoom.exits[direction];
  140. if (target == null) {
  141. alert("Tried to move to an empty room!");
  142. return;
  143. }
  144. moveTo(target,currentRoom.exitDescs[direction]);
  145. }
  146. function moveTo(room,desc="You go places lol") {
  147. actions = [];
  148. currentRoom = room;
  149. advanceTime(30);
  150. currentRoom.objects.forEach(function (object) {
  151. object.actions.forEach(function (action) {
  152. actions.push(action);
  153. });
  154. });
  155. update([desc,newline]);
  156. currentRoom.visit();
  157. }
  158. window.addEventListener('load', function(event) {
  159. loadActions();
  160. loadCompass();
  161. loadDialog();
  162. currentRoom = createWorld();
  163. respawnRoom = currentRoom;
  164. moveTo(currentRoom);
  165. updateDisplay();
  166. });
  167. function update(lines=[]) {
  168. let log = document.getElementById("log");
  169. for (let i=0; i<lines.length; i++) {
  170. let div = document.createElement("div");
  171. div.innerHTML = lines[i];
  172. log.appendChild(div);
  173. }
  174. log.scrollTop = log.scrollHeight;
  175. updateDisplay();
  176. }
  177. function changeMode(newMode) {
  178. mode = newMode;
  179. let body = document.querySelector("body");
  180. body.className = "";
  181. switch(mode) {
  182. case "explore":
  183. case "dialog":
  184. body.classList.add("explore");
  185. break;
  186. case "combat":
  187. body.classList.add("combat");
  188. break;
  189. case "eaten":
  190. body.classList.add("eaten");
  191. break;
  192. }
  193. updateDisplay();
  194. }
  195. function startCombat(opponent) {
  196. changeMode("combat");
  197. currentFoe = opponent;
  198. update(["Oh shit it's a " + opponent.description()]);
  199. }
  200. function attackClicked(index) {
  201. update([player.attacks[index].attack(currentFoe)]);
  202. if (currentFoe.health <= 0) {
  203. update(["The " + currentFoe.description() + " falls to the ground!"]);
  204. startDialog(new FallenFoe(currentFoe));
  205. } else {
  206. let attack = pick(currentFoe.attacks);
  207. update([attack.attackPlayer(player)]);
  208. if (player.health <= 0) {
  209. update(["You fall to the ground..."]);
  210. changeMode("eaten");
  211. updateDisplay();
  212. }
  213. }
  214. }
  215. function attackHovered(index) {
  216. document.getElementById("combat-desc").innerHTML = player.attacks[index].desc;
  217. }
  218. function struggleClicked(index) {
  219. let struggle = currentFoe.struggles[index];
  220. let result = struggle.struggle(player);
  221. update([result.lines]);
  222. if (result.escape) {
  223. changeMode("explore");
  224. } else {
  225. player.health -= 20;
  226. if (player.health <= -100) {
  227. update(["You digest in the depths of the " + currentFoe.description()]);
  228. moveTo(respawnRoom);
  229. changeMode("explore");
  230. player.health = 100;
  231. update(["You wake back up in your bed."]);
  232. }
  233. }
  234. }
  235. function struggleHovered(index) {
  236. document.getElementById("eaten-desc").innerHTML = player.struggles[index].desc;
  237. }
  238. function startDialog(dialog) {
  239. currentDialog = dialog;
  240. changeMode("dialog");
  241. update([currentDialog.text]);
  242. currentDialog.visit();
  243. updateDisplay();
  244. }
  245. function dialogClicked(index) {
  246. currentDialog = currentDialog.choices[index].node;
  247. update([currentDialog.text]);
  248. currentDialog.visit();
  249. if (currentDialog.choices.length == 0) {
  250. changeMode("explore");
  251. updateDisplay();
  252. }
  253. }
  254. function loadDialog() {
  255. dialogButtons = Array.from( document.querySelectorAll(".dialog-button"));
  256. for (let i = 0; i < dialogButtons.length; i++) {
  257. dialogButtons[i].addEventListener("click", function() { dialogClicked(i); });
  258. }
  259. }
  260. function actionClicked(index) {
  261. actions[index].action();
  262. }
  263. function loadActions() {
  264. actionButtons = Array.from( document.querySelectorAll(".action-button"));
  265. for (let i = 0; i < actionButtons.length; i++) {
  266. actionButtons[i].addEventListener("click", function() { actionClicked(i); });
  267. }
  268. }
  269. function loadCompass() {
  270. dirButtons[NORTH_WEST] = document.getElementById("compass-north-west");
  271. dirButtons[NORTH_WEST].addEventListener("click", function() {
  272. move(NORTH_WEST);
  273. });
  274. dirButtons[NORTH] = document.getElementById("compass-north");
  275. dirButtons[NORTH].addEventListener("click", function() {
  276. move(NORTH);
  277. });
  278. dirButtons[NORTH_EAST] = document.getElementById("compass-north-east");
  279. dirButtons[NORTH_EAST].addEventListener("click", function() {
  280. move(NORTH_EAST);
  281. });
  282. dirButtons[WEST] = document.getElementById("compass-west");
  283. dirButtons[WEST].addEventListener("click", function() {
  284. move(WEST);
  285. });
  286. dirButtons[EAST] = document.getElementById("compass-east");
  287. dirButtons[EAST].addEventListener("click", function() {
  288. move(EAST);
  289. });
  290. dirButtons[SOUTH_WEST] = document.getElementById("compass-south-west");
  291. dirButtons[SOUTH_WEST].addEventListener("click", function() {
  292. move(SOUTH_WEST);
  293. });
  294. dirButtons[SOUTH] = document.getElementById("compass-south");
  295. dirButtons[SOUTH].addEventListener("click", function() {
  296. move(SOUTH);
  297. });
  298. dirButtons[SOUTH_EAST] = document.getElementById("compass-south-east");
  299. dirButtons[SOUTH_EAST].addEventListener("click", function() {
  300. move(SOUTH_EAST);
  301. });
  302. }