DragAffector.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using UnityEngine;
  3. namespace Xft
  4. {
  5. public class DragAffector : Affector
  6. {
  7. public DragAffector(Transform dragObj, bool useDir, Vector3 dir, float mag, bool useMaxDist, float maxDist, float atten, EffectNode node) : base(node, AFFECTORTYPE.DragAffector)
  8. {
  9. this.DragObj = dragObj;
  10. this.UseDirection = useDir;
  11. this.Direction = dir;
  12. this.Magnitude = mag;
  13. this.UseMaxDistance = useMaxDist;
  14. this.MaxDistance = maxDist;
  15. this.Attenuation = atten;
  16. }
  17. protected void UpdateNoAttenuationNoDir(float deltaTime)
  18. {
  19. float sqrMagnitude = (this.Node.GetOriginalPos() - this.DragObj.position).sqrMagnitude;
  20. float num = this.Magnitude * deltaTime;
  21. if (num != 0f && sqrMagnitude <= this.MaxDistance * this.MaxDistance)
  22. {
  23. if (num < 1f)
  24. {
  25. this.Node.Velocity *= 1f - num;
  26. }
  27. else
  28. {
  29. this.Node.Velocity = Vector3.zero;
  30. }
  31. }
  32. }
  33. protected void UpdateNoAttenuationNoDirNoDist(float deltaTime)
  34. {
  35. float num = this.Magnitude * deltaTime;
  36. if (num < 1f)
  37. {
  38. this.Node.Velocity *= 1f - num;
  39. }
  40. else
  41. {
  42. this.Node.Velocity = Vector3.zero;
  43. }
  44. }
  45. public override void Update(float deltaTime)
  46. {
  47. if (!this.UseDirection && this.Attenuation == 0f)
  48. {
  49. if (this.UseMaxDistance)
  50. {
  51. this.UpdateNoAttenuationNoDir(deltaTime);
  52. }
  53. else
  54. {
  55. this.UpdateNoAttenuationNoDirNoDist(deltaTime);
  56. }
  57. return;
  58. }
  59. Vector3 rhs = Vector3.one;
  60. if (this.UseDirection && this.Direction != Vector3.zero)
  61. {
  62. rhs = this.DragObj.rotation * this.Direction;
  63. rhs.Normalize();
  64. }
  65. float magnitude = (this.Node.GetOriginalPos() - this.DragObj.position).magnitude;
  66. if (!this.UseMaxDistance || magnitude <= this.MaxDistance)
  67. {
  68. float num = 1f;
  69. if (this.UseDirection)
  70. {
  71. Vector3 velocity = this.Node.Velocity;
  72. velocity.Normalize();
  73. num = Vector3.Dot(velocity, rhs);
  74. }
  75. float num2 = this.Magnitude * deltaTime * num / (1f + magnitude * this.Attenuation);
  76. if (num2 < 1f)
  77. {
  78. this.Node.Velocity -= num2 * this.Node.Velocity;
  79. }
  80. else
  81. {
  82. this.Node.Velocity = Vector3.zero;
  83. }
  84. }
  85. }
  86. protected Transform DragObj;
  87. protected bool UseDirection;
  88. protected Vector3 Direction;
  89. protected float Magnitude;
  90. protected bool UseMaxDistance;
  91. protected float MaxDistance;
  92. protected float Attenuation;
  93. }
  94. }