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.1KB

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