public bool IsHearable(IListener listener, IAudioSource source, AudioSourceInfo info)
        {
            if (info.volume >= listener.config.minHearingVolume)
            {
                var  path      = new UnityEngine.AI.NavMeshPath();
                bool foundPath = UnityEngine.AI.NavMesh.CalculatePath(source.transform.position, listener.transform.position, UnityEngine.AI.NavMesh.AllAreas, path);
                if (foundPath && path.status == UnityEngine.AI.NavMeshPathStatus.PathComplete)
                {
                    var corners = path.corners;

                    float pathLength = 0f;
                    for (int i = 0; i < corners.Length - 1; i++)
                    {
                        pathLength += Vector3.Distance(corners[i], corners[i + 1]);
                        if (_config.debug)
                        {
                            Debug.DrawLine(corners[i], corners[i + 1], Color.red, 0.2f);
                        }
                    }

                    //Debug.Log("Path length is " + pathLength);
                    return(pathLength < Vector3.Distance(source.transform.position, listener.transform.position) * PathMaxLengthMultiplier);
                }
            }

            return(false);
        }
 public void OnHeardTarget(AudioSourceInfo info)
 {
     lastHeardAudioSource = info;
     if (config.debug)
     {
         Debug.Log("<color=green>Heard target " + info.audioSource.audioSource.clip + " (volume: " + info.volume + ")</color>", info.audioSource.gameObject);
     }
 }
 public void OnHeardTarget(AudioSourceInfo info)
 {
     if (info.audioSource.emitter != null)
     {
         var target = info.audioSource.emitter.GetComponentInParent <ISightTarget>();
         if (target != null)
         {
             AddTargetToAggroList(target);
         }
     }
 }
        public bool IsHearable(IListener listener, IAudioSource source, AudioSourceInfo info)
        {
            if (info.volume >= listener.config.minHearingVolume)
            {
                var raycastPosition = new Vector3[]
                {
                    new Vector3(0f, 0f),  // Middle
                    new Vector3(0f, 1f),  // Up
                    new Vector3(0f, -1f), // Down
                    new Vector3(-1f, 0f), // Left side
                    new Vector3(1f, 0f),  // Right side
                };

                const int   levels  = 1;
                const float scaling = 1.5f;
                for (int i = 1; i < levels + 1; i++)
                {
                    foreach (var pos in raycastPosition)
                    {
                        var raycastToPosition = listener.transform.position + (pos * i * scaling);

                        RaycastHit hitInfo;
                        bool       hit = _raycaster.Linecast(source.transform.position, raycastToPosition, out hitInfo, _config.raycastLayer);
                        // If nothing was hit the object is visible.
                        if (hit == false)
                        {
                            hitInfo.gameObject = listener.gameObject;
                            hitInfo.point      = raycastToPosition;
                            hitInfo.normal     = Vector3.zero;
                        }

                        // Direct path from audio source to listener.
                        if (_config.debug)
                        {
                            LosDebugUtility.DrawDebugLine(source.gameObject, hitInfo.gameObject, listener.gameObject, source.transform.position, hitInfo.point, raycastToPosition, Color.magenta, Color.black);
                        }

                        // If we did hit something, is it the target?
                        if (LosUtility.IsValidTarget(listener.gameObject, hitInfo.gameObject) || hit == false)
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
        protected virtual void OnTargetEnter(GameObject target)
        {
            var listener = target.GetComponentInParent <IListener>();

            if (listener != null && LosUtility.IsValidTarget(listener.gameObject, target))
            {
                if (audioSource.IsHeardBy(listener))
                {
                    // Prevent objects with multiple colliders to invoke events more than once.
                    return;
                }

                if (((listener.config.targetCategoryMask & audioSource.emitterCategory) == 0) || audioSource.emitterCategory == 0)
                {
                    return;
                }

                var dist = Vector3.Distance(transform.position, listener.gameObject.transform.position);
                var info = new AudioSourceInfo()
                {
                    volume = Mathf.Clamp01((audioSource.maxGrowthSize - dist) / audioSource.maxGrowthSize),
                    lastHeardAtPosition = new AudioSourceSampleData()
                    {
                        position = transform.position,
                        time     = LosUtility.time
                    },
                    audioSource = audioSource,
                    listener    = listener
                };

                if (listener.hearableValidators.Length == 0)
                {
                    HeardBy(ref info);
                }
                else
                {
                    foreach (var validator in listener.hearableValidators)
                    {
                        if (validator.IsHearable(listener, audioSource, info))
                        {
                            HeardBy(ref info);
                            break;
                        }
                    }
                }
            }
        }
        private void HeardBy(ref AudioSourceInfo info)
        {
            info.listener.gameObject.GetComponents(_listenerCallbacks);
            foreach (var callback in _listenerCallbacks)
            {
                callback.OnHeardTarget(info);
            }

            gameObject.GetComponents(_callbacks);
            foreach (var callback in _callbacks)
            {
                callback.OnHeardBy(info);
            }

            if (audioSource.emitter != null && gameObject != audioSource.emitter)
            {
                audioSource.emitter.GetComponents(_callbacks);
                foreach (var callback in _callbacks)
                {
                    callback.OnHeardBy(info);
                }
            }
        }
        public void OnHeardBy(AudioSourceInfo info)
        {
            _heardBy.Add(info.listener);

            // Debug.Log("Audio source " + gameObject.name + " is heard by " + listener.gameObject.name, gameObject);
        }
 public void OnHeardTarget(AudioSourceInfo info)
 {
     FireEvent("OnHeardTarget", info.audioSource.emitter);
 }
 public void OnHeardBy(AudioSourceInfo info)
 {
     FireEvent("OnHeardBy", info.audioSource.gameObject);
 }
Example #10
0
 public void OnHeardTarget(AudioSourceInfo info)
 {
     _hearingHistory.Add(new KeyValuePair <AudioSourceInfo, float>(info, LosUtility.time + forgetSoundAfterSeconds));
 }