File size: 968 Bytes
ae4627d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import Entity from './Entity';

export default class Polygon extends Entity {

  constructor(id, x, y, angle, center, color, strength, polys) {
    super(id, x, y, angle, center, color, strength);
    this.polys = polys;
  }

  draw(ctx, SCALE) {
    SCALE = SCALE || 30;

    ctx.save();
    ctx.translate(this.x * SCALE, this.y * SCALE);
    ctx.rotate(this.angle);
    ctx.translate(-(this.x) * SCALE, -(this.y) * SCALE);
    ctx.fillStyle = this.getColor();

    for (var i = 0; i < this.polys.length; i++) {
      var points = this.polys[i];
      ctx.beginPath();
      ctx.moveTo((this.x + points[0].x) * SCALE, (this.y + points[0].y) * SCALE);
      for (var j = 1; j < points.length; j++) {
         ctx.lineTo((points[j].x + this.x) * SCALE, (points[j].y + this.y) * SCALE);
      }
      ctx.lineTo((this.x + points[0].x) * SCALE, (this.y + points[0].y) * SCALE);
      ctx.closePath();
      ctx.fill();
      ctx.stroke();
    }

    ctx.restore();
  }
}