DetonatorForce.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using UnityEngine;
  3. [RequireComponent(typeof(Detonator))]
  4. [AddComponentMenu("Detonator/Force")]
  5. public class DetonatorForce : DetonatorComponent
  6. {
  7. public override void Init()
  8. {
  9. }
  10. private void Update()
  11. {
  12. if (this._delayedExplosionStarted)
  13. {
  14. this._explodeDelay -= Time.deltaTime;
  15. if (this._explodeDelay <= 0f)
  16. {
  17. this.Explode();
  18. }
  19. }
  20. }
  21. public override void Explode()
  22. {
  23. if (!this.on)
  24. {
  25. return;
  26. }
  27. if (this.detailThreshold > this.detail)
  28. {
  29. return;
  30. }
  31. if (!this._delayedExplosionStarted)
  32. {
  33. this._explodeDelay = this.explodeDelayMin + UnityEngine.Random.value * (this.explodeDelayMax - this.explodeDelayMin);
  34. }
  35. if (this._explodeDelay <= 0f)
  36. {
  37. this._explosionPosition = base.transform.position;
  38. this._colliders = Physics.OverlapSphere(this._explosionPosition, this.radius);
  39. foreach (Collider collider in this._colliders)
  40. {
  41. if (collider)
  42. {
  43. if (collider.GetComponent<Rigidbody>())
  44. {
  45. collider.GetComponent<Rigidbody>().AddExplosionForce(this.power * this.size, this._explosionPosition, this.radius * this.size, 4f * base.MyDetonator().upwardsBias * this.size);
  46. collider.gameObject.SendMessage("OnDetonatorForceHit", null, SendMessageOptions.DontRequireReceiver);
  47. if (this.fireObject)
  48. {
  49. if (collider.transform.Find(this.fireObject.name + "(Clone)"))
  50. {
  51. return;
  52. }
  53. this._tempFireObject = UnityEngine.Object.Instantiate<GameObject>(this.fireObject, base.transform.position, base.transform.rotation);
  54. this._tempFireObject.transform.parent = collider.transform;
  55. this._tempFireObject.transform.localPosition = new Vector3(0f, 0f, 0f);
  56. if (this._tempFireObject.GetComponent<ParticleEmitter>())
  57. {
  58. this._tempFireObject.GetComponent<ParticleEmitter>().emit = true;
  59. UnityEngine.Object.Destroy(this._tempFireObject, this.fireObjectLife);
  60. }
  61. }
  62. }
  63. }
  64. }
  65. this._delayedExplosionStarted = false;
  66. this._explodeDelay = 0f;
  67. }
  68. else
  69. {
  70. this._delayedExplosionStarted = true;
  71. }
  72. }
  73. public void Reset()
  74. {
  75. this.radius = this._baseRadius;
  76. this.power = this._basePower;
  77. }
  78. private float _baseRadius = 50f;
  79. private float _basePower = 4000f;
  80. private float _scaledRange;
  81. private float _scaledIntensity;
  82. private bool _delayedExplosionStarted;
  83. private float _explodeDelay;
  84. public float radius;
  85. public float power;
  86. public GameObject fireObject;
  87. public float fireObjectLife;
  88. private Collider[] _colliders;
  89. private GameObject _tempFireObject;
  90. private Vector3 _explosionPosition;
  91. }