Game for OLC Code Jam 2022
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.

entity.js 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. class Entity {
  2. constructor(x, y, w, h) {
  3. this.x = x;
  4. this.y = y;
  5. this.w = w;
  6. this.h = h;
  7. this.element = document.createElement('div');
  8. this.alive = true;
  9. }
  10. getScreenCoords() {
  11. return {
  12. x: (this.x - this.w / 2) * CANVAS.width,
  13. y: (this.y - this.h / 2) * CANVAS.height,
  14. w: this.w * CANVAS.width,
  15. h: this.h * CANVAS.height,
  16. }
  17. }
  18. draw() {
  19. if (this.alive) {
  20. const { x, y, w, h } = this.getScreenCoords();
  21. if (this.image) {
  22. CTX.drawImage(this.image, x, y, w, h);
  23. } else {
  24. CTX.fillStyle = this.color ?? 'red';
  25. CTX.fillRect(x, y, w, h);
  26. }
  27. }
  28. }
  29. destroy() {
  30. this.alive = false;
  31. }
  32. isOverlapped(other) {
  33. if (!this.alive || !other.alive) return false;
  34. if (other.x + other.w / 2 < this.x - this.w / 2) return false;
  35. if (other.x - other.w / 2 > this.x + this.w / 2) return false;
  36. if (other.y + other.h / 2 < this.y - this.h / 2) return false;
  37. if (other.y - other.h / 2 > this.y + this.h / 2) return false;
  38. return true;
  39. }
  40. }