summaryrefslogtreecommitdiff
path: root/Assets/Scripts/Unit/Components/UnitAnimation/PCAnimation.cs
blob: f80a83125541681bc99548400ecbb6be59840db9 (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
#define ANIM_CROSS_FADE
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PCAnimation : UnitAnimation
{

#if !ANIM_CROSS_FADE
    // 切换动画
    public enum ETrigger
    {
        ToIdle,
        ToMove,
        ToJump,
        ToAttack,
        ToAirAttack,
        ToLanding,
    }
#endif

	public enum ELayer
    {
        Basic = 0,
        Attack,
        SwordAttack,
        GunAttack,
        UpperBody,
        LowerBody,
        Count,
    }


    // 动作名,和animator里的state对应
    public enum EAnimState
    {
        // layer 0 
        Idle = 0,
        Move,
        Jump,
        Hit,
        Attack,
        Rise,
        Stinger,
        Turn,
        Landing,

        Attack0,
        Attack1,
        Attack2,
        Attack3,

        AttackToAir,

        AirAttack0,
        AirAttack1,
        AirAttack2,
        AirAttack3,
		AirAttack4,

		AirDash,

		LandingGround,
	}

    public override AnimatorLayerInfo baseLayer
    {
        get { return layers[(int)ELayer.Basic]; }
    }

    private UnitActionData m_ActionData;

    public bool applyRootMotion { get; set; }
    public bool applyRootCurve { get; set; } // 程序生成的root motion

	public bool updateAnimationAuto { get; private set; } // 自动更新动画

	private AnimatorOverrideController controller
	{
		get
		{
			Debug.Assert(owner.unitAnimation.animator.runtimeAnimatorController is AnimatorOverrideController);
			return owner.unitAnimation.animator.runtimeAnimatorController as AnimatorOverrideController;
		}
	}

    public override void Initialize()
    {
        base.Initialize();

        m_Animator = this.m_Owner.unitObj.GetComponent<Animator>();

        m_Animator.speed = 0;

        m_LayerInfo = new AnimatorLayerInfo[2];
        m_LayerInfo[0] = new AnimatorLayerInfo(this, m_Animator, (int)ELayer.Basic);
        //m_LayerInfo[1] = new AnimatorLayerInfo(this, m_Animator, ELayer.Attack);

        if (m_Animator == null)
        {
            LogHelper.LogError("没有挂Animator组件");
        }

        applyRootMotion = true;

		updateAnimationAuto = true;
	}

    public override void OnUpdate()
    {
        base.OnUpdate();

        UpdateLayer();
        UpdateAnimation();
        UpdateRootMotion();
        UpdateRootCurve();
    }

    void UpdateLayer()
    {
        m_LayerInfo[0].OnUpdate();
        return;
        for (int i = 0; i < m_LayerInfo.Length; ++i)
        {
            m_LayerInfo[i].OnUpdate();
        }
    }

    void UpdateAnimation()
    {
		if(updateAnimationAuto)
		{
			m_Animator.speed = 1;
			m_Animator.Update(Time.deltaTime);
			m_Animator.speed = 0;
		}
	}

    void UpdateRootMotion()
    {
		if (!applyRootMotion)
			return;

        m_Owner.unitRootMotion.UpdateRootMotion();
    }

    void UpdateRootCurve()
    {
		if (!applyRootCurve)
			return;
    }

	AnimationData GetAnimationDataOfState(string name)
	{
		string animName = controller[name].name;
		string path = owner.folder + "AnimationData/" + animName + ".asset";
		AnimationData data = ResourceManager.Instance.LoadAsset<AnimationData>(path);
		return data;
	}

	////////////////////////////////////////////////////////////////////////////////////////////////////////

	public void AnimIdle()
    {
        CrossFade(EAnimState.Idle, 0.2f, 0);
    }

    public void AnimMove()
    {
        CrossFade(EAnimState.Move, 0.01f, 0);
    }

    public void AnimJump()
    {
        CrossFade(EAnimState.Jump, 0.01f);
    }

    public void AnimAirAttack(int id)
    {
        m_Owner.unitCollider.OnAnimationChange();
		EAnimState state = EAnimState.AirAttack0;
		if (id == 1) state = EAnimState.AirAttack1;
		else if (id == 2) state = EAnimState.AirAttack2;
		else if (id == 3) state = EAnimState.AirAttack3;
		else if (id == 4) state = EAnimState.AirAttack4;

		AnimationData data = GetAnimationDataOfState(state.ToString());
		Debug.Assert(data != null);
		float offset = data.GetProperty(EAnimationProperty.ComboTimeOffset, 0);
		float duration = data.GetProperty(EAnimationProperty.TransitionInDuration, 0.1f);
		CrossFade(state, duration, offset);
	}

    public void AnimAttackToAir(float offset)
    {
        m_Owner.unitCollider.OnAnimationChange();
        Play(EAnimState.AttackToAir, offset);
    }

    public void AnimAttack(int id)
	{
        m_Owner.unitCollider.OnAnimationChange();
		switch (id)
		{
			case 0:
				CrossFade(EAnimState.Attack0, 0.02f);
				break;
			case 1:
				CrossFade(EAnimState.Attack1, 0.02f);
				break;
			case 2:
				CrossFade(EAnimState.Attack2, 0.02f);
				break;
			case 3:
				CrossFade(EAnimState.Attack3, 0.02f);
				break;
		}
	}

	public void AnimAirDash()
    {
        AnimationData data = GetAnimationDataOfState(EAnimState.AirDash.ToString());
        if (baseLayer.stateInfo.IsName(EAnimState.AirDash.ToString()))
        {
            float offset = data.GetProperty(EAnimationProperty.ComboTimeOffset, 0);
            this.Play(EAnimState.AirDash, offset);
        }
        else
        {
            CrossFade(EAnimState.AirDash, 0.02f);
        }
    }

    public void AnimLanding()
    {
        CrossFade(EAnimState.Landing, 0.05f);
    }

    public void AnimLandingGround()
    {
        CrossFade(EAnimState.LandingGround, 0.00f);
    }

    private void Play(EAnimState animState, float normalizedTime = float.NegativeInfinity, int layerIndex = 0)
    {
        base.Play(animState.ToString(), layerIndex, normalizedTime);
	}

	private void CrossFade(EAnimState animState, float normalizedTransitionDuration, float normalizedTimeOffset = float.NegativeInfinity, float normalizedTransitionTime = 0.0f, int layerIndex = 0)
	{
        base.CrossFade(animState.ToString(), normalizedTransitionDuration, layerIndex, normalizedTimeOffset, normalizedTransitionTime);
	}

}