Beispiel #1
0
  private Move.Rotation getBestRotationDirection() {
    Point2D centerPosition = getPlayerTank().getCenterPoint();
    double rotationAngle = (getPlayerTank().getRotationAngle() + 270) % 360;
    double fieldWidth = GameState.getFieldWidth(), fieldHeight = GameState.getFieldHeight();

    Point2D crossPoints[] = new Point2D[2];
    GameState.SystemSides targetSides[] = new GameState.SystemSides[2];
    targetSides[0] = findSystemSide(centerPosition, rotationAngle, fieldWidth, fieldHeight);
    targetSides[1] =
        findSystemSide(centerPosition, (rotationAngle + 180) % 360, fieldWidth, fieldHeight);

    for (int i = 0; i < 2; i++) {
      crossPoints[i] =
          getCrossPointWithSide(
              centerPosition, rotationAngle, fieldWidth, fieldHeight, targetSides[i]);
    }

    return getShortestRotation(fieldWidth, fieldHeight, crossPoints);
  }
Beispiel #2
0
  public Move makeMove(double deltaTime) {
    Point2D tankPosition = getPlayerTank().getCenterPoint();
    switch (currentAction) {
      case Searching:
        if (target == null) {
          searchForTarget();
        }
        if (target != null) {
          currentAction = Action.ReachingTarget;
        }
        break;

      case ReachingTarget:
        if ((euclideanSpaceDistance(target, getPlayerTank().getCenterPoint()) < bonusRadius)) {
          target = null;
          isAngleSet = false;
        }
        if (target == null) {
          currentAction = Action.Searching;
          isSearchingStarted = false;
        }
        break;
    }

    switch (currentAction) {
      case Idle:
        break;

      case Searching:
        if (!isSearchingStarted) {
          rotationToDo = getBestRotationDirection();
          isSearchingStarted = true;
        }
        movementToDo = Move.Movement.Staying;
        break;

      case ReachingTarget:
        double bonusAngle =
            (Math.toDegrees(
                        Math.atan2(
                            target.getY() - tankPosition.getY(),
                            target.getX() - tankPosition.getX()))
                    + 360)
                % 360;
        double tankRotationAngle = (getPlayerTank().getRotationAngle() + 270) % 360;
        double bonusVisionAngle =
            (Math.toDegrees(Math.atan2(bonusRadius, euclideanSpaceDistance(tankPosition, target)))
                    + 360)
                % 360;
        double deltaAngle = (bonusAngle - tankRotationAngle + 360) % 360;

        double shift = euclideanSpaceDistance(tankPosition, lastPosition);
        double maxShift =
            2 * getPlayerTank().getVelocity() * (1.0 / GameState.getFramesPerSecond());
        if (shift > maxShift) {
          isAngleSet = false;
        }

        if (!isAngleSet) {
          if (deltaAngle < 180 + bonusVisionAngle && deltaAngle > 180 - bonusVisionAngle) {
            isAngleSet = true;
          } else if (deltaAngle >= 0 && deltaAngle <= 180 - bonusVisionAngle) {
            rotationToDo = Move.Rotation.CounterClockwise;
          } else if (deltaAngle >= 180 + bonusVisionAngle && deltaAngle < 360) {
            rotationToDo = Move.Rotation.Clockwise;
          }
          movementToDo = Move.Movement.Staying;
        } else {
          double tankRadius = getPlayerTank().getRadius();
          if (tankPosition.getY() - tankRadius <= tankRadius / 8) {
            if (tankRotationAngle >= 0 && tankRotationAngle < 90) {
              rotationToDo = Move.Rotation.CounterClockwise;
            } else if (tankRotationAngle > 90 && tankRotationAngle <= 180) {
              rotationToDo = Move.Rotation.Clockwise;
            }
          } else if (tankPosition.getX() + tankRadius
              >= GameState.getFieldWidth() - tankRadius / 8) {
            if (tankRotationAngle >= 90 && tankRotationAngle < 180) {
              rotationToDo = Move.Rotation.CounterClockwise;
            } else if (tankRotationAngle > 180 && tankRotationAngle <= 270) {
              rotationToDo = Move.Rotation.Clockwise;
            }
          } else if (tankPosition.getY() + tankRadius >= GameState.fieldHeight - tankRadius / 8) {
            if (tankRotationAngle >= 180 && tankRotationAngle < 270) {
              rotationToDo = Move.Rotation.CounterClockwise;
            } else if (tankRotationAngle > 270 && tankRotationAngle <= 360) {
              rotationToDo = Move.Rotation.Clockwise;
            }
          } else if (tankPosition.getX() - tankRadius <= tankRadius / 8) {
            if (tankRotationAngle >= 270 && tankRotationAngle < 360) {
              rotationToDo = Move.Rotation.CounterClockwise;
            } else if (tankRotationAngle > 0 && tankRotationAngle <= 90) {
              rotationToDo = Move.Rotation.Clockwise;
            }
          } else {
            rotationToDo = Move.Rotation.Staying;
          }
          if (rotationToDo != Move.Rotation.Staying) {
            isAngleSet = false;
          }
          movementToDo = Move.Movement.Backward;
        }

        lastPosition = getPlayerTank().getCenterPoint();

        break;
    }

    return new Move(movementToDo, rotationToDo, shootingToDo);
  }