void AddStateToFSM(string stateName)
        {
            // 判断当前状态机是否有该状态
            if (_stateInFSM.IndexOf(stateName) != -1)
            {
                return;
            }

            // 获取该状态能返回的所有条件
            FieldInfo[] fields = _stateRegister.GetStateType(stateName).GetNestedType("Conditions").GetFields();

            // 初始化状态节点
            AIFSMData.StateTransitionGraphNode node;
            node.currentState         = stateName;
            node.stateTransitionRules = new List <AIFSMData.StateTransitionGraphNode.StateTransitionRule>();

            for (int i = 0; i < fields.Length; i++)
            {
                AIFSMData.StateTransitionGraphNode.StateTransitionRule rule;
                rule.condition = (string)fields[i].GetValue(null);
                rule.nextState = null;
                node.stateTransitionRules.Add(rule);
            }

            //在当前状态机注册状态
            _currentFSMData.stateTransitionGraphNodes.Add(node);

            // 初始化Editor中与该状态相关的变量
            _currentFSMData.showStateTransitionConditions.Add(true);
            _stateInFSM.Add(stateName);
        }
Ejemplo n.º 2
0
        public void Init(GameObject gameObject)
        {
            _gameObject    = gameObject;
            _stateRegister = Utility.GetAIStateRegister();

            // 在FSM所在GameObject添加状态脚本
            if (_fsm.StateTransitionGraph != null)
            {
                foreach (var node in _fsm.StateTransitionGraph)
                {
                    Type state = _stateRegister.GetStateType(node.Key);
                    if (state != null && !_gameObject.GetComponent(state))
                    {
                        _gameObject.AddComponent(state);
                    }
                }
            }

            // 设置当前状态,并初始化
            Type initStateType = _stateRegister.GetStateType(_fsm.initStateName);

            if (initStateType != null)
            {
                _currState     = (_gameObject.GetComponent(initStateType) as AIState);
                _currStateName = _fsm.initStateName;
                _currState.Init();
            }
        }
Ejemplo n.º 3
0
        AIState GetNextState(string currentState, string condition)
        {
            string nextState           = _fsm.StateTransitionGraph[currentState][condition];
            Type   nextStateScriptType = _stateRegister.GetStateType(nextState);

            return(nextStateScriptType == null ? null : _gameObject.GetComponent(nextStateScriptType) as AIState);
        }