DetonatorSound.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using UnityEngine;
  3. [RequireComponent(typeof(Detonator))]
  4. [AddComponentMenu("Detonator/Sound")]
  5. public class DetonatorSound : DetonatorComponent
  6. {
  7. public override void Init()
  8. {
  9. this._soundComponent = base.gameObject.AddComponent<AudioSource>();
  10. }
  11. private void Update()
  12. {
  13. if (this._soundComponent == null)
  14. {
  15. return;
  16. }
  17. this._soundComponent.pitch = Time.timeScale;
  18. if (this._delayedExplosionStarted)
  19. {
  20. this._explodeDelay -= Time.deltaTime;
  21. if (this._explodeDelay <= 0f)
  22. {
  23. this.Explode();
  24. }
  25. }
  26. }
  27. public override void Explode()
  28. {
  29. if (this.detailThreshold > this.detail)
  30. {
  31. return;
  32. }
  33. if (!this._delayedExplosionStarted)
  34. {
  35. this._explodeDelay = this.explodeDelayMin + UnityEngine.Random.value * (this.explodeDelayMax - this.explodeDelayMin);
  36. }
  37. if (this._explodeDelay <= 0f)
  38. {
  39. if (Vector3.Distance(Camera.main.transform.position, base.transform.position) < this.distanceThreshold)
  40. {
  41. this._idx = (int)(UnityEngine.Random.value * (float)this.nearSounds.Length);
  42. this._soundComponent.PlayOneShot(this.nearSounds[this._idx]);
  43. }
  44. else
  45. {
  46. this._idx = (int)(UnityEngine.Random.value * (float)this.farSounds.Length);
  47. this._soundComponent.PlayOneShot(this.farSounds[this._idx]);
  48. }
  49. this._delayedExplosionStarted = false;
  50. this._explodeDelay = 0f;
  51. }
  52. else
  53. {
  54. this._delayedExplosionStarted = true;
  55. }
  56. }
  57. public void Reset()
  58. {
  59. }
  60. public AudioClip[] nearSounds;
  61. public AudioClip[] farSounds;
  62. public float distanceThreshold = 50f;
  63. public float minVolume = 0.4f;
  64. public float maxVolume = 1f;
  65. public float rolloffFactor = 0.5f;
  66. private AudioSource _soundComponent;
  67. private bool _delayedExplosionStarted;
  68. private float _explodeDelay;
  69. private int _idx;
  70. }