示例#1
0
    // =========================================================
    // FSM STATES
    // =========================================================

    // STATE start ---------------------------------------------
    void state_AvoidObstacles()
    {
        // START of checking the state change conditions ---------------------
        if (!obstacle1)
        {
            if (!playerShip)
            {
                ChangeStateTo_PlayerSearch_Or_GoToLastKnownPlayerShipPosition();
                return;
            }

            if (playerShip)
            {
                activeState = state_GoToPlayerShip;
                return;
            }
        }
        // END of checking the state change conditions---------------

        Vector3 ourShipDirection = transform.forward;

        ourShipDirection.y = 0;

        float angleInDegToAvoidObstacle;

        // if there is only one obstacle
        if (obstacle1 && !obstacle2)
        {
            angleInDegToAvoidObstacle = AIHelperFunctions.GetAngleInDegToAvoidObstacle(ourShip, obstacle1, windArea, additionalRandomAngle);
        }
        else // if there are two obstacles
        {
            float angleBetweenShortestShiptToObstacle1VectorAndShipDirection =
                AIHelperFunctions.GetAngleBetweenShortestShiptToObstacleVectorAndShipDirection(obstacle1, transform);
            float angleBetweenShortestShiptToObstacle2VectorAndShipDirection =
                AIHelperFunctions.GetAngleBetweenShortestShiptToObstacleVectorAndShipDirection(obstacle2, transform);

            if ((angleBetweenShortestShiptToObstacle1VectorAndShipDirection > 0 &&
                 angleBetweenShortestShiptToObstacle2VectorAndShipDirection < 0)
                ||
                (angleBetweenShortestShiptToObstacle1VectorAndShipDirection < 0 &&
                 angleBetweenShortestShiptToObstacle2VectorAndShipDirection > 0))
            {
                if (Mathf.Abs(angleBetweenShortestShiptToObstacle1VectorAndShipDirection) <
                    Mathf.Abs(angleBetweenShortestShiptToObstacle2VectorAndShipDirection))
                {
                    angleInDegToAvoidObstacle = AIHelperFunctions.GetGreaterAngleInDegToAvoidObstacle(ourShip, obstacle1, additionalRandomAngle);
                }
                else
                {
                    angleInDegToAvoidObstacle = AIHelperFunctions.GetGreaterAngleInDegToAvoidObstacle(ourShip, obstacle2, additionalRandomAngle);
                }
            }
            else if ((angleBetweenShortestShiptToObstacle1VectorAndShipDirection > 0 &&
                      angleBetweenShortestShiptToObstacle2VectorAndShipDirection == 0)
                     ||
                     (angleBetweenShortestShiptToObstacle2VectorAndShipDirection > 0 &&
                      angleBetweenShortestShiptToObstacle1VectorAndShipDirection == 0))
            {
                angleInDegToAvoidObstacle = 90 + additionalRandomAngle;
            }
            else if ((angleBetweenShortestShiptToObstacle1VectorAndShipDirection < 0 &&
                      angleBetweenShortestShiptToObstacle2VectorAndShipDirection == 0)
                     ||
                     (angleBetweenShortestShiptToObstacle2VectorAndShipDirection < 0 &&
                      angleBetweenShortestShiptToObstacle1VectorAndShipDirection == 0))
            {
                angleInDegToAvoidObstacle = -90 - additionalRandomAngle;
            }
            else if ((angleBetweenShortestShiptToObstacle1VectorAndShipDirection > 0 &&
                      angleBetweenShortestShiptToObstacle2VectorAndShipDirection > 0)
                     ||
                     (angleBetweenShortestShiptToObstacle1VectorAndShipDirection < 0 &&
                      angleBetweenShortestShiptToObstacle2VectorAndShipDirection < 0))
            {
                if (Mathf.Abs(angleBetweenShortestShiptToObstacle1VectorAndShipDirection) <
                    Mathf.Abs(angleBetweenShortestShiptToObstacle2VectorAndShipDirection))
                {
                    angleInDegToAvoidObstacle = AIHelperFunctions.GetAngleInDegToAvoidObstacle(ourShip, obstacle1, windArea, additionalRandomAngle);
                }
                else
                {
                    angleInDegToAvoidObstacle = AIHelperFunctions.GetAngleInDegToAvoidObstacle(ourShip, obstacle2, windArea, additionalRandomAngle);
                }
            }
            else //such situation should not exist!!!
            {
                angleInDegToAvoidObstacle = 0;
                Debug.Log("Such situation should not exist!!!");
            }
        }

        Vector3 rotatedVector = AIHelperFunctions.GetRotatedVector(ourShipDirection, angleInDegToAvoidObstacle * Mathf.Deg2Rad);

        enemyBoatController.SetNewShipDirection(rotatedVector);
    }
示例#2
0
    // =========================================================
    // FSM STATES
    // =========================================================

    // STATE start ---------------------------------------------
    void state_AvoidingObstacle()
    {
        if (!obstacle1)
        {
            activeState = state_MoveRandomly;
            return;
        }

        // if (!obstacle1) {
        //   if (!playerShip) {
        //     activeState = state_PlayerSearching;
        //     return;
        //   } else {
        //     activeState = state_GoToPlayerShip;
        //     return;
        //   }
        // }

        // Vector3 shipPosition = transform.position;
        Vector3 shipDirection = transform.forward;
        float   angleInDegToAvoidObstacle;

        if (obstacle1 && !obstacle2)
        {
            angleInDegToAvoidObstacle = AIHelperFunctions.GetAngleInDegToAvoidObstacle(ourShip, obstacle1, windArea, additionalRandomAngle);
        }
        else
        {
            float angleBetweenShortestShiptToObstacle1VectorAndShipDirection =
                AIHelperFunctions.GetAngleBetweenShortestShiptToObstacleVectorAndShipDirection(obstacle1, transform);
            float angleBetweenShortestShiptToObstacle2VectorAndShipDirection =
                AIHelperFunctions.GetAngleBetweenShortestShiptToObstacleVectorAndShipDirection(obstacle2, transform);

            if ((angleBetweenShortestShiptToObstacle1VectorAndShipDirection > 0 &&
                 angleBetweenShortestShiptToObstacle2VectorAndShipDirection < 0)
                ||
                (angleBetweenShortestShiptToObstacle1VectorAndShipDirection < 0 &&
                 angleBetweenShortestShiptToObstacle2VectorAndShipDirection > 0))
            {
                if (Mathf.Abs(angleBetweenShortestShiptToObstacle1VectorAndShipDirection) <
                    Mathf.Abs(angleBetweenShortestShiptToObstacle2VectorAndShipDirection))
                {
                    angleInDegToAvoidObstacle = AIHelperFunctions.GetGreaterAngleInDegToAvoidObstacle(ourShip, obstacle1, additionalRandomAngle);
                }
                else
                {
                    angleInDegToAvoidObstacle = AIHelperFunctions.GetGreaterAngleInDegToAvoidObstacle(ourShip, obstacle2, additionalRandomAngle);
                }
            }
            else if ((angleBetweenShortestShiptToObstacle1VectorAndShipDirection > 0 &&
                      angleBetweenShortestShiptToObstacle2VectorAndShipDirection == 0)
                     ||
                     (angleBetweenShortestShiptToObstacle2VectorAndShipDirection > 0 &&
                      angleBetweenShortestShiptToObstacle1VectorAndShipDirection == 0))
            {
                angleInDegToAvoidObstacle = 90 + 20;
            }
            else if ((angleBetweenShortestShiptToObstacle1VectorAndShipDirection < 0 &&
                      angleBetweenShortestShiptToObstacle2VectorAndShipDirection == 0)
                     ||
                     (angleBetweenShortestShiptToObstacle2VectorAndShipDirection < 0 &&
                      angleBetweenShortestShiptToObstacle1VectorAndShipDirection == 0))
            {
                angleInDegToAvoidObstacle = -90 - 20;
            }
            else if ((angleBetweenShortestShiptToObstacle1VectorAndShipDirection > 0 &&
                      angleBetweenShortestShiptToObstacle2VectorAndShipDirection > 0)
                     ||
                     (angleBetweenShortestShiptToObstacle1VectorAndShipDirection < 0 &&
                      angleBetweenShortestShiptToObstacle2VectorAndShipDirection < 0))
            {
                if (Mathf.Abs(angleBetweenShortestShiptToObstacle1VectorAndShipDirection) <
                    Mathf.Abs(angleBetweenShortestShiptToObstacle2VectorAndShipDirection))
                {
                    angleInDegToAvoidObstacle = AIHelperFunctions.GetAngleInDegToAvoidObstacle(ourShip, obstacle1, windArea, additionalRandomAngle);
                }
                else
                {
                    angleInDegToAvoidObstacle = AIHelperFunctions.GetAngleInDegToAvoidObstacle(ourShip, obstacle2, windArea, additionalRandomAngle);
                }
            }
            else
            {
                angleInDegToAvoidObstacle = 0;
                Debug.Log("Such situation should not exist!!!");
            }
        }

        Vector3 rotatedVector = AIHelperFunctions.GetRotatedVector(shipDirection, angleInDegToAvoidObstacle * Mathf.Deg2Rad);

        enemyBoatController.SetNewShipDirection(rotatedVector);
    }