基本没变化,状态机乱了

This commit is contained in:
xhxy 2025-02-24 20:39:34 +08:00
parent 9467caad36
commit b0b30f8b0b
6 changed files with 108 additions and 7 deletions

View File

@ -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();
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 92a199f381e763741a0ee8a3abf7cb36
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -10,3 +10,9 @@ public class Boar : Enemy
anim.SetBool("walk", true);
}
}
/* protected override void Awake()
{
base.Awake();
patrolState = new BoarPatrolState();
}
}*/

View File

@ -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()
{
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e965041899cec6843820e607bbb64b67
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -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,8 +39,14 @@ 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)
@ -44,18 +54,26 @@ public class Enemy : MonoBehaviour
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);