You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

script.js 911B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. let Game = require("./game");
  2. let game, sock;
  3. function startGame(name) {
  4. if (typeof name !== "string")
  5. name = document.getElementById("playerName").value || "Guest";
  6. view("game");
  7. location.hash = name;
  8. sock = new SockSugar(conf.address);
  9. game = new Game(sock, document.getElementById("canvas"), name);
  10. sock.on("close", () => {
  11. alert("Server closed.");
  12. game.stop();
  13. });
  14. game.onloss = () => view("game-over");
  15. }
  16. document.querySelector("#startForm").addEventListener("submit", (evt) => {
  17. evt.preventDefault();
  18. startGame();
  19. });
  20. document.querySelector("#restartGameBtn").addEventListener("click", () => {
  21. location.reload();
  22. });
  23. document.querySelector("#storyBtn").addEventListener("click", () => {
  24. location.hash = "";
  25. location.reload();
  26. });
  27. window.addEventListener("load", () => {
  28. let name = location.hash.substring(1);
  29. if (name) {
  30. startGame(name);
  31. console.log("starting");
  32. }
  33. });