123456789101112131415161718192021222324252627282930313233343536 |
- class Entity {
- constructor(x, y, w, h) {
- this.x = x;
- this.y = y;
- this.w = w;
- this.h = h;
- this.element = document.createElement('div');
- ROOT.appendChild(this.element);
- this.draw();
- this.alive = true;
- }
-
- draw() {
- if (this.alive) {
- this.element.style.left = `${this.x * 100}%`;
- this.element.style.top = `${this.y * 100}%`;
- this.element.style.width = `${this.w * 100}%`;
- this.element.style.height = `${this.h * 100}%`;
- }
- }
-
- destroy() {
- this.element.remove();
- this.alive = false;
- }
-
- isOverlapped(other) {
- if (!this.alive || !other.alive) return false;
- if (other.x + other.w / 2 < this.x - this.w / 2) return false;
- if (other.x - other.w / 2 > this.x + this.w / 2) return false;
- if (other.y + other.h / 2 < this.y - this.h / 2) return false;
- if (other.y - other.h / 2 > this.y + this.h / 2) return false;
-
- return true;
- }
- }
|