asteroidtoy.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { Ship } from "./ship.js";
  2. export class AsteroidToy {
  3. constructor(container) {
  4. this.container = container;
  5. this.lastTime = 0;
  6. this.canvas = null;
  7. this.context = null;
  8. this.updateIntervalId = 0;
  9. this.ships = [];
  10. this.mousePosition = { x: 0, y: 0 };
  11. this.debug = false;
  12. }
  13. init() {
  14. //add dom
  15. this.canvas = document.getElementById(this.container);
  16. this.context = this.canvas.getContext("2d");
  17. //attach listeners
  18. window.addEventListener("resize", this.onResize);
  19. window.addEventListener("mousedown", this.onMouseDown);
  20. window.addEventListener("mousemove", this.onMouseMove);
  21. window.addEventListener("mouseup", this.onMouseUp);
  22. window.addEventListener("contextmenu", this.onRightClick);
  23. window.addEventListener("touchstart", this.onTouchStart);
  24. window.addEventListener("touchmove", this.onTouchMove);
  25. window.addEventListener("touchend", this.onTouchEnd);
  26. this.lastTime = new Date().getUTCMilliseconds();
  27. this.updateIntervalId = setInterval(() => {
  28. let currentTime = new Date().getUTCMilliseconds();
  29. this.tick(1 + ((currentTime - this.lastTime) / 1000));
  30. this.lastTime = currentTime;
  31. }, 16);
  32. this.onResize();
  33. //populate game objects
  34. let canvasBounds = this.canvas.getBoundingClientRect();
  35. this.mousePosition.x = canvasBounds.width / 2;
  36. this.mousePosition.y = canvasBounds.height / 2;
  37. this.ships = [];
  38. this.ships.push(new Ship(this.canvas, canvasBounds.width / 2 + 50, canvasBounds.height / 2 + 50, "crimson"));
  39. this.ships.push(new Ship(this.canvas, canvasBounds.width / 2 + 50, canvasBounds.height / 2 - 50, "limegreen"));
  40. this.ships.push(new Ship(this.canvas, canvasBounds.width / 2 - 50, canvasBounds.height / 2 + 50, "cornflowerblue"));
  41. this.ships.push(new Ship(this.canvas, canvasBounds.width / 2 - 50, canvasBounds.height / 2 - 50, "magenta"));
  42. //start animation
  43. this.animate();
  44. }
  45. animate = () => {
  46. this.clearFrame(this.context);
  47. this.draw(this.context);
  48. requestAnimationFrame(this.animate);
  49. }
  50. tick(delta) {
  51. this.update(delta);
  52. }
  53. update(delta) {
  54. this.ships.forEach(ship => ship.setTarget(this.mousePosition.x, this.mousePosition.y));
  55. this.ships.forEach(ship => ship.update(delta));
  56. // for (let i = 1; i < this.ships.length; i++) {
  57. // this.ships[i].setTarget(this.ships[i - 1].position.x, this.ships[i - 1].position.y);
  58. // }
  59. }
  60. clearFrame(context) {
  61. context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  62. }
  63. draw(context) {
  64. this.clearFrame(context);
  65. if (this.debug) {
  66. context.fillStyle = "blue";
  67. context.beginPath();
  68. context.arc(this.mousePosition.x, this.mousePosition.y, 4, 0, 2 * Math.PI);
  69. context.fill();
  70. }
  71. this.ships.forEach(ship => ship.draw(context));
  72. }
  73. getPixelRatio(context) {
  74. let dpr = window.devicePixelRatio || 1;
  75. let bsr = context.webkitBackingStorePixelRatio ||
  76. context.mozBackingStorePixelRatio ||
  77. context.msBackingStorePixelRatio ||
  78. context.oBackingStorePixelRatio ||
  79. context.backingStorePixelRatio || 1;
  80. return dpr / bsr;
  81. }
  82. onResize = () => {
  83. let ratio = this.getPixelRatio(this.context);
  84. let canvasBounds = this.canvas.getBoundingClientRect();
  85. this.canvas.width = canvasBounds.width * ratio;
  86. this.canvas.height = canvasBounds.height * ratio;
  87. this.context.scale(ratio, ratio);
  88. }
  89. onMouseMove = event => {
  90. // this.ships[0].setTarget(event.clientX, event.clientY);
  91. this.mousePosition.x = event.clientX;
  92. this.mousePosition.y = event.clientY;
  93. let canvasBounds = this.canvas.getBoundingClientRect();
  94. if (this.mousePosition.x < canvasBounds.x ||
  95. this.mousePosition.x > canvasBounds.x + canvasBounds.width ||
  96. this.mousePosition.y < canvasBounds.y ||
  97. this.mousePosition.y > canvasBounds.y + canvasBounds.height) {
  98. this.mousePosition.x = canvasBounds.width / 2;
  99. this.mousePosition.y = canvasBounds.height / 2;
  100. }
  101. }
  102. }