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.

main.js 975B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import Level from "./Level.js";
  2. import Player from "./entities/Player.js";
  3. import Platform from "./entities/Platform.js";
  4. import structures from "./structures.js";
  5. import Vec2 from "./Vec2.js";
  6. let canvas = document.getElementById("canvas");
  7. let level = new Level(canvas);
  8. level.spawnEntity(new Player(level), 10, 1);
  9. level.spawnEntity(new Platform(level), 16, 4);
  10. level.spawnStructure(structures.floor(8), 4, 4);
  11. level.start();
  12. // Pause the game when the tab has been out of focus for more than half a second
  13. let blurTimeout = null;
  14. window.addEventListener("focus", () => {
  15. if (blurTimeout != null) {
  16. clearTimeout(blurTimeout);
  17. blurTimeout = null;
  18. }
  19. level.start();
  20. });
  21. window.addEventListener("blur", () => {
  22. if (blurTimeout == null) {
  23. blurTimeout = setTimeout(() => level.stop(), 500);
  24. }
  25. });
  26. // Resize canvas
  27. function resize() {
  28. canvas.width = window.innerWidth;
  29. canvas.height = window.innerHeight;
  30. }
  31. window.addEventListener("resize", resize);
  32. resize();