diff options
Diffstat (limited to 'Assets/Scripts/Unit/Components/UnitAnimation/PCAnimation.cs')
-rw-r--r-- | Assets/Scripts/Unit/Components/UnitAnimation/PCAnimation.cs | 221 |
1 files changed, 221 insertions, 0 deletions
diff --git a/Assets/Scripts/Unit/Components/UnitAnimation/PCAnimation.cs b/Assets/Scripts/Unit/Components/UnitAnimation/PCAnimation.cs new file mode 100644 index 00000000..41f9e8b6 --- /dev/null +++ b/Assets/Scripts/Unit/Components/UnitAnimation/PCAnimation.cs @@ -0,0 +1,221 @@ +#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, + + AirAttack0, + AirAttack1, + AirAttack2, + AirAttack3, + + Attack0, + Attack1, + Attack2, + Attack3, + + AirDash, + + LandingGround, + } + + 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, ELayer.Basic.ToString()); + //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() + { + this.CrossFade(EAnimState.Idle, 0.2f, 0); + } + + public void AnimMove() + { + this.CrossFade(EAnimState.Move, 0.01f, 0); + } + + public void AnimJump() + { + this.CrossFade(EAnimState.Jump, 0.01f); + } + + public void AnimAirAttack(int id) + { + m_Owner.unitCollider.OnAnimationChange(); + switch (id) + { + case 0: + this.CrossFade(EAnimState.AirAttack0, 0.05f); + break; + case 1: + this.CrossFade(EAnimState.AirAttack1, 0.05f); + break; + case 2: + this.CrossFade(EAnimState.AirAttack2, 0.05f); + break; + case 3: + this.CrossFade(EAnimState.AirAttack3, 0.05f); + break; + } + } + + public void AnimAttack(int id) + { + m_Owner.unitCollider.OnAnimationChange(); + switch (id) + { + case 0: + this.CrossFade(EAnimState.Attack0, 0.05f); + break; + case 1: + this.CrossFade(EAnimState.Attack1, 0.05f); + break; + case 2: + this.CrossFade(EAnimState.Attack2, 0.05f); + break; + case 3: + this.CrossFade(EAnimState.Attack3, 0.05f); + break; + } + } + + public void AnimAirDash() + { + if (layers[0].stateInfo.IsName("AirDash")) + { + this.Play(EAnimState.AirDash, 0, 0); + } + else + { + this.CrossFade(EAnimState.AirDash, 0.05f); + } + } + + public void AnimLanding() + { + this.CrossFade(EAnimState.Landing, 0.05f); + } + + public void AnimLandingGround() + { + this.CrossFade(EAnimState.LandingGround, 0.00f); + } + + private void Play(EAnimState animState, int layerIndex = 0, float normalizedTime = float.NegativeInfinity) + { + AnimatorLayerInfo layer = this.layers[layerIndex]; + if (layer == null) + return; + layer.OnPlay(animState.ToString(), normalizedTime); + } + + public void CrossFade(EAnimState animState, float normalizedTransitionDuration, int layerIndex = 0, float normalizedTimeOffset = float.NegativeInfinity, float normalizedTransitionTime = 0.0f) + { + AnimatorLayerInfo layer = this.layers[layerIndex]; + if (layer == null) + return; + layer.OnCrossFade(animState.ToString(), normalizedTransitionDuration, normalizedTimeOffset, normalizedTransitionTime); + } + +} |