index.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0">
  6. <title>Platformer 2D</title>
  7. <style>
  8. html, body, #canvas {
  9. margin: 0;
  10. padding: 0;
  11. border: 0;
  12. }
  13. body {
  14. color: white;
  15. background-color: black;
  16. overflow: hidden;
  17. touch-action: none;
  18. }
  19. #canvas {
  20. display: block;
  21. }
  22. #canvas:focus {
  23. outline: none;
  24. }
  25. #status, #status-splash, #status-progress {
  26. position: absolute;
  27. left: 0;
  28. right: 0;
  29. }
  30. #status, #status-splash {
  31. top: 0;
  32. bottom: 0;
  33. }
  34. #status {
  35. background-color: #242424;
  36. display: flex;
  37. flex-direction: column;
  38. justify-content: center;
  39. align-items: center;
  40. visibility: hidden;
  41. }
  42. #status-splash {
  43. max-height: 100%;
  44. max-width: 100%;
  45. margin: auto;
  46. }
  47. #status-progress, #status-notice {
  48. display: none;
  49. }
  50. #status-progress {
  51. bottom: 10%;
  52. width: 50%;
  53. margin: 0 auto;
  54. }
  55. #status-notice {
  56. background-color: #5b3943;
  57. border-radius: 0.5rem;
  58. border: 1px solid #9b3943;
  59. color: #e0e0e0;
  60. font-family: 'Noto Sans', 'Droid Sans', Arial, sans-serif;
  61. line-height: 1.3;
  62. margin: 0 2rem;
  63. overflow: hidden;
  64. padding: 1rem;
  65. text-align: center;
  66. z-index: 1;
  67. }
  68. </style>
  69. <link id="-gd-engine-icon" rel="icon" type="image/png" href="index.icon.png" />
  70. <link rel="apple-touch-icon" href="index.apple-touch-icon.png"/>
  71. <link rel="manifest" href="index.manifest.json">
  72. </head>
  73. <body>
  74. <canvas id="canvas">
  75. Your browser does not support the canvas tag.
  76. </canvas>
  77. <noscript>
  78. Your browser does not support JavaScript.
  79. </noscript>
  80. <div id="status">
  81. <img id="status-splash" src="index.png" alt="">
  82. <progress id="status-progress"></progress>
  83. <div id="status-notice"></div>
  84. </div>
  85. <script src="index.js"></script>
  86. <script>
  87. const GODOT_CONFIG = {"args":[],"canvasResizePolicy":2,"ensureCrossOriginIsolationHeaders":true,"executable":"index","experimentalVK":true,"fileSizes":{"index.pck":2459696,"index.wasm":33829596},"focusCanvas":true,"gdextensionLibs":[],"serviceWorker":"index.service.worker.js"};
  88. const GODOT_THREADS_ENABLED = true;
  89. const engine = new Engine(GODOT_CONFIG);
  90. (function () {
  91. const statusOverlay = document.getElementById('status');
  92. const statusProgress = document.getElementById('status-progress');
  93. const statusNotice = document.getElementById('status-notice');
  94. let initializing = true;
  95. let statusMode = '';
  96. function setStatusMode(mode) {
  97. if (statusMode === mode || !initializing) {
  98. return;
  99. }
  100. if (mode === 'hidden') {
  101. statusOverlay.remove();
  102. initializing = false;
  103. return;
  104. }
  105. statusOverlay.style.visibility = 'visible';
  106. statusProgress.style.display = mode === 'progress' ? 'block' : 'none';
  107. statusNotice.style.display = mode === 'notice' ? 'block' : 'none';
  108. statusMode = mode;
  109. }
  110. function setStatusNotice(text) {
  111. while (statusNotice.lastChild) {
  112. statusNotice.removeChild(statusNotice.lastChild);
  113. }
  114. const lines = text.split('\n');
  115. lines.forEach((line) => {
  116. statusNotice.appendChild(document.createTextNode(line));
  117. statusNotice.appendChild(document.createElement('br'));
  118. });
  119. }
  120. function displayFailureNotice(err) {
  121. console.error(err);
  122. if (err instanceof Error) {
  123. setStatusNotice(err.message);
  124. } else if (typeof err === 'string') {
  125. setStatusNotice(err);
  126. } else {
  127. setStatusNotice('An unknown error occured');
  128. }
  129. setStatusMode('notice');
  130. initializing = false;
  131. }
  132. const missing = Engine.getMissingFeatures({
  133. threads: GODOT_THREADS_ENABLED,
  134. });
  135. if (missing.length !== 0) {
  136. if (GODOT_CONFIG['serviceWorker'] && GODOT_CONFIG['ensureCrossOriginIsolationHeaders'] && 'serviceWorker' in navigator) {
  137. // There's a chance that installing the service worker would fix the issue
  138. Promise.race([
  139. navigator.serviceWorker.getRegistration().then((registration) => {
  140. if (registration != null) {
  141. return Promise.reject(new Error('Service worker already exists.'));
  142. }
  143. return registration;
  144. }).then(() => engine.installServiceWorker()),
  145. // For some reason, `getRegistration()` can stall
  146. new Promise((resolve) => {
  147. setTimeout(() => resolve(), 2000);
  148. }),
  149. ]).catch((err) => {
  150. console.error('Error while registering service worker:', err);
  151. }).then(() => {
  152. window.location.reload();
  153. });
  154. } else {
  155. // Display the message as usual
  156. const missingMsg = 'Error\nThe following features required to run Godot projects on the Web are missing:\n';
  157. displayFailureNotice(missingMsg + missing.join('\n'));
  158. }
  159. } else {
  160. setStatusMode('progress');
  161. engine.startGame({
  162. 'onProgress': function (current, total) {
  163. if (current > 0 && total > 0) {
  164. statusProgress.value = current;
  165. statusProgress.max = total;
  166. } else {
  167. statusProgress.removeAttribute('value');
  168. statusProgress.removeAttribute('max');
  169. }
  170. },
  171. }).then(() => {
  172. setStatusMode('hidden');
  173. }, displayFailureNotice);
  174. }
  175. }());
  176. </script>
  177. </body>
  178. </html>