moveToTargetAnimated method

void moveToTargetAnimated(
  1. GameComponent target, {
  2. double? zoom,
  3. double? angle,
  4. VoidCallback? finish,
  5. Duration? duration,
  6. Curve curve = Curves.decelerate,
})

Implementation

void moveToTargetAnimated(
  GameComponent target, {
  double? zoom,
  double? angle,
  VoidCallback? finish,
  Duration? duration,
  Curve curve = Curves.decelerate,
}) {
  if ((zoom != null && zoom <= 0.0) || _isMoving) return;
  this.target = null;
  _isMoving = true;

  double newZoom = zoom ?? this.zoom;
  double newAngle = angle ?? this.angle;
  Vector2 originPosition = this.position.clone();

  double diffZoom = this.zoom - newZoom;
  double initialZoom = this.zoom;

  double diffAngle = this.angle - newAngle;
  double originAngle = this.angle;

  gameRef.getValueGenerator(
    duration ?? Duration(seconds: 1),
    onChange: (value) {
      double diffX = (originPosition.x + gameSize.x / 2) - target.center.x;
      double diffY = (originPosition.y + gameSize.y / 2) - target.center.y;

      snapTo(
        Vector2(
          originPosition.x - (diffX * value),
          originPosition.y - (diffY * value),
        ),
      );
      this.zoom = initialZoom - (diffZoom * value);
      this.angle = originAngle - (diffAngle * value);

      if (this.moveOnlyMapArea) {
        _keepInMapArea();
      }
    },
    onFinish: () {
      this.target = target;
      _isMoving = false;
      finish?.call();
    },
    curve: curve,
  ).start();
}