Exemplo n.º 1
0
    private void SelectCover()
    {
        //Stand up
        _anim.SetBool("isCrouching", false);
        _aiWeapon.SetTarget(null);
        _aiWeapon.SetFiring(false);

        //Clear the change cover timer
        _changeCoverTimer = 0.0f;

        //Remove the AI from their current cover
        if (_currentCover)
        {
            _currentCover.RemoveUser();
        }

        CoverController closestCover    = null;
        float           closestDistance = 10000.0f;

        //Cycle through each cover in the array
        foreach (CoverController cover in _coversInZone)
        {
            if (_currentCover != null && _currentCover == cover)
            {
                continue;
            }

            //If the cover is full then don't go to this cover
            if (cover.IsFull)
            {
                continue;
            }

            //Calculate the distance between the agent and the cover
            float distance = Vector3.Distance(_agent.transform.position, cover.transform.position);


            //if the distance from this cover is less than the distance from the closest cover
            if (distance < closestDistance)
            {
                //Set the new closest cover
                closestCover    = cover;
                closestDistance = distance;
            }
        }
        //Set our new cover
        _currentCover = closestCover;

        if (_currentCover != null)
        {
            //Move the agent to the cover
            _currentCover.AddUser();


            //Find the cover point which is furthest away from the player
            float     furthestCoverPointDistance = 0.0f;
            Transform furthestCoverPoint         = _currentCover.GetCoverPoints()[0];
            foreach (Transform coverPoint in _currentCover.GetCoverPoints())
            {
                //Get the distance from the player to the cover points
                float distance = Vector3.Distance(_agent.Player.position, coverPoint.transform.position);

                //Find the furthest one
                if (distance > furthestCoverPointDistance)
                {
                    furthestCoverPoint         = coverPoint;
                    furthestCoverPointDistance = distance;
                }
            }

            //Move to the cover point
            _navAgent.SetDestination(furthestCoverPoint.position);
        }
    }