summaryrefslogtreecommitdiff
path: root/Assets/Scripts/Unit/AnimationData.cs
blob: 7ec5ddd08aebf29228084ffbc76795763ad2534b (plain)
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif

// 不要修改枚举的顺序,只能在后面新加,不能删除

public enum EAnimationToogle
{
	Combo = 0, // 连击
	SuperArmor = 1, //霸体
	Break = 2, // 打断
}

[Serializable]
public struct FromTo
{
	[SerializeField] public float from;
	[SerializeField] public float to;
	public Vector2 fromTo
	{
		get
		{
			return new Vector2(from, to);
		}
		set
		{
			from = value.x;
			to = value.y;
		}
	}
	public FromTo(float from, float to)
	{
		this.from = from;
		this.to = to;
	}
	public FromTo(Vector2 fromTo)
	{
		this.from = fromTo.x;
		this.to = fromTo.y;
	}
}

[Serializable]
public class ToggleTimeDictionary : SerializableDictionary<EAnimationToogle, FromTo> { }

public enum EAnimationCurve
{
	TimeScale = 0,  // 
	RootMotionScale = 1,
}

[Serializable]
public class CurveDictionary : SerializableDictionary<EAnimationCurve, AnimationCurve> { }

[Serializable]
public struct AnimationParameter
{
	[Serializable]
	public struct Setter
	{
		[SerializeField] public float normalizedTime;
		[SerializeField] public float value;
	}
	[SerializeField] public List<Setter> setters;
}

public enum EAnimationParameter
{
	Visibility = 0,
}

[Serializable]
public class ParameterDictionary : SerializableDictionary<EAnimationParameter, AnimationParameter> { }

// 动画属性
public enum EAnimationProperty
{
	ComboTimeOffset      = 1, 
	IgnoreY              = 2, 
	TransitionInDuration = 3, 
    CanBeInterrupted     = 4, // 是否可以被打断
}

[Serializable]
public class PropertyDictionary : SerializableDictionary<EAnimationProperty, float> { }

[Serializable]
public class RootMotionOverrideData
{
    [Serializable]
    public class PosData
	{
		[SerializeField] public Vector3 position;
        [SerializeField] public int frame;
        public PosData(int frame, Vector3 pos)
        {
            this.frame = frame;
            this.position = pos;
        }
	}
    [SerializeField] public List<PosData> positions;
    public RootMotionOverrideData()
    {
        positions = new List<PosData>();
    }
    public Vector3 GetPosition(float frame)
    {
        if (positions == null || positions.Count == 0)
            return Vector3.zero;
        positions.Sort((p1, p2) => {
            if (p1.frame < p2.frame)
                return -1;
            if (p1.frame > p2.frame)
                return 1;
            return 0;
        });
        int prev = 0;
        int next = 0;
        for(int i = 0;i < positions.Count; ++i)
        {
            if(positions[i].frame > frame)
            {
                break;
            }
            prev = i;
            next = Mathf.Clamp(i + 1, 0, positions.Count - 1);
        }
        float t = 0;
        if(prev != next)
        {
            t = (frame - positions[prev].frame) / (positions[next].frame - positions[prev].frame);
        }
        Vector3 pos = Vector3.Lerp(positions[prev].position, positions[next].position, t);
        return pos;
    }

    public Vector3 GetRootMotionDistance(float prevFrame, float curFrame)
    {
        Vector3 p1 = GetPosition(prevFrame);
        Vector3 p2 = GetPosition(curFrame);
        return p2 - p1;
    }

    public void SetPosition(int frame, Vector3 position)
    {
        if(positions == null)
        {
            positions = new List<PosData>();
        }
        PosData pd = positions.Find(s => s.frame == frame);
        if(pd != null)
        {
            pd.position = position;
        }
        else
        {
            positions.Add(new PosData(frame, position));
        }
    }

    public void RemovePositionAtFrame(float frame)
    {
        if (positions == null)
            return;
        PosData pd = positions.Find(s => s.frame == frame);
        if(pd != null)
        {
            positions.Remove(pd);
        }
    }
}

// 某个动画的数据,包括帧事件、碰撞盒、速度曲线
[CreateAssetMenu(fileName = "Animation Data")]
public class AnimationData : ScriptableObject
{
	public string animationName;
	public string animationPath;

	public List<AnimationEventBase> animationEvents;

	public List<ColliderData> hurtBoxes;
	public List<ColliderData> hitBoxes;
	public List<ColliderData> throwBoxes;
	public List<ColliderData> blockBoxes;
	public List<ColliderData> defendBoxes;

	public bool overrideRootMotion;
	public RootMotionOverrideData rootMotionOverrideData;

	// 对应的进度的播放速度,默认是1
	[UnityEngine.Serialization.FormerlySerializedAs("curve")]
	public AnimationCurve speedCurve;

	public CurveDictionary curves;

	public ToggleTimeDictionary toggles;

	public ParameterDictionary parameters;

	public PropertyDictionary properties;

	public const int FPS = 30;

	public AnimationData()
	{
		Keyframe frame0 = new Keyframe(0, 1);
		Keyframe frame1 = new Keyframe(1, 1);
		speedCurve = new AnimationCurve(frame0, frame1);
	}

	public List<ColliderData> GetColliderBoxesByType(ColliderBox.EColliderType type)
	{
		switch (type)
		{
			case ColliderBox.EColliderType.HurtBox:
				return hurtBoxes;
			case ColliderBox.EColliderType.HitBox:
				return hitBoxes;
			case ColliderBox.EColliderType.BlockBox:
				return blockBoxes;
			case ColliderBox.EColliderType.ThrowBox:
				return throwBoxes;
			case ColliderBox.EColliderType.DefendBox:
				return defendBoxes;
		}
		return null;
	}

	public bool HasProperty(EAnimationProperty property)
	{
		if (!properties.ContainsKey(property))
			return false;
		return true;
	}

	public float GetProperty(EAnimationProperty property, float defaultValue = 0)
	{
		if (!HasProperty(property))
			return defaultValue;
		return properties[property];
	}

	public bool HasParameter(EAnimationParameter parameterName)
	{
		if (!parameters.ContainsKey(parameterName))
			return false;
		return true;
	}

	public float GetParameter(EAnimationParameter parameterName, float normalizedTime)
	{
		if (!parameters.ContainsKey(parameterName))
			return 0;
		var parameter = parameters[parameterName];
		parameter.setters.Sort((AnimationParameter.Setter a, AnimationParameter.Setter b) =>
		{
			return a.normalizedTime - b.normalizedTime < 0 ? -1 : 1;
		});
		float value = 0;
		for (int i = 0; i < parameter.setters.Count; ++i)
		{
			AnimationParameter.Setter setter = parameter.setters[i];
			if (setter.normalizedTime > normalizedTime)
				break;
			value = setter.value;
		}
		return value;
	}

	public bool HasCurve(EAnimationCurve curve)
	{
		return curves != null && curves.ContainsKey(curve);
	}

	public AnimationCurve GetCurve(EAnimationCurve curve)
	{
		if (!HasCurve(curve))
			return null;
		return curves[curve];
	}

	public bool HasToggle(EAnimationToogle toggle)
	{
		return toggles != null && toggles.ContainsKey(toggle);
	}

	public bool IsToggleOpen(EAnimationToogle toggle, float normalizedTime)
	{
		if (!HasToggle(toggle))
        {
            return false;
        }
        return toggles[toggle].to >= normalizedTime && normalizedTime >= toggles[toggle].from;
	}

    public ColliderInfo GetColliderInfo(ColliderBox.EColliderType type, int index, float playbackTime)
    {
        return GetColliderInfoByFrame(type, index, playbackTime * FPS);
    }

    public ColliderInfo GetColliderInfoByFrame(ColliderBox.EColliderType type, int index, float frame)
    {
        ColliderInfo info = new ColliderInfo();
        var colliders = GetColliderBoxesByType(type);
        if (colliders == null || colliders.Count <= index)
            return info; //info.isValid == false
        info = colliders[index].GetColliderInfo(frame);
        return info;
    }

    public ColliderInfo[] GetCollidersInfo(ColliderBox.EColliderType type, float playbackTime)
    {
        return GetCollidersInfoByFrame(type, playbackTime * FPS);
    }

    public ColliderInfo[] GetCollidersInfoByFrame(ColliderBox.EColliderType type, float frame)
    {
        var colliders = GetColliderBoxesByType(type);
        if (colliders == null || colliders.Count == 0)
            return null;
        ColliderInfo[] infos = new ColliderInfo[colliders.Count];
        for(int i = 0; i < colliders.Count; ++i)
        {
            infos[i] = colliders[i].GetColliderInfo(frame);
        }
        return infos;
    }

    public ColliderInfo[] GetActiveCollidersInfo(ColliderBox.EColliderType type, float playbackTime)
    {
        return GetActiveCollidersInfoByFrame(type , playbackTime * FPS);
    }

    public ColliderInfo[] GetActiveCollidersInfoByFrame(ColliderBox.EColliderType type, float frame)
    {
        var all = GetCollidersInfoByFrame(type, frame);
        if (all == null || all.Length == 0)
            return null;
        int activeCount = 0;
        foreach(var c in all)
        {
            if (c.active)
                activeCount++;
        }
        if (activeCount == 0)
            return null;
        ColliderInfo[] active = new ColliderInfo[activeCount];
        int i = 0;
        foreach(var c in all)
        {
            if(c.active)
            {
                active[i++] = c;
            }
        }
        return active;
    }

    public int GetBoxesCount()
    {
        int hurt = hurtBoxes != null ? hurtBoxes.Count : 0;
        int hit = hitBoxes != null ? hitBoxes.Count : 0;
        int thro = throwBoxes != null ? throwBoxes.Count : 0;
        int block = blockBoxes != null ? blockBoxes.Count : 0;
        int defend = defendBoxes != null ? defendBoxes.Count : 0;
        return hurt + hit + thro + block + defend;
    }

    public void AddBox(ref List<ColliderData> boxList, ColliderData box)
    {
        if (boxList == null)
        {
            boxList = new List<ColliderData>();
            return;
        }
        boxList.Add(box);
    }

    public void DeleteBox(ColliderData box)
    {
        if (hurtBoxes != null) hurtBoxes.Remove(box);
        if (hitBoxes != null) hitBoxes.Remove(box);
        if (throwBoxes != null) throwBoxes.Remove(box);
        if (blockBoxes != null) blockBoxes.Remove(box);
        if (defendBoxes != null) defendBoxes.Remove(box);
    }

    public ColliderData GetColliderByIndex(int index)
    {
        if (hurtBoxes != null && hurtBoxes.Count > index)
            return hurtBoxes[index];
        else
            index -= hurtBoxes.Count;
        if (hitBoxes != null && hitBoxes.Count > index)
            return hitBoxes[index];
        else
            index -= hitBoxes.Count;
        if (throwBoxes != null && throwBoxes.Count > index)
            return throwBoxes[index];
        else
            index -= throwBoxes.Count;
        if (blockBoxes != null && blockBoxes.Count > index)
            return blockBoxes[index];
        else
            index -= blockBoxes.Count;
        if (defendBoxes != null && defendBoxes.Count > index)
            return defendBoxes[index];
        else
            index -= defendBoxes.Count;
        return null;
    }

    public void AddEvent(AnimationEventBase animEvent)
    {
        if (this.animationEvents == null)
            this.animationEvents = new List<AnimationEventBase>();
        animationEvents.Add(animEvent);
    }

    public List<AnimationEventBase> GetAnimationEventsAtFrame(int frame)
    {
        if (animationEvents == null)
            return null;

        List<AnimationEventBase> events = ListPool<AnimationEventBase>.Get();
        events.Clear();
        foreach (var animeEvent in animationEvents)
        {
            if(animeEvent.startFrame == frame)
            {
                events.Add(animeEvent);
            }
        }
        return events;
    }

    public int GetMaxAnimationEventsCount()
    {
        List<int> frames = GetAnimationEventFrameIndices();
        if(frames == null)
        {
            ListPool<int>.Release(frames);
            return 0;
        }
        int count = 0;
        for(int i = 0; i < frames.Count; ++i)
        {
            List<AnimationEventBase> events = GetAnimationEventsAtFrame(frames[i]);
            if (count < events.Count)
            {
                count = events.Count;
            }
            ListPool<AnimationEventBase>.Release(events);
        }
        ListPool<int>.Release(frames);
        return count;
    }

    public List<int> GetAnimationEventFrameIndices()
    {
        if (animationEvents == null)
            return null;

        List<int> frames = ListPool<int>.Get();
        frames.Clear();
        foreach (var animeEvent in animationEvents)
        {
            if (!frames.Contains(animeEvent.startFrame))
            {
                frames.Add(animeEvent.startFrame);
            }
        }
        return frames;
    }

    public void DeleteEvent(AnimationEventBase animEvent)
    {
        if(animationEvents.Contains(animEvent))
        {
            animationEvents.Remove(animEvent);
        }
    }

    public void AddRootMotionOverriderData( )
    {
        this.overrideRootMotion = true;
        this.rootMotionOverrideData = new RootMotionOverrideData();
    }

    public void DeleteRootMotionOverrideData()
    {
        this.overrideRootMotion = false;
        this.rootMotionOverrideData = null;
    }

#if UNITY_EDITOR
    public void OnSaveToDisk()
    {
        foreach(var animEvent in animationEvents)
        {
            if(!AssetDatabase.IsSubAsset(animEvent))
            {
                AssetDatabase.AddObjectToAsset(animEvent, this);
            }
        }
    }
#endif

}