1 using UnityEngine;
2 using
System.Collections;
3
4 [RequireComponent (
typeof (Detonator))]
5 [AddComponentMenu(
"Detonator/Sparks")]
6 public
class DetonatorSparks : DetonatorComponent
7 {
8     
private float _baseSize = 1f;
9     
private float _baseDuration = 4f;
10     
private Vector3 _baseVelocity = new Vector3(400f, 400f, 400f);
11     
private Color _baseColor = Color.white;
12 //
private float _baseDamping = 0.185f;
13     
private Vector3 _baseForce = Physics.gravity;
14     
private float _scaledDuration;
15     
16     
private GameObject _sparks;
17     
private DetonatorBurstEmitter _sparksEmitter;
18     
public Material sparksMaterial;
19         
20     
override public void Init()
21     {
22         
//make sure there are materials at all
23         FillMaterials(
false);
24         BuildSparks();
25     }
26     
27     
//if materials are empty fill them with defaults
28     
public void FillMaterials(bool wipe)
29     {
30         
if (!sparksMaterial || wipe)
31         {
32             sparksMaterial = MyDetonator().sparksMaterial;
33         }
34     }
35
36     
//Build these to look correct at the stock Detonator size of 10m... then let the size parameter
37     
//cascade through to the emitters and let them do the scaling work... keep these absolute.
38     
public void BuildSparks()
39     {
40         _sparks =
new GameObject("Sparks");
41         _sparksEmitter = (DetonatorBurstEmitter)_sparks.AddComponent<DetonatorBurstEmitter>();
42         _sparks.transform.parent =
this.transform;
43         _sparks.transform.localPosition = localPosition;
44         _sparks.transform.localRotation = Quaternion.identity;
45         _sparksEmitter.material = sparksMaterial;
46         _sparksEmitter.force = Physics.gravity /
3; //don't fall fast - these are sparks
47         _sparksEmitter.useExplicitColorAnimation =
false;
48         _sparksEmitter.useWorldSpace = MyDetonator().useWorldSpace;
49         _sparksEmitter.upwardsBias = MyDetonator().upwardsBias;
50     }
51     
52     
public void UpdateSparks()
53     {
54         _scaledDuration = (duration * timeScale);
55         _sparksEmitter.color = color;
56         _sparksEmitter.duration = _scaledDuration/
4;
57         _sparksEmitter.durationVariation = _scaledDuration;
58         _sparksEmitter.count = (
int)(detail * 50f);
59         _sparksEmitter.particleSize = .
5f;
60         _sparksEmitter.sizeVariation = .
25f;
61         
62         
//get wider as upwardsBias goes up - counterintuitive, but right in this case?
63         
if (_sparksEmitter.upwardsBias > 0f)
64         {
65             _sparksEmitter.velocity =
new Vector3(
66             (velocity.x / Mathf.Log(_sparksEmitter.upwardsBias)),
67             (velocity.y * Mathf.Log(_sparksEmitter.upwardsBias)),
68             (velocity.z / Mathf.Log(_sparksEmitter.upwardsBias)));
69         }
70         
else
71         {
72             _sparksEmitter.velocity =
this.velocity;
73         }
74         
75         _sparksEmitter.startRadius =
0f;
76         _sparksEmitter.size = size;
77         _sparksEmitter.explodeDelayMin = explodeDelayMin;
78         _sparksEmitter.explodeDelayMax = explodeDelayMax;
79     }
80
81     
public void Reset()
82     {
83         FillMaterials(
true);
84         
on = true;
85         size = _baseSize;
86         duration = _baseDuration;
87         explodeDelayMin =
0f;
88         explodeDelayMax =
0f;
89         color = _baseColor;
90         velocity = _baseVelocity;
91         force = _baseForce;
92     }
93
94     
override public void Explode()
95     {
96         
if (on)
97         {
98             UpdateSparks();
99             _sparksEmitter.Explode();
100         }
101     }
102
103 }