12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- class Entity {
- constructor(x, y, w, h) {
- this.x = x;
- this.y = y;
- this.w = w;
- this.h = h;
- this.element = document.createElement('div');
- this.alive = true;
- }
-
- getScreenCoords() {
- return {
- x: (this.x - this.w / 2) * CANVAS.width,
- y: (this.y - this.h / 2) * CANVAS.height,
- w: this.w * CANVAS.width,
- h: this.h * CANVAS.height,
- }
- }
-
- draw() {
- if (this.alive) {
- const { x, y, w, h } = this.getScreenCoords();
-
- if (this.image) {
- CTX.drawImage(this.image, x, y, w, h);
- } else {
- CTX.fillStyle = this.color ?? 'red';
- CTX.fillRect(x, y, w, h);
- }
- }
- }
-
- destroy() {
- 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;
- }
- }
|