Exemple #1
0
        public FSM47 <StateEnum, EventEnum> Goto(StateEnum stateName)
        {
            if (_BuildState == null) // from In
            {
                throw new InvalidOperationException("Unknown build state. Use In() first.");
            }
            if (_BuildEvent == null) // from On
            {
                throw new InvalidOperationException("Unknown Event Name. Use On() first.");
            }

            var newState = _States[stateName];

            _BuildAction = new FSMAction <StateEnum, EventEnum>(_BuildState, _BuildEvent, newState);
            ActionKey key = new ActionKey()
            {
                SourceStateID = _BuildState.ID, SourceEventID = _BuildEvent.ID
            };

            _Actions.Add(key, _BuildAction);

            _BuildEvent = null;

            return(this);
        }
Exemple #2
0
        public FSM47 <StateEnum, EventEnum> Go(StateEnum stateName)
        {
            if (_BuildState == null) // from In
            {
                throw new InvalidOperationException("Unknown build state. Use In() first.");
            }
            if (_BuildEvent != null) // from On
            {
                throw new InvalidOperationException("Already has an Event. Use Go() instead of On().");
            }

            var newState = _States[stateName];

            _BuildAction = new FSMAction <StateEnum, EventEnum>(_BuildState, null, newState);
            ActionKey key = new ActionKey()
            {
                SourceStateID = _BuildState.ID, SourceEventID = INSTANT_ACTION
            };

            _Actions.Add(key, _BuildAction);

            _BuildEvent = null;

            return(this);
        }
Exemple #3
0
        internal bool AddOutgoing(TInput action, FSMState <TInput, TOutput> destinationState, TOutput output, double probability)
        {
            var act = new FSMAction <TInput, TOutput>(action, this);

            act.AddTransitionRes(destinationState, output, probability);
            bool result = false;

            if (!Outgoing.ContainsKey(act.Name))
            {
                if (output != null)
                {
                    FSM.AddOutput(output);
                }
                if (action != null)
                {
                    FSM.AddInput(action);
                }
                Outgoing.Add(act.Name, act);
                result = true;
            }
            else
            {
                result = Outgoing[act.Name].AddTransitionRes(destinationState, output, probability);
            }
            return(result);
        }
Exemple #4
0
        void ShowContextMenu(FSMState state)
        {
            var menu = new GenericMenu();

            menu.AddItem(new GUIContent("New Action"), false, () =>
            {
                Undo.RecordObject(target, "New Action");
                var action = new FSMAction()
                {
                    actionName = "New Action", guid = target.NextGUID()
                };
                target.actions.Insert(0, action);
                OpenActionEditor(state.backgroundRect, state, action.guid);
            });
            menu.AddItem(new GUIContent("On Enter Action/None"), false, () => state.onEnterAction.guid = 0);
            menu.AddItem(new GUIContent("On Stay Action/None"), false, () => state.onStayAction.guid   = 0);
            menu.AddItem(new GUIContent("On Exit Action/None"), false, () => state.onExitAction.guid   = 0);
            foreach (var a in target.actions)
            {
                menu.AddItem(new GUIContent($"On Enter Action/{a.actionName}"), false, SetActionRef(state.onEnterAction, a.guid));
                menu.AddItem(new GUIContent($"On Stay Action/{a.actionName}"), false, SetActionRef(state.onStayAction, a.guid));
                menu.AddItem(new GUIContent($"On Exit Action/{a.actionName}"), false, SetActionRef(state.onExitAction, a.guid));
            }
            menu.ShowAsContext();
        }
Exemple #5
0
 public State(string name, FSMAction entryAction, FSMAction updateAction, FSMAction exitAction)
 {
     this.name = name;
     this.entryAction = entryAction;
     this.updateAction = updateAction;
     this.exitAction = exitAction;
     this.transitionList = new Dictionary<string,Transition>();
 }
Exemple #6
0
 public FSMState(string name, FSMAction entryAction, FSMAction updateAction, FSMAction exitAction)
 {
     this.name           = name;
     this.entryAction    = entryAction;
     this.updateAction   = updateAction;
     this.exitAction     = exitAction;
     this.transitionList = new Dictionary <string, FSMTransition>();
 }
Exemple #7
0
 public void AddAction(FSMAction action)
 {
     if (actions.ContainsKey(action.name))
     {
         UnityEngine.Debug.LogError("状态" + name + "已经包含名为:" + action.name + "的动作!");
     }
     else
     {
         actions.Add(action.name, action);
     }
 }
Exemple #8
0
        private void CheckForInstantAction()
        {
            FSMAction <StateEnum, EventEnum> instantAction = null;

            foreach (var key in _Actions.Keys)
            {
                if (key.SourceStateID == _CurrentState.ID && key.SourceEventID == INSTANT_ACTION)
                {
                    instantAction = _Actions[key];
                    break;
                }
            }

            if (instantAction == null)
            {
                return;
            }

            ExitState();
            EnterState(instantAction.FinalState);
        }
Exemple #9
0
        public void Act(EventEnum eventType)
        {
            if (!_Events.ContainsKey(eventType))
            {
                throw new InvalidOperationException(string.Format("Event list is missing: {0}", eventType));
            }
            if (_isInAction)
            {
                throw new InvalidOperationException(string.Format("Cannot start a new Act '{0}' when already evaluating '{1}' -- QueueAct() it instead", eventType, _isInActionName));
            }

            var fsmEvent = _Events[eventType];

            _isInAction     = true;
            _isInActionName = fsmEvent.Name;

            FSMAction <StateEnum, EventEnum> transition = null;

            foreach (var key in _Actions.Keys)
            {
                if (key.SourceStateID == _CurrentState.ID && key.SourceEventID == fsmEvent.ID)
                {
                    transition = _Actions[key];
                    break;
                }
            }
            if (transition == null)
            {
                FinishAct();
                return; // Invalid Event for the Current State
            }

            ExitState();

            EnterState(transition.FinalState);

            FinishAct();
        }
Exemple #10
0
 public Transition(State target, FSMAction action)
 {
     this.target = target;
     this.action = action;
 }
Exemple #11
0
 public static FSMContext createFSMInstance(State startState,FSMAction initAction)
 {
     return new FSMContext(startState,initAction);
 }
Exemple #12
0
 public FSMContext(FSMState startState, FSMAction initAction)
 {
     this.currentState = startState;
     this.initAction   = initAction;
     this.initAction.execute(this);
 }
Exemple #13
0
 public static FSMContext createFSMInstance(FSMState startState, FSMAction initAction)
 {
     return(new FSMContext(startState, initAction));
 }
Exemple #14
0
        public FsmStateMachine(XmlDocument xmlDoc)
        {
            this.states = new Dictionary <string, State>();
            XmlNode root = xmlDoc.SelectSingleNode("StateMachine");

            fsmName = root.Attributes["name"].Value;
            XmlNodeList states = root.SelectNodes("State");

            for (int i = 0, size = states.Count; i < size; i++)
            {
                XmlNode stateNode = states[i];
                State   state     = new State(stateNode.Attributes["name"].Value);
                state.isDefault = stateNode.Attributes["isDefault"].Value == "true";
                XmlNodeList actions = stateNode.SelectSingleNode("Actions").SelectNodes("Action");
                for (int j = 0; j < actions.Count; j++)
                {
                    XmlNode   actionNode = actions[j];
                    string    className  = actionNode.Attributes["name"].Value;
                    FSMAction action     = (FSMAction)Reflection.GetExecutingAssembly().CreateInstance(className);
                    action.name = className;
                    XmlNodeList paramsNodes = actionNode.SelectSingleNode("params").SelectNodes("param");
                    for (int k = 0; k < paramsNodes.Count; k++)
                    {
                        XmlNode   paramNode    = paramsNodes[k];
                        string    propertyName = paramNode.Attributes["key"].Value;
                        Type      tp           = action.GetType();
                        FieldInfo pInfo        = tp.GetField(propertyName);
                        if (null == pInfo)
                        {
                            UnityEngine.Debug.LogError("获取条件名为: " + action.name + "的属性 " + paramNode.Attributes["key"].Value + "不存在");
                            return;
                        }
                        pInfo.SetValue(action, paramNode.Attributes["value"].Value);
                    }
                    state.AddAction(action);
                }

                XmlNodeList transitions = stateNode.SelectSingleNode("Transitions").SelectNodes("Transition");
                for (int j = 0; j < transitions.Count; j++)
                {
                    XmlNode     transitionNode  = transitions[j];
                    Transition  trans           = new Transition(transitionNode.Attributes["name"].Value, state.name, transitionNode.Attributes["toState"].Value);
                    XmlNodeList conditionsNodes = transitionNode.SelectSingleNode("Conditions").SelectNodes("Condition");
                    for (int k = 0; k < conditionsNodes.Count; k++)
                    {
                        XmlNode      conditionNode = conditionsNodes[k];
                        string       className     = conditionNode.Attributes["name"].Value;
                        FSMCondition condition     = (FSMCondition)Reflection.GetExecutingAssembly().CreateInstance(className);
                        condition.name = className;
                        XmlNodeList paramsNodes = conditionNode.SelectSingleNode("params").SelectNodes("param");
                        for (int m = 0; m < paramsNodes.Count; m++)
                        {
                            XmlNode   paramNode = paramsNodes[m];
                            FieldInfo pInfo     = condition.GetType().GetField(paramNode.Attributes["key"].Value);
                            if (null == pInfo)
                            {
                                UnityEngine.Debug.LogError("获取条件名为: " + condition.name + "的属性 " + paramNode.Attributes["key"].Value + "不存在");
                                return;
                            }
                            // UnityEngine.Debug.Log("获取条件名为: " + condition.name + "的属性 " + paramNode.Attributes["key"].Value + "不存在");
                            pInfo.SetValue(condition, paramNode.Attributes["value"].Value);
                        }
                        trans.AddCondition(condition);
                    }
                    state.AddTransition(trans);
                }

                AddState(state);
            }
        }
        public FSMContext(State startState, FSMAction initAction,object o)
        {
            this.currentState = startState;
            this.initAction = initAction;
			this.initAction.execute(this, o);
        }
Exemple #16
0
Fichier : FSM.cs Projet : sseng/fsm
 public static FSMContext createFSMInstance(State currentState, FSMAction init, string name)
 {
     return new FSMContext(currentState, init, name);
 }
Exemple #17
0
 public FSMTransition(FSMState targetState, FSMAction action)
 {
     this.target = targetState;
     this.action = action;
 }