From cf4e1f9833c810e18429ddf40f4bcf9052ff17ac Mon Sep 17 00:00:00 2001 From: chai Date: Sun, 29 Aug 2021 19:44:31 +0800 Subject: *monster --- .../Scripts/Unit/Action/WaitForActionReachEnd.cs | 6 +- Assets/Scripts/Unit/Component/MonsterAnimation.cs | 94 +++++ .../Unit/Component/MonsterAnimation.cs.meta | 11 + Assets/Scripts/Unit/Component/MonsterState.cs | 101 +++++ Assets/Scripts/Unit/Component/MonsterState.cs.meta | 11 + Assets/Scripts/Unit/Component/PCAnimation.cs | 210 ++++++++++ Assets/Scripts/Unit/Component/PCAnimation.cs.meta | 11 + Assets/Scripts/Unit/Component/PCState.cs | 432 +++++++++++++++++++++ Assets/Scripts/Unit/Component/PCState.cs.meta | 11 + Assets/Scripts/Unit/Component/UnitAnimation.cs | 223 +---------- Assets/Scripts/Unit/Component/UnitCollider.cs | 2 +- Assets/Scripts/Unit/Component/UnitState.cs | 423 +------------------- .../Scripts/Unit/Controller/MonsterController.cs | 27 ++ .../Unit/Controller/MonsterController.cs.meta | 11 + Assets/Scripts/Unit/Controller/PCController.cs | 11 +- Assets/Scripts/Unit/Controller/UnitController.cs | 11 +- Assets/Scripts/Unit/UnitActionData.cs | 2 +- Assets/Scripts/Unit/UnitRootMotion.cs | 4 +- 18 files changed, 946 insertions(+), 655 deletions(-) create mode 100644 Assets/Scripts/Unit/Component/MonsterAnimation.cs create mode 100644 Assets/Scripts/Unit/Component/MonsterAnimation.cs.meta create mode 100644 Assets/Scripts/Unit/Component/MonsterState.cs create mode 100644 Assets/Scripts/Unit/Component/MonsterState.cs.meta create mode 100644 Assets/Scripts/Unit/Component/PCAnimation.cs create mode 100644 Assets/Scripts/Unit/Component/PCAnimation.cs.meta create mode 100644 Assets/Scripts/Unit/Component/PCState.cs create mode 100644 Assets/Scripts/Unit/Component/PCState.cs.meta create mode 100644 Assets/Scripts/Unit/Controller/MonsterController.cs create mode 100644 Assets/Scripts/Unit/Controller/MonsterController.cs.meta (limited to 'Assets/Scripts/Unit') diff --git a/Assets/Scripts/Unit/Action/WaitForActionReachEnd.cs b/Assets/Scripts/Unit/Action/WaitForActionReachEnd.cs index 47aa639c..c2dd5f92 100644 --- a/Assets/Scripts/Unit/Action/WaitForActionReachEnd.cs +++ b/Assets/Scripts/Unit/Action/WaitForActionReachEnd.cs @@ -6,9 +6,9 @@ using UnityEngine; public class WaitForActionReachEnd : IEnumerator { UnitAnimation m_UnitAnimation; - UnitAnimation.ELayer m_Layer; + int m_Layer; - public WaitForActionReachEnd(UnitAnimation unitAnim, UnitAnimation.ELayer layer = UnitAnimation.ELayer.Basic) + public WaitForActionReachEnd(UnitAnimation unitAnim, int layer = 0) { m_UnitAnimation = unitAnim; m_Layer = layer; @@ -18,7 +18,7 @@ public class WaitForActionReachEnd : IEnumerator public bool MoveNext() { - var stateInfo = m_UnitAnimation.layers[(int)m_Layer].stateInfo; + var stateInfo = m_UnitAnimation.layers[m_Layer].stateInfo; float normalTime = stateInfo.normalizedTime; return normalTime < 1f; } diff --git a/Assets/Scripts/Unit/Component/MonsterAnimation.cs b/Assets/Scripts/Unit/Component/MonsterAnimation.cs new file mode 100644 index 00000000..f536c5ef --- /dev/null +++ b/Assets/Scripts/Unit/Component/MonsterAnimation.cs @@ -0,0 +1,94 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class MonsterAnimation : UnitAnimation +{ + public enum ELayer + { + Basic = 0, + + 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, + } + + public override void Initialize() + { + base.Initialize(); + + m_Animator = this.m_Owner.unitObj.GetComponent(); + + 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组件"); + } + + } + + public override void OnUpdate() + { + base.OnUpdate(); + + UpdateLayer(); + UpdateAnimation(); + UpdateRootMotion(); + } + + 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(); + } + + + public void AnimIdle() + { + m_Animator.CrossFade("Idle", 0.2f, 0); + } +} diff --git a/Assets/Scripts/Unit/Component/MonsterAnimation.cs.meta b/Assets/Scripts/Unit/Component/MonsterAnimation.cs.meta new file mode 100644 index 00000000..448e6a1a --- /dev/null +++ b/Assets/Scripts/Unit/Component/MonsterAnimation.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: cfaa5c94122a41d4f8fa844112514102 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Unit/Component/MonsterState.cs b/Assets/Scripts/Unit/Component/MonsterState.cs new file mode 100644 index 00000000..2beba0d5 --- /dev/null +++ b/Assets/Scripts/Unit/Component/MonsterState.cs @@ -0,0 +1,101 @@ +using System; +using System.Reflection; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class MonsterState : UnitState +{ + public enum EUnitState + { + Nien, + + Idle, + + Move, + + HitAir, + HitGround, + HitFall, + KnockDown, + Rise, + + Pull, + Landing, + } + + [SerializeField] private EUnitState m_State; + public EUnitState CurrentState { get { return m_State; } } + + UnitAnimation unitAnimation { get { return m_Owner.unitAnimation; } } + + public override void Initialize() + { + base.Initialize(); + } + + public void ChangeState(EUnitState nextState, T param = default, bool bForce = false) + { + if (!IsChange(nextState, bForce)) + return; + + LogHelper.Log("Monster UnitState: " + m_State.ToString() + " -> " + nextState.ToString()); + + StopAllCoroutines(); + + EUnitState prevState = m_State; + string methodFunc = "On" + m_State.ToString() + "Exit"; + MethodInfo exitMethod = GetType().GetMethod(methodFunc, BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[] { typeof(EUnitState) }, null); + if (exitMethod != null) + { + exitMethod.Invoke(this, new object[] { nextState }); + } + else + { + LogHelper.LogError("缺少 " + methodFunc); + } + m_State = nextState; + + StartCoroutine(m_State.ToString(), param); + } + + bool IsChange(EUnitState newState, bool bForce) + { + if (newState != m_State || bForce) + return true; + return false; + } + + IEnumerator Nein() { yield break; } + void OnNienExit(EUnitState nextState) { } + + public struct IdleParam { } + + public struct LandingParam { } + + #region Idle + + IEnumerator Idle(IdleParam param) + { + if (m_Owner.isInAir) // 浮空切换到landing + { + ChangeState(EUnitState.Landing, new LandingParam()); + } + else // idle + { + m_Owner.SetYPosition(0); + m_Owner.monsterAnimation.AnimIdle(); + while (true) + { + yield return null; + } + } + } + + void OnIdleExit(EUnitState nextState) + { + } + #endregion + + +} \ No newline at end of file diff --git a/Assets/Scripts/Unit/Component/MonsterState.cs.meta b/Assets/Scripts/Unit/Component/MonsterState.cs.meta new file mode 100644 index 00000000..7d60499f --- /dev/null +++ b/Assets/Scripts/Unit/Component/MonsterState.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b7b1805198282374fbc67108671f8a72 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Unit/Component/PCAnimation.cs b/Assets/Scripts/Unit/Component/PCAnimation.cs new file mode 100644 index 00000000..3d88336d --- /dev/null +++ b/Assets/Scripts/Unit/Component/PCAnimation.cs @@ -0,0 +1,210 @@ +#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(); + + 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) + { +#if ANIM_CROSS_FADE + m_Animator.CrossFade("AirAttack" + id, 0.05f); +#else + ResetAllTriggers(); + SetTrigger(ETrigger.ToAirAttack); +#endif + } + + 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); + } +} diff --git a/Assets/Scripts/Unit/Component/PCAnimation.cs.meta b/Assets/Scripts/Unit/Component/PCAnimation.cs.meta new file mode 100644 index 00000000..6999e268 --- /dev/null +++ b/Assets/Scripts/Unit/Component/PCAnimation.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: f844378e1edf4b249b3b2f9a8e4a6842 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Unit/Component/PCState.cs b/Assets/Scripts/Unit/Component/PCState.cs new file mode 100644 index 00000000..594ae131 --- /dev/null +++ b/Assets/Scripts/Unit/Component/PCState.cs @@ -0,0 +1,432 @@ +using System; +using System.Reflection; +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +// 角色状态机 +[DisallowMultipleComponent] +public class PCState : UnitState +{ + public enum EUnitState + { + Nien, + + Idle, + Move, + Spawn, + Die, + Dead, + // + Attack, + AirAttack, + AirDash, + // + HitAir, + HitAirHit, + Knockdown, + // + HitGuard, + // + Walk, + // + Rise, + // + Jump, + // 转身 + Turn, + Landing, // 从空中降落 + } + + [SerializeField] private EUnitState m_State; + public EUnitState CurrentState { get { return m_State; } } + + public override void Initialize() + { + base.Initialize(); + } + + PCAnimation pcAnimation { get { return m_Owner.pcAnimation; } } + + #region state param + public struct IdleParam { } + + public struct MoveParam + { + public bool isRight; + public string key; + } + + public struct SkillParam + { + + } + + public struct AirDashParam + { + + } + + public struct JumpParam + { } + + public struct TurnParam + { + EUnitState nextState; + } + + public struct LandingParam { } + + #endregion + public void ChangeState(EUnitState nextState, T param = default, bool bForce = false) + { + if (!IsChange(nextState, bForce)) + return; + + LogHelper.Log("UnitState: " + m_State.ToString() + " -> " + nextState.ToString()); + + StopAllCoroutines(); + + EUnitState prevState = m_State; + string methodFunc = "On" + m_State.ToString() + "Exit"; + MethodInfo exitMethod = GetType().GetMethod(methodFunc, BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[] { typeof(EUnitState) }, null); + if (exitMethod != null) + { + exitMethod.Invoke(this, new object[] { nextState }); + } + else + { + LogHelper.LogError("缺少 " + methodFunc); + } + m_State = nextState; + + StartCoroutine(m_State.ToString(), param); + } + + bool IsChange(EUnitState newState, bool bForce) + { + if (newState != m_State || bForce) + return true; + return false; + } + + IEnumerator Nein() { yield break; } + void OnNienExit(EUnitState nextState) { } + + public void TurnAround(bool bRight) + { + m_Owner.transform.rotation = Quaternion.Euler(0, bRight ? 0 : 180, 0); + } + + public void TurnLeft() + { + TurnAround(false); + } + + public void TurnRight() + { + TurnAround(true); + } + + #region Idle + + IEnumerator Idle(IdleParam param) + { + if (m_Owner.isInAir) // 浮空切换到landing + { + ChangeState(EUnitState.Landing, new LandingParam()); + } + else // idle + { + m_Owner.SetYPosition(0); + m_Owner.pcAnimation.AnimIdle(); + while (true) + { + //if (Input.GetKeyDown("j")) + //{ + // ChangeState(EUnitState.Attack, new SkillParam()); + //} + if (Input.GetKeyDown("space")) + { + ChangeState(EUnitState.Jump, new JumpParam()); + } + if (Input.GetKey("d")) + { + MoveParam move = new MoveParam(); + move.isRight = true; + move.key = "d"; + ChangeState(EUnitState.Move, move); + } + if (Input.GetKey("a")) + { + MoveParam move = new MoveParam(); + move.isRight = false; + move.key = "a"; + ChangeState(EUnitState.Move, move); + } + yield return null; + } + } + } + + void OnIdleExit(EUnitState nextState) + { + } + #endregion + + #region Move + IEnumerator Move(MoveParam param) + { + if (m_Owner.isTowardRight && !param.isRight + || !m_Owner.isTowardRight && param.isRight) + { + //m_Owner.pcAnimation.Play(UnitAnimation.EAnimState.Turn); + //yield return new WaitForActionReachEnd(m_Owner.pcAnimation); + //if (param.isRight) + // m_Owner.transform.rotation = Quaternion.Euler(0, 0, 0); + //else + // m_Owner.transform.rotation = Quaternion.Euler(0, 180, 0); + m_Owner.transform.rotation = Quaternion.Euler(0, param.isRight ? 0 : 180, 0); + } + //if (Input.GetKey(param.key)) + m_Owner.pcAnimation.AnimMove(); + while (Input.GetKey(param.key)) + { + yield return null; + } + ChangeState(EUnitState.Idle, new IdleParam()); + } + + void OnMoveExit(EUnitState nextState) + { + m_Owner.pcAnimation.animator.ResetTrigger("ToMove"); + } + + #endregion + + #region Attack + + IEnumerator Attack(SkillParam param) + { + while (true) + { + + + yield return null; + } + } + + void OnAttackExit(EUnitState next) + { + + } + + #endregion + + #region AirAttack + + IEnumerator AirAttack(SkillParam param) + { + int id = 0; + m_Owner.pcAnimation.AnimAirAttack(id); + yield return null; // 等待animator更新 + while (true) + { + bool canCombo = m_Owner.pcAnimation.layers[0].IsToggleOpen(EAnimationToogle.Combo); + if (canCombo) + { + if (InputManager.Instance.TryCommand(0.5f, KeyCode.A, KeyCode.A)) + { + TurnLeft(); + ChangeState(EUnitState.AirDash, new AirDashParam()); + } + if (InputManager.Instance.TryCommand(0.5f, KeyCode.D, KeyCode.D)) + { + TurnRight(); + ChangeState(EUnitState.AirDash, new AirDashParam()); + } + + if (Input.GetKeyDown("j")) + { + if (Input.GetKey("a")) + { + TurnAround(false); + } + if (Input.GetKey("d")) + { + TurnAround(true); + } + ++id; + m_Owner.pcAnimation.AnimAirAttack(id); + yield return null; // 等待animator更新 + yield return new WaitForTransitionDone(m_Owner.pcAnimation); + } + } + + bool reachEnd = m_Owner.pcAnimation.layers[0].playbackNomralizedTime == 1; + if (reachEnd) + { + ChangeState(EUnitState.Landing, new LandingParam()); + } + + yield return null; + } + } + + void OnAirAttackExit(EUnitState next) + { + + } + + #endregion + + #region AirDash + + IEnumerator AirDash(AirDashParam param) + { + m_Owner.pcAnimation.AnimAirDash(); + yield return null; + while (true) + { + bool reachEnd = m_Owner.pcAnimation.layers[0].playbackNomralizedTime == 1; + if (reachEnd) + { + ChangeState(EUnitState.Landing, new LandingParam()); + } + + bool canCombo = m_Owner.pcAnimation.layers[0].IsToggleOpen(EAnimationToogle.Combo); + if (canCombo) + { + if (InputManager.Instance.TryCommand(0.5f, KeyCode.A, KeyCode.A)) + { + TurnLeft(); + m_Owner.pcAnimation.AnimAirDash(); + } + if (InputManager.Instance.TryCommand(0.5f, KeyCode.D, KeyCode.D)) + { + TurnRight(); + m_Owner.pcAnimation.AnimAirDash(); + } + + if (Input.GetKeyDown("j")) + { + if (Input.GetKey("a")) + { + TurnAround(false); + } + if (Input.GetKey("d")) + { + TurnAround(true); + } + ChangeState(EUnitState.AirAttack, new SkillParam()); + } + } + + yield return null; + } + } + + void OnAirDashExit(EUnitState next) + { + } + + #endregion + + #region Jump + + IEnumerator Jump(JumpParam param) + { + pcAnimation.AnimJump(); + yield return null; + yield return new WaitForTransitionDone(pcAnimation); + while (true) + { + if (InputManager.Instance.TryCommand(0.5f, KeyCode.A, KeyCode.A)) + { + TurnLeft(); + ChangeState(EUnitState.AirDash, new AirDashParam()); + } + if (InputManager.Instance.TryCommand(0.5f, KeyCode.D, KeyCode.D)) + { + TurnRight(); + ChangeState(EUnitState.AirDash, new AirDashParam()); + } + if (pcAnimation.layers[0].playbackNomralizedTime >= 1) + ChangeState(EUnitState.Idle, new IdleParam()); + bool canAttack = m_Owner.pcAnimation.layers[0].IsToggleOpen(EAnimationToogle.Combo); + if (Input.GetKeyDown("j") && canAttack) + { + ChangeState(EUnitState.AirAttack, new SkillParam()); + } + yield return null; + } + } + + void OnJumpExit(EUnitState next) + { + } + + #endregion + + #region Landing + + IEnumerator Landing(LandingParam param) + { + m_Owner.pcAnimation.AnimLanding(); + yield return null; + yield return new WaitForTransitionDone(m_Owner.pcAnimation); + float vy = 2; + float g = 9.8f; + bool landingGround = false; + float vz = 5; + while (true) + { + Vector3 pos = m_Owner.transform.position; + vy += g * Time.deltaTime; + pos.y -= vy * Time.deltaTime; + pos.y = Mathf.Max(0, pos.y); + if (Input.GetKey("a")) + { + TurnAround(false); + pos.x -= vz * Time.deltaTime; + } + if (Input.GetKey("d")) + { + TurnAround(true); + pos.x += vz * Time.deltaTime; + } + m_Owner.transform.position = pos; + if (pos.y > 0 && pos.y <= 1 && !landingGround) + { + landingGround = true; + m_Owner.pcAnimation.AnimLandingGround(); + } + if (pos.y <= 0) + { + pos.y = 0; + m_Owner.transform.position = pos; + ChangeState(EUnitState.Idle, new IdleParam()); + } + + if (InputManager.Instance.TryCommand(0.5f, KeyCode.A, KeyCode.A)) + { + TurnLeft(); + ChangeState(EUnitState.AirDash, new AirDashParam()); + } + if (InputManager.Instance.TryCommand(0.5f, KeyCode.D, KeyCode.D)) + { + TurnRight(); + ChangeState(EUnitState.AirDash, new AirDashParam()); + } + + yield return null; + } + } + + void OnLandingExit(EUnitState next) + { + } + + #endregion + +} diff --git a/Assets/Scripts/Unit/Component/PCState.cs.meta b/Assets/Scripts/Unit/Component/PCState.cs.meta new file mode 100644 index 00000000..548a0a91 --- /dev/null +++ b/Assets/Scripts/Unit/Component/PCState.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9a888cbca17562d4dbea1f28fd4dcbab +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Unit/Component/UnitAnimation.cs b/Assets/Scripts/Unit/Component/UnitAnimation.cs index 4532023d..3df9ef89 100644 --- a/Assets/Scripts/Unit/Component/UnitAnimation.cs +++ b/Assets/Scripts/Unit/Component/UnitAnimation.cs @@ -10,7 +10,7 @@ using UnityEditor; // 单独一层动画 public class AnimatorLayerInfo { - public UnitAnimation.ELayer layer; + public int layer; public int layerIndex { get { return (int)layer; } } @@ -178,7 +178,7 @@ public class AnimatorLayerInfo TimelineEventProxy m_TimelineEventProxy; - public AnimatorLayerInfo(UnitAnimation unitAnimation, Animator animator, UnitAnimation.ELayer layer) + public AnimatorLayerInfo(UnitAnimation unitAnimation, Animator animator, int layer) { this.m_UnitAnimation = unitAnimation; this.m_Animator = animator; @@ -231,65 +231,14 @@ public class AnimatorLayerInfo [DisallowMultipleComponent] public class UnitAnimation : UnitComponent { - 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, - } - - // 切换动画 - public enum ETrigger - { - ToIdle, - ToMove, - ToJump, - ToAttack, - ToAirAttack, - ToLanding, - } - - public Animator animator { get { return m_Animator; } } - private Animator m_Animator; - - private UnitActionData m_ActionData; - public AnimatorLayerInfo[] layers { get { return m_LayerInfo; } } - private readonly AnimatorLayerInfo[] m_LayerInfo = new AnimatorLayerInfo[(int)ELayer.Count]; + protected AnimatorLayerInfo[] m_LayerInfo; + + public Animator animator { get { return m_Animator; } } + protected Animator m_Animator; - public bool applyRootMotion { get; set; }// 动态设置root motion - public bool applyRootCurve { get; set; } // 程序生成的root motion - public bool isInTransition + public bool isInTransition { get { @@ -297,162 +246,4 @@ public class UnitAnimation : UnitComponent } } - public override void Initialize() - { - base.Initialize(); - - m_Animator = this.m_Owner.unitObj.GetComponent(); - - m_Animator.speed = 0; - - m_LayerInfo[0] = new AnimatorLayerInfo(this, m_Animator, 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 AnimAttack() - { - ResetAllTriggers(); - SetTrigger(ETrigger.ToAttack); - } - - void SetTrigger(ETrigger trigger) - { - ResetAllTriggers(); - m_Animator.SetTrigger(trigger.ToString()); - } - - public void AnimJump() - { -#if ANIM_CROSS_FADE - m_Animator.CrossFade("Jump", 0.01f); -#else - ResetAllTriggers(); - SetTrigger(ETrigger.ToJump); -#endif - } - - public void AnimAirAttack(int id) - { -#if ANIM_CROSS_FADE - m_Animator.CrossFade("AirAttack" + id, 0.05f); -#else - ResetAllTriggers(); - SetTrigger(ETrigger.ToAirAttack); -#endif - } - - 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 - } - - void ResetAllTriggers() - { - var values = Enum.GetValues(typeof(ETrigger)); - foreach(var e in values) - { - m_Animator.ResetTrigger(e.ToString()); - } - } - - private void Play(string animState, float fade = 0.1f, float offset = 0f, int layer = 0) - { - m_Animator.CrossFade(animState, fade, layer, offset); - } - } diff --git a/Assets/Scripts/Unit/Component/UnitCollider.cs b/Assets/Scripts/Unit/Component/UnitCollider.cs index 11da3603..8813ccef 100644 --- a/Assets/Scripts/Unit/Component/UnitCollider.cs +++ b/Assets/Scripts/Unit/Component/UnitCollider.cs @@ -31,7 +31,7 @@ public class UnitCollider : UnitComponent } // 返回当前激活的对应类型的碰撞盒数据 - public ColliderInfo[] GetCurrentBoxesInfoByType(ColliderBox.EColliderType type, UnitAnimation.ELayer layer = UnitAnimation.ELayer.Basic) + public ColliderInfo[] GetCurrentBoxesInfoByType(ColliderBox.EColliderType type, int layer = 0) { var layerInfo = m_Owner.unitAnimation.layers[0]; AnimationData animData = layerInfo.animationData; diff --git a/Assets/Scripts/Unit/Component/UnitState.cs b/Assets/Scripts/Unit/Component/UnitState.cs index 1de97901..2825f606 100644 --- a/Assets/Scripts/Unit/Component/UnitState.cs +++ b/Assets/Scripts/Unit/Component/UnitState.cs @@ -8,426 +8,5 @@ using UnityEngine; [DisallowMultipleComponent] public class UnitState : UnitComponent { - public enum EUnitState - { - Nien, - Idle , - Move , - Spawn , - Die , - Dead , - // - Attack , - AirAttack , - AirDash , - // - HitAir , - HitAirHit , - Knockdown , - // - HitGuard , - // - Walk , - // - Rise , - // - Jump , - // 转身 - Turn , - Landing , // 从空中降落 - } - - [SerializeField] private EUnitState m_State; - public EUnitState CurrentState { get { return m_State; } } - - public override void Initialize() - { - base.Initialize(); - - InitState(); - } - - UnitAnimation unitAnimation { get { return m_Owner.unitAnimation; } } - - #region state param - public struct IdleParam {} - - public struct MoveParam - { - public bool isRight; - public string key; - } - - public struct SkillParam - { - - } - - public struct AirDashParam - { - - } - - public struct JumpParam - { } - - public struct TurnParam - { - EUnitState nextState; - } - - public struct LandingParam { } - - #endregion - - void InitState() - { - } - - public void ChangeState(EUnitState nextState, T param = default, bool bForce = false) - { - if (!IsChange(nextState, bForce)) - return; - - LogHelper.Log("UnitState: " + m_State.ToString() + " -> " + nextState.ToString()); - - StopAllCoroutines(); - - EUnitState prevState = m_State; - string methodFunc = "On" + m_State.ToString() + "Exit"; - MethodInfo exitMethod = GetType().GetMethod(methodFunc, BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[] { typeof(EUnitState) }, null); - if(exitMethod != null) - { - exitMethod.Invoke(this, new object[] { nextState }); - } - else - { - LogHelper.LogError("缺少 " + methodFunc); - } - m_State = nextState; - - StartCoroutine(m_State.ToString(), param); - } - - bool IsChange(EUnitState newState, bool bForce) - { - if (newState != m_State || bForce) - return true; - return false; - } - - IEnumerator Nein() { yield break; } - void OnNienExit(EUnitState nextState) { } - - public void TurnAround(bool bRight) - { - m_Owner.transform.rotation = Quaternion.Euler(0, bRight ? 0 : 180, 0); - } - - public void TurnLeft() - { - TurnAround(false); - } - - public void TurnRight() - { - TurnAround(true); - } - - #region Idle - - IEnumerator Idle(IdleParam param) - { - if(m_Owner.isInAir) // 浮空切换到landing - { - ChangeState(EUnitState.Landing, new LandingParam()); - } - else // idle - { - m_Owner.SetYPosition(0); - m_Owner.unitAnimation.AnimIdle(); - while (true) - { - //if (Input.GetKeyDown("j")) - //{ - // ChangeState(EUnitState.Attack, new SkillParam()); - //} - if (Input.GetKeyDown("space")) - { - ChangeState(EUnitState.Jump, new JumpParam()); - } - if (Input.GetKey("d")) - { - InputManager.Instance.OnMoveRight(); - } - if (Input.GetKey("a")) - { - InputManager.Instance.OnMoveLeft(); - } - yield return null; - } - } - } - - void OnIdleExit(EUnitState nextState) - { - } - #endregion - - #region Move - IEnumerator Move(MoveParam param) - { - if (m_Owner.isTowardRight && !param.isRight - || !m_Owner.isTowardRight && param.isRight) - { - //m_Owner.unitAnimation.Play(UnitAnimation.EAnimState.Turn); - //yield return new WaitForActionReachEnd(m_Owner.unitAnimation); - //if (param.isRight) - // m_Owner.transform.rotation = Quaternion.Euler(0, 0, 0); - //else - // m_Owner.transform.rotation = Quaternion.Euler(0, 180, 0); - m_Owner.transform.rotation = Quaternion.Euler(0, param.isRight ? 0 : 180, 0); - } - //if (Input.GetKey(param.key)) - m_Owner.unitAnimation.AnimMove(); - while (Input.GetKey(param.key)) - { - yield return null; - } - ChangeState(EUnitState.Idle, new IdleParam()); - } - - void OnMoveExit(EUnitState nextState) - { - m_Owner.unitAnimation.animator.ResetTrigger("ToMove"); - } - - #endregion - - #region Attack - - IEnumerator Attack(SkillParam param) - { - while(true) - { - - - yield return null; - } - } - - void OnAttackExit(EUnitState next) - { - - } - - #endregion - - #region AirAttack - - IEnumerator AirAttack(SkillParam param) - { - int id = 0; - m_Owner.unitAnimation.AnimAirAttack(id); - yield return null; // 等待animator更新 - while (true) - { - bool canCombo = m_Owner.unitAnimation.layers[0].IsToggleOpen(EAnimationToogle.Combo); - if(canCombo) - { - if (InputManager.Instance.TryCommand(0.5f, KeyCode.A, KeyCode.A)) - { - TurnLeft(); - ChangeState(EUnitState.AirDash, new AirDashParam()); - } - if (InputManager.Instance.TryCommand(0.5f, KeyCode.D, KeyCode.D)) - { - TurnRight(); - ChangeState(EUnitState.AirDash, new AirDashParam()); - } - - if (Input.GetKeyDown("j")) - { - if (Input.GetKey("a")) - { - TurnAround(false); - } - if (Input.GetKey("d")) - { - TurnAround(true); - } - ++id; - m_Owner.unitAnimation.AnimAirAttack(id); - 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) - { - - } - - #endregion - - #region AirDash - - IEnumerator AirDash(AirDashParam param) - { - m_Owner.unitAnimation.AnimAirDash(); - yield return null; - while(true) - { - bool reachEnd = m_Owner.unitAnimation.layers[0].playbackNomralizedTime == 1; - if (reachEnd) - { - ChangeState(EUnitState.Landing, new LandingParam()); - } - - bool canCombo = m_Owner.unitAnimation.layers[0].IsToggleOpen(EAnimationToogle.Combo); - if(canCombo) - { - if (InputManager.Instance.TryCommand(0.5f, KeyCode.A, KeyCode.A)) - { - TurnLeft(); - m_Owner.unitAnimation.AnimAirDash(); - } - if (InputManager.Instance.TryCommand(0.5f, KeyCode.D, KeyCode.D)) - { - TurnRight(); - m_Owner.unitAnimation.AnimAirDash(); - } - - if (Input.GetKeyDown("j")) - { - if (Input.GetKey("a")) - { - TurnAround(false); - } - if (Input.GetKey("d")) - { - TurnAround(true); - } - ChangeState(EUnitState.AirAttack, new SkillParam()); - } - } - - yield return null; - } - } - - void OnAirDashExit(EUnitState next) - { - } - - #endregion - - #region Jump - - IEnumerator Jump(JumpParam param) - { - unitAnimation.AnimJump(); - yield return null; - yield return new WaitForTransitionDone(unitAnimation); - while (true) - { - if (InputManager.Instance.TryCommand(0.5f, KeyCode.A, KeyCode.A)) - { - TurnLeft(); - ChangeState(EUnitState.AirDash, new AirDashParam()); - } - if (InputManager.Instance.TryCommand(0.5f, KeyCode.D, KeyCode.D)) - { - TurnRight(); - ChangeState(EUnitState.AirDash, new AirDashParam()); - } - if (unitAnimation.layers[0].playbackNomralizedTime >= 1) - ChangeState(EUnitState.Idle, new IdleParam()); - bool canAttack = m_Owner.unitAnimation.layers[0].IsToggleOpen(EAnimationToogle.Combo); - if (Input.GetKeyDown("j") && canAttack) - { - ChangeState(EUnitState.AirAttack, new SkillParam()); - } - yield return null; - } - } - - void OnJumpExit(EUnitState next) - { - } - - #endregion - - #region Landing - - IEnumerator Landing(LandingParam param) - { - m_Owner.unitAnimation.AnimLanding(); - yield return null; - yield return new WaitForTransitionDone(m_Owner.unitAnimation); - float vy = 2; - float g = 9.8f; - bool landingGround = false; - float vz = 5; - while (true) - { - Vector3 pos = m_Owner.transform.position; - vy += g * Time.deltaTime; - pos.y -= vy * Time.deltaTime; - pos.y = Mathf.Max(0, pos.y); - if (Input.GetKey("a")) - { - TurnAround(false); - pos.x -= vz * Time.deltaTime; - } - if (Input.GetKey("d")) - { - TurnAround(true); - pos.x += vz * Time.deltaTime; - } - m_Owner.transform.position = pos; - if (pos.y > 0 && pos.y <= 1 && !landingGround) - { - landingGround = true; - m_Owner.unitAnimation.AnimLandingGround(); - } - if(pos.y <= 0) - { - pos.y = 0; - m_Owner.transform.position = pos; - ChangeState(EUnitState.Idle, new IdleParam()); - } - - if (InputManager.Instance.TryCommand(0.5f, KeyCode.A, KeyCode.A)) - { - TurnLeft(); - ChangeState(EUnitState.AirDash, new AirDashParam()); - } - if (InputManager.Instance.TryCommand(0.5f, KeyCode.D, KeyCode.D)) - { - TurnRight(); - ChangeState(EUnitState.AirDash, new AirDashParam()); - } - - yield return null; - } - } - - void OnLandingExit(EUnitState next) - { - } - - #endregion - -} +} \ No newline at end of file diff --git a/Assets/Scripts/Unit/Controller/MonsterController.cs b/Assets/Scripts/Unit/Controller/MonsterController.cs new file mode 100644 index 00000000..ba738dfc --- /dev/null +++ b/Assets/Scripts/Unit/Controller/MonsterController.cs @@ -0,0 +1,27 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class MonsterController : UnitController +{ + + public override void Initialize(GameObject obj, string folder) + { + base.Initialize(obj, folder); + + unitState = gameObject.GetOrAddComponent(); + unitState.Initialize(); + + unitAnimation = gameObject.GetOrAddComponent(); + unitAnimation.Initialize(); + } + + public override void Update() + { + base.Update(); + + + } + + +} diff --git a/Assets/Scripts/Unit/Controller/MonsterController.cs.meta b/Assets/Scripts/Unit/Controller/MonsterController.cs.meta new file mode 100644 index 00000000..4762487a --- /dev/null +++ b/Assets/Scripts/Unit/Controller/MonsterController.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ba16c8e0e6cac0c43b8a80d13dafdda1 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Unit/Controller/PCController.cs b/Assets/Scripts/Unit/Controller/PCController.cs index 5834470a..5ad3abc1 100644 --- a/Assets/Scripts/Unit/Controller/PCController.cs +++ b/Assets/Scripts/Unit/Controller/PCController.cs @@ -17,13 +17,16 @@ public class PCController : UnitController { base.Initialize(obj, folder); - } + unitState = gameObject.GetOrAddComponent(); + unitState.Initialize(); + + unitAnimation = gameObject.GetOrAddComponent(); + unitAnimation.Initialize(); + } - public override void Update() + public override void Update() { base.Update(); - - } } diff --git a/Assets/Scripts/Unit/Controller/UnitController.cs b/Assets/Scripts/Unit/Controller/UnitController.cs index 5523f256..b1db29d4 100644 --- a/Assets/Scripts/Unit/Controller/UnitController.cs +++ b/Assets/Scripts/Unit/Controller/UnitController.cs @@ -16,8 +16,13 @@ public class UnitController : MonoBehaviour/*, Interactable*/ public UnitRender unitRender; public UnitState unitState; + public PCState pcState { get { return unitState as PCState; } } + public MonsterState monsterState { get { return unitState as MonsterState; } } + public UnitAnimation unitAnimation; + public PCAnimation pcAnimation { get { return unitAnimation as PCAnimation; } } + public MonsterAnimation monsterAnimation { get { return unitAnimation as MonsterAnimation; } } public UnitSkill unitSkill; @@ -78,12 +83,6 @@ public class UnitController : MonoBehaviour/*, Interactable*/ unitRender = gameObject.GetOrAddComponent(); unitRender.Initialize(); - unitState = gameObject.GetOrAddComponent(); - unitState.Initialize(); - - unitAnimation = gameObject.GetOrAddComponent(); - unitAnimation.Initialize(); - unitSkill = gameObject.GetOrAddComponent(); unitSkill.Initialize(); diff --git a/Assets/Scripts/Unit/UnitActionData.cs b/Assets/Scripts/Unit/UnitActionData.cs index 8e45ca76..a2bb7a69 100644 --- a/Assets/Scripts/Unit/UnitActionData.cs +++ b/Assets/Scripts/Unit/UnitActionData.cs @@ -4,7 +4,7 @@ using System.Collections.Generic; using UnityEngine; [Serializable] -public class TriggerAnimationDictionary : SerializableDictionary { } +public class TriggerAnimationDictionary : SerializableDictionary { } // 配置角色的动画 diff --git a/Assets/Scripts/Unit/UnitRootMotion.cs b/Assets/Scripts/Unit/UnitRootMotion.cs index 8c6edaa0..c920010a 100644 --- a/Assets/Scripts/Unit/UnitRootMotion.cs +++ b/Assets/Scripts/Unit/UnitRootMotion.cs @@ -11,8 +11,6 @@ public class UnitRootMotion : UnitComponent { RootMotionData m_RootMotionData; - Dictionary m_RootMotionDic = new Dictionary(); - private float m_PrevNormalTime; public override void Initialize() @@ -22,6 +20,8 @@ public class UnitRootMotion : UnitComponent #if false // 用自定义root motion + Dictionary m_RootMotionDic = new Dictionary(); + public void Reset() { m_PrevNormalTime = 0; -- cgit v1.1-26-g67d0