-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTweenManager.cs
More file actions
182 lines (164 loc) · 5 KB
/
TweenManager.cs
File metadata and controls
182 lines (164 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace TweenEngine
{
/**
* A TweenManager let you pack many tweens together, and update them at once.
* Its main interest is that it handles the pooling complexity for you if you
* decided to enable object pooling using "Tween.setPoolEnabled()".
*
* <br/><br/>
* Just add a bunch of tweens or tween groups to it and call update()
* periodically.
*
* @see Tween
* @see TweenGroup
* @author Aurelien Ribon (aurelien.ribon@gmail.com)
*/
public class TweenManager
{
// -------------------------------------------------------------------------
// Implementation
// -------------------------------------------------------------------------
private readonly List<Tween> _tweens;
/**
* Instantiates a new manager.
*/
public TweenManager() {
this._tweens = new List<Tween>(20);
}
// -------------------------------------------------------------------------
// API
// -------------------------------------------------------------------------
/**
* Adds a new tween to the manager.
* @param tween A tween. Does nothing if it is already present.
* @return The manager, for instruction chaining.
*/
public TweenManager Add(Tween tween) {
_tweens.Add(tween);
return this;
}
/**
* Adds every tween from a tween group to the manager. Note that the group
* will be cleared from its tweens, as says its specification. This is a
* mandatory operation for a better management of the memory.
* @param tweenGroup A tween group.
* @return The manager, for instruction chaining.
*/
public TweenManager Add(TweenGroup tweenGroup) {
while (tweenGroup.Tweens.Count > 0)
Add(tweenGroup.Tweens[0]);
tweenGroup.Tweens.RemoveAt(0);
return this;
}
/**
* Clears the manager from every tween.
*/
public void Clear() {
_tweens.Clear();
}
/**
* Returns true if the manager contains any valid tween associated to the
* given target.
*/
public bool Contains(ITweenable target) {
for (int i=0; i<_tweens.Count; i++) {
Tween tween = _tweens[i];
if (tween.GetTarget() == target && !tween.GetFinished())
return true;
}
return false;
}
/**
* Returns true if the manager contains any valid tween associated to the
* given target and tween type.
*/
public bool Contains(ITweenable target, PositionType tweenType) {
for (int i=0; i<_tweens.Count; i++) {
Tween tween = _tweens[i];
if (tween.GetTarget() == target && tween.GetTweenType() == tweenType && !tween.GetFinished())
return true;
}
return false;
}
/**
* Kills every valid tween associated to the given target.
*/
public void Kill(ITweenable target) {
for (int i=0; i<_tweens.Count; i++) {
Tween tween = _tweens[i];
if (tween.GetTarget() == target && !tween.GetFinished())
tween.Kill();
}
}
/**
* Kills every valid tween associated to the given target and tween type.
*/
public void Kill(ITweenable target, PositionType tweenType) {
for (int i=0; i<_tweens.Count; i++) {
Tween tween = _tweens[i];
if (tween.GetTarget() == target && tween.GetTweenType() == tweenType && !tween.GetFinished())
tween.Kill();
}
}
/**
* Gets the number of tweens managed by this manager.
* @return The number of tweens in the manager.
*/
public int GetTweenCount() {
return _tweens.Count;
}
/**
* Gets an array containing every tween in the manager.
* <b>Warning:</b> this method allocates an array.
*/
public Tween[] GetTweens() {
return _tweens.ToArray();
}
/**
* Gets an array containing every tween in the manager dedicated to the
* given target.
* <b>Warning:</b> this method allocates an ArrayList and an array.
*/
public Tween[] GetTweens(ITweenable target) {
List<Tween> selectedTweens = new List<Tween>();
for (int i=0; i<_tweens.Count; i++) {
Tween tween = _tweens[i];
if (tween.GetTarget() == target && !tween.GetFinished())
selectedTweens.Add(tween);
}
return selectedTweens.ToArray();
}
/**
* Gets an array containing every tween in the manager dedicated to the
* given target and tween type.
* <b>Warning:</b> this method allocates an ArrayList and an array.
*/
public Tween[] GetTweens(ITweenable target, PositionType tweenType) {
List<Tween> selectedTweens = new List<Tween>();
for (int i=0; i<_tweens.Count; i++) {
Tween tween = _tweens[i];
if (tween.GetTarget() == target && tween.GetTweenType() == tweenType && !tween.GetFinished())
selectedTweens.Add(tween);
}
return selectedTweens.ToArray();
}
/**
* Updates every tween with the current time. Handles the tween life-cycle
* automatically. If a tween is finished, it will be removed from the
* manager.
*/
public void Update() {
long currentMillis = DateTime.Now.Millisecond;
for (int i=0; i<_tweens.Count; i++) {
Tween tween = _tweens[i];
if (tween.GetFinished()) {
_tweens.RemoveAt(i);
i -= 1;
}
tween.Update(currentMillis);
}
} }
}