Code Review

1 using UnityEngine;
2 using
System.Collections;
3
4 [RequireComponent (
typeof (Detonator))]
5 [AddComponentMenu(
"Detonator/Fireball")]
6 public
class DetonatorFireball : DetonatorComponent
7 {
8     
private float _baseSize = 1f;
9     
private float _baseDuration = 3f;
10     
private Color _baseColor = new Color(1f, .423f, 0f, .5f);
11     
private Vector3 _baseVelocity = new Vector3(60f, 60f, 60f);
12 //
private float _baseDamping = 0.1300004f;
13     
private float _scaledDuration;
14
15     
private GameObject _fireballA;
16     
private DetonatorBurstEmitter _fireballAEmitter;
17     
public Material fireballAMaterial;
18     
19     
private GameObject _fireballB;
20     
private DetonatorBurstEmitter _fireballBEmitter;
21     
public Material fireballBMaterial;
22     
23     
private GameObject _fireShadow;
24     
private DetonatorBurstEmitter _fireShadowEmitter;
25     
public Material fireShadowMaterial;
26     
27     
public bool drawFireballA = true;
28     
public bool drawFireballB = true;
29     
public bool drawFireShadow = true;
30     
31     
override public void Init()
32     {
33         
//make sure there are materials at all
34         FillMaterials(
false);
35         BuildFireballA();
36         BuildFireballB();
37         BuildFireShadow();
38     }
39     
40     
//if materials are empty fill them with defaults
41     
public void FillMaterials(bool wipe)
42     {
43         
if (!fireballAMaterial || wipe)
44         {
45             fireballAMaterial = MyDetonator().fireballAMaterial;
46         }
47         
if (!fireballBMaterial || wipe)
48         {
49             fireballBMaterial = MyDetonator().fireballBMaterial;
50         }
51         
if (!fireShadowMaterial || wipe)
52         {
53             
if (Random.value > 0.5)
54             {
55                 fireShadowMaterial = MyDetonator().smokeAMaterial;
56             }
57             
else
58             {
59                 fireShadowMaterial = MyDetonator().smokeBMaterial;
60             }
61         }
62     }
63     
64     
private Color _detailAdjustedColor;
65     
66     
//Build these to look correct at the stock Detonator size of 10m... then let the size parameter
67     
//cascade through to the emitters and let them do the scaling work... keep these absolute.
68     
public void BuildFireballA()
69     {
70         _fireballA =
new GameObject("FireballA");
71         _fireballAEmitter = (DetonatorBurstEmitter)_fireballA.AddComponent<DetonatorBurstEmitter>();
72         _fireballA.transform.parent =
this.transform;
73         _fireballA.transform.localRotation = Quaternion.identity;
74         _fireballAEmitter.material = fireballAMaterial;
75         _fireballAEmitter.useWorldSpace = MyDetonator().useWorldSpace;
76         _fireballAEmitter.upwardsBias = MyDetonator().upwardsBias;
77     }
78     
79     
public void UpdateFireballA()
80     {
81         _fireballA.transform.localPosition = Vector3.Scale(localPosition,(
new Vector3(size, size, size)));
82         _fireballAEmitter.color = color;
83         _fireballAEmitter.duration = duration * .
5f;
84         _fireballAEmitter.durationVariation = duration * .
5f;
85         _fireballAEmitter.count =
2f;
86         _fireballAEmitter.timeScale = timeScale;
87         _fireballAEmitter.detail = detail;
88         _fireballAEmitter.particleSize =
14f;
89         _fireballAEmitter.sizeVariation =
3f;
90         _fireballAEmitter.velocity = velocity;
91         _fireballAEmitter.startRadius =
4f;
92         _fireballAEmitter.size = size;
93         _fireballAEmitter.useExplicitColorAnimation =
true;
94
95         
//make the starting colors more intense, towards white
96         Color fadeWhite =
new Color(1f, 1f, 1f, .5f);
97         Color fadeRed =
new Color(.6f, .15f, .15f, .3f);
98         Color fadeBlue =
new Color(.1f, .2f, .45f, 0f);
99         
100         _fireballAEmitter.colorAnimation[
0] = Color.Lerp(color, fadeWhite, .8f);
101         _fireballAEmitter.colorAnimation[
1] = Color.Lerp(color, fadeWhite, .5f);
102         _fireballAEmitter.colorAnimation[
2] = color;
103         _fireballAEmitter.colorAnimation[
3] = Color.Lerp(color, fadeRed, .7f);
104         _fireballAEmitter.colorAnimation[
4] = fadeBlue;
105         
106         _fireballAEmitter.explodeDelayMin = explodeDelayMin;
107         _fireballAEmitter.explodeDelayMax = explodeDelayMax;
108     }
109     
110     
public void BuildFireballB()
111     {
112         _fireballB =
new GameObject("FireballB");
113         _fireballBEmitter = (DetonatorBurstEmitter)_fireballB.AddComponent<DetonatorBurstEmitter>();
114         _fireballB.transform.parent =
this.transform;
115         _fireballB.transform.localRotation = Quaternion.identity;
116         _fireballBEmitter.material = fireballBMaterial;
117         _fireballBEmitter.useWorldSpace = MyDetonator().useWorldSpace;
118         _fireballBEmitter.upwardsBias = MyDetonator().upwardsBias;
119     }
120     
121     
public void UpdateFireballB()
122 {
123         _fireballB.transform.localPosition = Vector3.Scale(localPosition,(
new Vector3(size, size, size)));
124         _fireballBEmitter.color = color;
125         _fireballBEmitter.duration = duration * .
5f;
126         _fireballBEmitter.durationVariation = duration * .
5f;
127         _fireballBEmitter.count =
2f;
128         _fireballBEmitter.timeScale = timeScale;
129         _fireballBEmitter.detail = detail;
130         _fireballBEmitter.particleSize =
10f;
131         _fireballBEmitter.sizeVariation =
6f;
132         _fireballBEmitter.velocity = velocity;
133         _fireballBEmitter.startRadius =
4f;
134         _fireballBEmitter.size = size;
135         _fireballBEmitter.useExplicitColorAnimation =
true;
136
137         
//make the starting colors more intense, towards white
138         Color fadeWhite =
new Color(1f, 1f, 1f, .5f);
139         Color fadeRed =
new Color(.6f, .15f, .15f, .3f);
140         Color fadeBlue =
new Color(.1f, .2f, .45f, 0f);
141         
142         _fireballBEmitter.colorAnimation[
0] = Color.Lerp(color, fadeWhite, .8f);
143         _fireballBEmitter.colorAnimation[
1] = Color.Lerp(color, fadeWhite, .5f);
144         _fireballBEmitter.colorAnimation[
2] = color;
145         _fireballBEmitter.colorAnimation[
3] = Color.Lerp(color, fadeRed, .7f);
146         _fireballBEmitter.colorAnimation[
4] = fadeBlue;
147         
148         _fireballBEmitter.explodeDelayMin = explodeDelayMin;
149         _fireballBEmitter.explodeDelayMax = explodeDelayMax;
150     }
151     
152     
public void BuildFireShadow()
153     {
154         _fireShadow =
new GameObject("FireShadow");
155         _fireShadowEmitter = (DetonatorBurstEmitter)_fireShadow.AddComponent<DetonatorBurstEmitter>();
156         _fireShadow.transform.parent =
this.transform;
157         _fireShadow.transform.localRotation = Quaternion.identity;
158         _fireShadowEmitter.material = fireShadowMaterial;
159         _fireShadowEmitter.useWorldSpace = MyDetonator().useWorldSpace;
160         _fireShadowEmitter.upwardsBias = MyDetonator().upwardsBias;
161     }
162     
163     
public void UpdateFireShadow()
164     {
165         _fireShadow.transform.localPosition = Vector3.Scale(localPosition,(
new Vector3(size, size, size)));
166         
167             
//move slightly towards the main camera so it sorts properly
168         _fireShadow.transform.LookAt(Camera.main.transform);
169         _fireShadow.transform.localPosition = -(Vector3.forward *
1f);
170         
171         _fireShadowEmitter.color =
new Color(.1f, .1f, .1f, .6f);
172         _fireShadowEmitter.duration = duration * .
5f;
173         _fireShadowEmitter.durationVariation = duration * .
5f;
174         _fireShadowEmitter.timeScale = timeScale;
175         _fireShadowEmitter.detail =
1; //don't scale up count
176         _fireShadowEmitter.particleSize =
13f;
177         _fireShadowEmitter.velocity = velocity;
178         _fireShadowEmitter.sizeVariation =
1f;
179         _fireShadowEmitter.count =
4;
180         _fireShadowEmitter.startRadius =
6f;
181         _fireShadowEmitter.size = size;
182         _fireShadowEmitter.explodeDelayMin = explodeDelayMin;
183         _fireShadowEmitter.explodeDelayMax = explodeDelayMax;
184     }
185
186     
public void Reset()
187     {
188         FillMaterials(
true);
189         
on = true;
190         size = _baseSize;
191         duration = _baseDuration;
192         explodeDelayMin =
0f;
193         explodeDelayMax =
0f;
194         color = _baseColor;
195         velocity = _baseVelocity;
196     }
197
198     
override public void Explode()
199     {
200         
if (detailThreshold > detail) return;
201         
202         
if (on)
203         {
204             UpdateFireballA();
205             UpdateFireballB();
206             UpdateFireShadow();
207             
if (drawFireballA) _fireballAEmitter.Explode();
208             
if (drawFireballB) _fireballBEmitter.Explode();
209             
if (drawFireShadow) _fireShadowEmitter.Explode();
210         }
211     }
212
213 }


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 *