Exemple #1
0
        //两个state最靠近的父hfsm
        public HFSM GetNearestSameParentHFSM(HFSMState state2)
        {
            if (state2 == null)
            {
                return(this.GetRootHFSM());
            }
            List <HFSM> hfsmList1 = GetParentHFSMList();
            List <HFSM> hfsmList2 = state2.GetParentHFSMList();

            List <HFSM>             hfsmDeeperList;
            Dictionary <HFSM, bool> hfsmDict = new Dictionary <HFSM, bool>();

            hfsmDeeperList = hfsmList1.Count > hfsmList2.Count ? hfsmList1 : hfsmList2;
            var hfsmLowerList = hfsmDeeperList == hfsmList1 ? hfsmList2 : hfsmList1;

            foreach (var hfsm in hfsmLowerList)
            {
                hfsmDict[hfsm] = true;
            }

            for (var i = 0; i < hfsmDeeperList.Count; i++)
            {
                var hfsm = hfsmDeeperList[i];
                if (hfsmDict.ContainsKey(hfsm))
                {
                    return(hfsm);
                }
            }

            return(null);
        }
Exemple #2
0
        public virtual void ChangeToState(HFSMState toState, bool isForce = false, params object[] args)
        {
            HFSM      rootHFSM  = this.GetRootHFSM();
            HFSMState fromState = rootHFSM.GetCurrentState();

            if (fromState == toState)
            {
                return;
            }

            if (!isForce && fromState != null && !fromState.IsCanChangeToState(toState, args))
            {
                return;
            }

            HFSM nearestSameParentHFSM = toState.GetNearestSameParentHFSM(fromState);

            if (fromState != null)
            {
                this.Broadcast(rootHFSM.eventDispatchers, HFSMEventNameConst.Pre_State_Exit, fromState);
                fromState.ExitLoopTo(nearestSameParentHFSM);
                this.Broadcast(rootHFSM.eventDispatchers, HFSMEventNameConst.Post_State_Exit, fromState);
            }

            this.Broadcast(rootHFSM.eventDispatchers, HFSMEventNameConst.Pre_State_Enter, toState);
            nearestSameParentHFSM.EnterLoopTo(toState, args);
            this.Broadcast(rootHFSM.eventDispatchers, HFSMEventNameConst.Post_State_Enter, toState);

            previousState = fromState;
            this.Broadcast(rootHFSM.eventDispatchers, HFSMEventNameConst.State_Change_Finish, fromState, toState);
        }
Exemple #3
0
        public HFSMState AddSubDirectState(string key, Type subDirectStateType, params object[] init_args)
        {
            HFSMState subDirectState = AddSubDirectStateWithoutInit(key, subDirectStateType);

            subDirectState.InvokeMethod("Init", true, init_args);
            subDirectState.PostInit();
            return(subDirectState);
        }
Exemple #4
0
        //////////////////////////////////////////////////////////////////////
        // Add
        //////////////////////////////////////////////////////////////////////
        public HFSMState AddSubDirectStateWithoutInit(string key, Type subDirectStateType)
        {
            HFSMState subDirectState = base.AddChildWithoutInit(key, subDirectStateType) as HFSMState;

            subDirectState.parentHFSM = this;
            this.subDirectStateDict[subDirectState.key] = subDirectState;
            return(subDirectState);
        }
Exemple #5
0
        public virtual void ChangeToHFSM(string key, bool isForce = false, params object[] args)
        {
            HFSM toHFSM = this.GetRootHFSM().GetSubHFSM(key, true);

            while (toHFSM.defaultSubDirectHFSM != null)
            {
                toHFSM = toHFSM.defaultSubDirectHFSM;
            }
            HFSMState toState = toHFSM.defaultSubDirectState;

            this.ChangeToState(toState, isForce, args);
        }
Exemple #6
0
        public virtual void EnterLoopTo(HFSMState toState, params object[] args)
        {
            var hfsmList = new List <HFSM>();           //倒序
            var hfsm     = toState.parentHFSM;

            while (hfsm != this)
            {
                hfsmList.Add(hfsm);
                hfsm = hfsm.parentHFSM;
            }

            for (int i = hfsmList.Count - 1; i >= 0; i--)
            {
                hfsmList[i].Enter(args);
            }
            toState.Enter(args);
        }
Exemple #7
0
        //////////////////////////////////////////////////////////////////////
        // Get
        //////////////////////////////////////////////////////////////////////
        public HFSMState GetSubState(string key, bool isLoopSubHFSMDict)
        {
            if (this.subDirectStateDict.ContainsKey(key))
            {
                return(this.subDirectStateDict[key]);
            }
            if (isLoopSubHFSMDict)
            {
                foreach (var subDirectHFSM in this.subDirectHFSMDict.Values)
                {
                    HFSMState instance = subDirectHFSM.GetSubState(key, true);
                    if (instance != null)
                    {
                        return(instance);
                    }
                }
            }

            return(null);
        }
Exemple #8
0
 public override void ChangeToState(HFSMState toState, bool isForce = false, params object[] args)
 {
     StartCoroutine(IEChangeToState(toState as CoroutineHFSMState, isForce, args));
 }
Exemple #9
0
 //////////////////////////////////////////////////////////////////////
 // Set
 //////////////////////////////////////////////////////////////////////
 public void SetDefaultSubDirectState(string key)
 {
     this.defaultSubDirectState = subDirectStateDict[key];
 }
Exemple #10
0
 public virtual bool IsCanChangeToState(HFSMState toState, params object[] args)
 {
     return(true);
 }
Exemple #11
0
 public void ChangeToState(HFSMState toState, bool isForce = false, params object[] args)
 {
     this.GetRootHFSM().ChangeToState(toState, isForce, args);
 }
Exemple #12
0
        public virtual void ChangeToState(string key, bool isForce = false, params object[] args)
        {
            HFSMState toState = this.GetRootHFSM().GetSubState(key, true);

            ChangeToState(toState, isForce, args);
        }