From b0b30f8b0b9011f5a50c78bdd440035883b75955 Mon Sep 17 00:00:00 2001 From: xhxy <2290327506@qq.com> Date: Mon, 24 Feb 2025 20:39:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9F=BA=E6=9C=AC=E6=B2=A1=E5=8F=98=E5=8C=96,?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E6=9C=BA=E4=B9=B1=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/Scripts/Enemy/BaseState.cs | 17 +++++++++ Assets/Scripts/Enemy/BaseState.cs.meta | 11 ++++++ Assets/Scripts/Enemy/Boar.cs | 6 ++++ Assets/Scripts/Enemy/BoarPatrolState.cs | 38 ++++++++++++++++++++ Assets/Scripts/Enemy/BoarPatrolState.cs.meta | 11 ++++++ Assets/Scripts/Enemy/Enemy.cs | 32 +++++++++++++---- 6 files changed, 108 insertions(+), 7 deletions(-) create mode 100644 Assets/Scripts/Enemy/BaseState.cs create mode 100644 Assets/Scripts/Enemy/BaseState.cs.meta create mode 100644 Assets/Scripts/Enemy/BoarPatrolState.cs create mode 100644 Assets/Scripts/Enemy/BoarPatrolState.cs.meta 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<Rigidbody2D>(); anim = GetComponent<Animator>(); @@ -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);