Enemy.js 1.42 KB
Newer Older
1 2 3 4 5 6
const DEFAULT_VELOCITY = 1,
  DEFAULT_HEALTH = 100,
  DEFAULT_WIDTH = 50,
  DEFAULT_HEIGHT = 50,
  DEFAULT_DAMAGE = 10,
  DEFAULT_HIT_BOX_RADIUS = 30,
7 8 9 10 11
  DEFAULT_ENEMY_COLOR = "#3f0d76",
  DEFAULT_ENEMEY_TIP_COLOR = "#d2ccf3",
  DEFAULT_ENEMY_HEALTH_COLOR = "#d2ccf3",
  DEFAULT_ENEMY_HEALTH_WIDTH = 10,
  DEFAULT_ENEMY_HEALTH_RADIUS = 50;
12

13
class Enemy {
14

15
  static draw(context) {
16
    context.save();
17 18 19 20 21 22 23 24 25 26 27 28
    // Draw health bar  if focused
    context.translate(this.x, this.y);
    if (this.focus === true) {
      context.strokeStyle = DEFAULT_ENEMY_HEALTH_COLOR;
      context.lineWidth = DEFAULT_ENEMY_HEALTH_WIDTH;
      context.beginPath();
      context.arc(0, 0, DEFAULT_ENEMY_HEALTH_RADIUS, 0, 3.6 * this.health *
        Math.PI / 100);
      context.stroke();
      context.closePath();
    }
    // Draw enemy
29
    context.fillStyle = this.DEFAULT_ENEMY_COLOR;
30
    context.beginPath();
31
    context.rotate((90 + this.direction) * Math.PI / 180);
32 33 34
    context.moveTo(0, -this.height / 2);
    context.lineTo(-(this.width / 2), this.height / 2);
    context.lineTo(this.width / 2, this.height / 2);
35
    context.fill();
36
    context.closePath();
37 38 39 40 41 42
    // Draw tip
    context.fillStyle = DEFAULT_ENEMEY_TIP_COLOR;
    context.beginPath();
    context.moveTo(0, -this.height / 2);
    context.lineTo(-(this.width / 4), 0);
    context.lineTo(this.width / 4, 0);
43
    context.fill();
44
    context.closePath();
45 46 47 48 49 50
    context.restore();
  }

}

export default Enemy;