Enemy.js 1.19 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
import GameObject from "./GameObject.js";

const DEFAULT_VELOCITY = 1,
  DEFAULT_HEALTH = 100,
  DEFAULT_WIDTH = 50,
  DEFAULT_HEIGHT = 50,
  DEFAULT_DAMAGE = 10,
  DEFAULT_HIT_BOX_RADIUS = 30,
  DEFAULT_ENEMEY_COLOR = "#dd3939";

class Enemy extends GameObject {

  constructor(x, y, direction) {
    super(x, y, DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_HIT_BOX_RADIUS,
      DEFAULT_VELOCITY, direction, DEFAULT_ENEMEY_COLOR);
    this.health = DEFAULT_HEALTH;
    this.damage = DEFAULT_DAMAGE;
  }

  draw(context) {
    context.save();
    context.fillStyle = this.color;
    context.beginPath();
    context.translate(this.x, this.y);
    context.rotate(this.direction * Math.PI / 180);
    context.moveTo(0, -this.height / 2);
    context.lineTo(-(this.width / 2), this.height / 2);
    context.lineTo(this.width / 2, this.height / 2);
    context.closePath();
    context.fill();
    context.restore();
  }

  static createEnemy(rangeX, rangeY, targetX, targetY) {
    let xPos = parseInt(Math.random() * rangeX),
      yPos = parseInt(Math.random() * rangeY),
      direction = Math.atan2(targetY - yPos, targetX - xPos) * 180 / Math.PI;
    return new Enemy(xPos, yPos, direction);
  }

}

export default Enemy;