moveFromAngleDodgeObstacles method

bool moveFromAngleDodgeObstacles(
  1. double speed,
  2. double angle
)

Move to direction by radAngle with dodge obstacles

Implementation

bool moveFromAngleDodgeObstacles(
  double speed,
  double angle,
) {
  isIdle = false;
  double innerSpeed = (speed * dtUpdate);

  Vector2 diffBase = BonfireUtil.diffMovePointByAngle(
    center,
    innerSpeed,
    angle,
  );

  var collisionX = _verifyTranslateCollision(diffBase.x, 0);

  var collisionY = _verifyTranslateCollision(0, diffBase.y);

  Vector2 newDiffBase = diffBase;

  if (collisionX) {
    newDiffBase = Vector2(0, newDiffBase.y);
  }
  if (collisionY) {
    newDiffBase = Vector2(newDiffBase.x, 0);
  }

  if (collisionX && newDiffBase.y != 0) {
    double speedY = innerSpeed;
    if (newDiffBase.y < 0) {
      speedY *= -1;
    }
    final collisionY = _verifyTranslateCollision(
      0,
      speedY,
    );

    if (!collisionY) newDiffBase = Vector2(0, speedY);
  }

  if (collisionY && newDiffBase.x != 0) {
    double speedX = innerSpeed;
    if (newDiffBase.x < 0) {
      speedX *= -1;
    }
    final collisionX = _verifyTranslateCollision(
      speedX,
      0,
    );
    if (!collisionX) newDiffBase = Vector2(speedX, 0);
  }

  _updateDirectionBuAngle(angle);

  if (newDiffBase == Vector2.zero()) {
    onMove(0, lastDirection, angle);
    return false;
  }
  position.add(newDiffBase);
  onMove(speed, lastDirection, angle);
  return true;
}