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 1.2KB

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