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.

index.html 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <link rel="stylesheet" href="style.css">
  6. </head>
  7. <body>
  8. <div id="sections">
  9. <div class="loading hidden">
  10. <div class="arg"></div>
  11. <img src="assets/loading.gif">
  12. </div>
  13. <div class="error hidden">
  14. <div class="arg"></div>
  15. </div>
  16. <div class="pre hidden">
  17. <button id="play-god">Play as God</button>
  18. <button id="play-runner">Play as Runner</button>
  19. </div>
  20. <div class="game hidden">
  21. <canvas id="game-canvas"></canvas>
  22. </div>
  23. </div>
  24. <script src="node_modules/jquery/dist/jquery.min.js"></script>
  25. <script src="node_modules/socksugar/client.js"></script>
  26. <script src="js/game.js"></script>
  27. <script>
  28. window.page = {
  29. switchTo: function(name, arg) {
  30. if (page.switchTo.prev)
  31. page.switchTo.prev.addClass("hidden");
  32. //We don't want to switch to a page if we're going to
  33. //switch again a couple of milliseconds later.
  34. if (page.switchTo.timeout)
  35. clearTimeout(page.switchTo.timeout);
  36. page.switchTo.timeout = setTimeout(function() {
  37. var elem = $("#sections > ."+name);
  38. elem.removeClass("hidden");
  39. if (arg)
  40. elem.children(".arg").html(arg);
  41. else
  42. elem.children(".ard").html("");
  43. page.switchTo.prev = elem;
  44. }, 150);
  45. }
  46. }
  47. window.game = null;
  48. function startGame(type) {
  49. page.switchTo(
  50. "loading",
  51. "Waiting for opponent..."
  52. );
  53. sock.send("game/start", {
  54. type: type
  55. }, function(err, res) {
  56. if (err)
  57. return page.switchTo("error", err);
  58. page.switchTo("game");
  59. window.game = new Game(
  60. $("#game-canvas")[0],
  61. res.players,
  62. res.index,
  63. sock
  64. );
  65. game.start();
  66. sock.on("gameover", function(data) {
  67. game.stop();
  68. page.switchTo("error", data.msg);
  69. });
  70. });
  71. }
  72. //Start with showincg a loading animation
  73. page.switchTo("loading");
  74. //Initiate a socket connection
  75. window.sock = new SockSugar("ws://weblet.mort.coffee:8083");
  76. //If we can't connect to the server, display an error
  77. var errTimeout = setTimeout(function() {
  78. page.switchTo("error", "Couldn't connect to the server.");
  79. }, 7000);
  80. sock.on("ready", function() {
  81. clearTimeout(errTimeout);
  82. page.switchTo("pre");
  83. $("#play-god").on("click", function() {
  84. startGame("god");
  85. });
  86. $("#play-runner").on("click", function() {
  87. startGame("runner");
  88. });
  89. });
  90. </script>
  91. </body>
  92. </html>