-
Notifications
You must be signed in to change notification settings - Fork 74
/
Copy pathAnimator.cs
302 lines (257 loc) · 10 KB
/
Animator.cs
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Copyright (C) The original author or authors
//
// This software may be modified and distributed under the terms
// of the zlib license. See the LICENSE file for details.
using SpriterDotNet.Providers;
using System;
using System.Collections.Generic;
using System.Linq;
namespace SpriterDotNet
{
public abstract class Animator<TSprite, TSound>
{
/// <summary>
/// Occurs when the animation finishes playing or loops.
/// </summary>
public event Action<string> AnimationFinished = s => { };
/// <summary>
/// Occurs when an animation events gets triggered.
/// </summary>
public event Action<string> EventTriggered = s => { };
/// <summary>
/// The animated Entity.
/// </summary>
public SpriterEntity Entity { get; protected set; }
/// <summary>
/// The current animation.
/// </summary>
public SpriterAnimation CurrentAnimation { get; protected set; }
/// <summary>
/// The animation transitioned to or blended with the current animation.
/// </summary>
public SpriterAnimation NextAnimation { get; protected set; }
/// <summary>
/// The name of the current animation.
/// </summary>
public string Name { get; protected set; }
/// <summary>
/// Playback speed. Defaults to 1.0f. Negative values reverse the animation.<para />
/// For example:<para />
/// 0.5f corresponds to 50% of the default speed<para />
/// 2.0f corresponds to 200% of the default speed<para />
/// </summary>
public float Speed { get; set; }
/// <summary>
/// The legth of the current animation in milliseconds.
/// </summary>
public float Length { get; protected set; }
/// <summary>
/// The current time in milliseconds.
/// </summary>
public float Time { get; set; }
/// <summary>
/// Allow external class to check if an animation exists for a given name.
/// </summary>
public bool HasAnimation(string name) { return Animations.ContainsKey(name); }
/// <summary>
/// The current progress. Ranges from 0.0f - 1.0f.
/// </summary>
public float Progress
{
get { return Time / Length; }
set { Time = value * Length; }
}
/// <summary>
/// The provider of the animation data.
/// </summary>
public IFrameDataProvider DataProvider { get; set; }
/// <summary>
/// The provider of sprite assets.
/// </summary>
public IAssetProvider<TSprite> SpriteProvider { get; set; }
/// <summary>
/// The provider of sound assets.
/// </summary>
public IAssetProvider<TSound> SoundProvider { get; set; }
/// <summary>
/// The latest FrameData.
/// </summary>
public FrameData FrameData { get; protected set; }
protected Dictionary<string, SpriterAnimation> Animations { get; set; }
private float totalTransitionTime;
private float transitionTime;
private float factor;
protected Animator(SpriterEntity entity, IProviderFactory<TSprite, TSound> providerFactory = null)
{
Entity = entity;
Animations = entity.Animations.ToDictionary(a => a.Name, a => a);
Speed = 1.0f;
if (providerFactory != null)
{
DataProvider = providerFactory.GetDataProvider(entity);
SpriteProvider = providerFactory.GetSpriteProvider(entity);
SoundProvider = providerFactory.GetSoundProvider(entity);
}
else
{
DataProvider = new DefaultFrameDataProvider();
SpriteProvider = new DefaultAssetProvider<TSprite>();
SoundProvider = new DefaultAssetProvider<TSound>();
}
}
/// <summary>
/// Returns a list of all the animations for the entity
/// </summary>
public IEnumerable<string> GetAnimations()
{
return Animations.Keys;
}
/// <summary>
/// Plays the animation with the given name. Playback starts from the beginning.
/// </summary>
public virtual void Play(string name)
{
SpriterAnimation animation = Animations[name];
Play(animation);
}
/// <summary>
/// Plays the given animation. Playback starts from the beginning.
/// </summary>
public virtual void Play(SpriterAnimation animation)
{
Progress = 0;
CurrentAnimation = animation;
Name = animation.Name;
NextAnimation = null;
Length = CurrentAnimation.Length;
}
/// <summary>
/// Transitions to given animation doing a progressive blend in the given time.
/// <remarks>Animation blending works only for animations with identical hierarchies.</remarks>
/// </summary>
public virtual void Transition(string name, float totalTransitionTime)
{
this.totalTransitionTime = totalTransitionTime;
transitionTime = 0.0f;
factor = 0.0f;
NextAnimation = Animations[name];
}
/// <summary>
/// Blends two animations with the given weight factor. Factor ranges from 0.0f - 1.0f.<para />
/// Animation blending works only for animations with identical hierarchies.<para />
/// For example:<para />
/// factor == 0.0f corresponds to 100% of the first animation and 0% of the second<para />
/// factor == 0.25f corresponds to 75% of the first animation and 25% of the second<para />
/// factor == 0.5f corresponds to 50% of each animation<para />
/// </summary>
public virtual void Blend(string first, string second, float factor)
{
Play(first);
NextAnimation = Animations[second];
totalTransitionTime = 0;
this.factor = factor;
}
/// <summary>
/// Advances the animation for the deltaTime increment.
/// </summary>
public virtual void Update(float deltaTime)
{
if (CurrentAnimation == null) Play(Animations.Keys.First());
float initialTime = Time;
float elapsed = deltaTime * Speed;
if (NextAnimation != null && totalTransitionTime != 0.0f)
{
elapsed += elapsed * factor * CurrentAnimation.Length / NextAnimation.Length;
transitionTime += Math.Abs(elapsed);
factor = transitionTime / totalTransitionTime;
if (transitionTime >= totalTransitionTime)
{
float progress = Progress;
Play(NextAnimation.Name);
Progress = progress;
NextAnimation = null;
}
}
Time += elapsed;
if (Time < 0.0f)
{
if (CurrentAnimation.Looping) Time += Length;
else Time = 0.0f;
if (Time != initialTime) AnimationFinished(Name);
}
else if (Time >= Length)
{
if (CurrentAnimation.Looping) Time -= Length;
else Time = Length;
if (Time != initialTime) AnimationFinished(Name);
}
Animate(elapsed);
}
/// <summary>
/// Gets the transform information for all object types and calls the relevant apply method for each one.
/// </summary>
protected virtual void Animate(float deltaTime)
{
FrameData = DataProvider.GetFrameData(Time, deltaTime, factor, CurrentAnimation, NextAnimation);
for (int i = 0; i < FrameData.SpriteData.Count; ++i)
{
SpriterObject info = FrameData.SpriteData[i];
TSprite sprite = SpriteProvider.Get(info.FolderId, info.FileId);
if (sprite != null) ApplySpriteTransform(sprite, info);
}
for (int i = 0; i < FrameData.Sounds.Count; ++i)
{
SpriterSound info = FrameData.Sounds[i];
TSound sound = SoundProvider.Get(info.FolderId, info.FileId);
if (sound != null) PlaySound(sound, info);
}
var pointE = FrameData.PointData.GetEnumerator();
while (pointE.MoveNext())
{
var e = pointE.Current;
ApplyPointTransform(e.Key, e.Value);
}
var boxE = FrameData.BoxData.GetEnumerator();
while (boxE.MoveNext())
{
var e = boxE.Current;
ApplyBoxTransform(Entity.ObjectInfos[e.Key], e.Value);
}
for (int i = 0; i < FrameData.Events.Count; ++i)
{
DispatchEvent(FrameData.Events[i]);
}
}
/// <summary>
/// Applies the transform to the concrete sprite isntance.
/// </summary>
protected virtual void ApplySpriteTransform(TSprite sprite, SpriterObject info)
{
}
/// <summary>
/// Plays the concrete sound isntance.
/// </summary>
protected virtual void PlaySound(TSound sound, SpriterSound info)
{
}
/// <summary>
/// Applies the transforms for the point with the given name.
/// </summary>
protected virtual void ApplyPointTransform(string name, SpriterObject info)
{
}
/// <summary>
/// Applies the transform for the given box.
/// </summary>
protected virtual void ApplyBoxTransform(SpriterObjectInfo objInfo, SpriterObject info)
{
}
/// <summary>
/// Dispatches event when triggered in animation.
/// </summary>
protected virtual void DispatchEvent(string eventName)
{
EventTriggered(eventName);
}
}
}