/**
   * 获取某张地图上以center为圆心,朝向target,且半径为radius,角度为angle的扇形区域内的UnitObject,isFront用于选择是正面还是背面,true正面
   *
   * @param stageObj
   * @param center
   * @param target
   * @param radius
   * @param angle
   * @param isFront
   * @return
   */
  public List<UnitObject> getUnitObjsInSector(
      StageObject stageObj,
      Vector2D center,
      Vector2D target,
      double radius,
      int angle,
      boolean isFront) {
    // 如果是取背面,把目标点变为中心点的对称点
    if (!isFront) {
      double x = 2 * target.x - center.x;
      double y = 2 * target.y - center.y;
      target = new Vector2D(x, y);
    }

    List<UnitObject> result = new ArrayList<>();
    for (WorldObject wo : stageObj.getWorldObjs().values()) {
      if (!(wo instanceof UnitObject)) continue;
      // 如果该UnitObject在扇形区域内
      if (isPosInSector(wo.posNow, center, target, radius, angle)) {
        result.add((UnitObject) wo);
      }
    }

    return result;
  }
  /**
   * 获取以pos为圆心,半径为radius的圆形区域内的UnitObject
   *
   * @param stageObj
   * @param pos
   * @param radius
   * @return
   */
  public List<UnitObject> getUnitObjsInCircle(StageObject stageObj, Vector2D pos, double radius) {
    List<UnitObject> result = new ArrayList<>();
    for (UnitObject uo : stageObj.getUnitObjs().values()) {
      // 判断距离是否符合要求
      Vector2D p = uo.posNow;
      if (pos.distance(p) < radius) {
        result.add(uo);
      }
    }

    return result;
  }
  /**
   * 范围内HP 最少的
   *
   * @param stageObj
   * @param exclude
   * @param pos
   * @param radius
   * @return
   */
  public UnitObject getUnitObjsInCircleHpLeast(
      StageObject stageObj, List<UnitObject> exclude, Vector2D pos, double radius) {
    int hpMax = 9999999;
    UnitObject hpLeastObj = null;
    for (UnitObject uo : stageObj.getUnitObjs().values()) {
      // 判断距离是否符合要求
      Vector2D p = uo.posNow;
      if (pos.distance(p) < radius) {
        if (uo.getUnit().getHpCur() < hpMax && !exclude.contains(uo)) {
          hpLeastObj = uo;
          hpMax = uo.getUnit().getHpCur();
        }
      }
    }

    return hpLeastObj;
  }
  public List<UnitObject> getUnitObjsInCircleProfession(
      StageObject stageObj, int profession, Vector2D pos, double radius) {
    List<UnitObject> result = new ArrayList<>();
    int pro = 0;
    if ((profession & 1) > 0) {
      pro = 1;
    } else if ((profession & 2) > 0) {
      pro = 2;
    } else if ((profession & 4) > 0) {
      pro = 3;
    }
    for (UnitObject uo : stageObj.getUnitObjs().values()) {
      // 判断距离是否符合要求
      Vector2D p = uo.posNow;
      if (pos.distance(p) < radius) {
        if (uo.profession == pro) {
          result.add(uo);
        }
      }
    }

    return result;
  }
  /**
   * 获取以pos位置为起点,target为终点的线段,往线段两边扩展width/2,并且从起始点为长度length的矩形区域内的unitObj
   *
   * @param stageObj
   * @param pos
   * @param length
   * @param width
   * @return
   */
  public List<UnitObject> getUnitObjsInRectangle(
      StageObject stageObj, Vector2D pos, Vector2D target, double height, double width) {
    List<UnitObject> result = new ArrayList<>();

    double c = pos.distance(target);

    // 利用余弦定理计算夹角a的余弦值
    double cosA = (double) (target.x - pos.x) / c;
    double sinA = (double) (target.y - pos.y) / c;

    for (UnitObject uo : stageObj.getUnitObjs().values()) {
      // 判断距离是否符合要求

      Vector2D p = uo.posNow;
      // p绕pos点旋转-A角度到新坐标
      // 如果P点绕另一点P0(x0,y0)旋转β到点P1,旋转后位置计算公式如下:
      // dx = x-x0;
      // dy = y-y0;
      // x1=cos(β)*dx-sin(β)*dy+x0;
      // y1=cos(β)*dy+sin(β)*dx+y0;
      double cosB = cosA;
      double sinB = -sinA;
      double dx = p.x - pos.x;
      double dy = p.y - pos.y;
      double x1 = cosB * dx - sinB * dy + pos.x;
      double y1 = cosB * dy + sinB * dx + pos.y;

      if (x1 >= pos.x
          && x1 <= (pos.x + width)
          && y1 <= (pos.y + height / 2)
          && pos.y >= pos.y - height / 2) {
        result.add(uo);
      }
    }

    return result;
  }