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

123456789101112131415161718192021222324252627282930313233343536
  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. ROOT.appendChild(this.element);
  9. this.draw();
  10. this.alive = true;
  11. }
  12. draw() {
  13. if (this.alive) {
  14. this.element.style.left = `${this.x * 100}%`;
  15. this.element.style.top = `${this.y * 100}%`;
  16. this.element.style.width = `${this.w * 100}%`;
  17. this.element.style.height = `${this.h * 100}%`;
  18. }
  19. }
  20. destroy() {
  21. this.element.remove();
  22. this.alive = false;
  23. }
  24. isOverlapped(other) {
  25. if (!this.alive || !other.alive) return false;
  26. if (other.x + other.w / 2 < this.x - this.w / 2) return false;
  27. if (other.x - other.w / 2 > this.x + this.w / 2) return false;
  28. if (other.y + other.h / 2 < this.y - this.h / 2) return false;
  29. if (other.y - other.h / 2 > this.y + this.h / 2) return false;
  30. return true;
  31. }
  32. }