using System.Collections; using NavKeypad; using UnityEngine; public class Door : OpenableObject { [SerializeField] private float rotateByDegrees = -90f; [SerializeField] private float enemyDetectionRange = 3f; [SerializeField] private Collider doorCollider; [SerializeField] private float openTime = 0.5f; [SerializeField] private float closeTime = 1f; [SerializeField] private bool isForbiddenForEnemy = false; [SerializeField] public AudioClip openSound; [SerializeField] public AudioClip closeSound; private Vector3 startRotation; private Vector3 endRotation; private bool isMoving; private bool isOpen; private AudioSource audioSource; void Start() { // Проверяем и добавляем AudioSource audioSource = GetComponent(); if (audioSource == null) { audioSource = gameObject.AddComponent(); audioSource.playOnAwake = false; audioSource.loop = false; audioSource.spatialBlend = 1f; audioSource.volume = 0.7f; Debug.Log($"Добавлен AudioSource на {gameObject.name}"); } startRotation = transform.rotation.eulerAngles; endRotation = startRotation + Vector3.up * rotateByDegrees; isOpen = false; // Проверяем звуки if (openSound == null) Debug.LogWarning($"Дверь {gameObject.name}: openSound не назначен!"); if (closeSound == null) Debug.LogWarning($"Дверь {gameObject.name}: closeSound не назначен!"); // Синхронизируем громкость GameUIManager uiManager = FindObjectOfType(); if (uiManager != null && uiManager.soundVolumeSlider != null) { UpdateVolume(uiManager.soundVolumeSlider.value); } } void Update() { if (!isMoving && !isOpen && !isForbiddenForEnemy) { Collider[] colliders = Physics.OverlapSphere(transform.position, enemyDetectionRange); foreach (Collider col in colliders) { if (col.CompareTag("Enemy")) { StartCoroutine(Open()); break; } } } } public override IEnumerator Open() { if (IsLockedByKeypad) { Debug.Log("Дверь заблокирована кодом!"); yield break; } isMoving = true; isOpen = true; // Проигрываем звук открывания if (openSound != null) { audioSource.clip = openSound; audioSource.Play(); Debug.Log($"Дверь {gameObject.name} открывается!"); } else { Debug.LogWarning($"Дверь {gameObject.name}: openSound не имеет звука!"); } if (doorCollider != null) { doorCollider.enabled = false; } float timer = 0f; while (timer < 1f) { timer += Time.deltaTime / openTime; openOrCloseLerp = timer; transform.rotation = Quaternion.Lerp(Quaternion.Euler(startRotation), Quaternion.Euler(endRotation), timer); yield return null; } isMoving = false; } public override IEnumerator Close() { isMoving = true; isOpen = false; // Проигрываем звук закрывания if (closeSound != null) { audioSource.clip = closeSound; audioSource.Play(); Debug.Log($"Дверь {gameObject.name} закрывается!"); } else { Debug.LogWarning($"Дверь {gameObject.name}: closeSound не имеет звука!"); } if (doorCollider != null) { doorCollider.enabled = true; } float timer = 0f; while (timer < 1f) { timer += Time.deltaTime / closeTime; openOrCloseLerp = 1f - timer; transform.rotation = Quaternion.Lerp(Quaternion.Euler(startRotation), Quaternion.Euler(endRotation), 1f - timer); yield return null; } isMoving = false; } public void UpdateVolume(float volume) { if (audioSource != null) { audioSource.volume = volume * 0.8f; Debug.Log($"Door: Громкость звука двери {gameObject.name} = {audioSource.volume}"); } } void OnDrawGizmosSelected() { Gizmos.color = Color.green; Gizmos.DrawWireSphere(transform.position, enemyDetectionRange); } }