1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
using UILib;
using UnityEngine;
using System.Collections.Generic;
using XUtliPoolLib;
public class XSthCollector : XUIObject, IXUISthCollector
{
public List<GameObject> SthList = new List<GameObject>();
public Vector3 Src = new Vector3(0, 0, 0);
public Vector3 Des = new Vector3(-450, -280, 0);
public int Count = 15;
public float EmitInterval = 0.01f;
public int EmitDegreeRange = 300;
public float MinEmitSpeed = 1900;
public float MaxEmitSpeed = 2100;
public float SrcAcceleration = 20000;
public float DesAcceleration = 8000;
public float SthAcceleration = 10000;
public float MinStartFindDesTime = 0.3f;
public float MaxStartFindDesTime = 0.6f;
public float MinIdleSpeed = 30;
List<XSth> m_SthList = null;
bool m_bActive;
//string m_CurName;
GameObject m_CurSthGo;
//Dictionary<string, List<XSth>> m_SthListMap = new Dictionary<string, List<XSth>>();
Dictionary<string, GameObject> m_SthTplMap = new Dictionary<string, GameObject>();
List<XSth> m_ExternalSthList = new List<XSth>();
Vector3 m_Direction;
SthArrivedEventHandler m_SthArrivedEventHandler;
CollectFinishEventHandler m_CollectFinishEventHandler;
int m_ArrivedCount;
void Awake()
{
for (int i = 0; i < SthList.Count; ++i)
{
m_SthTplMap.Add(SthList[i].name, SthList[i]);
SthList[i].SetActive(false);
}
m_Direction = (Des - Src).normalized;
}
void OnEnable()
{
m_bActive = false;
if (m_SthList != null)
{
for (int i = 0; i < m_SthList.Count; ++i)
{
m_SthList[i].bEnable = false;
}
}
}
public void SetPosition(Vector3 srcGlobalPos, Vector3 desGlobalPos)
{
Src = transform.worldToLocalMatrix * srcGlobalPos;
Des = transform.worldToLocalMatrix * desGlobalPos;
m_Direction = (Des - Src).normalized;
}
public void SetSth(List<GameObject> goes)
{
for (int i = m_ExternalSthList.Count; i < goes.Count; ++i)
{
XSth sth = new XSth();
m_ExternalSthList.Add(sth);
}
for (int i = m_ExternalSthList.Count - 1; i >= goes.Count; --i)
{
m_ExternalSthList.RemoveAt(i);
}
m_SthList = m_ExternalSthList;
for (int i = 0; i < m_ExternalSthList.Count; ++i)
{
m_SthList[i].Go = goes[i];
goes[i].SetActive(false);
m_SthList[i].bEnable = false;
}
}
public void SetSth(string name)
{
//if (m_SthList != null && m_CurName != name)
//{
// for (int i = 0; i < m_SthList.Count; ++i)
// {
// m_SthList[i].bEnable = false;
// }
//}
//if (!m_SthTplMap.TryGetValue(name, out m_CurSthGo))
//{
// Debug.LogError("Cant find tpl name: " + name);
// return;
//}
//if (!m_SthListMap.TryGetValue(name, out m_SthList))
//{
// m_SthList = new List<XSth>();
// m_SthListMap.Add(name, m_SthList);
//}
//_GenerateSth();
}
private void _GenerateSth()
{
if (m_SthList.Count != Count)
{
m_CurSthGo.SetActive(true);
for (int i = m_SthList.Count; i < Count; ++i)
{
XSth sth = new XSth();
GameObject go = UnityEngine.Object.Instantiate(m_CurSthGo) as GameObject;
go.transform.parent = transform;
go.transform.localScale = Vector3.one;
sth.Go = go;
go.SetActive(false);
m_SthList.Add(sth);
}
m_CurSthGo.SetActive(false);
for (int i = m_SthList.Count - 1; i >= Count; --i)
{
m_SthList[i].Destroy();
m_SthList.RemoveAt(i);
}
}
for (int i = 0; i < m_SthList.Count; ++i)
{
m_SthList[i].bEnable = false;
}
}
[ContextMenu("Emit")]
public void Emit()
{
float fDelayTime = 0.0f;
for (int i = 0; i < m_SthList.Count; ++i, fDelayTime += EmitInterval)
{
XSth sth = m_SthList[i];
sth.DelayTime = fDelayTime;
sth.Speed = XCommon.singleton.RandomFloat(MinEmitSpeed, MaxEmitSpeed) * _RandEmitDir();
sth.StartFindDesTime = XCommon.singleton.RandomFloat(MinStartFindDesTime, MaxStartFindDesTime);
sth.Time = 0.0f;
sth.MinIdleSpeed = MinIdleSpeed;
sth.Go.transform.localPosition = Src;// transform.worldToLocalMatrix* Src;
sth.Des = Des;// transform.worldToLocalMatrix* Des;
sth.bEnable = true;
}
m_bActive = true;
m_ArrivedCount = 0;
}
Vector3 _RandEmitDir()
{
float radRange = EmitDegreeRange * Mathf.Deg2Rad;
float rad = XCommon.singleton.RandomFloat(radRange) - radRange / 2;
float sinA = Mathf.Sin(rad);
float cosA = Mathf.Cos(rad);
Vector3 vec = m_Direction;
vec.x = m_Direction.x * cosA - m_Direction.y * sinA;
vec.y = m_Direction.x * sinA + m_Direction.y * cosA;
return vec;
}
Vector3 _GetAcceleration(XSth sth, float t)
{
Vector3 curPos = sth.Go.transform.localPosition;
Vector3 acc = Vector3.zero;
Vector3 gDes = (Des - curPos).normalized;
if (sth.State == XSth.SthState.FLAME_OUT)
{
acc += gDes * DesAcceleration;
}
Vector3 gSrc = (Src - curPos).normalized;
if (sth.State == XSth.SthState.IDLE)
{
acc += gSrc * SrcAcceleration;
}
if (sth.State == XSth.SthState.DIRECTION_ADJUSTING)
{
float desCross = gDes.x * sth.Speed.y - gDes.y * sth.Speed.x;
Vector3 perpendicular = new Vector3(-sth.Speed.y, sth.Speed.x, sth.Speed.z);
if ((perpendicular.x * sth.Speed.y - perpendicular.y * sth.Speed.x) * desCross < 0)
{
perpendicular.x = -perpendicular.x;
perpendicular.y = -perpendicular.y;
}
perpendicular = perpendicular.normalized * SthAcceleration;
acc += perpendicular;
}
return acc;
}
void Update()
{
if (!m_bActive)
return;
m_bActive = false;
for (int i = 0; i < m_SthList.Count; ++i)
{
XSth sth = m_SthList[i];
if (!sth.bEnable)
continue;
m_bActive = true;
if (!sth.Update(Time.deltaTime))
{
if (m_SthArrivedEventHandler != null)
m_SthArrivedEventHandler(m_ArrivedCount++);
}
if (sth.DelayTime <= 0.0f)
sth.Acceleration = _GetAcceleration(sth, Time.deltaTime);
}
if (!m_bActive && m_CollectFinishEventHandler != null)
m_CollectFinishEventHandler();
}
public void RegisterSthArrivedEventHandler(SthArrivedEventHandler eventHandler)
{
m_SthArrivedEventHandler = eventHandler;
}
public void RegisterCollectFinishEventHandler(CollectFinishEventHandler eventHandler)
{
m_CollectFinishEventHandler = eventHandler;
}
}
public class XSth
{
public enum SthState
{
IDLE,
DIRECTION_ADJUSTING,
FLAME_OUT
}
public GameObject Go;
public Vector3 Speed;
public float MinIdleSpeed;
public Vector3 Acceleration;
public float Time;
public float DelayTime;
public float StartFindDesTime;
public Vector3 Des;
public SthState State;
private bool m_bEnable = false;
public bool bEnable
{
get { return m_bEnable; }
set
{
m_bEnable = value;
Acceleration = Vector3.zero;
State = SthState.IDLE;
if (!m_bEnable)
{
Go.SetActive(false);
}
}
}
public void Destroy()
{
UnityEngine.Object.Destroy(Go);
}
public bool Update(float t)
{
if (!bEnable)
return bEnable;
if (DelayTime > 0.0f)
{
DelayTime -= t;
if (DelayTime > 0.0f)
return true;
t = -DelayTime;
}
Time += t;
Vector3 newSpeed = Speed + (Acceleration * t);
Vector3 pos = Go.transform.localPosition;
if (State == SthState.IDLE)
{
if (newSpeed.x * Speed.x + newSpeed.y * Speed.y <= 0)
{
// 速度减到回落了,避免这种情况,让它以极低速度运行
newSpeed = Speed.normalized * MinIdleSpeed;
}
if (Time > StartFindDesTime)
{
State = SthState.DIRECTION_ADJUSTING;
}
}
if (State == SthState.DIRECTION_ADJUSTING)
{
Vector3 desDir = Des - pos;
float crossProduct0 = Speed.x * desDir.y - Speed.y * desDir.x;
float crossProduct1 = desDir.x * newSpeed.y - desDir.y * newSpeed.x;
if (crossProduct0 * crossProduct1 > 0)
{
newSpeed = newSpeed.magnitude * desDir.normalized;
State = SthState.FLAME_OUT;
}
}
if (!Go.activeSelf)
Go.SetActive(true);
Speed = newSpeed;
Vector3 newPos = pos + Speed * t;
if ((newPos - pos).sqrMagnitude >= (Des - pos).sqrMagnitude)
bEnable = false;
else
Go.transform.localPosition = newPos;
return bEnable;
}
}
|