Unity角色行为状态机框架设计

本文提供了一个简单的角色行为状态机的实现,把角色状态类当成角色类的一个属性来实现,把角色的行为拆分成多个状态子类来实现,降低代码耦合度。 源码下载地址

行为状态基类:只提供几个最基本的方法。

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;public class BaseMode
{public GAME_MODE_TYPE mModeType;protected PlayerController mPlayer;//状态初始化public virtual void Init(PlayerController player){mPlayer = player;}//状态开始public virtual void Begin (object[] mParams){Debug.Log(mModeType.ToString() + "|Begin");}//状态更新public virtual void Update(){Debug.Log(mModeType.ToString() + "|Update");}//状态结束public virtual void End (){Debug.Log(mModeType.ToString() + "|End");}//输入控制public virtual void InputHandle(){}
}

状态机子类:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;public class RunningMode : BaseMode
{public RunningMode(){mModeType = GAME_MODE_TYPE.Running;}public override void Begin(object[] mParams){base.Begin(mParams);}public override void Update(){base.Update();}public override void End(){base.End();}public override void InputHandle(){}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;public class DeathMode : BaseMode
{public DeathMode(){mModeType = GAME_MODE_TYPE.Death;}public override void Begin(object[] mParams){base.Begin(mParams);}public override void Update(){base.Update();}public override void End(){base.End();}public override void InputHandle(){}
}


状态机配置表:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;public static class GameModeConfig
{public static DictionaryCreatGameModeDict(PlayerController mPlayer){var mDict = new Dictionary(){{ GAME_MODE_TYPE.Running,new RunningMode()},{ GAME_MODE_TYPE.Death,new DeathMode()},};foreach (var item in mDict){item.Value.Init(mPlayer);}return mDict;}
}public enum GAME_MODE_TYPE
{Running,Death,
}

角色控制器
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;public class PlayerController : MonoBehaviour
{private BaseMode mCurGameMode;private DictionarymGameModeDict = new Dictionary();void Start(){//角色初始化应该在角色管理器初始化,此处仅作测试Init();}//角色控制器初始化void Init(){InitMode();ChangeMode(GAME_MODE_TYPE.Running);}void Update(){//当前状态更新if (mCurGameMode != null){mCurGameMode.Update();}}//初始化状态public void InitMode(){mGameModeDict.Clear();mGameModeDict = GameModeConfig.CreatGameModeDict(this);}//切换状态public void ChangeMode(GAME_MODE_TYPE mType, object[] mParams = null){//执行上个状态的结束if (mCurGameMode != null){mCurGameMode.End();}mCurGameMode = mGameModeDict[mType];//执行当前状态的开始mCurGameMode.Begin(mParams);}//是否是该状态public bool IsGameMode(GAME_MODE_TYPE mType){if (mCurGameMode != null && mCurGameMode.mModeType == mType){return true;}return false;}#region 测试代码void OnGUI(){if (GUILayout.Button("ChangeRunning")){ChangeMode(GAME_MODE_TYPE.Running);}if (GUILayout.Button("ChangeDeath")){ChangeMode(GAME_MODE_TYPE.Death);}}#endregion
}





本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部