summaryrefslogtreecommitdiff
path: root/Assets/Scripts/Unit/AnimationData.cs
blob: 74f5eee10ab50824b59645a55fd5b9896b16cca5 (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
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif

// 某个动画的数据,包括帧事件、碰撞盒、速度曲线
[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;

    // 对应的进度的播放速度,默认是1
    public AnimationCurve curve;

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

    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 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);
        }
    }

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

}