diff --git a/Assets/Scripts/勍/EntryTrigger.cs b/Assets/Scripts/勍/EntryTrigger.cs new file mode 100644 index 0000000..3e9fff8 --- /dev/null +++ b/Assets/Scripts/勍/EntryTrigger.cs @@ -0,0 +1,31 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +/// +/// 进入式触发器的基类,继承自事件,每一个进入式触发器都必须继承这个类 +/// +public class EntryTrigger : Event +{ + private Collider2D m_collider; + + void Start() + { + //如果没有挂载碰撞盒,则报错 + if(!TryGetComponent(out m_collider)) + { + Debug.LogError(this.GetType() + gameObject.name + "没有挂载碰撞盒,请检查!"); + } + //如果不是触发器,则报错 + if(!m_collider.isTrigger) + { + Debug.LogError(this.GetType() + gameObject.name + "碰撞盒没有设置为触发器,请检查!"); + } + } + + virtual public void OnTriggerEnter2D(Collider2D other) + { + //如果玩家进入碰撞盒则触发事件 + if(other.TryGetComponent(out Player player)) OnCall(); + } +} diff --git a/Assets/Scripts/勍/EntryTrigger.cs.meta b/Assets/Scripts/勍/EntryTrigger.cs.meta new file mode 100644 index 0000000..4d6f486 --- /dev/null +++ b/Assets/Scripts/勍/EntryTrigger.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a08702285d1216941b48b6d42e06bf82 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/勍/Event.cs b/Assets/Scripts/勍/Event.cs new file mode 100644 index 0000000..d2889f1 --- /dev/null +++ b/Assets/Scripts/勍/Event.cs @@ -0,0 +1,24 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +/// +/// 事件基类,事件需要继承这个类 +/// +public class Event : MonoBehaviour +{ + /// + /// 事件触发 + /// + public virtual void OnCall(){} + + /// + /// 要手动终止事件的时候调用 + /// + public virtual void CancleEvent(){ OnCancle(); } + + /// + /// 事件结束时触发 + /// + protected virtual void OnCancle(){} +} diff --git a/Assets/Scripts/勍/Event.cs.meta b/Assets/Scripts/勍/Event.cs.meta new file mode 100644 index 0000000..1a4248e --- /dev/null +++ b/Assets/Scripts/勍/Event.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 17fed7225ff8045449129f8106e9d559 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/勍/StateBase.cs b/Assets/Scripts/勍/StateBase.cs new file mode 100644 index 0000000..dfef886 --- /dev/null +++ b/Assets/Scripts/勍/StateBase.cs @@ -0,0 +1,23 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +/// +/// 状态模式中,单个状态的父类,所有的状态都需要继承自这个类。注意,这里是三级继承关系,这个是最底层的类,再往上是某个具 +/// 体状态体系的父类,继承于这个类,最上层才是要实现功能的具体状态类,继承自中间层,只需要完善虚函数即可 +/// +public class StateBase : MonoBehaviour +{ + /// + /// 进入状态时调用 + /// + public virtual void Enter(){} + /// + /// 状态结束时调用 + /// + public virtual void End(){} + /// + /// 状态中每一帧调用 + /// + public virtual void StateUpdate(){} +} diff --git a/Assets/Scripts/勍/StateBase.cs.meta b/Assets/Scripts/勍/StateBase.cs.meta new file mode 100644 index 0000000..af08bc4 --- /dev/null +++ b/Assets/Scripts/勍/StateBase.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 2aae34169cb209342bdfae44b74a6761 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/勍/StateMachineBase.cs b/Assets/Scripts/勍/StateMachineBase.cs new file mode 100644 index 0000000..e4198b6 --- /dev/null +++ b/Assets/Scripts/勍/StateMachineBase.cs @@ -0,0 +1,41 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +/// +/// 状态模式的状态管理员基类,每一组状态的管理员都必须继承这个类 +/// +public class StateMachineBase : MonoBehaviour +{ + protected StateBase currentState; + protected bool isBegin = false; + /// + /// 改变状态的时候调用 + /// + /// + public void ChangeState(StateBase newState) + { + isBegin = false; + if (currentState != null) + { + currentState.End(); + } + currentState = newState; + } + + /// + /// 状态的每一帧调用,需要手动安排到主程序的Update中,切记! + /// + public void StateUpdate() + { + if (currentState != null) + { + if (!isBegin) + { + currentState.Enter(); + isBegin = true; + } + currentState.StateUpdate(); + } + } +} diff --git a/Assets/Scripts/勍/StateMachineBase.cs.meta b/Assets/Scripts/勍/StateMachineBase.cs.meta new file mode 100644 index 0000000..6d986b7 --- /dev/null +++ b/Assets/Scripts/勍/StateMachineBase.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 570a7bc236b60d84195606d29616a6e6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/勍/UnitySingleton.cs b/Assets/Scripts/勍/UnitySingleton.cs new file mode 100644 index 0000000..49f9505 --- /dev/null +++ b/Assets/Scripts/勍/UnitySingleton.cs @@ -0,0 +1,40 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +/// +/// 单例模式类模板 +/// +public class UnitySingleton : MonoBehaviour + where T : Component +{ + private static T m_instance; + + public static T Instance + { + get + { + if (m_instance == null) + { + m_instance = FindObjectOfType(); + if (m_instance == null) + { + Debug.LogError("缺少 " + typeof(T) + " 这个单例,没法在场景中找到"); + } + } + return m_instance; + } + } + + protected virtual void Awake() + { + if (m_instance == null) + { + m_instance = this as T; + } + else + { + Destroy(gameObject); + } + } +} diff --git a/Assets/Scripts/勍/UnitySingleton.cs.meta b/Assets/Scripts/勍/UnitySingleton.cs.meta new file mode 100644 index 0000000..6f3d135 --- /dev/null +++ b/Assets/Scripts/勍/UnitySingleton.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8d3368bff7857324580bba4842015865 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/UserSettings/EditorUserSettings.asset b/UserSettings/EditorUserSettings.asset index d91275c..6881a83 100644 --- a/UserSettings/EditorUserSettings.asset +++ b/UserSettings/EditorUserSettings.asset @@ -15,9 +15,6 @@ EditorUserSettings: value: 224247031146466b011b0b2b1e301034131a112d25292824620d3207f5e53136d2f539a9c2223e31290eea2f4b1a2e0be50f0c05d7050306101af4011fc0321202cc1bd654dd1115df00 flags: 0 RecentlyUsedScenePath-3: - value: 22424703114646680e0b0227036cdafbfb5831243c3d3204283a097df7ee3d2cfb - flags: 0 - RecentlyUsedScenePath-4: value: 22424703114646680e0b0227036c6c111b07142f1f2b233e2867083debf42d flags: 0 RecentlyUsedScenePath-5: