1
0
mirror of https://github.com/Cardidi/dotween-upm-fork.git synced 2026-02-14 11:01:38 +08:00

LoopType.Incremental now works with paths too

This commit is contained in:
Daniele Giardini 2015-06-16 13:07:15 +02:00
parent 8b77246cfc
commit eae6a855c0
51 changed files with 110 additions and 14 deletions

View File

@ -11,11 +11,13 @@ public class Jump : BrainBase
public float duration = 1; public float duration = 1;
public Ease ease = Ease.OutQuad; public Ease ease = Ease.OutQuad;
public int loops = -1; public int loops = -1;
public bool isRelative;
IEnumerator Start() IEnumerator Start()
{ {
yield return new WaitForSeconds(1); yield return new WaitForSeconds(1);
target.DOJump(jump, jumpHeight, numJumps, duration).SetEase(ease).SetLoops(loops, LoopType.Yoyo).SetRelative(); Tween t = target.DOJump(jump, jumpHeight, numJumps, duration).SetEase(ease).SetLoops(loops, LoopType.Yoyo);
if (isRelative) t.SetRelative();
} }
} }

View File

@ -32,7 +32,7 @@ namespace DG.Tweening
public class DOTween public class DOTween
{ {
/// <summary>DOTween's version</summary> /// <summary>DOTween's version</summary>
public static readonly string Version = "1.0.790"; public static readonly string Version = "1.0.800";
/////////////////////////////////////////////// ///////////////////////////////////////////////
// Options //////////////////////////////////// // Options ////////////////////////////////////

View File

@ -22,5 +22,10 @@ namespace DG.Tweening.Plugins.Core.PathCore
this.a = a; this.a = a;
this.b = b; this.b = b;
} }
public static ControlPoint operator +(ControlPoint cp, Vector3 v)
{
return new ControlPoint(cp.a + v, cp.b + v);
}
} }
} }

View File

@ -31,6 +31,8 @@ namespace DG.Tweening.Plugins.Core.PathCore
[SerializeField] internal float[] timesTable; // Connected to lengthsTable, used for constant speed calculations [SerializeField] internal float[] timesTable; // Connected to lengthsTable, used for constant speed calculations
[SerializeField] internal float[] lengthsTable; // Connected to timesTable, used for constant speed calculations [SerializeField] internal float[] lengthsTable; // Connected to timesTable, used for constant speed calculations
internal int linearWPIndex = -1; // Waypoint towards which we're moving (only stored for linear paths, when calling GetPoint) internal int linearWPIndex = -1; // Waypoint towards which we're moving (only stored for linear paths, when calling GetPoint)
Path _incrementalClone; // Last incremental clone. Stored in case of incremental loops, to avoid recreating a new path every time
int _incrementalIndex = 0;
ABSPathDecoder _decoder; ABSPathDecoder _decoder;
@ -59,6 +61,11 @@ namespace DG.Tweening.Plugins.Core.PathCore
if (DOTween.isUnityEditor) DOTween.GizmosDelegates.Add(Draw); if (DOTween.isUnityEditor) DOTween.GizmosDelegates.Add(Draw);
} }
internal Path()
{
// Used when cloning it
}
// Needs to be called once waypoints and decoder are assigned, to setup or refresh path data. // Needs to be called once waypoints and decoder are assigned, to setup or refresh path data.
internal void FinalizePath(bool isClosedPath, AxisConstraint lockPositionAxes, Vector3 currTargetVal) internal void FinalizePath(bool isClosedPath, AxisConstraint lockPositionAxes, Vector3 currTargetVal)
{ {
@ -181,6 +188,52 @@ namespace DG.Tweening.Plugins.Core.PathCore
isFinalized = false; isFinalized = false;
} }
// Clones this path with the given loop increment
internal Path CloneIncremental(int loopIncrement)
{
if (_incrementalClone != null) {
if (_incrementalIndex == loopIncrement) return _incrementalClone;
_incrementalClone.Destroy();
}
int wpsLen = wps.Length;
Vector3 diff = wps[wpsLen - 1] - wps[0];
Vector3[] incrWps = new Vector3[wps.Length];
for (int i = 0; i < wpsLen; ++i) incrWps[i] = wps[i] + (diff * loopIncrement);
int cpsLen = controlPoints.Length;
ControlPoint[] incrCps = new ControlPoint[cpsLen];
for (int i = 0; i < cpsLen; ++i) incrCps[i] = controlPoints[i] + (diff * loopIncrement);
Vector3[] incrNonLinearDrawWps = null;
if (nonLinearDrawWps != null) {
int nldLen = nonLinearDrawWps.Length;
incrNonLinearDrawWps = new Vector3[nldLen];
for (int i = 0; i < nldLen; ++i) incrNonLinearDrawWps[i] = nonLinearDrawWps[i] + (diff * loopIncrement);
}
_incrementalClone = new Path();
_incrementalIndex = loopIncrement;
_incrementalClone.type = type;
_incrementalClone.subdivisionsXSegment = subdivisionsXSegment;
_incrementalClone.subdivisions = subdivisions;
_incrementalClone.wps = incrWps;
_incrementalClone.controlPoints = incrCps;
if (DOTween.isUnityEditor) DOTween.GizmosDelegates.Add(_incrementalClone.Draw);
_incrementalClone.length = length;
_incrementalClone.wpLengths = wpLengths;
_incrementalClone.timesTable = timesTable;
_incrementalClone.lengthsTable = lengthsTable;
_incrementalClone._decoder = _decoder;
_incrementalClone.nonLinearDrawWps = incrNonLinearDrawWps;
_incrementalClone.targetPosition = targetPosition;
_incrementalClone.lookAtPosition = lookAtPosition;
_incrementalClone.isFinalized = true;
return _incrementalClone;
}
#endregion #endregion
// Deletes the previous waypoints and assigns the new ones // Deletes the previous waypoints and assigns the new ones

View File

@ -102,6 +102,11 @@ namespace DG.Tweening.Plugins
public override void EvaluateAndApply(PathOptions options, Tween t, bool isRelative, DOGetter<Vector3> getter, DOSetter<Vector3> setter, float elapsed, Path startValue, Path changeValue, float duration, bool usingInversePosition, UpdateNotice updateNotice) public override void EvaluateAndApply(PathOptions options, Tween t, bool isRelative, DOGetter<Vector3> getter, DOSetter<Vector3> setter, float elapsed, Path startValue, Path changeValue, float duration, bool usingInversePosition, UpdateNotice updateNotice)
{ {
if (t.loopType == LoopType.Incremental && !options.isClosedPath) {
int increment = (t.isComplete ? t.completedLoops - 1 : t.completedLoops);
if (increment > 0) changeValue = changeValue.CloneIncremental(increment);
}
float pathPerc = EaseManager.Evaluate(t.easeType, t.customEase, elapsed, duration, t.easeOvershootOrAmplitude, t.easePeriod); float pathPerc = EaseManager.Evaluate(t.easeType, t.customEase, elapsed, duration, t.easeOvershootOrAmplitude, t.easePeriod);
float constantPathPerc = changeValue.ConvertToConstantPathPerc(pathPerc); float constantPathPerc = changeValue.ConvertToConstantPathPerc(pathPerc);
Vector3 newPos = changeValue.GetPoint(constantPathPerc); Vector3 newPos = changeValue.GetPoint(constantPathPerc);

View File

@ -166,7 +166,8 @@ namespace DG.Tweening
// Set relative nested tweens // Set relative nested tweens
if (s.isRelative) { if (s.isRelative) {
for (int len = s.sequencedTweens.Count, i = 0; i < len; ++i) { for (int len = s.sequencedTweens.Count, i = 0; i < len; ++i) {
s.sequencedTweens[i].isRelative = true; Tween t = s.sequencedTweens[i];
if (!s.isBlendable) s.sequencedTweens[i].isRelative = true;
} }
} }
return true; return true;

View File

@ -439,7 +439,9 @@ namespace DG.Tweening
public static Sequence DOJump(this Rigidbody target, Vector3 endValue, float jumpPower, int numJumps, float duration, bool snapping = false) public static Sequence DOJump(this Rigidbody target, Vector3 endValue, float jumpPower, int numJumps, float duration, bool snapping = false)
{ {
if (numJumps < 1) numJumps = 1; if (numJumps < 1) numJumps = 1;
float offsetY = endValue.y - target.position.y; float startPosY = target.position.y;
float offsetY = -1;
bool offsetYSet = false;
Sequence s = DOTween.Sequence() Sequence s = DOTween.Sequence()
#if COMPATIBLE #if COMPATIBLE
.Append(DOTween.To(() => target.position, x => target.MovePosition(x.value), new Vector3(endValue.x, 0, 0), duration) .Append(DOTween.To(() => target.position, x => target.MovePosition(x.value), new Vector3(endValue.x, 0, 0), duration)
@ -459,9 +461,13 @@ namespace DG.Tweening
).Join(DOTween.To(() => target.position, target.MovePosition, new Vector3(0, jumpPower, 0), duration / (numJumps * 2)) ).Join(DOTween.To(() => target.position, target.MovePosition, new Vector3(0, jumpPower, 0), duration / (numJumps * 2))
#endif #endif
.SetOptions(AxisConstraint.Y, snapping).SetEase(Ease.OutQuad) .SetOptions(AxisConstraint.Y, snapping).SetEase(Ease.OutQuad)
.SetLoops(numJumps * 2, LoopType.Yoyo) .SetLoops(numJumps * 2, LoopType.Yoyo).SetRelative()
).SetTarget(target).SetEase(DOTween.defaultEaseType); ).SetTarget(target).SetEase(DOTween.defaultEaseType);
s.OnUpdate(() => { s.OnUpdate(() => {
if (!offsetYSet) {
offsetYSet = false;
offsetY = s.isRelative ? endValue.y : endValue.y - startPosY;
}
Vector3 pos = target.position; Vector3 pos = target.position;
pos.y += DOVirtual.EasedValue(0, offsetY, s.ElapsedDirectionalPercentage(), Ease.OutQuad); pos.y += DOVirtual.EasedValue(0, offsetY, s.ElapsedDirectionalPercentage(), Ease.OutQuad);
target.MovePosition(pos); target.MovePosition(pos);
@ -794,17 +800,23 @@ namespace DG.Tweening
public static Sequence DOJump(this Transform target, Vector3 endValue, float jumpPower, int numJumps, float duration, bool snapping = false) public static Sequence DOJump(this Transform target, Vector3 endValue, float jumpPower, int numJumps, float duration, bool snapping = false)
{ {
if (numJumps < 1) numJumps = 1; if (numJumps < 1) numJumps = 1;
float offsetY = endValue.y - target.position.y; float startPosY = target.position.y;
float offsetY = -1;
bool offsetYSet = false;
Sequence s = DOTween.Sequence() Sequence s = DOTween.Sequence()
.Append(DOTween.To(() => target.position, x => target.position = x, new Vector3(endValue.x, 0, 0), duration) .Append(DOTween.To(() => target.position, x => target.position = x, new Vector3(endValue.x, 0, 0), duration)
.SetOptions(AxisConstraint.X, snapping).SetEase(Ease.Linear) .SetOptions(AxisConstraint.X, snapping).SetEase(Ease.Linear)
).Join(DOTween.To(() => target.position, x => target.position = x, new Vector3(0, 0, endValue.z), duration) ).Join(DOTween.To(() => target.position, x => target.position = x, new Vector3(0, 0, endValue.z), duration)
.SetOptions(AxisConstraint.Z, snapping).SetEase(Ease.Linear) .SetOptions(AxisConstraint.Z, snapping).SetEase(Ease.Linear)
).Join(DOTween.To(() => target.position, x => target.position = x, new Vector3(0, jumpPower, 0), duration / (numJumps * 2)) ).Join(DOTween.To(() => target.position, x => target.position = x, new Vector3(0, jumpPower, 0), duration / (numJumps * 2))
.SetOptions(AxisConstraint.Y, snapping).SetEase(Ease.OutQuad) .SetOptions(AxisConstraint.Y, snapping).SetEase(Ease.OutQuad).SetRelative()
.SetLoops(numJumps * 2, LoopType.Yoyo) .SetLoops(numJumps * 2, LoopType.Yoyo)
).SetTarget(target).SetEase(DOTween.defaultEaseType); ).SetTarget(target).SetEase(DOTween.defaultEaseType);
s.OnUpdate(() => { s.OnUpdate(() => {
if (!offsetYSet) {
offsetYSet = false;
offsetY = s.isRelative ? endValue.y : endValue.y - startPosY;
}
Vector3 pos = target.position; Vector3 pos = target.position;
pos.y += DOVirtual.EasedValue(0, offsetY, s.ElapsedDirectionalPercentage(), Ease.OutQuad); pos.y += DOVirtual.EasedValue(0, offsetY, s.ElapsedDirectionalPercentage(), Ease.OutQuad);
target.position = pos; target.position = pos;
@ -822,17 +834,23 @@ namespace DG.Tweening
public static Sequence DOLocalJump(this Transform target, Vector3 endValue, float jumpPower, int numJumps, float duration, bool snapping = false) public static Sequence DOLocalJump(this Transform target, Vector3 endValue, float jumpPower, int numJumps, float duration, bool snapping = false)
{ {
if (numJumps < 1) numJumps = 1; if (numJumps < 1) numJumps = 1;
float offsetY = endValue.y - target.localPosition.y; float startPosY = target.localPosition.y;
float offsetY = -1;
bool offsetYSet = false;
Sequence s = DOTween.Sequence() Sequence s = DOTween.Sequence()
.Append(DOTween.To(() => target.localPosition, x => target.localPosition = x, new Vector3(endValue.x, 0, 0), duration) .Append(DOTween.To(() => target.localPosition, x => target.localPosition = x, new Vector3(endValue.x, 0, 0), duration)
.SetOptions(AxisConstraint.X, snapping).SetEase(Ease.Linear) .SetOptions(AxisConstraint.X, snapping).SetEase(Ease.Linear)
).Join(DOTween.To(() => target.localPosition, x => target.localPosition = x, new Vector3(0, 0, endValue.z), duration) ).Join(DOTween.To(() => target.localPosition, x => target.localPosition = x, new Vector3(0, 0, endValue.z), duration)
.SetOptions(AxisConstraint.Z, snapping).SetEase(Ease.Linear) .SetOptions(AxisConstraint.Z, snapping).SetEase(Ease.Linear)
).Join(DOTween.To(() => target.localPosition, x => target.localPosition = x, new Vector3(0, jumpPower, 0), duration / (numJumps * 2)) ).Join(DOTween.To(() => target.localPosition, x => target.localPosition = x, new Vector3(0, jumpPower, 0), duration / (numJumps * 2))
.SetOptions(AxisConstraint.Y, snapping).SetEase(Ease.OutQuad) .SetOptions(AxisConstraint.Y, snapping).SetEase(Ease.OutQuad).SetRelative()
.SetLoops(numJumps * 2, LoopType.Yoyo) .SetLoops(numJumps * 2, LoopType.Yoyo)
).SetTarget(target).SetEase(DOTween.defaultEaseType); ).SetTarget(target).SetEase(DOTween.defaultEaseType);
s.OnUpdate(() => { s.OnUpdate(() => {
if (!offsetYSet) {
offsetYSet = false;
offsetY = s.isRelative ? endValue.y : endValue.y - startPosY;
}
Vector3 pos = target.localPosition; Vector3 pos = target.localPosition;
pos.y += DOVirtual.EasedValue(0, offsetY, s.ElapsedDirectionalPercentage(), Ease.OutQuad); pos.y += DOVirtual.EasedValue(0, offsetY, s.ElapsedDirectionalPercentage(), Ease.OutQuad);
target.localPosition = pos; target.localPosition = pos;

View File

@ -449,7 +449,7 @@ namespace DG.Tweening
} }
float perc = t.position / t.duration; float perc = t.position / t.duration;
bool isInverse = t.completedLoops > 0 && t.loopType == LoopType.Yoyo && t.completedLoops % 2 != 0; bool isInverse = t.completedLoops > 0 && t.loopType == LoopType.Yoyo && (!t.isComplete && t.completedLoops % 2 != 0 || t.isComplete && t.completedLoops % 2 == 0);
return isInverse ? 1 - perc : perc; return isInverse ? 1 - perc : perc;
} }

View File

@ -102,7 +102,9 @@ namespace DG.Tweening
public static Sequence DOJump(this Rigidbody2D target, Vector2 endValue, float jumpPower, int numJumps, float duration, bool snapping = false) public static Sequence DOJump(this Rigidbody2D target, Vector2 endValue, float jumpPower, int numJumps, float duration, bool snapping = false)
{ {
if (numJumps < 1) numJumps = 1; if (numJumps < 1) numJumps = 1;
float offsetY = endValue.y - target.position.y; float startPosY = target.position.y;
float offsetY = -1;
bool offsetYSet = false;
Sequence s = DOTween.Sequence() Sequence s = DOTween.Sequence()
#if COMPATIBLE #if COMPATIBLE
.Append(DOTween.To(() => target.position, x => target.MovePosition(x.value), new Vector3(endValue.x, 0, 0), duration) .Append(DOTween.To(() => target.position, x => target.MovePosition(x.value), new Vector3(endValue.x, 0, 0), duration)
@ -116,9 +118,13 @@ namespace DG.Tweening
).Join(DOTween.To(() => target.position, target.MovePosition, new Vector2(0, jumpPower), duration / (numJumps * 2)) ).Join(DOTween.To(() => target.position, target.MovePosition, new Vector2(0, jumpPower), duration / (numJumps * 2))
#endif #endif
.SetOptions(AxisConstraint.Y, snapping).SetEase(Ease.OutQuad) .SetOptions(AxisConstraint.Y, snapping).SetEase(Ease.OutQuad)
.SetLoops(numJumps * 2, LoopType.Yoyo) .SetLoops(numJumps * 2, LoopType.Yoyo).SetRelative()
).SetTarget(target).SetEase(DOTween.defaultEaseType); ).SetTarget(target).SetEase(DOTween.defaultEaseType);
s.OnUpdate(() => { s.OnUpdate(() => {
if (!offsetYSet) {
offsetYSet = false;
offsetY = s.isRelative ? endValue.y : endValue.y - startPosY;
}
Vector2 pos = target.position; Vector2 pos = target.position;
pos.y += DOVirtual.EasedValue(0, offsetY, s.ElapsedDirectionalPercentage(), Ease.OutQuad); pos.y += DOVirtual.EasedValue(0, offsetY, s.ElapsedDirectionalPercentage(), Ease.OutQuad);
target.MovePosition(pos); target.MovePosition(pos);

View File

@ -261,15 +261,21 @@ namespace DG.Tweening
public static Sequence DOJumpAnchorPos(this RectTransform target, Vector2 endValue, float jumpPower, int numJumps, float duration, bool snapping = false) public static Sequence DOJumpAnchorPos(this RectTransform target, Vector2 endValue, float jumpPower, int numJumps, float duration, bool snapping = false)
{ {
if (numJumps < 1) numJumps = 1; if (numJumps < 1) numJumps = 1;
float offsetY = endValue.y - target.anchoredPosition.y; float startPosY = target.anchoredPosition.y;
float offsetY = -1;
bool offsetYSet = false;
Sequence s = DOTween.Sequence() Sequence s = DOTween.Sequence()
.Append(DOTween.To(() => target.anchoredPosition, x => target.anchoredPosition = x, new Vector2(endValue.x, 0), duration) .Append(DOTween.To(() => target.anchoredPosition, x => target.anchoredPosition = x, new Vector2(endValue.x, 0), duration)
.SetOptions(AxisConstraint.X, snapping).SetEase(Ease.Linear) .SetOptions(AxisConstraint.X, snapping).SetEase(Ease.Linear)
).Join(DOTween.To(() => target.anchoredPosition, x => target.anchoredPosition = x, new Vector2(0, jumpPower), duration / (numJumps * 2)) ).Join(DOTween.To(() => target.anchoredPosition, x => target.anchoredPosition = x, new Vector2(0, jumpPower), duration / (numJumps * 2))
.SetOptions(AxisConstraint.Y, snapping).SetEase(Ease.OutQuad) .SetOptions(AxisConstraint.Y, snapping).SetEase(Ease.OutQuad)
.SetLoops(numJumps * 2, LoopType.Yoyo) .SetLoops(numJumps * 2, LoopType.Yoyo).SetRelative()
).SetTarget(target).SetEase(DOTween.defaultEaseType); ).SetTarget(target).SetEase(DOTween.defaultEaseType);
s.OnUpdate(() => { s.OnUpdate(() => {
if (!offsetYSet) {
offsetYSet = false;
offsetY = s.isRelative ? endValue.y : endValue.y - startPosY;
}
Vector2 pos = target.anchoredPosition; Vector2 pos = target.anchoredPosition;
pos.y += DOVirtual.EasedValue(0, offsetY, s.ElapsedDirectionalPercentage(), Ease.OutQuad); pos.y += DOVirtual.EasedValue(0, offsetY, s.ElapsedDirectionalPercentage(), Ease.OutQuad);
target.anchoredPosition = pos; target.anchoredPosition = pos;

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.