• 状态模式设计动画状态机


    状态基类:所有状态继承自它

    public abstract class IState
    {
        public virtual AnimState selfState { get; }
        public abstract int animHash { get; }
        public virtual void EnterState(Animator animator, PlayerController playerController)
        {
    
        }
        public virtual void ExitState(Animator animator)
        {
    
        }
        public virtual void UpdateState(Animator animator,PlayerController playerController, AnimStateMachine animStateMachine)
        {
    
        }
    }

    状态机:管理所有状态 的切换更新

    public class AnimStateMachine 
    {
        public Dictionary<AnimState, IState> stateDic = new Dictionary<AnimState, IState>();
        Animator _animator;
        IState currState;
        public AnimState GetCurrState
        {
            get { return currState.selfState; }
        }
        PlayerController controller;
        public AnimStateMachine(Animator animator,PlayerController player,AnimState initState)
        {
            _animator = animator;
            controller = player;
            stateDic.Add(AnimState.Idle,new IdleState());
            stateDic.Add(AnimState.Jump,new JumpState());
            stateDic.Add(AnimState.CombatMove,new CombatMoveState());
            stateDic.Add(AnimState.Move,new MoveState());
            currState = stateDic[initState];
        }
        public void Update()
        {
            currState.UpdateState(_animator,controller,this);
        }
        public void ChangeState(AnimState state)
        {
            if (!stateDic.TryGetValue(state,out IState st))
            {
                return;
            }
        currState.ExitState(_animator); 
        currState
    = st;
        currState.EnterState(_animator,controller);
      }
    }

    状态例子:

    /// <summary>
    /// 普通移动
    /// </summary>
    public class MoveState : IState
    {
        public override int animHash => Animator.StringToHash("ToMove");
        public int speedHash => Animator.StringToHash("Speed");
        public override AnimState selfState => AnimState.Move;
    
        public override void EnterState(Animator animator, PlayerController playerController)
        {
            animator.SetTrigger(animHash);
        }
        public override void ExitState(Animator animator)
        {
            animator.ResetTrigger(animHash);
    
        }
        public override void UpdateState(Animator animator, PlayerController playerController, AnimStateMachine animStateMachine)
        {
            if (Input.GetKeyDown(KeyCode.Tab))
            {
                playerController.UpdateCombatState();
            }
    
                playerController.Move();
            
        }
    }
    /// <summary>
    /// IDle
    /// </summary>
    public class IdleState : IState
    {
        public override int animHash => Animator.StringToHash("ToStand");
        public override AnimState selfState => AnimState.Idle;
    
        public override void EnterState(Animator animator, PlayerController playerController)
        {
            animator.SetTrigger(animHash);
        }
        public override void ExitState(Animator animator)
        {
            animator.ResetTrigger(animHash);
        }
        public override void UpdateState(Animator animator,PlayerController playerController, AnimStateMachine animStateMachine)
        {
            if (Input.GetKeyDown(KeyCode.Tab))
            {
                playerController.UpdateCombatState();
            }
            if (playerController.HasMoveAxis)
            {
                animStateMachine.ChangeState(AnimState.Move);
            }
        }
    }
  • 相关阅读:
    单例模式
    二、CSS
    十一、多线程
    十二、协程
    十、多进程
    九、内存管理
    八、元类
    七、上下文管理器/魔术方法
    六、单例模式
    五、装饰器
  • 原文地址:https://www.cnblogs.com/DazeJiang/p/14341086.html
Copyright © 2020-2023  润新知