DetonatorSpray.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System;
  2. using UnityEngine;
  3. [RequireComponent(typeof(Detonator))]
  4. [AddComponentMenu("Detonator/Object Spray")]
  5. public class DetonatorSpray : 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._delayedExplosionStarted)
  24. {
  25. this._explodeDelay = this.explodeDelayMin + UnityEngine.Random.value * (this.explodeDelayMax - this.explodeDelayMin);
  26. }
  27. if (this._explodeDelay <= 0f)
  28. {
  29. int num = (int)(this.detail * (float)this.count);
  30. for (int i = 0; i < num; i++)
  31. {
  32. Vector3 b = UnityEngine.Random.onUnitSphere * (this.startingRadius * this.size);
  33. Vector3 b2 = new Vector3(this.velocity.x * this.size, this.velocity.y * this.size, this.velocity.z * this.size);
  34. GameObject gameObject = UnityEngine.Object.Instantiate<GameObject>(this.sprayObject, base.transform.position + b, base.transform.rotation);
  35. gameObject.transform.parent = base.transform;
  36. this._tmpScale = this.minScale + UnityEngine.Random.value * (this.maxScale - this.minScale);
  37. this._tmpScale *= this.size;
  38. gameObject.transform.localScale = new Vector3(this._tmpScale, this._tmpScale, this._tmpScale);
  39. if (base.MyDetonator().upwardsBias > 0f)
  40. {
  41. b2 = new Vector3(b2.x / Mathf.Log(base.MyDetonator().upwardsBias), b2.y * Mathf.Log(base.MyDetonator().upwardsBias), b2.z / Mathf.Log(base.MyDetonator().upwardsBias));
  42. }
  43. gameObject.GetComponent<Rigidbody>().velocity = Vector3.Scale(b.normalized, b2);
  44. gameObject.GetComponent<Rigidbody>().velocity = Vector3.Scale(b.normalized, b2);
  45. UnityEngine.Object.Destroy(gameObject, this.duration * this.timeScale);
  46. this._delayedExplosionStarted = false;
  47. this._explodeDelay = 0f;
  48. }
  49. }
  50. else
  51. {
  52. this._delayedExplosionStarted = true;
  53. }
  54. }
  55. public void Reset()
  56. {
  57. this.velocity = new Vector3(15f, 15f, 15f);
  58. }
  59. public GameObject sprayObject;
  60. public int count = 10;
  61. public float startingRadius;
  62. public float minScale = 1f;
  63. public float maxScale = 1f;
  64. private bool _delayedExplosionStarted;
  65. private float _explodeDelay;
  66. private Vector3 _explosionPosition;
  67. private float _tmpScale;
  68. }