示例#1
0
        // called from Start() of a SubState
        public void OnEnterNewSubState(StateInfo in_stateInfo)
        {
            // next sub state is now current sub state
            if (in_stateInfo != null && in_stateInfo.State != null &&
                in_stateInfo.StateId == m_sNextSubStateId)
            {
                GDebug.Log("Time Loading SubState -- " + m_sNextSubStateId + " --- " +
                           (Time.realtimeSinceStartup - m_fOriginalRealTimeSinceStartupSubState));

                m_bLoadingSubState = false;
                m_fOriginalRealTimeSinceStartupSubState = Time.realtimeSinceStartup;

                // current sub state is now the previous
                if (m_currentSubState != null)
                {
                    m_sPreviousSubStateId = m_currentSubState.StateId;
                }
                else
                {
                    m_sPreviousSubStateId = UNDEFINED_STATE;
                }

                m_currentSubState = in_stateInfo;
                m_gameSubStates.Insert(0, in_stateInfo);

                m_sNextSubStateId = UNDEFINED_STATE;

                if (OnSubStateChange != null)
                {
                    OnSubStateChange(m_currentSubState);
                }
            }
        }
示例#2
0
        // called from the Start() of a State
        public void OnEnterNewState(StateInfo in_stateInfo)
        {
            // if in the editor we want to force the state info if there wasn't any before
#if UNITY_EDITOR || UNITY_EDITOR_OSX
            if (m_currentState == null)
            {
                ForceStateInfo(in_stateInfo);
            }
#endif
            // next state is now current state
            if (in_stateInfo != null && in_stateInfo.State != null &&
                in_stateInfo.StateId == m_sNextStateId)
            {
                GDebug.Log("Time Loading -- " + in_stateInfo.StateId + "  " +
                           m_sNextStateId + " --- " + (Time.realtimeSinceStartup - m_fOriginalRealTimeSinceStartup));

                m_bLoading = false;
                m_fOriginalRealTimeSinceStartup = Time.realtimeSinceStartup;

                // current state is now the previous
                m_sPreviousStateId = m_currentState.StateId;

                m_currentState = in_stateInfo;

                m_sNextStateId = UNDEFINED_STATE;

                ResumeState();
            }

            if (OnStateChange != null)
            {
                OnStateChange(m_currentState);
            }
        }
        protected void Connect()
        {
            // kick off the thread that tries to connect
            connected = false;
            Thread thread = null;

            try
            {
                thread = new Thread(new ThreadStart(BeginConnect));
                thread.IsBackground = true; // 作为后台线程处理

                thread.Start();
                thread.Join(timeout);
            }
            catch (ArgumentNullException ex)
            {
                GDebug.LogError("Connect:new TcpClient: host name is null, " + ex.Message);
            }
            catch (ArgumentOutOfRangeException ex)
            {
                GDebug.LogError("Connect:new TcpClient: port error, " + ex.Message);
            }
            catch (SocketException ex)
            {
                if (ex.SocketErrorCode == SocketError.NetworkUnreachable)
                {
                    GDebug.LogError("-----客户端连接不上网络,断线重连处理-----");
                }
                else if (ex.SocketErrorCode == SocketError.ConnectionRefused)
                {
                    GDebug.LogError("-----服务器重启中,客户端返回登录界面------");
                    if (netState != NETWORKSTATE.NS_RETRY)
                    {
                        parent.ChangeState(NETWORKSTATE.NS_DISCONNECTED);
                    }
                }
                else
                {
                    GDebug.LogError("-----其它网络连接失败错误码------ : " + ex.SocketErrorCode);
                }
                GDebug.Log("Connect:new TcpClient: socket error1, " + ex.Message);
            }
            finally
            {
                thread.Abort();
                if (connected)
                {
                    parent.SetClient(tcpClient);
                    parent.OnConnected();
                }
                else
                {
                    if (netState != NETWORKSTATE.NS_RETRY)
                    {
                        parent.SetConnectErrState("连接失败");
                    }
                }
            }
        }
示例#4
0
 public virtual void Exit()
 {
     GDebug.Log("Exit : " + netState);
     if (onExit != null)
     {
         onExit();
     }
 }
示例#5
0
 public virtual void Enter()
 {
     GDebug.Log("Enter : " + netState);
     if (onEnter != null)
     {
         onEnter();
     }
 }
示例#6
0
 public void SetConnectErrState(string log)
 {
     GDebug.LogError("连接错误: " + log);
     if (GetNetState() != NETWORKSTATE.NS_RETRY)
     {
         //ChangeState(NETWORKSTATE.NS_CONNECTSERVERERROR);
         ChangeState(NETWORKSTATE.NS_DISCONNECTED);
     }
 }
示例#7
0
 public void ReConnectServer()
 {
     if (tcpSocket == null)
     {
         return;
     }
     GDebug.Log("TCP连接超时,重新连接服务器");
     tcpSocket.ReConnect();
 }
示例#8
0
 private void Start(NETWORKSTATE state)
 {
     curState = stateDict[state];
     if (curState != null)
     {
         curState.Enter();
     }
     GDebug.Log("NetWorkStateManager Start : " + state);
 }
示例#9
0
        public static Rigidbody Rigidbody(this GameObject in_object)
        {
            Rigidbody toReturn = in_object.GetComponent <Rigidbody>();

            if (toReturn == null)
            {
                GDebug.LogError("Trying to access " + in_object + "'s Rigidbody when there is none attached!");
            }
            return(toReturn);
        }
示例#10
0
        void OnDestroy()
        {
            GDebug.Log(TypeName + ": OnDestroy");

            if (Instance == this)
            {
                SaveState();
                GameDestroy();
            }
        }
示例#11
0
        public void ConnectServer(string strIp, int nPort, NetWorkCallBack callBack)
        {
            if (tcpSocket == null)
            {
                return;
            }

            GDebug.Log("TCP开始连接服务器: strIp: " + strIp + " nPort:" + nPort);
            connetCallback = callBack;
            tcpSocket.Connect(strIp, nPort);
        }
示例#12
0
        /// <summary>
        /// Returns the file path of the save data at index, or null if the index is invalid.
        /// </summary>
        /// <param name="in_iSaveDataIndex">Save data index</param>
        /// <returns>Save data file path</returns>
        public static string GetSaveFilePath(int in_iSaveDataIndex)
        {
            // Error checking
            if (in_iSaveDataIndex < 0 || in_iSaveDataIndex > MaxSaveFileCount)
            {
                GDebug.LogError("GetSaveFilePath: Invalid SaveData index - " + in_iSaveDataIndex);
                return(null);
            }

            return(SaveDataPaths[in_iSaveDataIndex]);
        }
示例#13
0
        public static void Save(SaveData in_saveData)
        {
            if (in_saveData == null || in_saveData.SaveDataIndex < 0 || in_saveData.SaveDataIndex >= MaxSaveFileCount)
            {
                return;
            }

            try
            {
                string json = JsonUtility.ToJson(in_saveData, true);

                string filePath = GetSaveFilePath(in_saveData.SaveDataIndex);

                GDebug.Log("Saving game data to file: " + filePath);

                StreamWriter file = new StreamWriter(filePath);
                file.WriteLine(json);
                file.Close();
            }
            catch (Exception ex)
            {
                GDebug.LogError("Error writing file - " + SaveDataPaths[in_saveData.SaveDataIndex] + ":\n" + ex.Message);
            }

            #region Save (Old PlayerPrefs code)
            //SetBool(SAVE_DATA_EXISTS + in_saveData.SaveDataIndex.ToString(), true);

            //SetInt(SECURITY_CLEARANCE, in_saveData.SecurityClearance);
            //SetInt(SAVE_POINT, (int)in_saveData.CurrentSavePoint);

            //SetBool(UNLOCKED_REI + in_saveData.SaveDataIndex.ToString(), in_saveData.UnlockedREI);
            //SetBool(UPGRADED_REI + in_saveData.SaveDataIndex.ToString(), in_saveData.UpgradedREI);
            //SetBool(UNLOCKED_GUIDED_ROCKET + in_saveData.SaveDataIndex.ToString(), in_saveData.UnlockedGuidedRocket);
            //SetBool(UPGRADED_GUIDED_ROCKET + in_saveData.SaveDataIndex.ToString(), in_saveData.UpgradedGuidedRocket);

            //SetBool(UNLOCKED_THRUST_JUMP + in_saveData.SaveDataIndex.ToString(), in_saveData.UnlockedThrustJump);
            //SetBool(UPGRADED_THRUST_JUMP + in_saveData.SaveDataIndex.ToString(), in_saveData.UpgradedThrustJump);
            //SetBool(UNLOCKED_GRAPPLE_HOOK + in_saveData.SaveDataIndex.ToString(), in_saveData.UnlockedGrappleHook);
            //SetBool(UPGRADED_GRAPPLE_HOOK + in_saveData.SaveDataIndex.ToString(), in_saveData.UpgradedGrappleHook);

            //SetBool(UNLOCKED_HEALTH_UPGRADE_1 + in_saveData.SaveDataIndex.ToString(), in_saveData.HealthUpgrade1);
            //SetBool(UNLOCKED_HEALTH_UPGRADE_2 + in_saveData.SaveDataIndex.ToString(), in_saveData.HealthUpgrade2);
            //SetBool(UNLOCKED_HEALTH_UPGRADE_3 + in_saveData.SaveDataIndex.ToString(), in_saveData.HealthUpgrade3);
            //SetBool(UNLOCKED_HEALTH_UPGRADE_4 + in_saveData.SaveDataIndex.ToString(), in_saveData.HealthUpgrade4);

            //SetBool(DEATH_HINT_ACTIVATED + in_saveData.SaveDataIndex.ToString(), in_saveData.DeathHintActivated);
            //SetBool(DEFEATED_BOSS + in_saveData.SaveDataIndex.ToString(), in_saveData.DefeatedBoss);
            #endregion
        }
        private IEnumerator SafeOnAnimationEvent(object in_name)
        {
            yield return(YieldFactory.GetWaitForEndOfFrame());

            string name = (string)in_name;

            if (m_arEventListeners != null && m_arEventListeners.ContainsKey(name))
            {
                m_arEventListeners[name](this);
            }
            else
            {
                GDebug.LogWarning("Animation event key not found : " + name, gameObject);
            }
        }
示例#15
0
        public void OnConnected()
        {
            if (!IsConnected())
            {
                return;
            }

            GDebug.Log("TCP连接成功");
            bool success = false;

            try
            {
                //成功, 设置属性
                SetProperty();
                success = true;
            }
            catch (Exception ex)
            {
                success = false;
                if (ex is SocketException)
                {
                    SocketException socketExcept = (SocketException)ex;
                    if (socketExcept.ErrorCode == 10055)//No buffer space available. iphone4上会出这个异常,但实际连接还是有效
                    {
                        success = true;
                    }
                }
            }
            finally
            {
                if (success)
                {
                    ChangeState(NETWORKSTATE.NS_CONNECTED);
                    netStream = tcpClient.GetStream();
                    if (IsUseThread())
                    {
                        Run();
                    }
                }
                else
                {
                    SetConnectErrState("设置属性失败");
                }
            }
        }
示例#16
0
        public static void Delete(int in_iSaveDataIndex)
        {
            try
            {
                if (!FileExist(in_iSaveDataIndex))
                {
                    return;
                }

                string filePath = GetSaveFilePath(in_iSaveDataIndex);

                if (File.Exists(filePath))
                {
                    File.Delete(filePath);
                }
            }
            catch (Exception ex)
            {
                GDebug.LogError("Error while deleting save file - " + SaveDataPaths[in_iSaveDataIndex] + ":\n" + ex.Message);
            }

            #region Delete (Old PlayerPrefs code)
            //PlayerPrefs.DeleteKey(UNLOCKED_REI + in_iSaveDataIndex.ToString());
            //PlayerPrefs.DeleteKey(UPGRADED_REI + in_iSaveDataIndex.ToString());
            //PlayerPrefs.DeleteKey(UNLOCKED_GUIDED_ROCKET + in_iSaveDataIndex.ToString());
            //PlayerPrefs.DeleteKey(UPGRADED_GUIDED_ROCKET + in_iSaveDataIndex.ToString());

            //PlayerPrefs.DeleteKey(UNLOCKED_THRUST_JUMP + in_iSaveDataIndex.ToString());
            //PlayerPrefs.DeleteKey(UPGRADED_THRUST_JUMP + in_iSaveDataIndex.ToString());
            //PlayerPrefs.DeleteKey(UNLOCKED_GRAPPLE_HOOK + in_iSaveDataIndex.ToString());
            //PlayerPrefs.DeleteKey(UPGRADED_GRAPPLE_HOOK + in_iSaveDataIndex.ToString());

            //PlayerPrefs.DeleteKey(UNLOCKED_HEALTH_UPGRADE_1 + in_iSaveDataIndex.ToString());
            //PlayerPrefs.DeleteKey(UNLOCKED_HEALTH_UPGRADE_2 + in_iSaveDataIndex.ToString());
            //PlayerPrefs.DeleteKey(UNLOCKED_HEALTH_UPGRADE_3 + in_iSaveDataIndex.ToString());
            //PlayerPrefs.DeleteKey(UNLOCKED_HEALTH_UPGRADE_4 + in_iSaveDataIndex.ToString());

            //PlayerPrefs.DeleteKey(DEATH_HINT_ACTIVATED + in_iSaveDataIndex.ToString());
            //PlayerPrefs.DeleteKey(DEFEATED_BOSS + in_iSaveDataIndex.ToString());
            //PlayerPrefs.DeleteKey(SAVE_POINT + in_iSaveDataIndex.ToString());
            //PlayerPrefs.DeleteKey(SECURITY_CLEARANCE + in_iSaveDataIndex.ToString());

            //PlayerPrefs.DeleteKey(SAVE_DATA_EXISTS + in_iSaveDataIndex.ToString());
            #endregion
        }
示例#17
0
        void Awake()
        {
            TypeName = typeof(T).FullName;
            GDebug.Log(TypeName + ": Awake");

            if (Instance != null)
            {
                if (Instance != this)
                {
                    Destroy(gameObject);
                }
                return;
            }

            Instance = this as T;

            GameSetup();
        }
示例#18
0
        public static SaveData Load(int in_iSaveDataIndex)
        {
            if (!FileExist(in_iSaveDataIndex))
            {
                return(new SaveData(in_iSaveDataIndex));
            }

            SaveData toReturn = null;

            try
            {
                GDebug.Log("Reading game data from file: " + SaveDataPaths[in_iSaveDataIndex]);

                StreamReader file = new StreamReader(SaveDataPaths[in_iSaveDataIndex]);
                string       json = file.ReadToEnd();
                file.Close();

                toReturn = JsonUtility.FromJson <SaveData>(json);
            }
            catch (Exception ex)
            {
                GDebug.LogError("Error reading file - " + SaveDataPaths[in_iSaveDataIndex] + ":\n" + ex.Message);
            }

            // toReturn could possibly be null after file read
            if (toReturn == null)
            {
                toReturn = new SaveData(in_iSaveDataIndex);
            }
            // If we do have a valid save file, perform any validations needed in here.
            else
            {
                // Make sure DataPools are valid
                //if (toReturn.DataPools == null || toReturn.DataPools.Length != (int)DataPoolID.MAX_POOL_IDS)
                //{
                //    // TODO If the data is invalid, should we cancel loading the file? Reset the save data?
                //}
            }

            // Test incoming SaveData
            //CDebug.Log(toReturn.ToString());

            #region Load (Old PlayerPrefs code)
            //Dictionary<string, object> objects = new Dictionary<string, object>();
            //objects.Add(UNLOCKED_REI, GetBool(UNLOCKED_REI + in_iSaveDataIndex.ToString()));
            //objects.Add(UPGRADED_REI, GetBool(UPGRADED_REI + in_iSaveDataIndex.ToString()));
            //objects.Add(UNLOCKED_GUIDED_ROCKET, GetBool(UNLOCKED_GUIDED_ROCKET + in_iSaveDataIndex.ToString()));
            //objects.Add(UPGRADED_GUIDED_ROCKET, GetBool(UPGRADED_GUIDED_ROCKET + in_iSaveDataIndex.ToString()));

            //objects.Add(UNLOCKED_THRUST_JUMP, GetBool(UNLOCKED_THRUST_JUMP + in_iSaveDataIndex.ToString()));
            //objects.Add(UPGRADED_THRUST_JUMP, GetBool(UPGRADED_THRUST_JUMP + in_iSaveDataIndex.ToString()));
            //objects.Add(UNLOCKED_GRAPPLE_HOOK, GetBool(UNLOCKED_GRAPPLE_HOOK + in_iSaveDataIndex.ToString()));
            //objects.Add(UPGRADED_GRAPPLE_HOOK, GetBool(UPGRADED_GRAPPLE_HOOK + in_iSaveDataIndex.ToString()));

            //objects.Add(UNLOCKED_HEALTH_UPGRADE_1, GetBool(UNLOCKED_HEALTH_UPGRADE_1 + in_iSaveDataIndex.ToString()));
            //objects.Add(UNLOCKED_HEALTH_UPGRADE_2, GetBool(UNLOCKED_HEALTH_UPGRADE_2 + in_iSaveDataIndex.ToString()));
            //objects.Add(UNLOCKED_HEALTH_UPGRADE_3, GetBool(UNLOCKED_HEALTH_UPGRADE_3 + in_iSaveDataIndex.ToString()));
            //objects.Add(UNLOCKED_HEALTH_UPGRADE_4, GetBool(UNLOCKED_HEALTH_UPGRADE_4 + in_iSaveDataIndex.ToString()));

            //objects.Add(DEATH_HINT_ACTIVATED, GetBool(DEATH_HINT_ACTIVATED + in_iSaveDataIndex.ToString()));
            //objects.Add(DEFEATED_BOSS, GetBool(DEFEATED_BOSS + in_iSaveDataIndex.ToString()));

            //objects.Add(SAVE_POINT, GetInt(SAVE_POINT));
            //objects.Add(SECURITY_CLEARANCE, GetInt(SECURITY_CLEARANCE));

            //SaveData toReturn = new SaveData(in_iSaveDataIndex, objects);
            #endregion

            return(toReturn);
        }
示例#19
0
 protected virtual void OnApplicationPause(bool pauseStatus)
 {
     GDebug.Log(TypeName + ": OnApplicationPause");
     SaveState();
 }
示例#20
0
 /// <summary>
 /// Save any state when the application quits.
 /// </summary>
 /// Note that iOS applications are usually suspended and do not quit. You should tick "Exit on Suspend" in Player settings
 /// for iOS builds to cause the game to quit and not suspend, otherwise you may not see this call. If "Exit on Suspend" is
 /// not ticked then you will see calls to OnApplicationPause instead.
 protected virtual void OnApplicationQuit()
 {
     GDebug.Log(TypeName + ": OnApplicationQuit");
     SaveState();
 }