public void CreateSoundStim(bool isPlayerStanding = true, string soundName = null)
    {
        GameObject soundObject;

        if (soundName != null)
        {
            soundObject = new GameObject(soundName);
        }
        else
        {
            soundObject = new GameObject("Player Sound");
        }

        soundObject.transform.position = transform.position;

        SoundStim sound = soundObject.AddComponent <SoundStim>();

        float soundRaduisTypeModifier = 1;
        float soundRadiusModifier     = 1;

        if (m_soundType == SoundType.MEDIUM)
        {
            soundRaduisTypeModifier *= 1.2f;
        }
        else if (m_soundType == SoundType.LOUD)
        {
            soundRaduisTypeModifier *= 2;
        }


        if (m_FirstPersonController.m_MovingType == FirstPersonController.MovingType.SLOWWALK)
        {
            soundRadiusModifier = 0.4f;
        }
        else if (m_FirstPersonController.m_MovingType == FirstPersonController.MovingType.RUN)
        {
            soundRadiusModifier = 1.3f;
        }

        if (!isPlayerStanding)
        {
            soundRadiusModifier *= .8f;
        }

        sound.radius   = m_SoundRaduis * soundRaduisTypeModifier * soundRadiusModifier;
        sound.lifeTime = m_SoundLifeTime * soundRaduisTypeModifier * soundRadiusModifier;
    }
Example #2
0
    private void Update()
    {
        bool goalVisable  = m_Eyes.CanSeeGoal(BlackBoard.b_player.transform.position);
        bool canSeePlayer = false;

        //If in range to the player
        float DistanceToPlayer = (transform.position - BlackBoard.b_player.transform.position).sqrMagnitude;
        float viewDistancesqr  = m_Eyes.viewDistance * m_Eyes.viewDistance;

        if (DistanceToPlayer < viewDistancesqr)
        {
            //TODO Consider player height crouching or not
            //If can see player
            if (goalVisable)
            {
                //Consider how far away the player is
                float lightPercentage = BlackBoard.b_PlayerShadowAmount.LightPercentage;
                if (DistanceToPlayer >= viewDistancesqr * .2f)
                {
                    float normal = Mathf.InverseLerp(0, 1 - 1 * .2f, 1.0f - (DistanceToPlayer / viewDistancesqr));
                    float temp   = Mathf.Lerp(0, 1, normal);
                    lightPercentage = Mathf.Max(lightPercentage * temp, lightPercentage * .6f);
                }

                if (lightPercentage > 75)
                {
                    ChangeAlertState(AlertState.HIGH);
                    canSeePlayer = true;
                }
                else if (lightPercentage > 50)
                {
                    ChangeAlertState(AlertState.MEDIUM);
                    canSeePlayer = true;
                }
                else if (lightPercentage > 25)
                {
                    ChangeAlertState(AlertState.LOW);
                }
            }
        }

        //Can hear player
        //Set Alerted Level
        GameObject HeardObject = null;

        for (int i = 0, BlackBoardb_soundsCount = BlackBoard.b_sounds.Count; i < BlackBoardb_soundsCount; i++)
        {
            SoundStim s = BlackBoard.b_sounds[i];
            if ((transform.position - s.transform.position).sqrMagnitude < s.GetRadius() * s.GetRadius())
            {
                HeardObject = s.gameObject;
                break;
            }
        }

        if (HeardObject != null)
        {
            TimeHearingSound += Time.deltaTime;
        }
        else
        {
            TimeHearingSound = 0;
        }

        //Do we need to account for how loud the sound is?
        if (TimeHearingSound > 0.55)
        {
            ChangeAlertState(AlertState.HIGH);
        }
        else if (TimeHearingSound > 0.35)
        {
            ChangeAlertState(AlertState.MEDIUM);
        }
        else if (TimeHearingSound > 0.15)
        {
            ChangeAlertState(AlertState.LOW);
        }


        //Player in AI proximity and AlertState Low or Higher
        //Set Alerted Level
        bool playerInProximity = false;

        if (DistanceToPlayer < ProximityDistance * ProximityDistance)
        {
            playerInProximity = true;
            if (m_AlertState == AlertState.MEDIUM)
            {
                ChangeAlertState(AlertState.MEDIUM);
            }

            if (DistanceToPlayer < ImmediateDetectionRadius * ImmediateDetectionRadius)
            {
                ChangeAlertState(AlertState.HIGH);
            }
        }


        if (HeardObject != null && !canSeePlayer && !playerInProximity)
        {
            LastKnownSearchPostion = HeardObject.transform.position;
        }



        //If on high alert, Alert nearby guards
        //With players postion, or last know position
        AlertNearByAI();


        if (!pause)
        {
            if ((canSeePlayer || playerInProximity) && (m_AlertState == AlertState.MEDIUM || m_AlertState == AlertState.HIGH))
            {
                //TODO Change agent speed
                m_Movement.UpdateAgentDestination(BlackBoard.b_player.transform.position);
            }
            else if (m_AlertState == AlertState.MEDIUM || m_AlertState == AlertState.HIGH)
            {
                m_Movement.UpdateAgentDestination(LastKnownSearchPostion);

                if (!m_Movement.agent.pathPending && m_Movement.agent.path.corners.Length < 1)
                {
                    //Look at last know player locatation
                    Vector3 targetDir = (LastKnownSearchPostion + playerLastKnowVelocity) - transform.position;

                    // The step size is equal to speed times frame time.
                    float step = 4 * Time.deltaTime;

                    Vector3 newDir = Vector3.RotateTowards(transform.forward, targetDir, step, 0.0f);
                    Debug.DrawRay(transform.position, newDir, Color.red);

                    // Move our position a step closer to the target.
                    transform.rotation = Quaternion.LookRotation(newDir);
                }


                //TODO Search for player
                Debug.Log(name + " is searching for the player");
            }
            else
            {
                //TODO Set Agent speed back to default
                m_Movement.Guard(wasVisable);
            }
        }

        //TODO If Ai Alerted, make noise

        if (AlertStateTimer > 0)
        {
            AlertStateTimer -= Time.deltaTime;

            if (m_AlertState == AlertState.HIGH && AlertStateTimer < (AlertStateDecayTime / 1.5))
            {
                m_AlertState--;
                //m_AlertState = (AlertState)(((int)m_AlertState) - 1);
            }
            else if (m_AlertState == AlertState.MEDIUM && AlertStateTimer < (AlertStateDecayTime / 2.5))
            {
                m_AlertState--;
                //m_AlertState = (AlertState)(((int)m_AlertState) - 1);
            }
        }
        else
        {
            //TODO Makes noise to let the player know the enemy lost interest
            m_AlertState           = AlertState.OFF;
            LastKnownSearchPostion = Vector3.zero;
            playerLastKnowVelocity = Vector3.zero;
            CanBeAlertedByAI       = true;
        }

        wasVisable = canSeePlayer;
    }