From 01f06fb513dab303abeb02d7e1007e397874bcc0 Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 16 Nov 2020 21:08:39 +0800 Subject: *misc --- Assets/Scripts/Avatar/Avatar.cs | 2 + Assets/Scripts/Avatar/Avatar_Hurt.cs | 9 ++ Assets/Scripts/Avatar/HitDefination.cs | 13 ++- Assets/Scripts/Avatar/StateController.cs | 106 +++++++++++++++++++++++ Assets/Scripts/Avatar/StateController.cs.meta | 11 +++ Assets/Scripts/Avatar/StateSystem.cs | 106 ----------------------- Assets/Scripts/Avatar/StateSystem.cs.meta | 11 --- Assets/Scripts/Effects/Spark.cs | 36 ++++++++ Assets/Scripts/Effects/Spark.cs.meta | 11 +++ Assets/Scripts/Effects/SparkManager.cs | 18 ---- Assets/Scripts/Effects/SparkManager.cs.meta | 11 --- Assets/Scripts/Effects/SparksManager.cs | 91 +++++++++++++++++++ Assets/Scripts/Effects/SparksManager.cs.meta | 11 +++ Assets/Scripts/Test/ArmorSoldierScript_Anim.cs | 8 +- Assets/Scripts/Test/ArmorSoldierScript_States.cs | 20 +++-- Assets/Scripts/Test/SaionjiScript.cs | 1 - Assets/Scripts/Test/SaionjiScript_Effect.cs | 2 - Assets/Scripts/Test/SaionjiScript_States.cs | 18 ++-- 18 files changed, 318 insertions(+), 167 deletions(-) create mode 100644 Assets/Scripts/Avatar/StateController.cs create mode 100644 Assets/Scripts/Avatar/StateController.cs.meta delete mode 100644 Assets/Scripts/Avatar/StateSystem.cs delete mode 100644 Assets/Scripts/Avatar/StateSystem.cs.meta create mode 100644 Assets/Scripts/Effects/Spark.cs create mode 100644 Assets/Scripts/Effects/Spark.cs.meta delete mode 100644 Assets/Scripts/Effects/SparkManager.cs delete mode 100644 Assets/Scripts/Effects/SparkManager.cs.meta create mode 100644 Assets/Scripts/Effects/SparksManager.cs create mode 100644 Assets/Scripts/Effects/SparksManager.cs.meta (limited to 'Assets/Scripts') diff --git a/Assets/Scripts/Avatar/Avatar.cs b/Assets/Scripts/Avatar/Avatar.cs index 0ee7555f..8787b942 100644 --- a/Assets/Scripts/Avatar/Avatar.cs +++ b/Assets/Scripts/Avatar/Avatar.cs @@ -16,6 +16,8 @@ public partial class Avatar : MonoBehaviour, IInteractable public Hitbox[] m_Hitbox; public Hurtbox[] m_Hurtbox; + public Transform m_Hips; + protected StateController m_StateController = new StateController(); // 预定义的state,角色必须定义的 diff --git a/Assets/Scripts/Avatar/Avatar_Hurt.cs b/Assets/Scripts/Avatar/Avatar_Hurt.cs index eebc9a78..c6b840b9 100644 --- a/Assets/Scripts/Avatar/Avatar_Hurt.cs +++ b/Assets/Scripts/Avatar/Avatar_Hurt.cs @@ -24,6 +24,15 @@ public partial class Avatar : MonoBehaviour, IInteractable break; } + // spark + if(hit.sparkName != string.Empty) + { + if(hit.sparkHostType == HitSparkHost.Center) + { + SparksManager.Instance.PlaySpark(hit.sparkName, m_Hips); + } + } + } } diff --git a/Assets/Scripts/Avatar/HitDefination.cs b/Assets/Scripts/Avatar/HitDefination.cs index db07a677..d5d68b23 100644 --- a/Assets/Scripts/Avatar/HitDefination.cs +++ b/Assets/Scripts/Avatar/HitDefination.cs @@ -14,6 +14,14 @@ public enum HitType Air, // 击飞 } +public enum HitSparkHost +{ + Center, // 受击者质心 + Hitpoint, // 重叠盒子中心 + Fixed, // 固定挂点sparkHost + WorldPosition, // 世界空间位置sparkRotation +} + /// /// 一个hit的定义,如果一个attack有多个hit,需要定义多个HitDef /// @@ -27,8 +35,9 @@ public class HitDefination // 特效 public string sparkName = string.Empty; // 特效 - public Transform sparkHost = null; // 特效挂点 - public Vector3 sparkPosition; // 特效位置(sparkHost为空时生效) + public HitSparkHost sparkHostType = HitSparkHost.Center; + public Transform sparkHost = null; // 特效挂点 + public Vector3 sparkPosition; // 特效位置 public Quaternion sparkRotation; // 特效旋转 public Vector3 sparkScale = Vector3.one; // 特效缩放 diff --git a/Assets/Scripts/Avatar/StateController.cs b/Assets/Scripts/Avatar/StateController.cs new file mode 100644 index 00000000..12ba6fe4 --- /dev/null +++ b/Assets/Scripts/Avatar/StateController.cs @@ -0,0 +1,106 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +/// +/// 每个角色拥有一个state system +/// +public class StateController +{ + /// + /// 当前执行的state + /// + private StateBase m_Currrent; + + public StateBase Current + { + get + { + return m_Currrent; + } + } + + private List m_States = new List(); + + private UberState m_UberState; + + public StateController() + { + } + + public void ForceStart(StateBase state) + { + if (state == null) + return; + + if (m_Currrent != null) + m_Currrent.OnExit(); + + m_Currrent = state; + m_Currrent.OnEnter(); + } + + public void SetUberState(UberState state) + { + m_UberState = state; + } + + public void AddState(StateBase state) + { + m_States.Add(state); + } + + public void OnUpdate() + { + if(m_Currrent != null) + { + m_Currrent.OnUpdate(); + } + if(m_UberState != null) + { + m_UberState.OnUpdate(); + } + } + + public void OnPhysicsUpdate() + { + if(m_Currrent != null) + { + m_Currrent.OnPhysicsUpdate(); + } + if(m_UberState != null) + { + m_UberState.OnPhysicsUpdate(); + } + } + + public void OnHit(HitInfo info) + { + if (m_Currrent != null) + m_Currrent.OnHit(info); + } + + public void OnHurt(HurtInfo info) + { + if (m_Currrent != null) + m_Currrent.OnHurt(info); + } + + public void SwitchToState(StateBase targetState) + { + if (m_Currrent != null) + m_Currrent.OnExit(); + m_Currrent = targetState; + m_Currrent.OnEnter(); + } + + // 获得当前击打如果有的话 + public Hit GetHit() + { + if (Current == null || !(Current is AttackState)) + return null; + AttackState state = Current as AttackState; + return state.GetHit(); + } + +} \ No newline at end of file diff --git a/Assets/Scripts/Avatar/StateController.cs.meta b/Assets/Scripts/Avatar/StateController.cs.meta new file mode 100644 index 00000000..8974eb37 --- /dev/null +++ b/Assets/Scripts/Avatar/StateController.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4dec6f0208c6e564980e1380ad151233 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Avatar/StateSystem.cs b/Assets/Scripts/Avatar/StateSystem.cs deleted file mode 100644 index 12ba6fe4..00000000 --- a/Assets/Scripts/Avatar/StateSystem.cs +++ /dev/null @@ -1,106 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -/// -/// 每个角色拥有一个state system -/// -public class StateController -{ - /// - /// 当前执行的state - /// - private StateBase m_Currrent; - - public StateBase Current - { - get - { - return m_Currrent; - } - } - - private List m_States = new List(); - - private UberState m_UberState; - - public StateController() - { - } - - public void ForceStart(StateBase state) - { - if (state == null) - return; - - if (m_Currrent != null) - m_Currrent.OnExit(); - - m_Currrent = state; - m_Currrent.OnEnter(); - } - - public void SetUberState(UberState state) - { - m_UberState = state; - } - - public void AddState(StateBase state) - { - m_States.Add(state); - } - - public void OnUpdate() - { - if(m_Currrent != null) - { - m_Currrent.OnUpdate(); - } - if(m_UberState != null) - { - m_UberState.OnUpdate(); - } - } - - public void OnPhysicsUpdate() - { - if(m_Currrent != null) - { - m_Currrent.OnPhysicsUpdate(); - } - if(m_UberState != null) - { - m_UberState.OnPhysicsUpdate(); - } - } - - public void OnHit(HitInfo info) - { - if (m_Currrent != null) - m_Currrent.OnHit(info); - } - - public void OnHurt(HurtInfo info) - { - if (m_Currrent != null) - m_Currrent.OnHurt(info); - } - - public void SwitchToState(StateBase targetState) - { - if (m_Currrent != null) - m_Currrent.OnExit(); - m_Currrent = targetState; - m_Currrent.OnEnter(); - } - - // 获得当前击打如果有的话 - public Hit GetHit() - { - if (Current == null || !(Current is AttackState)) - return null; - AttackState state = Current as AttackState; - return state.GetHit(); - } - -} \ No newline at end of file diff --git a/Assets/Scripts/Avatar/StateSystem.cs.meta b/Assets/Scripts/Avatar/StateSystem.cs.meta deleted file mode 100644 index 52e8267c..00000000 --- a/Assets/Scripts/Avatar/StateSystem.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 2cf426be55a8b8b48a1b794fa6938e94 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Effects/Spark.cs b/Assets/Scripts/Effects/Spark.cs new file mode 100644 index 00000000..5f8856c4 --- /dev/null +++ b/Assets/Scripts/Effects/Spark.cs @@ -0,0 +1,36 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Spark : MonoBehaviour +{ + public string Name; + public float LifeTime; + + private float time; + + public Transform Host; + + private void Awake() + { + } + + private void OnEnable() + { + time = 0; + } + + private void Update() + { + time += Time.deltaTime; + if(time > LifeTime) + { + SparksManager.Instance.CycleSpark(this); + } + if (Host != null) + { + transform.position = Host.position; + } + } + +} diff --git a/Assets/Scripts/Effects/Spark.cs.meta b/Assets/Scripts/Effects/Spark.cs.meta new file mode 100644 index 00000000..c1c05447 --- /dev/null +++ b/Assets/Scripts/Effects/Spark.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 35fa504215dc5194a944721d5cc378af +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Effects/SparkManager.cs b/Assets/Scripts/Effects/SparkManager.cs deleted file mode 100644 index 0146d2e6..00000000 --- a/Assets/Scripts/Effects/SparkManager.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; - -public class SparkManager : MonoBehaviour -{ - // Start is called before the first frame update - void Start() - { - - } - - // Update is called once per frame - void Update() - { - - } -} diff --git a/Assets/Scripts/Effects/SparkManager.cs.meta b/Assets/Scripts/Effects/SparkManager.cs.meta deleted file mode 100644 index af46cd69..00000000 --- a/Assets/Scripts/Effects/SparkManager.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 91eb1bc63c2a55144830875dedab3793 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Scripts/Effects/SparksManager.cs b/Assets/Scripts/Effects/SparksManager.cs new file mode 100644 index 00000000..6825f615 --- /dev/null +++ b/Assets/Scripts/Effects/SparksManager.cs @@ -0,0 +1,91 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using System; + +public class SparksManager : MonoBehaviour +{ + public static SparksManager Instance; + + public Spark[] SparkTemplates; + + public Transform Root_Pool; + + List m_Pool = new List(); + + private void Awake() + { + Instance = this; + } + + Spark GetSparkTemplate(string name) + { + foreach (var effect in SparkTemplates) + { + if (effect != null && effect.Name == name) + { + return effect; + } + } + return null; + } + + Spark RecycleSpark(string name) + { + foreach (var effect in m_Pool) + { + if (effect != null && effect.Name == name) + { + return effect; + } + } + return null; + } + + public void PlaySpark(string name, Vector3 position) + { + Spark effect = RecycleSpark(name); + if (effect == null) + { + Spark temp = GetSparkTemplate(name); + effect = UnityEngine.Object.Instantiate(temp); + } + else + { + m_Pool.Remove(effect); + } + + effect.Host = null; + effect.transform.position = position; + effect.transform.SetParent(this.transform); + effect.gameObject.SetActive(true); + } + + + public void PlaySpark(string name, Transform host) + { + Spark effect = RecycleSpark(name); + if (effect == null) + { + Spark temp = GetSparkTemplate(name); + effect = UnityEngine.Object.Instantiate(temp); + } + else + { + m_Pool.Remove(effect); + } + effect.Host = host; + effect.transform.position = host.position; + effect.transform.SetParent(this.transform); + effect.gameObject.SetActive(true); + } + + // 回收特效 + public void CycleSpark(Spark effect) + { + effect.gameObject.SetActive(false); + effect.transform.SetParent(Root_Pool); + m_Pool.Add(effect); + } + +} diff --git a/Assets/Scripts/Effects/SparksManager.cs.meta b/Assets/Scripts/Effects/SparksManager.cs.meta new file mode 100644 index 00000000..84d3b608 --- /dev/null +++ b/Assets/Scripts/Effects/SparksManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c1cf80a321371944a9a3e8dfbe7e5134 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Test/ArmorSoldierScript_Anim.cs b/Assets/Scripts/Test/ArmorSoldierScript_Anim.cs index cf21a504..fd632ab7 100644 --- a/Assets/Scripts/Test/ArmorSoldierScript_Anim.cs +++ b/Assets/Scripts/Test/ArmorSoldierScript_Anim.cs @@ -12,7 +12,9 @@ public partial class ArmorSoldierScript : Avatar, IInteractable int Anim_MidiumHurt; int Anim_HeavyHurt; int Anim_GroundHurt; - int Anim_AirHurt; + int Anim_AirHurt; + + int Anim_GetUp; void SetupAnim() @@ -26,6 +28,8 @@ public partial class ArmorSoldierScript : Avatar, IInteractable Anim_GroundHurt = Animator.StringToHash("Hurt_Ground"); Anim_AirHurt = Animator.StringToHash("Hurt_Air"); - } + Anim_GetUp = Animator.StringToHash("GetUp"); + + } } \ No newline at end of file diff --git a/Assets/Scripts/Test/ArmorSoldierScript_States.cs b/Assets/Scripts/Test/ArmorSoldierScript_States.cs index 04532dd2..3df583f2 100644 --- a/Assets/Scripts/Test/ArmorSoldierScript_States.cs +++ b/Assets/Scripts/Test/ArmorSoldierScript_States.cs @@ -23,17 +23,18 @@ public partial class ArmorSoldierScript : Avatar, IInteractable m_StateGroundHurt = groundHurt; m_StateAirHurt = airHurt; - - //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - // conditions - //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - ConditionInAir condInAir = new ConditionInAir(m_BodyCollider); - + HurtState getUp = new HurtState(m_Animator, Anim_GetUp); + + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + // conditions + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + ConditionInAir condInAir = new ConditionInAir(m_BodyCollider); //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // actions //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ActionSwitchState switchToIdle = new ActionSwitchState(m_StateController, idle); + ActionSwitchState switchToGetUp = new ActionSwitchState(m_StateController, getUp); Trigger trigger = null; @@ -49,9 +50,14 @@ public partial class ArmorSoldierScript : Avatar, IInteractable // air hurt ConditionMotionAtEnd airAtEnd = new ConditionMotionAtEnd(m_Animator, Anim_AirHurt); - trigger = new Trigger(And(airAtEnd, Not(condInAir)), switchToIdle); + trigger = new Trigger(And(airAtEnd, Not(condInAir)), switchToGetUp); airHurt.AddTrigger(trigger); + // getup + ConditionMotionAtEnd getUpEnd = new ConditionMotionAtEnd(m_Animator, Anim_GetUp); + trigger = new Trigger(getUpEnd, switchToIdle); + getUp.AddTrigger(trigger); + m_StateController.ForceStart(idle); } diff --git a/Assets/Scripts/Test/SaionjiScript.cs b/Assets/Scripts/Test/SaionjiScript.cs index ae8ee6ff..1178b42a 100644 --- a/Assets/Scripts/Test/SaionjiScript.cs +++ b/Assets/Scripts/Test/SaionjiScript.cs @@ -28,7 +28,6 @@ public partial class SaionjiScript : Avatar public EffectHandler[] Effects; - // Start is called before the first frame update void Start() { base.Init(); diff --git a/Assets/Scripts/Test/SaionjiScript_Effect.cs b/Assets/Scripts/Test/SaionjiScript_Effect.cs index f07ce915..54de744e 100644 --- a/Assets/Scripts/Test/SaionjiScript_Effect.cs +++ b/Assets/Scripts/Test/SaionjiScript_Effect.cs @@ -4,8 +4,6 @@ using UnityEngine; public partial class SaionjiScript : Avatar { - public Transform m_Hips; - public override Vector3 GetEffectPosition() { return m_Hips.position; diff --git a/Assets/Scripts/Test/SaionjiScript_States.cs b/Assets/Scripts/Test/SaionjiScript_States.cs index 0c733cd6..ce9fb8a6 100644 --- a/Assets/Scripts/Test/SaionjiScript_States.cs +++ b/Assets/Scripts/Test/SaionjiScript_States.cs @@ -40,13 +40,15 @@ public partial class SaionjiScript : Avatar AttackState airDash = new AttackState(animator, Anim_AirDash, m_Body); - AttackStateConfig config; - - //招式会绑定一个motion - HitDefination hitDef = new HitDefination - { + AttackStateConfig config; + + //招式会绑定一个motion + HitDefination hitDef = new HitDefination + { type = HitType.Air, - hurtAddForce = new Vector3(0, 20000, 0) + hurtAddForce = new Vector3(0, 20000, 0), + sparkName = "Bullet_WhiteSpark", + sparkHostType = HitSparkHost.Center }; AttackState attk1 = new AttackState(animator, Anim_LightAttack1, m_Body); attk1.AddHitDefination(hitDef); @@ -79,7 +81,9 @@ public partial class SaionjiScript : Avatar hitDef = new HitDefination { type = HitType.Air, - hurtAddForce = new Vector3(0,10000,0) + hurtAddForce = new Vector3(0,10000,0), + sparkName = "Bullet_WhiteSpark", + sparkHostType = HitSparkHost.Center }; airAttk1.AddHitDefination(hitDef); -- cgit v1.1-26-g67d0