示例#1
0
    // STATE end -----------------------------------------------

    // STATE start ---------------------------------------------
    void state_SailParallelToPlayerShip()
    {
        // START of checking the state change conditions ---------------------
        if (obstacle1)
        {
            activeState = state_AvoidObstacles;
            return;
        }

        if (!playerShip)
        {
            Debug.Log("SailParallelToPlayerShip ------> ChangeStateTo_PlayerSearch_Or_GoToLastKnownPosition");
            ChangeStateTo_PlayerSearch_Or_GoToLastKnownPlayerShipPosition();
            return;
        }

        // This check can be performed only if playerShip != null
        DetermineInWhichZoneIsOurShip();

        if (!ourShipIsInParallelZone && !ourShipIsInZoneBetweenParallelZones)
        {
            activeState = state_GoToPlayerShip;
            return;
        }

        if (!ourShipIsInParallelZone && ourShipIsInZoneBetweenParallelZones)
        {
            activeState = state_GoToParallelZone;
            return;
        }
        // END of checking the state change conditions---------------

        Vector3 towardsPlayerShipDirection = playerShip.transform.position - transform.position;

        Vector3 playerShipDirection = playerShip.transform.forward;

        playerShipDirection.y = 0;

        Vector3 ourShipDirection = transform.forward;

        ourShipDirection.y = 0;

        Debug.DrawLine(transform.position, playerShip.transform.position, Color.red, 0.0f, true);

        float angleBetweenPlayerShipDirectionAndOurShipDirection = Vector3.Angle(playerShipDirection, ourShipDirection);

        if (angleBetweenPlayerShipDirectionAndOurShipDirection > 90)
        {
            angleBetweenPlayerShipDirectionAndOurShipDirection = 180 - angleBetweenPlayerShipDirectionAndOurShipDirection;
        }

        if ((angleBetweenPlayerShipDirectionAndOurShipDirection <= 10) &&
            (AIHelperFunctions.GetDistanceBetweenPerpendicular(this.gameObject, playerShip) <= 3))
        {
            activeState = state_Battle;
            return;
        }

        if (closeToPlayerShip)
        {
            if (angleBetweenPlayerShipDirectionAndOurShipDirection > 0.5)
            {
                AIHelperFunctions.SetShipsParallel(ourShip, playerShip, enemyBoatController);
            }
        }
        else //not closeToPlayerShip
        {
            if (AIHelperFunctions.IsOurShipDirectionConsistentWithTowardsOtherShipDirection(ourShipDirection, towardsPlayerShipDirection))
            {
                if (angleBetweenPlayerShipDirectionAndOurShipDirection > 0.5)
                {
                    AIHelperFunctions.SetShipsParallel(ourShip, playerShip, enemyBoatController);
                }
            }
            else
            {
                activeState = state_GoToPlayerShip;
            }
        }
    }
示例#2
0
    // STATE end -----------------------------------------------

    // STATE start ---------------------------------------------
    void state_GoToPlayerShip()
    {
        // START of checking the state change conditions ---------------------
        if (obstacle1)
        {
            activeState = state_AvoidObstacles;
            return;
        }

        if (!playerShip)
        {
            Debug.Log("GoToPlayerShip ------> ChangeStateTo_PlayerSearch_Or_GoToLastKnownPosition");
            ChangeStateTo_PlayerSearch_Or_GoToLastKnownPlayerShipPosition();
            return;
        }

        Vector3 towardsPlayerShipDirection = playerShip.transform.position - transform.position;

        towardsPlayerShipDirection.y = 0;

        Vector3 playerShipDirection = playerShip.transform.forward;

        playerShipDirection.y = 0;

        Vector3 shipDirection = transform.forward;

        shipDirection.y = 0;

        // This check can be performed only if playerShip != null
        DetermineInWhichZoneIsOurShip();
        ourShipDirectionIsConsistentWithTowardsPlayerShipDirection =
            AIHelperFunctions.IsOurShipDirectionConsistentWithTowardsOtherShipDirection(shipDirection, towardsPlayerShipDirection);

        if (ourShipIsInParallelZone)
        {
            if (closeToPlayerShip)
            {
                activeState = state_SailParallelToPlayerShip;
                return;
            }

            if (!closeToPlayerShip)
            {
                if (ourShipDirectionIsConsistentWithTowardsPlayerShipDirection)
                {
                    activeState = state_SailParallelToPlayerShip;
                    return;
                }
            }
        }

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

        Debug.DrawLine(transform.position, playerShip.transform.position, Color.white, 0.0f, true);

        // Taking wind into account
        float angleBetweenTowardsPlayerShipDirectionAndWindDirection = Vector3.SignedAngle(towardsPlayerShipDirection, windArea.windDirection, Vector3.up);

        if (angleBetweenTowardsPlayerShipDirectionAndWindDirection > 150)
        {
            towardsPlayerShipDirection = AIHelperFunctions.GetRotatedVector(windArea.windDirection, -150 * Mathf.Deg2Rad);
        }
        else if (angleBetweenTowardsPlayerShipDirectionAndWindDirection < -150)
        {
            towardsPlayerShipDirection = AIHelperFunctions.GetRotatedVector(windArea.windDirection, 150 * Mathf.Deg2Rad);
        }

        enemyBoatController.SetNewShipDirection(towardsPlayerShipDirection);
    }