unity 有限状态机(一)
来源:互联网 发布:域名被腾讯浏览器拦截 编辑:程序博客网 时间:2024/05/16 09:17
原文链接:http://wiki.unity3d.com/index.php/Finite_State_Machine
出于学习目的,修改了部分注释和调试输出等内容,版权归原文所有,仅供大家学习使用。
状态机很万能的样子,但是容易被乱用,反而增加工程的复杂度,尤其是这种复杂状态机的实现方式。
所有状态继承FSMState,然后在updet中使用FSMsys。
FSMSystem.cs
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
-
-
- public enum Transition
- {
- NullTransition = 0,
-
- }
-
-
- public enum StateID
- {
- NullStateID = 0,
- PlayerMove = 2,
- PlayerAttack = 4,
- EnemyMove = 6,
- EnemyAttack = 8,
-
- }
-
-
- public class TransInfo
- {
- public string comment;
- }
-
-
-
-
-
-
- public abstract class FSMState
- {
- protected Dictionary<Transition, StateID> map = new Dictionary<Transition, StateID>();
- protected StateID stateID;
- public StateID ID { get { return stateID; } }
-
-
- public void AddTransition(Transition trans, StateID id)
- {
-
- if (trans == Transition.NullTransition)
- {
- Debug.LogError("FSMState ERROR: 不能添加空转换");
- return;
- }
-
- if (id == StateID.NullStateID)
- {
- Debug.LogError("FSMState ERROR: 不能添加空状态");
- return;
- }
-
-
- if (map.ContainsKey(trans))
- {
- Debug.LogError("FSMState ERROR: 状态 " + stateID.ToString() + " 已经包含转换 " + trans.ToString() + "不可添加另一个状态");
- return;
- }
-
- map.Add(trans, id);
- }
-
-
- public void DeleteTransition(Transition trans)
- {
-
- if (trans == Transition.NullTransition)
- {
- Debug.LogError("FSMState ERROR: 不能删除空转换");
- return;
- }
-
-
- if (map.ContainsKey(trans))
- {
- map.Remove(trans);
- return;
- }
- Debug.LogError("FSMState ERROR: 转换 " + trans.ToString() + " - 状态 " + stateID.ToString() + " 不存在");
- }
-
-
- public StateID GetOutputState(Transition trans)
- {
-
- if (map.ContainsKey(trans))
- {
- return map[trans];
- }
- return StateID.NullStateID;
- }
-
-
- public virtual void DoBeforeEntering() { }
-
-
- public virtual void DoBeforeLeaving() { }
-
-
- public abstract void Reason(GameObject player, GameObject npc);
-
-
- public abstract void Act(GameObject player, GameObject npc);
-
- }
-
-
-
-
-
-
- public class FSMSystem
- {
- private List<FSMState> states;
-
-
- private StateID currentStateID;
- public StateID CurrentStateID { get { return currentStateID; } }
- private FSMState currentState;
- public FSMState CurrentState { get { return currentState; } }
-
- public FSMSystem()
- {
- states = new List<FSMState>();
- }
-
-
- public void AddState(FSMState s)
- {
-
- if (s == null)
- {
- Debug.LogError("FSM ERROR: 不可添加空状态");
- }
-
-
- if (states.Count == 0)
- {
- states.Add(s);
- currentState = s;
- currentStateID = s.ID;
- return;
- }
-
-
- foreach (FSMState state in states)
- {
- if (state.ID == s.ID)
- {
- Debug.LogError("FSM ERROR: 无法添加状态 " + s.ID.ToString() + " 因为该状态已存在");
- return;
- }
- }
- states.Add(s);
- }
-
-
- public void DeleteState(StateID id)
- {
-
- if (id == StateID.NullStateID)
- {
- Debug.LogError("FSM ERROR: 状态ID 不可为空ID");
- return;
- }
-
-
- foreach (FSMState state in states)
- {
- if (state.ID == id)
- {
- states.Remove(state);
- return;
- }
- }
- Debug.LogError("FSM ERROR: 无法删除状态 " + id.ToString() + ". 状态列表中不存在");
- }
-
-
- public void PerformTransition(Transition trans)
- {
-
- if (trans == Transition.NullTransition)
- {
- Debug.LogError("FSM ERROR: 转换不可为空");
- return;
- }
-
-
- StateID id = currentState.GetOutputState(trans);
- if (id == StateID.NullStateID)
- {
- Debug.LogError("FSM ERROR: 状态 " + currentStateID.ToString() + " 不存在目标状态 " +
- " - 转换: " + trans.ToString());
- return;
- }
-
-
- currentStateID = id;
- foreach (FSMState state in states)
- {
- if (state.ID == currentStateID)
- {
-
- currentState.DoBeforeLeaving();
-
- currentState = state;
-
-
- currentState.DoBeforeEntering();
- break;
- }
- }
- }
- }
0 0