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 871B

1234567891011121314151617181920212223242526272829303132333435363738
  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("#startGameBtn").addEventListener("click", startGame);
  17. document.querySelector("#restartGameBtn").addEventListener("click", () => {
  18. location.reload();
  19. });
  20. document.querySelector("#storyBtn").addEventListener("click", () => {
  21. location.hash = "";
  22. location.reload();
  23. });
  24. window.addEventListener("load", () => {
  25. let name = location.hash.substring(1);
  26. if (name) {
  27. startGame(name);
  28. console.log("starting");
  29. }
  30. });