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

public class PCAnimation : UnitAnimation
{
    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,

        AirAttack0,
        AirAttack1,
        AirAttack2,
        AirAttack3,

        Attack0,
        Attack1,
        Attack2,
        Attack3,
    }

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

    private UnitActionData m_ActionData;

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

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

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

        UpdateLayer();
        UpdateAnimation();

        if (applyRootMotion)
            UpdateRootMotion();
        if (applyRootCurve)
            UpdateRootCurve();
    }

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

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

    void UpdateRootMotion()
    {
        m_Owner.unitRootMotion.UpdateRootMotion();
    }

    void UpdateRootCurve()
    {
    }

    public void AnimIdle()
    {
#if ANIM_CROSS_FADE
        m_Animator.CrossFade("Idle", 0.2f, 0);
#else
        ResetAllTriggers();
        m_Animator.SetTrigger(ETrigger.ToIdle.ToString());
#endif
    }

    public void AnimMove()
    {
#if ANIM_CROSS_FADE
        m_Animator.CrossFade("Move", 0.01f, 0);
#else
        ResetAllTriggers();
        m_Animator.SetTrigger(ETrigger.ToMove.ToString());
#endif
    }

    public void AnimJump()
    {
#if ANIM_CROSS_FADE
        m_Animator.CrossFade("Jump", 0.01f);
#else
        ResetAllTriggers();
        SetTrigger(ETrigger.ToJump);
#endif
    }

    public void AnimAirAttack(int id)
    {
        m_Owner.unitCollider.OnAnimationChange();
#if ANIM_CROSS_FADE
        m_Animator.CrossFade("AirAttack" + id, 0.05f);
#else
        ResetAllTriggers();
        SetTrigger(ETrigger.ToAirAttack);
#endif
    }

	public void AnimAttack(int id)
	{
        m_Owner.unitCollider.OnAnimationChange();
		m_Animator.CrossFade("Attack" + id, 0.05f);
	}

	public void AnimAirDash()
    {
        if (layers[0].stateInfo.IsName("AirDash"))
        {
            m_Animator.Play("AirDash", 0, 0);
        }
        else
        {
            m_Animator.CrossFade("AirDash", 0.05f);
        }
    }

    public void AnimLanding()
    {
#if ANIM_CROSS_FADE
        m_Animator.CrossFade("Landing", 0.05f);
#else
        ResetAllTriggers();
        SetTrigger(ETrigger.ToLanding);
#endif
    }

    public void AnimLandingGround()
    {
#if ANIM_CROSS_FADE
        m_Animator.CrossFade("LandingGround", 0.00f);
#else
        ResetAllTriggers();
        SetTrigger(ETrigger.ToLanding);
#endif
    }

#if !ANIM_CROSS_FADE
    void ResetAllTriggers()
    {
        var values = Enum.GetValues(typeof(ETrigger));
        foreach (var e in values)
        {
            m_Animator.ResetTrigger(e.ToString());
        }
    }
#endif

    private void Play(string animState, float fade = 0.1f, float offset = 0f, int layer = 0)
    {
        m_Animator.CrossFade(animState, fade, layer, offset);
    }
}