Beispiel #1
0
    private IEnumerator _SwitchToStateCo(EStateType newState)
    {
        if (mOldState == newState)
        {
            Debug.Log("SwitchState oldState == newState: " + newState);
            yield break;
        }

        if (OnPreStateChange != null)
        {
            OnPreStateChange(newState, mOldState);
        }

        CGameState[] oldStates = mConfig.mStateMap[mOldState];
        CGameState[] newStates = mConfig.mStateMap[newState];

        int sameDepth = -1;

        while (sameDepth + 1 < newStates.Length && sameDepth + 1 < oldStates.Length &&
               newStates[sameDepth + 1] == oldStates[sameDepth + 1])
        {
            ++sameDepth;
        }

        List <CGameSystem> leaveSystems = new List <CGameSystem>();

        for (int i = oldStates.Length - 1; i > sameDepth; --i)
        {
            foreach (Type sysType in oldStates[i].mSystems)
            {
                leaveSystems.Add(mSystemMap[sysType]);
            }
        }

        foreach (CGameSystem leaveSystem in leaveSystems)
        {
            leaveSystem._SysLeave();
        }

        if (OnStateChange != null)
        {
            OnStateChange(newState, mOldState);
        }

        List <CGameSystem> enterSystems = new List <CGameSystem>();

        for (int i = sameDepth + 1; i < newStates.Length; ++i)
        {
            foreach (Type sysType in newStates[i].mSystems)
            {
                if (!mSystemMap.ContainsKey(sysType))
                {
                    throw new Exception(string.Format("SystemMap.ContainsKey({0}) == false", sysType.Name));
                }

                mSystemMap[sysType].EnableInState = newStates[i].mStateType;
                enterSystems.Add(mSystemMap[sysType]);
            }
        }


        //Debug.Log("555555555555555555555555....." + enterSystems.Count);
        for (int i = 0; i < enterSystems.Count; ++i)
        {
            CGameSystem enterSystem = enterSystems[i];
            bool        haveEnterCo = enterSystem._SysEnter();
            if (haveEnterCo)
            {
                yield return(StartCoroutine(enterSystem.SysEnterCo()));
            }

            enterSystem._EnterFinish();
        }

        //加入了新系统之后,再给旧系统一次清理的机会。
        foreach (CGameSystem leaveSystem in leaveSystems)
        {
            leaveSystem.SysLastLeave();
        }

        foreach (CGameSystem enterSystem in enterSystems)
        {
            enterSystem.OnStateChangeFinish();
        }

        mPreState = mOldState;
        mOldState = newState;

        if (OnPostStateChange != null)
        {
            OnPostStateChange(newState, mPreState);
        }
    }
Beispiel #2
0
    private IEnumerator _SwitchToStateCo(EStateType newState)
    {
        mWillStateType = newState;
        if (mOldState == newState)
        {
            //Debug.LogWarning("SwitchState oldState == newState: " + newState);
            yield break;
        }

        CUility.Log("Begin: SwitchToStateCo " + newState);

        try
        {
            if (OnPreStateChange != null)
            {
                OnPreStateChange(newState, mOldState);
            }
        }
        catch (Exception ex)
        {
            CUility.LogError("Exception in OnPreStateChange: {0}", ex.ToString());
        }

        CGameState[] oldStates = mConfig.mStateMap[mOldState];
        CGameState[] newStates = mConfig.mStateMap[newState];

        int sameDepth = -1;

        while (sameDepth + 1 < newStates.Length && sameDepth + 1 < oldStates.Length &&
               newStates[sameDepth + 1] == oldStates[sameDepth + 1])
        {
            ++sameDepth;
        }
        //Debug.Log(sameDepth);

        //Debug.Log("Leave old system");
        List <CGameSystem> leaveSystems = new List <CGameSystem>();

        for (int i = oldStates.Length - 1; i > sameDepth; --i)
        {
            foreach (Type sysType in oldStates[i].mSystems)
            {
                leaveSystems.Add(mSystemMap[sysType]);
            }
        }

        foreach (CGameSystem leaveSystem in leaveSystems)
        {
            CUility.Log("Leave System {0}", leaveSystem.Name);
            try
            {
                leaveSystem._SysLeave();
            }
            catch (Exception ex)
            {
                Debug.LogError(string.Format("Exception in {1}'s SysLeave: {0}", ex.ToString(), leaveSystem.Name));
            }
        }

        CUility.Log("Leave System Finish");

        try
        {
            if (OnStateChange != null)
            {
                OnStateChange(newState, mOldState);
            }
        }
        catch (Exception ex)
        {
            Debug.LogError(string.Format("Exception in OnStateChange: {0}", ex.ToString()));
        }

        CUility.Log("Start EnterSystem");

        //Debug.Log("Enter new system");
        List <CGameSystem> enterSystems = new List <CGameSystem>();

        for (int i = sameDepth + 1; i < newStates.Length; ++i)
        {
            foreach (Type sysType in newStates[i].mSystems)
            {
                if (!mSystemMap.ContainsKey(sysType))
                {
                    throw new Exception(string.Format("SystemMap.ContainsKey({0}) == false", sysType.Name));
                }

                mSystemMap[sysType].EnableInState = newStates[i].mStateType;
                enterSystems.Add(mSystemMap[sysType]);
            }
        }


        for (int i = 0; i < enterSystems.Count; ++i)
        //foreach (CGameSystem enterSystem in enterSystems)
        {
            CGameSystem enterSystem = enterSystems[i];

            CUility.Log("{0} SysEnter Start", enterSystem.Name);

            bool haveEnterCo = false;
            try
            {
                haveEnterCo = enterSystem._SysEnter();
            }
            catch (Exception ex)
            {
                Debug.LogError(string.Format("Exception in {1}'s SysEnter: {0}", ex.ToString(), enterSystem.Name));
            }

            if (haveEnterCo)
            {
                yield return(StartCoroutine(enterSystem.SysEnterCo()));
            }

            enterSystem._EnterFinish();

            //CLogSys.Log(ELogLevel.Verbose, ELogTag.GameRoot, string.Format("{0} SysEnter Finish", enterSystem.Name));
        }

        //Debug.Log("Add new system...............Finish");


        //Debug.Log("Remove old system once more");

        //加入了新系统之后,再给旧系统一次清理的机会。
        foreach (CGameSystem leaveSystem in leaveSystems)
        {
            leaveSystem.SysLastLeave();
        }

        //Debug.Log("Remove old system once more...............Finish");

        foreach (CGameSystem enterSystem in enterSystems)
        {
            try
            {
                enterSystem.OnStateChangeFinish();
            }
            catch (Exception ex)
            {
                Debug.LogError("Exception in OnStateChangeFinish: +" + ex.ToString());
            }
        }

        //Debug.Log(string.Format("Enter State {0} from {1}", newState, mOldState));

        mPreState = mOldState;
        mOldState = newState;

        try
        {
            if (OnPostStateChange != null)
            {
                OnPostStateChange(newState, mPreState);
            }
        }
        catch (Exception ex)
        {
            Debug.Log(string.Format("Exception in OnPostStateChange: " + ex.ToString()));
        }

        CUility.Log("End: SwitchToStateCo " + newState);
    }