crunch
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 

430 Zeilen
12 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. let prefs = {
  12. player: {
  13. prey: true
  14. }
  15. };
  16. function round(number, digits) {
  17. return Math.round(number * Math.pow(10,digits)) / Math.pow(10,digits);
  18. }
  19. function updateExploreCompass() {
  20. for (let i = 0; i < dirButtons.length; i++) {
  21. let button = dirButtons[i];
  22. button.classList.remove("active-button");
  23. button.classList.remove("inactive-button");
  24. button.classList.remove("disabled-button");
  25. if (currentRoom.exits[i] == null) {
  26. button.disabled = true;
  27. button.classList.add("inactive-button");
  28. button.innerHTML = "";
  29. } else {
  30. if (currentRoom.exits[i].conditions.reduce((result, test) => result && test(prefs), true)) {
  31. button.disabled = false;
  32. button.classList.add("active-button");
  33. button.innerHTML = currentRoom.exits[i].name;
  34. } else {
  35. button.disabled = true;
  36. button.classList.add("disabled-button");
  37. button.innerHTML = currentRoom.exits[i].name;
  38. }
  39. }
  40. }
  41. }
  42. function updateExploreActions() {
  43. for (let i = 0; i < actionButtons.length; i++) {
  44. if (i < actions.length) {
  45. actionButtons[i].disabled = false;
  46. actionButtons[i].innerHTML = actions[i].name;
  47. actionButtons[i].classList.remove("inactive-button");
  48. actionButtons[i].classList.add("active-button");
  49. }
  50. else {
  51. actionButtons[i].disabled = true;
  52. actionButtons[i].innerHTML = "";
  53. actionButtons[i].classList.remove("active-button");
  54. actionButtons[i].classList.add("inactive-button");
  55. }
  56. }
  57. }
  58. function updateExplore() {
  59. updateExploreCompass();
  60. updateExploreActions();
  61. }
  62. function updateEaten() {
  63. let list = document.getElementById("eaten");
  64. while(list.firstChild) {
  65. list.removeChild(list.firstChild);
  66. }
  67. for (let i = 0; i < currentFoe.struggles.length; i++) {
  68. let li = document.createElement("li");
  69. let button = document.createElement("button");
  70. button.classList.add("eaten-button");
  71. button.innerHTML = currentFoe.struggles[i].name;
  72. button.addEventListener("click", function() { struggleClicked(i); } );
  73. button.addEventListener("mouseover", function() { struggleHovered(i); } );
  74. button.addEventListener("mouseout", function() { document.getElementById("eaten-desc").innerHTML = ""; } );
  75. li.appendChild(button);
  76. list.appendChild(li);
  77. }
  78. }
  79. function updateCombat() {
  80. let list = document.getElementById("combat");
  81. while(list.firstChild) {
  82. list.removeChild(list.firstChild);
  83. }
  84. for (let i = 0; i < player.attacks.length; i++) {
  85. let li = document.createElement("li");
  86. let button = document.createElement("button");
  87. button.classList.add("combat-button");
  88. button.innerHTML = player.attacks[i].name;
  89. button.addEventListener("click", function() { attackClicked(i); } );
  90. button.addEventListener("mouseover", function() { attackHovered(i); } );
  91. button.addEventListener("mouseout", function() { document.getElementById("combat-desc").innerHTML = ""; } );
  92. li.appendChild(button);
  93. list.appendChild(li);
  94. }
  95. }
  96. function updateDialog() {
  97. let list = document.getElementById("dialog");
  98. while(list.firstChild) {
  99. list.removeChild(list.firstChild);
  100. }
  101. for (let i = 0; i < currentDialog.choices.length; i++) {
  102. let li = document.createElement("li");
  103. let button = document.createElement("button");
  104. button.classList.add("dialog-button");
  105. button.innerHTML = currentDialog.choices[i].text;
  106. button.addEventListener("click", function() { dialogClicked(i); });
  107. li.appendChild(button);
  108. list.appendChild(li);
  109. }
  110. }
  111. function updateDisplay() {
  112. document.querySelectorAll(".selector").forEach(function (x) {
  113. x.style.display = "none";
  114. });
  115. switch(mode) {
  116. case "explore":
  117. document.getElementById("selector-explore").style.display = "flex";
  118. updateExplore();
  119. break;
  120. case "combat":
  121. document.getElementById("selector-combat").style.display = "flex";
  122. updateCombat();
  123. break;
  124. case "dialog":
  125. document.getElementById("selector-dialog").style.display = "flex";
  126. updateDialog();
  127. break;
  128. case "eaten":
  129. document.getElementById("selector-eaten").style.display = "flex";
  130. updateEaten();
  131. break;
  132. }
  133. document.getElementById("time").innerHTML = "Time: " + renderTime(time);
  134. document.getElementById("stat-name").innerHTML = "Name: " + player.name;
  135. document.getElementById("stat-health").innerHTML = "Health: " + player.health + "/" + player.maxHealth;
  136. document.getElementById("stat-fullness").innerHTML = "Fullness: " + round(player.fullness(),0);
  137. }
  138. function advanceTime(amount) {
  139. time = (time + amount) % 86400;
  140. update(player.stomach.digest(amount));
  141. }
  142. function renderTime(time) {
  143. let suffix = (time < 43200) ? "AM" : "PM";
  144. let hour = Math.floor((time % 43200) / 3600);
  145. if (hour == 0)
  146. hour = 12;
  147. let minute = Math.floor(time / 60) % 60;
  148. if (minute < 9)
  149. minute = "0" + minute;
  150. return hour + ":" + minute + " " + suffix;
  151. }
  152. function move(direction) {
  153. let target = currentRoom.exits[direction];
  154. if (target == null) {
  155. alert("Tried to move to an empty room!");
  156. return;
  157. }
  158. moveTo(target,currentRoom.exitDescs[direction]);
  159. }
  160. function moveTo(room,desc="You go places lol") {
  161. actions = [];
  162. currentRoom = room;
  163. advanceTime(30);
  164. currentRoom.objects.forEach(function (object) {
  165. object.actions.forEach(function (action) {
  166. actions.push(action);
  167. });
  168. });
  169. update([desc,newline]);
  170. currentRoom.visit();
  171. }
  172. window.addEventListener('load', function(event) {
  173. document.getElementById("start-button").addEventListener("click", start, false);
  174. });
  175. function start() {
  176. applySettings(generateSettings());
  177. document.getElementById("create").style.display = "none";
  178. document.getElementById("game").style.display = "block";
  179. loadActions();
  180. loadCompass();
  181. loadDialog();
  182. currentRoom = createWorld();
  183. respawnRoom = currentRoom;
  184. moveTo(currentRoom);
  185. updateDisplay();
  186. }
  187. // copied from Stroll LUL
  188. function generateSettings() {
  189. let form = document.forms.namedItem("character-form");
  190. let settings = {};
  191. for (let i=0; i<form.length; i++) {
  192. let value = form[i].value == "" ? form[i].placeholder : form[i].value;
  193. if (form[i].type == "text")
  194. if (form[i].value == "")
  195. settings[form[i].name] = form[i].placeholder;
  196. else
  197. settings[form[i].name] = value;
  198. else if (form[i].type == "number")
  199. settings[form[i].name] = parseFloat(value);
  200. else if (form[i].type == "checkbox") {
  201. settings[form[i].name] = form[i].checked;
  202. } else if (form[i].type == "radio") {
  203. let name = form[i].name;
  204. if (form[i].checked)
  205. settings[name] = form[i].value;
  206. } else if (form[i].type == "select-one") {
  207. settings[form[i].name] = form[i][form[i].selectedIndex].value;
  208. }
  209. }
  210. return settings;
  211. }
  212. function applySettings(settings) {
  213. player.name = settings.name;
  214. for (let key in settings) {
  215. if (settings.hasOwnProperty(key)) {
  216. if (key.match(/prefs/)) {
  217. let tokens = key.split("-");
  218. let pref = prefs;
  219. pref = tokens.slice(1,-1).reduce((pref, key) => pref[key], pref);
  220. pref[tokens.slice(-1)[0]] = settings[key];
  221. }
  222. }
  223. }
  224. }
  225. function saveSettings() {
  226. window.localStorage.setItem("settings", JSON.stringify(generateSettings()));
  227. }
  228. function retrieveSettings() {
  229. return JSON.parse(window.localStorage.getItem("settings"));
  230. }
  231. function update(lines=[]) {
  232. let log = document.getElementById("log");
  233. for (let i=0; i<lines.length; i++) {
  234. let div = document.createElement("div");
  235. div.innerHTML = lines[i];
  236. log.appendChild(div);
  237. }
  238. log.scrollTop = log.scrollHeight;
  239. updateDisplay();
  240. }
  241. function changeMode(newMode) {
  242. mode = newMode;
  243. let body = document.querySelector("body");
  244. body.className = "";
  245. switch(mode) {
  246. case "explore":
  247. case "dialog":
  248. body.classList.add("explore");
  249. break;
  250. case "combat":
  251. body.classList.add("combat");
  252. break;
  253. case "eaten":
  254. body.classList.add("eaten");
  255. break;
  256. }
  257. updateDisplay();
  258. }
  259. function startCombat(opponent) {
  260. changeMode("combat");
  261. currentFoe = opponent;
  262. update(["Oh shit it's a " + opponent.description()]);
  263. }
  264. function attackClicked(index) {
  265. update([player.attacks[index].attack(currentFoe)]);
  266. if (currentFoe.health <= 0) {
  267. update(["The " + currentFoe.description() + " falls to the ground!"]);
  268. startDialog(new FallenFoe(currentFoe));
  269. } else {
  270. let attack = pick(currentFoe.attacks.filter(attack => attack.conditions == undefined || attack.conditions.reduce((result, test) => result && test(prefs), true)));
  271. if (attack == null) {
  272. attack = currentFoe.backupAttack;
  273. }
  274. update([attack.attackPlayer(player)]);
  275. if (player.health <= 0) {
  276. update(["You fall to the ground..."]);
  277. changeMode("eaten");
  278. updateDisplay();
  279. }
  280. }
  281. }
  282. function attackHovered(index) {
  283. document.getElementById("combat-desc").innerHTML = player.attacks[index].desc;
  284. }
  285. function struggleClicked(index) {
  286. let struggle = currentFoe.struggles[index];
  287. let result = struggle.struggle(player);
  288. update([result.lines]);
  289. if (result.escape) {
  290. changeMode("explore");
  291. } else {
  292. player.health -= 20;
  293. if (player.health <= -100) {
  294. update(["You digest in the depths of the " + currentFoe.description()]);
  295. moveTo(respawnRoom,"You drift through space and time...");
  296. changeMode("explore");
  297. player.health = 100;
  298. update(["You wake back up in your bed."]);
  299. }
  300. }
  301. }
  302. function struggleHovered(index) {
  303. document.getElementById("eaten-desc").innerHTML = currentFoe.struggles[index].desc;
  304. }
  305. function startDialog(dialog) {
  306. currentDialog = dialog;
  307. changeMode("dialog");
  308. update([currentDialog.text]);
  309. currentDialog.visit();
  310. updateDisplay();
  311. }
  312. function dialogClicked(index) {
  313. currentDialog = currentDialog.choices[index].node;
  314. update([currentDialog.text]);
  315. currentDialog.visit();
  316. if (currentDialog.choices.length == 0) {
  317. changeMode("explore");
  318. updateDisplay();
  319. }
  320. }
  321. function loadDialog() {
  322. dialogButtons = Array.from( document.querySelectorAll(".dialog-button"));
  323. for (let i = 0; i < dialogButtons.length; i++) {
  324. dialogButtons[i].addEventListener("click", function() { dialogClicked(i); });
  325. }
  326. }
  327. function actionClicked(index) {
  328. actions[index].action();
  329. }
  330. function loadActions() {
  331. actionButtons = Array.from( document.querySelectorAll(".action-button"));
  332. for (let i = 0; i < actionButtons.length; i++) {
  333. actionButtons[i].addEventListener("click", function() { actionClicked(i); });
  334. }
  335. }
  336. function loadCompass() {
  337. dirButtons[NORTH_WEST] = document.getElementById("compass-north-west");
  338. dirButtons[NORTH_WEST].addEventListener("click", function() {
  339. move(NORTH_WEST);
  340. });
  341. dirButtons[NORTH] = document.getElementById("compass-north");
  342. dirButtons[NORTH].addEventListener("click", function() {
  343. move(NORTH);
  344. });
  345. dirButtons[NORTH_EAST] = document.getElementById("compass-north-east");
  346. dirButtons[NORTH_EAST].addEventListener("click", function() {
  347. move(NORTH_EAST);
  348. });
  349. dirButtons[WEST] = document.getElementById("compass-west");
  350. dirButtons[WEST].addEventListener("click", function() {
  351. move(WEST);
  352. });
  353. dirButtons[EAST] = document.getElementById("compass-east");
  354. dirButtons[EAST].addEventListener("click", function() {
  355. move(EAST);
  356. });
  357. dirButtons[SOUTH_WEST] = document.getElementById("compass-south-west");
  358. dirButtons[SOUTH_WEST].addEventListener("click", function() {
  359. move(SOUTH_WEST);
  360. });
  361. dirButtons[SOUTH] = document.getElementById("compass-south");
  362. dirButtons[SOUTH].addEventListener("click", function() {
  363. move(SOUTH);
  364. });
  365. dirButtons[SOUTH_EAST] = document.getElementById("compass-south-east");
  366. dirButtons[SOUTH_EAST].addEventListener("click", function() {
  367. move(SOUTH_EAST);
  368. });
  369. }