PolygonComponent constructor

PolygonComponent(
  1. List<Vector2> _vertices, {
  2. Vector2? position,
  3. Vector2? size,
  4. Vector2? scale,
  5. double? angle,
  6. Anchor? anchor,
  7. int? priority,
  8. Paint? paint,
  9. bool? shrinkToBounds,
})

With this constructor you create your PolygonComponent from positions in anywhere in the 2d-space. It will automatically calculate the size of the Polygon (the bounding box) if no size it given. NOTE: Always define your polygon in a counter-clockwise fashion (in the screen coordinate system).

Implementation

PolygonComponent(
  this._vertices, {
  Vector2? position,
  Vector2? size,
  Vector2? scale,
  double? angle,
  Anchor? anchor,
  int? priority,
  Paint? paint,
  bool? shrinkToBounds,
})  : assert(
        _vertices.length > 2,
        'Number of vertices are too few to create a polygon',
      ),
      shrinkToBounds = shrinkToBounds ?? size == null,
      manuallyPositioned = position != null,
      super(
        position: position,
        size: size,
        scale: scale,
        angle: angle,
        anchor: anchor,
        priority: priority,
        paint: paint,
      ) {
  refreshVertices(newVertices: _vertices);

  final verticesLength = _vertices.length;
  _globalVertices = List.generate(
    verticesLength,
    (_) => Vector2.zero(),
    growable: false,
  );
  _lineSegments = List.generate(
    verticesLength,
    (_) => LineSegment.zero(),
    growable: false,
  );
}