munch
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

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