从零开始的类银河恶魔城游戏02

[自学]从零开始的类银河恶魔城游戏02

part 2:Movement with State Machine 2025/1/27


注意这里一定要锁定z轴,否则player会旋转起来2333

//Player.cs
using System.Collections;
using System.Collections.Generic;
using UnityEditor.Callbacks;
using UnityEngine;

public class Player: MonoBehaviour
{
    [Header("Move info")]
    public float moveSpeed = 15f;  //

    #region Components
    public Animator anim { get; private set; }
    public Rigidbody2D rb { get; private set; }  //
    #endregion

    #region States
    public PlayerStateMachine stateMachine { get; private set; }

    public PlayerIdleState idleState { get; private set; }
    public PlayerMoveState moveState { get; private set; }
    #endregion

    private void Awake()
    {
        stateMachine = new PlayerStateMachine();

        idleState = new PlayerIdleState(this,stateMachine,"Idle");
        moveState = new PlayerMoveState(this,stateMachine,"Move");

    }

    public void Start()
    {
        anim = GetComponentInChildren<Animator>();
        rb = GetComponent<Rigidbody2D>();
        stateMachine.Initialize(idleState);// PlayerStateMachine.Initialize()
    }

    public void Update()
    {
        stateMachine.currentState.Update();// PlayerState.Update()
    }

//功能函数:设置速度
    public void SetVelocity(float _xVelocity, float _yVelocity)
    {
        rb.velocity = new Vector2(_xVelocity,_yVelocity);
    }

}
//PlayerState.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerState
{
    protected PlayerStateMachine stateMachine;
    protected Player player;
    protected Rigidbody2D rb;  //指向player.rb
    protected float xInput;    //

    private string animBoolName;

    public PlayerState(Player _player, PlayerStateMachine _stateMachine, string _animBoolName)
    {
        this.player = _player;
        this.stateMachine = _stateMachine;
        this.animBoolName = _animBoolName;
    }

    public virtual void Enter()
    {
        player.anim.SetBool(animBoolName, true);
        rb = player.rb;
    }

    public virtual void Update()
    {
        Debug.Log("I'm in " + animBoolName);
        xInput = Input.GetAxisRaw("Horizontal");  //实时获取水平方向的输入
    }

    public virtual void Exit()
    {
        player.anim.SetBool(animBoolName, false);
    }

}
//PlayerMoveState.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMoveState : PlayerState
{
    public PlayerMoveState(Player _player, PlayerStateMachine _stateMachine, string _animBoolName) : base(_player, _stateMachine, _animBoolName)
    {

    }

    public override void Enter()
    {
        base.Enter();
    }

    public override void Exit()
    {
        base.Exit();
    }

    public override void Update()
    {
        base.Update();

        player.SetVelocity(xInput * player.moveSpeed, rb.velocity.y);  //角色横向移动
        if (xInput == 0)  //设置状态机切换条件
        {
            player.stateMachine.ChangeState(player.idleState);
        }

    }
}
//PlayerIdleState.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerIdleState : PlayerState
{
    public PlayerIdleState(Player _player, PlayerStateMachine _stateMachine, string _animBoolName) : base(_player, _stateMachine, _animBoolName)
    {

    }

    public override void Enter()
    {
        base.Enter();
    }

    public override void Exit()
    {
        base.Exit();
    }

    public override void Update()
    {
        base.Update();
        if (xInput != 0)  //设置状态机切换条件
        {
            player.stateMachine.ChangeState(player.moveState);
        }
    }
}
posted @ 2025-03-06 20:39  EanoJiang  阅读(32)  评论(0)    收藏  举报