From 9f447daa59fe9393904206499e6872068a11e7f8 Mon Sep 17 00:00:00 2001 From: chai Date: Thu, 26 Aug 2021 19:03:33 +0800 Subject: *misc --- Assets/Scripts/Unit/AnimationData.cs | 6 +- Assets/Scripts/Unit/Component/UnitAnimation.cs | 93 +++++++++++++++++------ Assets/Scripts/Unit/Component/UnitCollider.cs | 9 ++- Assets/Scripts/Unit/Component/UnitState.cs | 101 +++++++++++++++++++------ 4 files changed, 160 insertions(+), 49 deletions(-) (limited to 'Assets/Scripts/Unit') diff --git a/Assets/Scripts/Unit/AnimationData.cs b/Assets/Scripts/Unit/AnimationData.cs index 84cc1e4c..32845da1 100644 --- a/Assets/Scripts/Unit/AnimationData.cs +++ b/Assets/Scripts/Unit/AnimationData.cs @@ -174,8 +174,10 @@ public class AnimationData : ScriptableObject public bool IsToggleOpen(EAnimationToogle toggle, float normalizedTime) { if (!HasToggle(toggle)) - return false; - return toggles[toggle].to <= normalizedTime && normalizedTime >= toggles[toggle].from; + { + return false; + } + return toggles[toggle].to >= normalizedTime && normalizedTime >= toggles[toggle].from; } public ColliderInfo GetColliderInfo(ColliderBox.EColliderType type, int index, float playbackTime) diff --git a/Assets/Scripts/Unit/Component/UnitAnimation.cs b/Assets/Scripts/Unit/Component/UnitAnimation.cs index 7491d44c..5649206e 100644 --- a/Assets/Scripts/Unit/Component/UnitAnimation.cs +++ b/Assets/Scripts/Unit/Component/UnitAnimation.cs @@ -1,4 +1,5 @@ -using System.Collections; +using System; +using System.Collections; using System.Collections.Generic; using UnityEngine; #if UNITY_EDITOR @@ -53,7 +54,13 @@ public class AnimatorLayerInfo { get { - return m_Animator.GetCurrentAnimatorStateInfo(layerIndex); + AnimatorStateInfo stateInfo = m_Animator.GetCurrentAnimatorStateInfo(layerIndex); + AnimatorClipInfo[] clips = m_Animator.GetCurrentAnimatorClipInfo(layerIndex); + if(clips == null || clips.Length == 0) + { + stateInfo = m_Animator.GetNextAnimatorStateInfo(layerIndex); + } + return stateInfo; } } @@ -64,7 +71,13 @@ public class AnimatorLayerInfo { get { - return m_Animator.GetCurrentAnimatorClipInfo(layerIndex); + AnimatorClipInfo[] clips = m_Animator.GetCurrentAnimatorClipInfo(layerIndex); + if(clips.Length == 0) + { + // 如果transition设置了打断,会出现GetCurrentAnimatorClipinfo返回空数组的情况 + clips = m_Animator.GetNextAnimatorClipInfo(layerIndex); + } + return clips; } } @@ -103,12 +116,15 @@ public class AnimatorLayerInfo } } - // 播放进度百分比 + // 播放进度百分比,[0-1],是逻辑上的,如果动画不是loop,那么播放完后normalizedTime是1 public float playbackNomralizedTime { get { - return stateInfo.normalizedTime; + //return stateInfo.normalizedTime % 1f; + if (!stateInfo.loop && stateInfo.normalizedTime > 1) + return 1; + return stateInfo.normalizedTime % 1f; } } @@ -185,13 +201,20 @@ public class AnimatorLayerInfo } } + public bool IsToggleOpen(EAnimationToogle toggle) + { + if (m_AnimationData == null) + return false; + return m_AnimationData.IsToggleOpen(toggle, playbackNomralizedTime); + } + public void OnUpdate() { - // 执行事件 - m_TimelineEventProxy.ExecuteAnimationEvents(animationData, playbackTimeInSeconds * TimelineEventProxy.FPS); + // 执行事件 + m_TimelineEventProxy.ExecuteAnimationEvents(animationData, playbackTimeInSeconds * TimelineEventProxy.FPS); - // 播放速度控制 - if(applySpeedCurve && animationData.speedCurve != null) + // 播放速度控制 + if (applySpeedCurve && animationData != null && animationData.speedCurve != null) { playbackSpeed = animationData.speedCurve.Evaluate(playbackNomralizedTime); } @@ -231,6 +254,16 @@ public class UnitAnimation : UnitComponent Stinger, Turn, Landing, + + AirAttack0, + AirAttack1, + AirAttack2, + AirAttack3, + + Attack0, + Attack1, + Attack2, + Attack3, } // 切换动画 @@ -238,9 +271,10 @@ public class UnitAnimation : UnitComponent { ToIdle, ToMove, - ToAttack, ToJump, - ToAirAttack, + ToAttack, + ToAirAttack, + ToLanding, } public Animator animator { get { return m_Animator; } } @@ -268,7 +302,7 @@ public class UnitAnimation : UnitComponent m_Animator.speed = 0; m_LayerInfo[0] = new AnimatorLayerInfo(this, m_Animator, ELayer.Basic); - m_LayerInfo[1] = new AnimatorLayerInfo(this, m_Animator, ELayer.Attack); + //m_LayerInfo[1] = new AnimatorLayerInfo(this, m_Animator, ELayer.Attack); if (m_Animator == null) { @@ -309,38 +343,53 @@ public class UnitAnimation : UnitComponent public void AnimIdle() { - if(layers[0].stateInfo.shortNameHash != Animator.StringToHash("Idle")) - { - m_Animator.SetTrigger(ETrigger.ToIdle.ToString()); - } + ResetAllTriggers(); + m_Animator.SetTrigger(ETrigger.ToIdle.ToString()); } public void AnimMove() { - if (layers[0].stateInfo.shortNameHash != Animator.StringToHash("Move")) - { - m_Animator.SetTrigger(ETrigger.ToMove.ToString()); - } + ResetAllTriggers(); + m_Animator.SetTrigger(ETrigger.ToMove.ToString()); } public void AnimAttack() { + ResetAllTriggers(); SetTrigger(ETrigger.ToAttack); } void SetTrigger(ETrigger trigger) { + ResetAllTriggers(); m_Animator.SetTrigger(trigger.ToString()); } public void AnimJump() { - SetTrigger(ETrigger.ToJump); + ResetAllTriggers(); + SetTrigger(ETrigger.ToJump); } public void AnimAirAttack() { - SetTrigger(ETrigger.ToAirAttack); + ResetAllTriggers(); + SetTrigger(ETrigger.ToAirAttack); } + public void AnimLanding() + { + ResetAllTriggers(); + SetTrigger(ETrigger.ToLanding); + } + + void ResetAllTriggers() + { + var values = Enum.GetValues(typeof(ETrigger)); + foreach(var e in values) + { + m_Animator.ResetTrigger(e.ToString()); + } + } + } diff --git a/Assets/Scripts/Unit/Component/UnitCollider.cs b/Assets/Scripts/Unit/Component/UnitCollider.cs index 4f2c5ef3..11da3603 100644 --- a/Assets/Scripts/Unit/Component/UnitCollider.cs +++ b/Assets/Scripts/Unit/Component/UnitCollider.cs @@ -33,9 +33,14 @@ public class UnitCollider : UnitComponent // 返回当前激活的对应类型的碰撞盒数据 public ColliderInfo[] GetCurrentBoxesInfoByType(ColliderBox.EColliderType type, UnitAnimation.ELayer layer = UnitAnimation.ELayer.Basic) { - var layerInfo = m_Owner.unitAnimation.layers[(int)layer]; + var layerInfo = m_Owner.unitAnimation.layers[0]; AnimationData animData = layerInfo.animationData; - float playbackTime = layerInfo.playbackNomralizedTime * layerInfo.clipInfo[0].clip.length; + AnimatorClipInfo[] clipInfos = layerInfo.clipInfo; + //if(clipInfos == null || clipInfos.Length == 0) + //{ + // return null; + //} + float playbackTime = layerInfo.playbackNomralizedTime * clipInfos[0].clip.length; //float playbackTime = layerInfo.playbackRealTimeInSeconds; ColliderInfo[] infos = animData.GetActiveCollidersInfo(type, playbackTime); return infos; diff --git a/Assets/Scripts/Unit/Component/UnitState.cs b/Assets/Scripts/Unit/Component/UnitState.cs index 0ad1ec58..e27aa00b 100644 --- a/Assets/Scripts/Unit/Component/UnitState.cs +++ b/Assets/Scripts/Unit/Component/UnitState.cs @@ -17,7 +17,9 @@ public class UnitState : UnitComponent Spawn , Die , Dead , - Skill , + // + Attack , + AirAttack , // HitAir , HitAirHit , @@ -126,10 +128,10 @@ public class UnitState : UnitComponent m_Owner.unitAnimation.AnimIdle(); while (true) { - if (Input.GetKeyDown("j")) - { - ChangeState(EUnitState.Skill, new SkillParam()); - } + //if (Input.GetKeyDown("j")) + //{ + // ChangeState(EUnitState.Attack, new SkillParam()); + //} if (Input.GetKeyDown("space")) { ChangeState(EUnitState.Jump, new JumpParam()); @@ -183,26 +185,60 @@ public class UnitState : UnitComponent #endregion - #region Skill + #region Attack - IEnumerator Skill(SkillParam param) + IEnumerator Attack(SkillParam param) { - m_Owner.unitAnimation.AnimAttack(); - yield return new WaitForTransitionDone(m_Owner.unitAnimation); - yield return new WaitForActionReachEnd(m_Owner.unitAnimation); - ChangeState(EUnitState.Idle, new IdleParam()); - } + while(true) + { + - void OnSkillExit(EUnitState next) + yield return null; + } + } + + void OnAttackExit(EUnitState next) { } - #endregion + #endregion + + #region AirAttack - #region Jump + IEnumerator AirAttack(SkillParam param) + { + m_Owner.unitAnimation.AnimAirAttack(); + while (true) + { + bool canAttack = m_Owner.unitAnimation.layers[0].IsToggleOpen(EAnimationToogle.Combo); + if(canAttack && Input.GetKeyDown("j") ) + { + m_Owner.unitAnimation.AnimAirAttack(); + yield return null; // 等待animator更新 + yield return new WaitForTransitionDone(m_Owner.unitAnimation); + } + + bool reachEnd = m_Owner.unitAnimation.layers[0].playbackNomralizedTime == 1; + if (reachEnd) + { + ChangeState(EUnitState.Landing, new LandingParam()); + } + + yield return null; + } + } + + void OnAirAttackExit(EUnitState next) + { - IEnumerator Jump(JumpParam param) + } + + #endregion + + #region Jump + + IEnumerator Jump(JumpParam param) { unitAnimation.AnimJump(); yield return null; @@ -211,9 +247,10 @@ public class UnitState : UnitComponent { if (unitAnimation.layers[0].playbackNomralizedTime >= 1) ChangeState(EUnitState.Idle, new IdleParam()); - if (Input.GetKeyDown("j")) + bool canAttack = m_Owner.unitAnimation.layers[0].IsToggleOpen(EAnimationToogle.Combo); + if (Input.GetKeyDown("j") && canAttack) { - ChangeState(EUnitState.Skill, new SkillParam()); + ChangeState(EUnitState.AirAttack, new SkillParam()); } yield return null; } @@ -229,11 +266,29 @@ public class UnitState : UnitComponent IEnumerator Landing(LandingParam param) { - yield return new WaitForLanding(m_Owner); - Vector3 pos = m_Owner.transform.position; - pos.y = 0; - m_Owner.transform.position = pos; - ChangeState(EUnitState.Idle); + m_Owner.unitAnimation.AnimLanding(); + float vy = 0; + float g = 9.8f * 1.5f; + bool landingGround = false; + while (true) + { + Vector3 pos = m_Owner.transform.position; + vy += g * Time.deltaTime; + pos.y -= vy * Time.deltaTime; + pos.y = Mathf.Max(0, pos.y); + m_Owner.transform.position = pos; + Debug.Log(pos.y); + if(pos.y > 0 && pos.y <= 1 && !landingGround) + { + landingGround = true; + m_Owner.unitAnimation.AnimLanding(); + } + if(pos.y <= 0) + { + ChangeState(EUnitState.Idle, new IdleParam()); + } + yield return null; + } } void OnLandingExit(EUnitState next) -- cgit v1.1-26-g67d0