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


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

    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);
            }
        }
    }
  • 相关阅读:
    vs2015驱动开发中使用RtlStringCchPrintfW()报错
    windbg双机调试配置
    修改Windows默认调试器
    kong配置service和route实现简单API代理
    konga的初步使用
    Kong Admin API — 核心对象
    Kong的API管理方式
    kong的管理UI选择-konga
    Kong 安装
    关于kong | API Gateway
  • 原文地址:https://www.cnblogs.com/DazeJiang/p/14341086.html
Copyright © 2020-2023  润新知