EffectNode.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace Xft
  5. {
  6. public class EffectNode : IComparable<EffectNode>
  7. {
  8. public EffectNode(int index, Transform clienttrans, bool sync, EffectLayer owner)
  9. {
  10. this.Index = index;
  11. this.ClientTrans = clienttrans;
  12. this.SyncClient = sync;
  13. this.Owner = owner;
  14. this.LowerLeftUV = Vector2.zero;
  15. this.UVDimensions = Vector2.one;
  16. this.Scale = Vector2.one;
  17. this.RotateAngle = 0f;
  18. this.Color = Color.white;
  19. }
  20. public Camera MyCamera
  21. {
  22. get
  23. {
  24. if (this.Owner == null)
  25. {
  26. UnityEngine.Debug.LogError("something wrong with camera init!");
  27. return null;
  28. }
  29. return this.Owner.MyCamera;
  30. }
  31. }
  32. public int CompareTo(EffectNode other)
  33. {
  34. return this.TotalIndex.CompareTo(other.TotalIndex);
  35. }
  36. public void SetAffectorList(List<Affector> afts)
  37. {
  38. this.AffectorList = afts;
  39. }
  40. public List<Affector> GetAffectorList()
  41. {
  42. return this.AffectorList;
  43. }
  44. public void Init(Vector3 oriDir, float speed, float life, int oriRot, float oriScaleX, float oriScaleY, Color oriColor, Vector2 oriLowerUv, Vector2 oriUVDimension)
  45. {
  46. this.OriDirection = oriDir;
  47. this.LifeTime = life;
  48. this.OriRotateAngle = oriRot;
  49. this.OriScaleX = oriScaleX;
  50. this.OriScaleY = oriScaleY;
  51. this.StartColor = oriColor;
  52. this.Color = oriColor;
  53. this.ElapsedTime = 0f;
  54. if (this.Owner.DirType != DIRECTION_TYPE.Sphere)
  55. {
  56. this.Velocity = this.Owner.ClientTransform.rotation * this.OriDirection * speed;
  57. }
  58. else
  59. {
  60. this.Velocity = this.OriDirection * speed;
  61. }
  62. this.LowerLeftUV = oriLowerUv;
  63. this.UVDimensions = oriUVDimension;
  64. this.IsCollisionEventSended = false;
  65. this.RenderObj.Initialize(this);
  66. }
  67. public float GetElapsedTime()
  68. {
  69. return this.ElapsedTime;
  70. }
  71. public float GetLifeTime()
  72. {
  73. return this.LifeTime;
  74. }
  75. public void SetLocalPosition(Vector3 pos)
  76. {
  77. if (this.Type == 1)
  78. {
  79. RibbonTrail ribbonTrail = this.RenderObj as RibbonTrail;
  80. if (!this.SyncClient)
  81. {
  82. ribbonTrail.OriHeadPos = pos;
  83. }
  84. else
  85. {
  86. ribbonTrail.OriHeadPos = this.GetRealClientPos() + pos;
  87. }
  88. }
  89. this.Position = pos;
  90. }
  91. public Vector3 GetLocalPosition()
  92. {
  93. return this.Position;
  94. }
  95. public Vector3 GetRealClientPos()
  96. {
  97. Vector3 vector = Vector3.one * this.Owner.Owner.Scale;
  98. Vector3 zero = Vector3.zero;
  99. zero.x = this.ClientTrans.position.x / vector.x;
  100. zero.y = this.ClientTrans.position.y / vector.y;
  101. zero.z = this.ClientTrans.position.z / vector.z;
  102. return zero;
  103. }
  104. public Vector3 GetOriginalPos()
  105. {
  106. Vector3 realClientPos = this.GetRealClientPos();
  107. Vector3 result;
  108. if (!this.SyncClient)
  109. {
  110. result = this.Position - realClientPos + this.ClientTrans.position;
  111. }
  112. else
  113. {
  114. result = this.Position + this.ClientTrans.position;
  115. }
  116. return result;
  117. }
  118. public Vector3 GetWorldPos()
  119. {
  120. return this.CurWorldPos;
  121. }
  122. protected bool IsSimpleSprite()
  123. {
  124. bool result = false;
  125. if (this.Owner.SpriteType == 2 && this.Owner.OriVelocityAxis == Vector3.zero && !this.Owner.ScaleAffectorEnable && !this.Owner.RotAffectorEnable && (double)this.Owner.OriSpeed < 0.0001 && !this.Owner.GravityAffectorEnable && !this.Owner.AirAffectorEnable && !this.Owner.TurbulenceAffectorEnable && !this.Owner.BombAffectorEnable && !this.Owner.UVRotAffectorEnable && !this.Owner.UVScaleAffectorEnable && (double)Mathf.Abs(this.Owner.OriRotationMax - this.Owner.OriRotationMin) < 0.0001 && (double)Mathf.Abs(this.Owner.OriScaleXMin - this.Owner.OriScaleXMax) < 0.0001 && (double)Mathf.Abs(this.Owner.OriScaleYMin - this.Owner.OriScaleYMax) < 0.0001 && (double)this.Owner.SpeedMin < 0.0001)
  126. {
  127. result = true;
  128. }
  129. return result;
  130. }
  131. public void SetRenderType(int type)
  132. {
  133. this.Type = type;
  134. if (type == 0)
  135. {
  136. this.RenderObj = this.Owner.GetVertexPool().AddSprite(this.Owner.SpriteWidth, this.Owner.SpriteHeight, (STYPE)this.Owner.SpriteType, (ORIPOINT)this.Owner.OriPoint, 60f, this.IsSimpleSprite());
  137. }
  138. else if (type == 1)
  139. {
  140. float width = this.Owner.RibbonWidth;
  141. float len = this.Owner.RibbonLen;
  142. if (this.Owner.UseRandomRibbon)
  143. {
  144. width = UnityEngine.Random.Range(this.Owner.RibbonWidthMin, this.Owner.RibbonWidthMax);
  145. len = UnityEngine.Random.Range(this.Owner.RibbonLenMin, this.Owner.RibbonLenMax);
  146. }
  147. this.RenderObj = this.Owner.GetVertexPool().AddRibbonTrail(this.Owner.FaceToObject, this.Owner.FaceObject, width, this.Owner.MaxRibbonElements, len, this.Owner.ClientTransform.position + this.Owner.EmitPoint, 60f);
  148. }
  149. else if (type == 2)
  150. {
  151. this.RenderObj = this.Owner.GetVertexPool().AddCone(this.Owner.ConeSize, this.Owner.ConeSegment, this.Owner.ConeAngle, this.Owner.OriVelocityAxis, 0, 60f, this.Owner.UseConeAngleChange, this.Owner.ConeDeltaAngle);
  152. }
  153. else if (type == 3)
  154. {
  155. if (this.Owner.CMesh == null)
  156. {
  157. UnityEngine.Debug.LogError("custom mesh layer has no mesh to display!", this.Owner.gameObject);
  158. }
  159. Vector3 dir = Vector3.zero;
  160. if (this.Owner.OriVelocityAxis == Vector3.zero)
  161. {
  162. this.Owner.OriVelocityAxis = Vector3.up;
  163. }
  164. dir = this.Owner.OriVelocityAxis;
  165. this.RenderObj = this.Owner.GetVertexPool().AddCustomMesh(this.Owner.CMesh, dir, 60f);
  166. }
  167. else if (type == 4)
  168. {
  169. this.RenderObj = this.Owner.GetVertexPool().AddRope();
  170. }
  171. else if (type == 5)
  172. {
  173. this.RenderObj = this.Owner.GetVertexPool().AddSphericalBillboard();
  174. }
  175. this.RenderObj.Node = this;
  176. }
  177. public void Reset()
  178. {
  179. if (this.Owner.UseSubEmitters && !string.IsNullOrEmpty(this.Owner.DeathSubEmitter))
  180. {
  181. XffectComponent effect = this.Owner.SpawnCache.GetEffect(this.Owner.DeathSubEmitter);
  182. if (effect == null)
  183. {
  184. return;
  185. }
  186. effect.transform.position = this.CurWorldPos;
  187. effect.Active();
  188. }
  189. this.Position = this.Owner.ClientTransform.position;
  190. this.Velocity = Vector3.zero;
  191. this.ElapsedTime = 0f;
  192. this.CurWorldPos = this.Owner.transform.position;
  193. this.LastWorldPos = this.CurWorldPos;
  194. this.IsCollisionEventSended = false;
  195. if (this.Owner.IsRandomStartColor)
  196. {
  197. this.StartColor = this.Owner.RandomColorGradient.Evaluate(UnityEngine.Random.Range(0f, 1f));
  198. }
  199. for (int i = 0; i < this.AffectorList.Count; i++)
  200. {
  201. Affector affector = this.AffectorList[i];
  202. affector.Reset();
  203. }
  204. this.Scale = Vector3.one;
  205. this.mIsFade = false;
  206. this.RenderObj.Reset();
  207. if (this.Owner.UseSubEmitters && this.SubEmitter != null && XffectComponent.IsActive(this.SubEmitter.gameObject) && this.Owner.SubEmitterAutoStop)
  208. {
  209. this.SubEmitter.StopEmit();
  210. }
  211. }
  212. public void Remove()
  213. {
  214. this.Owner.RemoveActiveNode(this);
  215. }
  216. public void Stop()
  217. {
  218. this.Reset();
  219. this.Remove();
  220. }
  221. public void Fade(float time)
  222. {
  223. ColorAffector colorAffector = null;
  224. for (int i = 0; i < this.AffectorList.Count; i++)
  225. {
  226. if (this.AffectorList[i].Type == AFFECTORTYPE.ColorAffector)
  227. {
  228. colorAffector = (ColorAffector)this.AffectorList[i];
  229. break;
  230. }
  231. }
  232. if (colorAffector == null)
  233. {
  234. colorAffector = new ColorAffector(this.Owner, this);
  235. this.AffectorList.Add(colorAffector);
  236. }
  237. this.mIsFade = true;
  238. colorAffector.Fade(time);
  239. }
  240. public void CollisionDetection()
  241. {
  242. if (!this.Owner.UseCollisionDetection || this.IsCollisionEventSended)
  243. {
  244. return;
  245. }
  246. bool flag = false;
  247. GameObject obj = null;
  248. if (this.Owner.CollisionType == COLLITION_TYPE.Sphere && this.Owner.CollisionGoal != null)
  249. {
  250. Vector3 lastCollisionDetectDir = this.CurWorldPos - this.Owner.CollisionGoal.position;
  251. float num = this.Owner.ColliisionPosRange + this.Owner.ParticleRadius;
  252. if (lastCollisionDetectDir.sqrMagnitude <= num * num)
  253. {
  254. flag = true;
  255. obj = this.Owner.CollisionGoal.gameObject;
  256. }
  257. this.LastCollisionDetectDir = lastCollisionDetectDir;
  258. }
  259. else if (this.Owner.CollisionType == COLLITION_TYPE.CollisionLayer)
  260. {
  261. int layerMask = 1 << this.Owner.CollisionLayer.value;
  262. Vector3 originalPos = this.GetOriginalPos();
  263. RaycastHit raycastHit;
  264. if (Physics.SphereCast(originalPos, this.Owner.ParticleRadius, this.Velocity.normalized, out raycastHit, this.Owner.ParticleRadius, layerMask))
  265. {
  266. flag = true;
  267. obj = raycastHit.collider.gameObject;
  268. }
  269. }
  270. else if (this.Owner.CollisionType == COLLITION_TYPE.Plane)
  271. {
  272. if (!this.Owner.CollisionPlane.GetSide(this.CurWorldPos - this.Owner.PlaneDir.normalized * this.Owner.ParticleRadius))
  273. {
  274. flag = true;
  275. obj = this.Owner.gameObject;
  276. }
  277. }
  278. if (flag)
  279. {
  280. if (this.Owner.EventHandleFunctionName != string.Empty && this.Owner.EventReceiver != null)
  281. {
  282. this.Owner.EventReceiver.SendMessage(this.Owner.EventHandleFunctionName, new CollisionParam(obj, this.GetOriginalPos(), this.Velocity.normalized));
  283. }
  284. this.IsCollisionEventSended = true;
  285. if (this.Owner.CollisionAutoDestroy)
  286. {
  287. this.ElapsedTime = float.PositiveInfinity;
  288. }
  289. if (this.Owner.UseSubEmitters && !string.IsNullOrEmpty(this.Owner.CollisionSubEmitter))
  290. {
  291. XffectComponent effect = this.Owner.SpawnCache.GetEffect(this.Owner.CollisionSubEmitter);
  292. if (effect == null)
  293. {
  294. return;
  295. }
  296. effect.transform.position = this.CurWorldPos;
  297. effect.Active();
  298. }
  299. }
  300. }
  301. public void Update(float deltaTime)
  302. {
  303. this.ElapsedTime += deltaTime;
  304. for (int i = 0; i < this.AffectorList.Count; i++)
  305. {
  306. Affector affector = this.AffectorList[i];
  307. affector.Update(deltaTime);
  308. }
  309. this.Position += this.Velocity * deltaTime;
  310. if (this.SyncClient)
  311. {
  312. this.CurWorldPos = this.Position + this.GetRealClientPos();
  313. }
  314. else
  315. {
  316. this.CurWorldPos = this.Position;
  317. }
  318. this.CollisionDetection();
  319. if (this.Owner.UseSubEmitters && this.SubEmitter != null && XffectComponent.IsActive(this.SubEmitter.gameObject))
  320. {
  321. this.SubEmitter.transform.position = this.CurWorldPos;
  322. }
  323. this.RenderObj.Update(deltaTime);
  324. if (this.Owner.UseShaderCurve2 || this.Owner.UseShaderCurve1)
  325. {
  326. float x = (!this.Owner.UseShaderCurve1) ? 1f : this.Owner.ShaderCurveX1.Evaluate(this.GetElapsedTime(), this);
  327. float y = (!this.Owner.UseShaderCurve2) ? 0f : this.Owner.ShaderCurveX2.Evaluate(this.GetElapsedTime(), this);
  328. this.RenderObj.ApplyShaderParam(x, y);
  329. }
  330. this.LastWorldPos = this.CurWorldPos;
  331. if (this.ElapsedTime > this.LifeTime && this.LifeTime > 0f)
  332. {
  333. this.Reset();
  334. this.Remove();
  335. }
  336. }
  337. public RenderObject RenderObj;
  338. protected int Type;
  339. public int Index;
  340. public ulong TotalIndex;
  341. public Transform ClientTrans;
  342. public bool SyncClient;
  343. public EffectLayer Owner;
  344. protected Vector3 CurDirection;
  345. protected Vector3 LastWorldPos = Vector3.zero;
  346. public Vector3 CurWorldPos;
  347. protected float ElapsedTime;
  348. public Vector3 Position;
  349. public Vector2 LowerLeftUV;
  350. public Vector2 UVDimensions;
  351. public Vector3 Velocity;
  352. public Vector2 Scale;
  353. public float RotateAngle;
  354. public Color Color;
  355. public XffectComponent SubEmitter;
  356. public List<Affector> AffectorList;
  357. public Vector3 OriDirection;
  358. public float LifeTime;
  359. public int OriRotateAngle;
  360. public float OriScaleX;
  361. public float OriScaleY;
  362. public bool SimpleSprite;
  363. public Color StartColor;
  364. protected bool IsCollisionEventSended;
  365. protected Vector3 LastCollisionDetectDir = Vector3.zero;
  366. public bool mIsFade;
  367. }
  368. }