DetonatorSparks.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using UnityEngine;
  3. [RequireComponent(typeof(Detonator))]
  4. [AddComponentMenu("Detonator/Sparks")]
  5. public class DetonatorSparks : DetonatorComponent
  6. {
  7. public override void Init()
  8. {
  9. this.FillMaterials(false);
  10. this.BuildSparks();
  11. }
  12. public void FillMaterials(bool wipe)
  13. {
  14. if (!this.sparksMaterial || wipe)
  15. {
  16. this.sparksMaterial = base.MyDetonator().sparksMaterial;
  17. }
  18. }
  19. public void BuildSparks()
  20. {
  21. this._sparks = new GameObject("Sparks");
  22. this._sparksEmitter = this._sparks.AddComponent<DetonatorBurstEmitter>();
  23. this._sparks.transform.parent = base.transform;
  24. this._sparks.transform.localPosition = this.localPosition;
  25. this._sparks.transform.localRotation = Quaternion.identity;
  26. this._sparksEmitter.material = this.sparksMaterial;
  27. this._sparksEmitter.force = Physics.gravity / 3f;
  28. this._sparksEmitter.useExplicitColorAnimation = false;
  29. this._sparksEmitter.useWorldSpace = base.MyDetonator().useWorldSpace;
  30. this._sparksEmitter.upwardsBias = base.MyDetonator().upwardsBias;
  31. }
  32. public void UpdateSparks()
  33. {
  34. this._scaledDuration = this.duration * this.timeScale;
  35. this._sparksEmitter.color = this.color;
  36. this._sparksEmitter.duration = this._scaledDuration / 4f;
  37. this._sparksEmitter.durationVariation = this._scaledDuration;
  38. this._sparksEmitter.count = (float)((int)(this.detail * 50f));
  39. this._sparksEmitter.particleSize = 0.5f;
  40. this._sparksEmitter.sizeVariation = 0.25f;
  41. if (this._sparksEmitter.upwardsBias > 0f)
  42. {
  43. this._sparksEmitter.velocity = new Vector3(this.velocity.x / Mathf.Log(this._sparksEmitter.upwardsBias), this.velocity.y * Mathf.Log(this._sparksEmitter.upwardsBias), this.velocity.z / Mathf.Log(this._sparksEmitter.upwardsBias));
  44. }
  45. else
  46. {
  47. this._sparksEmitter.velocity = this.velocity;
  48. }
  49. this._sparksEmitter.startRadius = 0f;
  50. this._sparksEmitter.size = this.size;
  51. this._sparksEmitter.explodeDelayMin = this.explodeDelayMin;
  52. this._sparksEmitter.explodeDelayMax = this.explodeDelayMax;
  53. }
  54. public void Reset()
  55. {
  56. this.FillMaterials(true);
  57. this.on = true;
  58. this.size = this._baseSize;
  59. this.duration = this._baseDuration;
  60. this.explodeDelayMin = 0f;
  61. this.explodeDelayMax = 0f;
  62. this.color = this._baseColor;
  63. this.velocity = this._baseVelocity;
  64. this.force = this._baseForce;
  65. }
  66. public override void Explode()
  67. {
  68. if (this.on)
  69. {
  70. this.UpdateSparks();
  71. this._sparksEmitter.Explode();
  72. }
  73. }
  74. private float _baseSize = 1f;
  75. private float _baseDuration = 4f;
  76. private Vector3 _baseVelocity = new Vector3(400f, 400f, 400f);
  77. private Color _baseColor = Color.white;
  78. private Vector3 _baseForce = Physics.gravity;
  79. private float _scaledDuration;
  80. private GameObject _sparks;
  81. private DetonatorBurstEmitter _sparksEmitter;
  82. public Material sparksMaterial;
  83. }