/
arti4
/
CourseWork
Обзор
Документация
Войти
/
arti4
/
CourseWork
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Scriptes/Bomb.cs
199 строк
6 KB
arti4
Загрузить файлы в «»
01 июн 2025, 11:56
01 июн 2025, 11:56
6a254d9
Код
Авторство
О чём код?
using UnityEngine; using System.Collections; public class Bomb : MonoBehaviour { public int attackDamage; public float explosionRadius; public float explosionDelay; private bool hasExploded = false; public Animator animator; public LayerMask damageableLayers; private Rigidbody2D rb; private bool isGrounded = false; [SerializeField] private AudioSource audioSource; [SerializeField] private AudioClip bombSound; [SerializeField] private AudioClip destructionSound; private Transform player; public float maxHearingDistance = 5f; private void Start() { animator = transform.Find("Bomb")?.GetComponent<Animator>(); rb = GetComponent<Rigidbody2D>(); if (audioSource != null && bombSound != null) { audioSource.clip = bombSound; audioSource.loop = true; audioSource.Play(); } GameObject playerObject = GameObject.FindGameObjectWithTag("Player"); if (playerObject != null) { player = playerObject.transform; } } private void Update() { //������ ������� if (!isGrounded) { rb.gravityScale = 1f; } else { rb.gravityScale = 0f; rb.linearVelocity = Vector2.zero; } AdjustSoundVolume(); } //��������� ��������� � ����������� �� ���������� �� ������ private void AdjustSoundVolume() { if (audioSource == null || player == null) return; float distance = Vector2.Distance(transform.position, player.position); if (distance > maxHearingDistance) { audioSource.volume = 0; return; } if (distance <= maxHearingDistance / 2) { audioSource.volume = 1; return; } float volumeFactor = Mathf.Clamp01(1 - ((distance - (maxHearingDistance / 2)) / (maxHearingDistance / 2))); audioSource.volume = volumeFactor; } //������ ��� ������� private void OnDrawGizmosSelected() { Gizmos.color = Color.red; Gizmos.DrawWireSphere(transform.position, explosionRadius); Gizmos.color = Color.blue; Gizmos.DrawWireSphere(transform.position, maxHearingDistance); } private void OnTriggerEnter2D(Collider2D other) { //��������� ������������ ����� if (other.CompareTag("Ground") || other.CompareTag("Barbwire") || other.CompareTag("Box")) { isGrounded = true; } //��������� �������, ������ ��� ����� if (other.CompareTag("Player") || other.CompareTag("Bullet") || other.CompareTag("Enemy")) { if (!hasExploded) { ExplodeImmediately(); } } } //����������� ������� private void OnTriggerExit2D(Collider2D other) { if (other.CompareTag("Ground") || other.CompareTag("Barbwire") || other.CompareTag("Box")) { isGrounded = false; } } //����� ����, ������� ���� private void ExplodeImmediately() { if (hasExploded) return; hasExploded = true; if (animator != null) { animator.SetTrigger("Explosion"); } if (audioSource != null) { audioSource.loop = false; audioSource.Stop(); } Collider2D bombCollider = GetComponent<Collider2D>(); if (bombCollider != null) { bombCollider.enabled = false; } Explode(); } //����� private void Explode() { Collider2D[] objectsInRange = Physics2D.OverlapCircleAll(transform.position, explosionRadius, damageableLayers); foreach (Collider2D collider in objectsInRange) { if (collider.CompareTag("Player")) { PlayerHealth playerHealth = collider.GetComponent<PlayerHealth>(); if (playerHealth != null) { playerHealth.TakeDamage(attackDamage); } } else if (collider.CompareTag("Enemy")) { if (collider.GetComponentInParent<Swordsman>() != null) { Swordsman enemy = collider.GetComponentInParent<Swordsman>(); enemy.TakeDamage(attackDamage); } else if (collider.GetComponent<Rifleman>() != null) { Rifleman rifleman = collider.GetComponent<Rifleman>(); rifleman.TakeDamage(attackDamage); } } else if (collider.CompareTag("Barbwire")) { Barbwire barbwire = collider.GetComponent<Barbwire>(); if (barbwire != null) { barbwire.TakeDamage(attackDamage); } } else if (collider.CompareTag("Bomb")) { Bomb otherBomb = collider.GetComponent<Bomb>(); if (otherBomb != null && otherBomb != this) { otherBomb.ExplodeImmediately(); } } } StartCoroutine(DestroyBombAfterAnimation()); } //����������� ������� ����� ���������� �������� private IEnumerator DestroyBombAfterAnimation() { if (destructionSound != null) { AudioSource.PlayClipAtPoint(destructionSound, transform.position); yield return new WaitForSeconds(destructionSound.length); } AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0); yield return new WaitForSeconds(stateInfo.length); Destroy(gameObject); } }