GravityAffector.cs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using UnityEngine;
  3. namespace Xft
  4. {
  5. public class GravityAffector : Affector
  6. {
  7. public GravityAffector(Transform obj, GAFTTYPE gtype, bool isacc, Vector3 dir, EffectNode node) : base(node, AFFECTORTYPE.GravityAffector)
  8. {
  9. this.GType = gtype;
  10. this.Dir = dir;
  11. this.Dir.Normalize();
  12. this.GravityObj = obj;
  13. this.IsAccelerate = isacc;
  14. }
  15. public void SetAttraction(Transform goal)
  16. {
  17. this.GravityObj = goal;
  18. }
  19. public override void Update(float deltaTime)
  20. {
  21. float d;
  22. if (this.Node.Owner.GravityMagType == MAGTYPE.Fixed)
  23. {
  24. d = this.Node.Owner.GravityMag;
  25. }
  26. else if (this.Node.Owner.GravityMagType == MAGTYPE.Curve_OBSOLETE)
  27. {
  28. d = this.Node.Owner.GravityCurve.Evaluate(this.Node.GetElapsedTime());
  29. }
  30. else
  31. {
  32. d = this.Node.Owner.GravityCurveX.Evaluate(this.Node.GetElapsedTime());
  33. }
  34. if (this.GType == GAFTTYPE.Planar)
  35. {
  36. Vector3 a = this.Node.Owner.ClientTransform.rotation * this.Dir;
  37. if (this.IsAccelerate)
  38. {
  39. this.Node.Velocity += a * d * deltaTime;
  40. }
  41. else
  42. {
  43. this.Node.Position += a * d * deltaTime;
  44. }
  45. }
  46. else if (this.GType == GAFTTYPE.Spherical)
  47. {
  48. Vector3 a2 = this.GravityObj.position - this.Node.GetOriginalPos();
  49. if (this.IsAccelerate)
  50. {
  51. this.Node.Velocity += a2 * d * deltaTime;
  52. }
  53. else
  54. {
  55. this.Node.Position += a2.normalized * d * deltaTime;
  56. }
  57. }
  58. }
  59. protected GAFTTYPE GType;
  60. protected Vector3 Dir;
  61. protected Transform GravityObj;
  62. protected bool IsAccelerate = true;
  63. }
  64. }