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

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