Code Review

1 using UnityEngine;
2 using
System.Collections;
3
4 [RequireComponent (
typeof (Detonator))]
5 [AddComponentMenu(
"Detonator/Smoke")]
6 public
class DetonatorSmoke : DetonatorComponent
7 {
8     
private const float _baseSize = 1f;
9     
private const float _baseDuration = 8f;
10     
private Color _baseColor = new Color(.5f, .5f, .5f, .5f);
11     
private const float _baseDamping = 0.1300004f;
12     
13     
private float _scaledDuration;
14
15     
private GameObject _smokeA;
16     
private DetonatorBurstEmitter _smokeAEmitter;
17     
public Material smokeAMaterial;
18     
19     
private GameObject _smokeB;
20     
private DetonatorBurstEmitter _smokeBEmitter;
21     
public Material smokeBMaterial;
22         
23     
public bool drawSmokeA = true;
24     
public bool drawSmokeB = true;
25     
26     
override public void Init()
27     {
28         FillMaterials(
false);
29         BuildSmokeA();
30         BuildSmokeB();
31     }
32     
33     
public void FillMaterials(bool wipe)
34     {
35         
if (!smokeAMaterial || wipe)
36         {
37             smokeAMaterial = MyDetonator().smokeAMaterial;
38         }
39         
if (!smokeBMaterial || wipe)
40         {
41             smokeBMaterial = MyDetonator().smokeBMaterial;
42         }
43     }
44
45     
public void BuildSmokeA()
46     {
47         _smokeA =
new GameObject("SmokeA");
48         _smokeAEmitter = (DetonatorBurstEmitter)_smokeA.AddComponent<DetonatorBurstEmitter>();
49         _smokeA.transform.parent =
this.transform;
50         _smokeA.transform.localPosition = localPosition;
51         _smokeA.transform.localRotation = Quaternion.identity;
52         _smokeAEmitter.material = smokeAMaterial;
53         _smokeAEmitter.exponentialGrowth =
false;
54         _smokeAEmitter.sizeGrow =
0.095f;
55         _smokeAEmitter.useWorldSpace = MyDetonator().useWorldSpace;
56         _smokeAEmitter.upwardsBias = MyDetonator().upwardsBias;
57     }
58     
59     
public void UpdateSmokeA()
60     {
61         _smokeA.transform.localPosition = Vector3.Scale(localPosition,(
new Vector3(size, size, size)));
62         
63         
//move slightly away from the main camera so it sorts properly
64         _smokeA.transform.LookAt(Camera.main.transform);
65         _smokeA.transform.localPosition = -(Vector3.forward * -
1.5f);
66         
67         _smokeAEmitter.color = color;
68         _smokeAEmitter.duration = duration * .
5f;
69         _smokeAEmitter.durationVariation =
0f;
70         _smokeAEmitter.timeScale = timeScale;
71         _smokeAEmitter.count =
4f;
72         _smokeAEmitter.particleSize =
25f;
73         _smokeAEmitter.sizeVariation =
3f;
74         _smokeAEmitter.velocity = velocity;
75         _smokeAEmitter.startRadius =
10f;
76         _smokeAEmitter.size = size;
77         _smokeAEmitter.useExplicitColorAnimation =
true;
78         _smokeAEmitter.explodeDelayMin = explodeDelayMin;
79         _smokeAEmitter.explodeDelayMax = explodeDelayMax;
80
81         Color color1 =
new Color(.2f, .2f, .2f, .4f);
82         Color color2 =
new Color(.2f, .2f, .2f, .7f);
83         Color color3 =
new Color(.2f, .2f, .2f, .4f);
84         Color color4 =
new Color(.2f, .2f, .2f, 0f);
85         
86         _smokeAEmitter.colorAnimation[
0] = color1;
87         _smokeAEmitter.colorAnimation[
1] = color2;
88         _smokeAEmitter.colorAnimation[
2] = color2;
89         _smokeAEmitter.colorAnimation[
3] = color3;
90         _smokeAEmitter.colorAnimation[
4] = color4;
91     }
92     
93     
public void BuildSmokeB()
94     {
95         _smokeB =
new GameObject("SmokeB");
96         _smokeBEmitter = (DetonatorBurstEmitter)_smokeB.AddComponent<DetonatorBurstEmitter>();
97         _smokeB.transform.parent =
this.transform;
98         _smokeB.transform.localPosition = localPosition;
99         _smokeB.transform.localRotation = Quaternion.identity;
100         _smokeBEmitter.material = smokeBMaterial;
101         _smokeBEmitter.exponentialGrowth =
false;
102         _smokeBEmitter.sizeGrow =
0.095f;
103         _smokeBEmitter.useWorldSpace = MyDetonator().useWorldSpace;
104         _smokeBEmitter.upwardsBias = MyDetonator().upwardsBias;
105     }
106     
107     
public void UpdateSmokeB()
108     {
109         _smokeB.transform.localPosition = Vector3.Scale(localPosition,(
new Vector3(size, size, size)));
110         
111         
//move slightly away from the main camera so it sorts properly
112         _smokeB.transform.LookAt(Camera.main.transform);
113         _smokeB.transform.localPosition = -(Vector3.forward * -
1f);
114         
115         _smokeBEmitter.color = color;
116         _smokeBEmitter.duration = duration * .
5f;
117         _smokeBEmitter.durationVariation =
0f;
118         _smokeBEmitter.count =
2f;
119         _smokeBEmitter.particleSize =
25f;
120         _smokeBEmitter.sizeVariation =
3f;
121         _smokeBEmitter.velocity = velocity;
122         _smokeBEmitter.startRadius =
10f;
123         _smokeBEmitter.size = size;
124         _smokeBEmitter.useExplicitColorAnimation =
true;
125         _smokeBEmitter.explodeDelayMin = explodeDelayMin;
126         _smokeBEmitter.explodeDelayMax = explodeDelayMax;
127
128         Color color1 =
new Color(.2f, .2f, .2f, .4f);
129         Color color2 =
new Color(.2f, .2f, .2f, .7f);
130         Color color3 =
new Color(.2f, .2f, .2f, .4f);
131         Color color4 =
new Color(.2f, .2f, .2f, 0f);
132         
133         _smokeBEmitter.colorAnimation[
0] = color1;
134         _smokeBEmitter.colorAnimation[
1] = color2;
135         _smokeBEmitter.colorAnimation[
2] = color2;
136         _smokeBEmitter.colorAnimation[
3] = color3;
137         _smokeBEmitter.colorAnimation[
4] = color4;
138     }
139
140     
public void Reset()
141     {
142         FillMaterials(
true);
143         
on = true;
144         size = _baseSize;
145         duration = _baseDuration;
146         explodeDelayMin =
0f;
147         explodeDelayMax =
0f;
148         color = _baseColor;
149         velocity =
new Vector3(3f,3f,3f);
150     }
151
152     
override public void Explode()
153     {
154         
if (detailThreshold > detail) return;
155         
156         
if (on)
157         {
158             UpdateSmokeA();
159             UpdateSmokeB();
160             
if (drawSmokeA) _smokeAEmitter.Explode();
161             
if (drawSmokeB) _smokeBEmitter.Explode();
162         }
163     }
164
165 }


Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *