123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- using System;
- using UnityEngine;
- namespace Xft
- {
- public class DragAffector : Affector
- {
- public DragAffector(Transform dragObj, bool useDir, Vector3 dir, float mag, bool useMaxDist, float maxDist, float atten, EffectNode node) : base(node, AFFECTORTYPE.DragAffector)
- {
- this.DragObj = dragObj;
- this.UseDirection = useDir;
- this.Direction = dir;
- this.Magnitude = mag;
- this.UseMaxDistance = useMaxDist;
- this.MaxDistance = maxDist;
- this.Attenuation = atten;
- }
- protected void UpdateNoAttenuationNoDir(float deltaTime)
- {
- float sqrMagnitude = (this.Node.GetOriginalPos() - this.DragObj.position).sqrMagnitude;
- float num = this.Magnitude * deltaTime;
- if (num != 0f && sqrMagnitude <= this.MaxDistance * this.MaxDistance)
- {
- if (num < 1f)
- {
- this.Node.Velocity *= 1f - num;
- }
- else
- {
- this.Node.Velocity = Vector3.zero;
- }
- }
- }
- protected void UpdateNoAttenuationNoDirNoDist(float deltaTime)
- {
- float num = this.Magnitude * deltaTime;
- if (num < 1f)
- {
- this.Node.Velocity *= 1f - num;
- }
- else
- {
- this.Node.Velocity = Vector3.zero;
- }
- }
- public override void Update(float deltaTime)
- {
- if (!this.UseDirection && this.Attenuation == 0f)
- {
- if (this.UseMaxDistance)
- {
- this.UpdateNoAttenuationNoDir(deltaTime);
- }
- else
- {
- this.UpdateNoAttenuationNoDirNoDist(deltaTime);
- }
- return;
- }
- Vector3 rhs = Vector3.one;
- if (this.UseDirection && this.Direction != Vector3.zero)
- {
- rhs = this.DragObj.rotation * this.Direction;
- rhs.Normalize();
- }
- float magnitude = (this.Node.GetOriginalPos() - this.DragObj.position).magnitude;
- if (!this.UseMaxDistance || magnitude <= this.MaxDistance)
- {
- float num = 1f;
- if (this.UseDirection)
- {
- Vector3 velocity = this.Node.Velocity;
- velocity.Normalize();
- num = Vector3.Dot(velocity, rhs);
- }
- float num2 = this.Magnitude * deltaTime * num / (1f + magnitude * this.Attenuation);
- if (num2 < 1f)
- {
- this.Node.Velocity -= num2 * this.Node.Velocity;
- }
- else
- {
- this.Node.Velocity = Vector3.zero;
- }
- }
- }
- protected Transform DragObj;
- protected bool UseDirection;
- protected Vector3 Direction;
- protected float Magnitude;
- protected bool UseMaxDistance;
- protected float MaxDistance;
- protected float Attenuation;
- }
- }
|