Skip to content

Commit 500ab51

Browse files
committed
Speed fixes, simplify update logic
1 parent 8746ef9 commit 500ab51

4 files changed

Lines changed: 114 additions & 92 deletions

File tree

Assets/SlimTween/Scripts/TweenClasses.cs

Lines changed: 77 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2525
using UnityEngine;
2626
using System;
2727
using System.Runtime.CompilerServices;
28+
using System.Collections.Specialized;
29+
using System.Collections;
30+
using System.Collections.Generic;
2831

2932
#if UNITY_EDITOR
3033
using UnityEditor;
@@ -50,30 +53,36 @@ public class TweenClass : ISlimRegister
5053
protected bool unscaledTime;
5154
/// <summary>The frameCount when it 1st initialized.</summary>
5255
protected int frameIn;
53-
public int ease {get; private set;}
56+
public int ease { get; private set; }
5457
/// <summary>Gets and sets the duration.</summary>
5558
float ISlimRegister.GetSetDuration { get => duration; set => duration = value; }
5659
/// <summary>Gets and sets the runningTime.</summary>
5760
float ISlimRegister.GetSetRunningTime { get => runningTime; set => runningTime = value; }
5861
/// <summary>Unscaled or scaled Time.delta.</summary>
5962
bool ISlimRegister.UnscaledTimeIs { get => unscaledTime; set => unscaledTime = value; }
60-
void ISlimRegister.SetEase(Ease easeType)=> ease = (int)easeType;
61-
void ISlimRegister.SetState(TweenState stateType)=> state = (int)stateType;
63+
void ISlimRegister.SetEase(Ease easeType) => ease = (int)easeType;
64+
void ISlimRegister.SetState(TweenState stateType) => state = (int)stateType;
65+
void ISlimRegister.SetEstimationTime()=> EstimateDuration(0, 1f, tprops.speed);
6266
/// <summary>Flips the delta ticks</summary>
6367
protected void FlipTick()
6468
{
65-
if (flipTick)
66-
{
67-
tprops.runningFloat = 1f;
68-
tprops.runningFloat -= 0.00013f;
69-
}
70-
else
71-
{
72-
tprops.runningFloat = 0f;
73-
tprops.runningFloat += 0.00013f;
74-
}
69+
if (flipTick)
70+
{
71+
tprops.runningFloat = 1f;
72+
}
73+
else
74+
{
75+
tprops.runningFloat = 0f;
76+
}
77+
7578

7679
flipTick = !flipTick;
80+
81+
//2 = speed based here
82+
if(tprops.lerptype != 2)
83+
{
84+
tprops.updatecondition += flipTick ? 1 : -1;
85+
}
7786
}
7887
protected bool flipTick = false;
7988
bool ISlimRegister.FlipTickIs => flipTick;
@@ -164,28 +173,25 @@ public void RunUpdate()
164173
if (state < 2)
165174
return;
166175

167-
if(tprops.lerptype > 2)
176+
if (tprops.updatecondition == 3)
168177
{
169-
if (!flipTick)
178+
if (runningTime > duration && CheckIfFinished())
170179
{
171-
if (runningTime > duration && CheckIfFinished())
172-
{
173-
return;
174-
}
180+
return;
175181
}
176-
else
182+
}
183+
else if (tprops.updatecondition == 4)
184+
{
185+
if (runningTime < 0.0001f && CheckIfFinished())
177186
{
178-
if (runningTime < 0.0001f && CheckIfFinished())
179-
{
180-
return;
181-
}
187+
return;
182188
}
183189
}
184190
else
185191
{
186-
if ((tprops.loopCounter & 1) == 0)
192+
if (!flipTick)
187193
{
188-
if (!flipTick && tprops.runningFloat + 0.00013 > 1f || (!flipTick && tprops.runningFloat - 0.00015 < 0f && Mathf.Approximately(runningTime, duration)))
194+
if (tprops.runningSpeed + 0.0001f > 1f)
189195
{
190196
if (CheckIfFinished())
191197
{
@@ -195,7 +201,7 @@ public void RunUpdate()
195201
}
196202
else
197203
{
198-
if (flipTick && tprops.runningFloat - 0.00015f < 0f)
204+
if (tprops.runningSpeed < 0.0001f)
199205
{
200206
if (CheckIfFinished())
201207
{
@@ -250,25 +256,6 @@ protected bool CheckIfFinished()
250256
[MethodImpl(MethodImplOptions.AggressiveInlining)]
251257
protected bool InvokeRepeat()
252258
{
253-
/*
254-
if (tprops.loopCounter < 0)
255-
{
256-
ResetLoop();
257-
258-
if (tprops.pingpong)
259-
{
260-
FlipTick();
261-
}
262-
else
263-
{
264-
runningTime = 0.00013f;
265-
tprops.runningFloat = 0.00013f;
266-
}
267-
268-
return true;
269-
}
270-
*/
271-
272259
if (tprops.pingpong)
273260
{
274261
if (checkInfinitePingPong())
@@ -350,6 +337,17 @@ protected void Clear()
350337
ease = 0;
351338
TweenManager.RemoveFromActiveTween(this);
352339
}
340+
public float EstimateDuration(float current, float target, float speed)
341+
{
342+
float deltaTime = !unscaledTime ? Time.deltaTime : Time.unscaledDeltaTime;
343+
344+
// Calculate the distance between current and target value
345+
float distance = Mathf.Abs(target - current);
346+
347+
// Calculate the time duration to reach the target value
348+
float duration = distance / (speed / 3f * deltaTime);
349+
return duration;
350+
}
353351
///<summary>Checks if tweening. Paused tween will also mean tweening.</summary>
354352
public bool IsTweening => state != 0;
355353
///<summary>Checks if paused.</summary>
@@ -466,6 +464,7 @@ public void Dispose()
466464
[Serializable]
467465
public sealed class TProps
468466
{
467+
public float runningSpeed;
469468
/// <summary>The running underlying tick value;</summary>
470469
public float runningFloat = 0.00012f;
471470
/// <summary>Instance id</summary>
@@ -486,8 +485,9 @@ public sealed class TProps
486485
public float speed = -1f;
487486
/// <summary>AnimationCurves</summary>
488487
public AnimationCurve animationCurve;
489-
/// <summary>1 speed, 2 curve, 3 regular.</summary>
488+
/// <summary>1 curve, 2 speed, 3 regular.</summary>
490489
public int lerptype = 0;
490+
public int updatecondition;
491491
///<summary>Sets to default value to be reused in a pool. If not then will be normally disposed.</summary>
492492
public void SetDefault()
493493
{
@@ -508,20 +508,23 @@ public void ResetLoopProperties()
508508
/// <summary>Sets the lerptype.</summary>
509509
public void SetLerpType()
510510
{
511-
if(speed < 0)
511+
if (speed < 0)
512512
{
513-
if(animationCurve == null)
513+
if (animationCurve == null)
514514
{
515515
lerptype = 3;
516+
updatecondition = 3;
516517
}
517518
else
518519
{
519520
lerptype = 1;
521+
updatecondition = 1;
520522
}
521523
}
522524
else
523525
{
524526
lerptype = 2;
527+
updatecondition = 2;
525528
}
526529
}
527530
}
@@ -553,6 +556,7 @@ public interface ISlimRegister
553556
public bool UnscaledTimeIs { get; set; }
554557
public void SetEase(Ease easeType);
555558
public void SetState(TweenState stateType);
559+
public void SetEstimationTime();
556560
}
557561
/// <summary>STTransform class to handle all Transforms.</summary>
558562
public sealed class SlimTransform : TweenClass, ISlimTween
@@ -562,7 +566,7 @@ public SlimTransform()
562566
interp = new InterpolatorStruct();
563567
}
564568
/// <summary>Previous assigned type.</summary>
565-
TransformType ISlimTween.GetTransformType { get => type; set => type = value; }
569+
TransformType ISlimTween.GetTransformType { get => (TransformType)type; set => type = value; }
566570
/// <summary>The transform.</summary>
567571
Transform transform;
568572
/// <summary>Starting value.</summary>
@@ -601,19 +605,19 @@ protected override void InternalOnUpdate()
601605

602606
switch (type)
603607
{
604-
case TransformType.Move:
608+
case TransformType.Move: //Move
605609
LerpPosition(this.FloatInterp(tick));
606610
break;
607-
case TransformType.Scale:
611+
case TransformType.Scale: //Scale
608612
LerpScale(this.FloatInterp(tick));
609613
break;
610-
case TransformType.Rotate:
614+
case TransformType.Rotate: //Rotate
611615
LerpEuler(this.FloatInterp(tick));
612616
break;
613-
case TransformType.RotateAround:
617+
case TransformType.RotateAround: //RotateAround
614618
LerpRotateAround(this.FloatInterp(tick));
615619
break;
616-
case TransformType.Translate:
620+
case TransformType.Translate: //Translate
617621
LerpTranslate(this.FloatInterp(tick));
618622
break;
619623
}
@@ -622,19 +626,19 @@ void InvokeLerps(float tick)
622626
{
623627
switch (type)
624628
{
625-
case TransformType.Move:
629+
case TransformType.Move: //Move
626630
LerpPosition(this.FloatInterp(tick));
627631
break;
628-
case TransformType.Scale:
632+
case TransformType.Scale: //Scale
629633
LerpScale(this.FloatInterp(tick));
630634
break;
631-
case TransformType.Rotate:
635+
case TransformType.Rotate: //Rotate
632636
LerpEuler(this.FloatInterp(tick));
633637
break;
634-
case TransformType.RotateAround:
638+
case TransformType.RotateAround: //RotateAround
635639
LerpRotateAround(this.FloatInterp(tick));
636640
break;
637-
case TransformType.Translate:
641+
case TransformType.Translate: //Translate
638642
LerpTranslate(this.FloatInterp(tick));
639643
break;
640644
}
@@ -774,7 +778,7 @@ public SlimRect()
774778
(Vector3 from, Vector3 to) ISlimTween.FromTo { get { return (interp.from(), interp.to()); } set { interp.SetFrom(value.from); interp.SetTo(value.to); } }
775779
bool ISlimTween.Locality { get => isLocal; set => isLocal = value; }
776780
/// <summary>Previous assigned type.</summary>
777-
TransformType ISlimTween.GetTransformType { get => type; set => type = value; }
781+
TransformType ISlimTween.GetTransformType { get => (TransformType)type; set => type = value; }
778782
/// <summary>Initialize transform base value.</summary>
779783
/// <param name="objectTransform">The transform.</param>
780784
/// <param name="from">Starting value</param>
@@ -863,22 +867,22 @@ void InvokeLerps(float tick)
863867
{
864868
switch (type)
865869
{
866-
case TransformType.Move:
870+
case TransformType.Move: //Move
867871
LerpPosition(this.FloatInterp(tick));
868872
break;
869-
case TransformType.Scale:
873+
case TransformType.Scale: //Scale
870874
LerpScale(this.FloatInterp(tick));
871875
break;
872-
case TransformType.Rotate:
876+
case TransformType.Rotate: //Rotate
873877
LerpEuler(this.FloatInterp(tick));
874878
break;
875-
case TransformType.RotateAround:
879+
case TransformType.RotateAround: //RotateAround
876880
LerpRotateAround(this.FloatInterp(tick));
877881
break;
878-
case TransformType.SizeDelta:
882+
case TransformType.SizeDelta: //SizeDelta
879883
LerpSizeDelta(this.FloatInterp(tick));
880884
break;
881-
case TransformType.SizeAnchored:
885+
case TransformType.SizeAnchored: //SizeAnchored
882886
LerpSizeAnchored(this.FloatInterp(tick));
883887
break;
884888
}
@@ -964,17 +968,16 @@ public interface ISlimTween
964968
/// <param name="start"></param>
965969
/// <param name="end"></param>
966970
/// <param name="time"></param>
967-
public enum TransformType : byte
971+
public enum TransformType
968972
{
969973
None = 0,
970974
Move = 1,
971975
Scale = 2,
972976
Rotate = 3,
973977
RotateAround = 4,
974-
Follow = 5,
975-
SizeDelta = 6,
976-
SizeAnchored = 7,
977-
Translate = 8
978+
SizeDelta = 5,
979+
SizeAnchored = 6,
980+
Translate = 7
978981
}
979982

980983
[Serializable]
@@ -997,15 +1000,15 @@ public ref Vector3 to()
9971000
vec.Set(x, y, z);
9981001
return ref vec;
9991002
}
1000-
public ref Vector3 GetRef(float tick)
1003+
public ref Vector3 UpdateRef(float tick)
10011004
{
10021005
vec.Set(
10031006
a + (x - a) * tick,
10041007
b + (y - b) * tick,
10051008
c + (z - c) * tick);
10061009
return ref vec;
10071010
}
1008-
public ref Vector3 GetVector()=> ref vec;
1011+
public ref Vector3 GetVector() => ref vec;
10091012

10101013
public void Set(Vector3 from, Vector3 to)
10111014
{

Assets/SlimTween/Scripts/TweenManager.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ public static void TweenBagCache()
9090
activeTweens.Add(tmp);
9191
temporary.Remove(tmp);
9292
tmp.tprops.SetLerpType();
93+
94+
if(tmp.tprops.speed > 0)
95+
{
96+
(tmp as ISlimRegister).GetSetDuration = float.PositiveInfinity;
97+
}
9398
}
9499
}
95100
}

Assets/SlimTween/Scripts/TweenUtility.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,20 @@ public static float FloatInterp(this TweenClass tclass, float time)
5959
return STEasing.Easing(tclass.ease, time); //disable easepower for now, change time param to : (tclass.tprops.easePower + time)
6060
}
6161
else if(tclass.tprops.lerptype > 1)
62+
{
63+
ISlimRegister islim = tclass;
64+
65+
var to = 1f;
66+
67+
if(islim.FlipTickIs)
68+
{
69+
to = 0f;
70+
}
71+
72+
tclass.tprops.runningSpeed = Mathf.MoveTowards(tclass.tprops.runningSpeed, to, tclass.tprops.speed / 3f * (!islim.UnscaledTimeIs ? Time.deltaTime : Time.unscaledDeltaTime));
73+
return tclass.tprops.runningSpeed; //TODO: if tprops.runningFloat won't work, rever it back to runningTime
74+
}
75+
else
6276
{
6377
ISlimRegister islim = tclass;
6478
var zero = 0f;
@@ -72,12 +86,6 @@ public static float FloatInterp(this TweenClass tclass, float time)
7286

7387
return Mathf.LerpUnclamped(zero, one, tclass.tprops.animationCurve.Evaluate(time));
7488
}
75-
else
76-
{
77-
ISlimRegister islim = tclass;
78-
tclass.tprops.runningFloat = Mathf.MoveTowards(tclass.tprops.runningFloat, !islim.FlipTickIs ? 1f : 0f, tclass.tprops.speed / 3f * (!islim.UnscaledTimeIs ? Time.deltaTime : Time.unscaledDeltaTime));
79-
return tclass.tprops.runningFloat; //TODO: if tprops.runningFloat won't work, rever it back to runningTime
80-
}
8189
}
8290
public static float LerpRefs(this TweenClass tclass, float time, ref Vector3 refs)
8391
{

0 commit comments

Comments
 (0)