diff --git a/Assets/Scripts/Enemy/BaseState.cs b/Assets/Scripts/Enemy/BaseState.cs new file mode 100644 index 0000000..26af6f3 --- /dev/null +++ b/Assets/Scripts/Enemy/BaseState.cs @@ -0,0 +1,17 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public abstract class BaseState +{ + protected Enemy currentEnemy; + + public abstract void OnEnter(Enemy enemy); + public abstract void LogicUpdate(); + + public abstract void PhysicsUpdate(); + + public abstract void OnExit(); + + +} diff --git a/Assets/Scripts/Enemy/BaseState.cs.meta b/Assets/Scripts/Enemy/BaseState.cs.meta new file mode 100644 index 0000000..2b579b3 --- /dev/null +++ b/Assets/Scripts/Enemy/BaseState.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 92a199f381e763741a0ee8a3abf7cb36 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Enemy/Boar.cs b/Assets/Scripts/Enemy/Boar.cs index 2e590bc..70c88d3 100644 --- a/Assets/Scripts/Enemy/Boar.cs +++ b/Assets/Scripts/Enemy/Boar.cs @@ -10,3 +10,9 @@ public class Boar : Enemy anim.SetBool("walk", true); } } + /* protected override void Awake() + { + base.Awake(); + patrolState = new BoarPatrolState(); + } +}*/ diff --git a/Assets/Scripts/Enemy/BoarPatrolState.cs b/Assets/Scripts/Enemy/BoarPatrolState.cs new file mode 100644 index 0000000..f23c156 --- /dev/null +++ b/Assets/Scripts/Enemy/BoarPatrolState.cs @@ -0,0 +1,38 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class BoarPatrolState : BaseState +{ + + public override void OnEnter(Enemy enemy) + { + currentEnemy = enemy; + + } + + public override void LogicUpdate() + { + //throw new System.NotImplementedException(); + } + + + + public override void PhysicsUpdate() + { + if (!currentEnemy.physicsCheck.isGround || currentEnemy.physicsCheck.touchLeftWall && currentEnemy.faceDir.x < 0 || currentEnemy.physicsCheck.touchRightWall && currentEnemy.faceDir.x > 0) + { + currentEnemy.wait = true; + currentEnemy.anim.SetBool("walk", false); + } + else + { + currentEnemy.anim.SetBool("walk", true); + } + } + + public override void OnExit() + { + + } +} diff --git a/Assets/Scripts/Enemy/BoarPatrolState.cs.meta b/Assets/Scripts/Enemy/BoarPatrolState.cs.meta new file mode 100644 index 0000000..c62a336 --- /dev/null +++ b/Assets/Scripts/Enemy/BoarPatrolState.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e965041899cec6843820e607bbb64b67 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Enemy/Enemy.cs b/Assets/Scripts/Enemy/Enemy.cs index b9af1de..0a2c6f1 100644 --- a/Assets/Scripts/Enemy/Enemy.cs +++ b/Assets/Scripts/Enemy/Enemy.cs @@ -5,13 +5,13 @@ using UnityEngine; public class Enemy : MonoBehaviour { Rigidbody2D rb; - protected Animator anim; - PhysicsCheck physicsCheck; + [HideInInspector]public Animator anim; + [HideInInspector]public PhysicsCheck physicsCheck; [Header("基本参数")] public float normalSpeed;//正常速度 public float chaseSpeed;//追击速度 - public float currentSpeed;//当前速度 + [HideInInspector] public float currentSpeed;//当前速度 public Vector3 faceDir;//面朝方向 public float hurtForce;//受伤的力 @@ -27,7 +27,11 @@ public class Enemy : MonoBehaviour public bool isHurt; public bool isDead; - private void Awake() + + private BaseState currentState; + protected BaseState patrolState; + protected BaseState chaseState; + protected virtual void Awake() { rb = GetComponent(); anim = GetComponent(); @@ -35,27 +39,41 @@ public class Enemy : MonoBehaviour currentSpeed = normalSpeed; waitTimeCounter = waitTime; } + public void OnEnable() + { + // currentState = patrolState; + // currentState.OnEnter(this); + } - private void Update() + + public void Update() { faceDir = new Vector3(-transform.localScale.x,0,0); - if (physicsCheck.touchLeftWall && faceDir.x <0 || physicsCheck.touchRightWall && faceDir.x >0) + if (physicsCheck.touchLeftWall && faceDir.x < 0 || physicsCheck.touchRightWall && faceDir.x > 0) { wait = true; anim.SetBool("walk", false); } + + //currentState.LogicUpdate(); TimeCounter(); } - private void FixedUpdate() + public void FixedUpdate() { if(!isHurt && !isDead) { Move(); } + //currentState.PhysicsUpdate(); } + private void OnDisable() + { + // currentState.OnExit(); + } + public virtual void Move() { rb.velocity = new Vector2(currentSpeed * faceDir.x * Time.deltaTime, rb.velocity.y);