Planet.js 1.41 KB
Newer Older
1 2 3
import GameObject from "./GameObject.js";

const DEFAULT_HEALTH = 100,
4 5 6
  DEFAULT_WIDTH = 500,
  DEFAULT_HEIGHT = 500,
  DEFAULT_HIT_BOX_RADIUS = 270,
7
  DEFAULT_PLANET_COLOR = "#900c47",
8
  DEFAULT_PLANET_BORDER_COLOR = "#dafffe",
9 10 11 12
  DEFAULT_PLANET_BORDER_WIDTH = "20",
  DEFAULT_PLANET_TEXT_OFFSET = 20,
  DEFAULT_PLANET_TEXT_FONT = "32px ShareTech",
  DEFAULT_PLANET_TEXT_COLOR = "#FFF";
13

14
class Planet extends GameObject {
15 16 17

  constructor(x, y) {
    super(x, y, DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_HIT_BOX_RADIUS,
18
      0, 0, DEFAULT_PLANET_COLOR);
19
    this.health = DEFAULT_HEALTH;
20 21
    this.borderColor = DEFAULT_PLANET_BORDER_COLOR;
    this.borderWidth = DEFAULT_PLANET_BORDER_WIDTH;
22 23 24 25
  }

  draw(context) {
    context.save();
26
    // Draw planet
27 28 29 30 31 32 33 34 35
    context.fillStyle = this.color;
    context.strokeStyle = this.borderColor;
    context.lineWidth = this.borderWidth;
    context.beginPath();
    context.ellipse(this.x, this.y, this.width / 2, this.height / 2, Math.PI /
      4, 0, 2 * Math.PI);
    context.closePath();
    context.fill();
    context.stroke();
36 37 38 39 40 41 42 43
    // Draw current shield/health status
    context.beginPath();
    context.font = DEFAULT_PLANET_TEXT_FONT;
    context.fillStyle = DEFAULT_PLANET_TEXT_COLOR;
    context.textAlign = "center";
    context.fillText(`Shields at ${this.health}%`, this.x, this.y -
      DEFAULT_PLANET_TEXT_OFFSET);
    context.closePath();
44 45 46 47 48
    context.restore();
  }

}

49
export default Planet;