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

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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.start();
  15. // Pause the game when the tab has been out of focus for more than half a second
  16. let blurTimeout = null;
  17. window.addEventListener("focus", () => {
  18. if (blurTimeout != null) {
  19. clearTimeout(blurTimeout);
  20. blurTimeout = null;
  21. }
  22. level.start();
  23. });
  24. window.addEventListener("blur", () => {
  25. if (blurTimeout == null) {
  26. blurTimeout = setTimeout(() => level.stop(), 500);
  27. }
  28. });
  29. // Resize canvas
  30. function resize() {
  31. canvas.width = window.innerWidth;
  32. canvas.height = window.innerHeight;
  33. }
  34. window.addEventListener("resize", resize);
  35. resize();