Skip to content

Commit fecbd5a

Browse files
committed
Add support for looping
1 parent c9468ee commit fecbd5a

File tree

6 files changed

+205
-45
lines changed

6 files changed

+205
-45
lines changed

Runtime/LoopType.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
namespace Zigurous.Tweening
2+
{
3+
/// <summary>
4+
/// A type of loop style.
5+
/// </summary>
6+
public enum LoopType
7+
{
8+
/// <summary>
9+
/// Restarts the tween from the beginning after it ends.
10+
/// </summary>
11+
Restart,
12+
13+
/// <summary>
14+
/// Restarts the tween from the beginning after it ends. If the tween
15+
/// has a delay it will be delayed again each loop.
16+
/// </summary>
17+
RestartWithDelay,
18+
19+
/// <summary>
20+
/// Plays the tween forwards then backwards then forwards then
21+
/// backwards, etc.
22+
/// </summary>
23+
PingPong,
24+
25+
/// <summary>
26+
/// Plays the tween forwards then backwards then forwards then
27+
/// backwards, etc. If the tween has a delay it will be delayed again
28+
/// each loop.
29+
/// </summary>
30+
PingPongWithDelay,
31+
}
32+
33+
}

Runtime/LoopType.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/PropertyChaining.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ public static T SetDelay<T>(this T tween, float delay) where T: Tween
4141
return tween;
4242
}
4343

44+
public static T SetLoops<T>(this T tween, int loops, LoopType loopType = LoopType.Restart) where T: Tween
45+
{
46+
tween.loops = loops;
47+
tween.loopType = loopType;
48+
return tween;
49+
}
50+
4451
public static T SetReversed<T>(this T tween, bool reversed = true) where T: Tween
4552
{
4653
tween.reversed = reversed;
@@ -83,6 +90,12 @@ public static T OnStop<T>(this T tween, TweenCallback callback) where T: Tween
8390
return tween;
8491
}
8592

93+
public static T OnLoop<T>(this T tween, TweenCallback callback) where T: Tween
94+
{
95+
tween.onLoop += callback;
96+
return tween;
97+
}
98+
8699
public static T OnComplete<T>(this T tween, TweenCallback callback) where T: Tween
87100
{
88101
tween.onComplete += callback;

Runtime/Sequence.cs

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ public Sequence() : base()
4141
this.type = TweenType.Sequence;
4242
}
4343

44+
public override void Animate()
45+
{
46+
// Do nothing. The individual tweens are animated on their own.
47+
}
48+
4449
/// <summary>
4550
/// Plays the tween sequence, whether starting for the first time or
4651
/// resuming from a stopped state.
@@ -79,28 +84,35 @@ private Tween Prepare(Tween tween)
7984

8085
private void Next()
8186
{
82-
this.currentIndex++;
83-
84-
if (CanComplete()) {
85-
Complete();
87+
if (this.reversed) {
88+
this.currentIndex--;
8689
} else {
87-
this.tweens[this.currentIndex].Play();
90+
this.currentIndex++;
8891
}
89-
}
9092

91-
protected override void Animate()
92-
{
93-
// Do nothing. The individual tweens are animated on their own.
93+
Tween tween = this.activeTween;
94+
95+
if (tween != null) {
96+
tween.Play();
97+
}
9498
}
9599

96-
protected override bool CanComplete()
100+
protected override bool IsFinished()
97101
{
98-
return this.currentIndex >= this.tweens.Count;
102+
if (this.reversed) {
103+
return this.currentIndex < 0;
104+
} else {
105+
return this.currentIndex >= this.tweens.Count;
106+
}
99107
}
100108

101109
protected override void OnStart()
102110
{
103-
this.currentIndex = 0;
111+
if (this.reversed) {
112+
this.currentIndex = this.tweens.Count - 1;
113+
} else {
114+
this.currentIndex = 0;
115+
}
104116

105117
Tween tween = this.activeTween;
106118

@@ -127,6 +139,19 @@ protected override void OnResume()
127139
}
128140
}
129141

142+
protected override void OnLoop()
143+
{
144+
foreach (Tween tween in this.tweens)
145+
{
146+
if (this.loopType == LoopType.PingPong || this.loopType == LoopType.PingPongWithDelay) {
147+
tween.reversed = !tween.reversed;
148+
}
149+
150+
tween.elapsed = 0.0f;
151+
tween.Animate();
152+
}
153+
}
154+
130155
protected override void OnComplete()
131156
{
132157
foreach (Tween tween in this.tweens)

0 commit comments

Comments
 (0)