Unity 3D 角色状态机实现

状态基类:

public abstract class StateBase
{
    private bool isAwake = true;
    protected bool isFinish = true;
    public void ActionStart(params object[] pars)
    {
        OnActionStart(pars);
    }

    public void ActionUpdate()
    {
        OnActionUpdate();
    }
    public void ActionCancel()
    {
        OnActionCancel();
        OnActionClean();
    }
    public virtual bool IsZhongduan()
    {
        return true;
    }

    public bool IsFinished()
    {
        return isFinish;
    }
    protected virtual void ActionFinish()
    {
        OnActionClean();
    }
    protected virtual void OnActionAwake()
    {
        isAwake = false;
    }

    protected virtual void OnActionStart(params object[] pars)
    {
        if (isAwake)
        {
            OnActionAwake();
        }
        isFinish = false;
    }
    protected virtual void OnActionUpdate()
    {

    }
    protected virtual void OnActionCancel()
    {

    }
    protected virtual void OnActionClean()
    {
        isAwake = true;
        isFinish = true;
    }
}

 状态机基类:

public abstract class StateMachineBase
{
    protected StateBase currentState = null;
    private Dictionary<string, StateBase> statesPool = new Dictionary<string, StateBase>();
    public virtual void Update()
    {
        if (currentState != null)
        {
            currentState.ActionUpdate();
        }
    }

    public void DoAction(string _stateName,params object[] _pars)
    {
        if (!IsRunNow(_stateName) && currentState != null && !currentState.IsFinished() && !currentState.IsZhongduan())
        {

        }
        else
        {
            StateBase state = GetState(_stateName);
            if (state != null)
            {
                if (currentState != null & currentState != state && !currentState.IsFinished())
                {
                    currentState.ActionCancel();
                }
                currentState = state;
                currentState.ActionStart(_pars);
            }
        }
    }

    private bool IsRunNow(string _stateName)
    {
        switch (_stateName)
        {
            case "die":
                return true;
            default:
                return false;
        }
    }

    private StateBase GetState(string _stateName)
    {
        StateBase state;
        if (!statesPool.TryGetValue(_stateName,out state))
        {
            state = NewState(_stateName);
            statesPool.Add(_stateName, state);
        }
        return state;
    }
    protected abstract StateBase NewState(string _stateName);
}

 实现一个UnitAI的状态机:

public class UnitAIStateMachine : StateMachineBase
{
    private UnitAI player;
    public UnitAIStateMachine(UnitAI _unit)
    {
        player = _unit;
    }
    protected override StateBase NewState(string _stateName)
    {
        return UnitAIStateFactory.NewState(_stateName, player);
    }
}

状态工厂:

public class UnitAIStateFactory
{
    public static StateBase NewState(string _stateName,UnitAI _unit)
    {
        switch (_stateName)
        {
            case "birth":
                return new UnitAIBirthState(_unit);
            case "stand":
                return new UnitAIStandState(_unit);
            case "walk":
                return new UnitAIWalkState(_unit);
            case "attack":
                return new UnitAIAttackState(_unit);
            default:
                Debug.Log(_stateName + " ---> what state?");
                return null;
        }
    }
}

 初始化好状态机后,默认开始一个状态。


版权声明:本文为songhuanfei2017原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。