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;
        }
    }
}

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Unity中,怪物状态机是用来控制怪物行为和状态转换的一种机制。根据引用\[1\]中的代码示例,可以看出在游戏中的角色类Character中,使用状态机来管理角色的状态。状态机的关键在于控制状态的切换,通过调用TransitionState方法来实现状态的切换。在游戏中,可以根据需要定义不同的状态,比如追逐玩家状态、停止追逐状态等。根据引用\[3\]中的描述,可以使用模糊状态机实现怪物的追逐行为。当玩家离怪物比较近时,怪物进入追逐状态,当玩家离怪物比较远时,怪物停止追逐并返回出生点。具体实现可以根据游戏需求来设计和编写相应的代码逻辑。 #### 引用[.reference_title] - *1* [GameFrameWork框架(Unity3D)使用笔记(七)游戏主流程ProcedureMain——虽然游戏本来没有怪物但是为了使用...](https://blog.csdn.net/HowToPause/article/details/128048168)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v4^insert_chatgpt"}} ] [.reference_item] - *2* [在Unity实现有限状态机](https://blog.csdn.net/nick1992111/article/details/129234818)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v4^insert_chatgpt"}} ] [.reference_item] - *3* [unity有限状态机和模糊状态机(怪物AI、自动寻路)](https://blog.csdn.net/lxy20011125/article/details/130318794)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v4^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值