void Start() { instance = this; Application.runInBackground = true; if (ReplayRecord.replayMode == ReplayMode.LOAD_REPLAY) { ReplayRecord replayRecord = ReplayRecord.replayToLoad; if (replayRecord == null) { Debug.LogError("Replay Record can't be loaded"); gameObject.SetActive(false); return; } } ICommunicator communicator = null; if (!PhotonNetwork.connected || !PhotonNetwork.inRoom) { Debug.LogWarning("You are not connected to Photon. TrueSync will start in offline mode."); } else { communicator = new PhotonTrueSyncCommunicator(PhotonNetwork.networkingPeer); } TrueSyncConfig activeConfig = ActiveConfig; lockstep = AbstractLockstep.NewInstance( lockedTimeStep, communicator, PhysicsManager.instance, activeConfig.syncWindow, activeConfig.panicWindow, activeConfig.rollbackWindow, OnGameStarted, OnGamePaused, OnGameUnPaused, OnGameEnded, OnPlayerDisconnection, OnStepUpdate, GetLocalData ); if (activeConfig.showStats) { this.gameObject.AddComponent <TrueSyncStats>().Lockstep = lockstep; } scheduler = new CoroutineScheduler(lockstep); if (ReplayRecord.replayMode != ReplayMode.LOAD_REPLAY) { if (communicator == null) { lockstep.AddPlayer(0, "Local_Player", true); } else { List <PhotonPlayer> players = new List <PhotonPlayer>(PhotonNetwork.playerList); players.Sort(UnityUtils.playerComparer); foreach (PhotonPlayer p in players) { lockstep.AddPlayer((byte)p.ID, p.NickName, p.IsLocal); } } } generalBehaviours = new List <TrueSyncManagedBehaviour>(); foreach (TrueSyncBehaviour tsb in FindObjectsOfType <TrueSyncBehaviour>()) { generalBehaviours.Add(NewManagedBehavior(tsb)); } initBehaviors(); initGeneralBehaviors(generalBehaviours, false); PhysicsManager.instance.OnRemoveBody(OnRemovedRigidBody); startState = StartState.BEHAVIOR_INITIALIZED; }
void Start() { instance = this; Application.runInBackground = true; ICommunicator communicator = null; if (!PhotonNetwork.connected || !PhotonNetwork.inRoom) { Debug.LogWarning("You are not connected to Photon. TrueSync will start in offline mode."); } else { communicator = new PhotonTrueSyncCommunicator(PhotonNetwork.networkingPeer); } TrueSyncConfig activeConfig = ActiveConfig; //帧同步逻辑的回调都交由TrueSyncManagedBehaviour执行 //例如:AbstractLockstep.OnGameStarted->TrueSyncManager.OnGameStarted->TrueSyncManagedBehaviour.OnGameStarted()(这是个staic method,之后遍历每一个TrueSyncManagedBehaviour实例的OnSyncedStart) lockstep = AbstractLockstep.NewInstance( lockedTimeStep.AsFloat(), communicator, PhysicsManager.instance, activeConfig.syncWindow, activeConfig.panicWindow, activeConfig.rollbackWindow, OnGameStarted, OnGamePaused, OnGameUnPaused, OnGameEnded, OnPlayerDisconnection, OnStepUpdate, GetLocalData, ProvideInputData ); if (ReplayRecord.replayMode == ReplayMode.LOAD_REPLAY) { ReplayPicker.replayToLoad.Load(); ReplayRecord replayRecord = ReplayRecord.replayToLoad; if (replayRecord == null) { Debug.LogError("Replay Record can't be loaded"); gameObject.SetActive(false); return; } else { lockstep.ReplayRecord = replayRecord; } } if (activeConfig.showStats) { this.gameObject.AddComponent <TrueSyncStats>().Lockstep = lockstep; } scheduler = new CoroutineScheduler(lockstep); if (ReplayRecord.replayMode != ReplayMode.LOAD_REPLAY) { if (communicator == null) { lockstep.AddPlayer(0, "Local_Player", true); } else { List <PhotonPlayer> players = new List <PhotonPlayer>(PhotonNetwork.playerList); players.Sort(UnityUtils.playerComparer); for (int index = 0, length = players.Count; index < length; index++) { PhotonPlayer p = players[index]; lockstep.AddPlayer((byte)p.ID, p.NickName, p.IsLocal); } } } TrueSyncBehaviour[] behavioursArray = FindObjectsOfType <TrueSyncBehaviour>(); for (int index = 0, length = behavioursArray.Length; index < length; index++) { generalBehaviours.Add(NewManagedBehavior(behavioursArray[index])); } initBehaviors(); initGeneralBehaviors(generalBehaviours, false); PhysicsManager.instance.OnRemoveBody(OnRemovedRigidBody); startState = StartState.BEHAVIOR_INITIALIZED; }
// LOGIC public void Initialize(TrueSyncConfig i_Config = null) { TrueSyncConfig config = (i_Config != null) ? i_Config : (Resources.Load <TrueSyncConfig>(s_TrueSyncConfigResourcePath)); // Load config. m_Config = config; if (config == null) { return; } m_LockedTimeStep = config.lockedTimeStep; // Init state tracker. StateTracker.Init(config.rollbackWindow); // Init time. m_TimeScaler = new TSTimeScaler(); m_TimeScaler.Init(); // Create physics world. IPhysicsManager worldManager = new Physics2DWorldManager(); worldManager.Gravity = new TSVector(config.gravity.x, config.gravity.y, 0); worldManager.SpeculativeContacts = config.speculativeContacts; worldManager.LockedTimeStep = config.lockedTimeStep; worldManager.Init(); // Create communicator. ICommunicator communicator = null; if (isOnline) { communicator = new PhotonTrueSyncCommunicator(PhotonNetwork.networkingPeer); } else { Debug.Log("You are not connected to Photon. TrueSync will start in offline mode."); } // Create lockstep. m_Lockstep = AbstractLockstep.NewInstance(m_LockedTimeStep, communicator, worldManager, m_Config.syncWindow, m_Config.panicWindow, m_Config.rollbackWindow, OnGameStarted, OnGamePaused, OnGameUnPaused, OnGameEnded, OnPlayerDisconnection, OnStepUpdate, GetLocalData); Debug.Log("Lockstep initialized (Sync: " + m_Config.syncWindow + ", Rollback: " + m_Config.rollbackWindow + ")"); // Stats if (m_Config.showStats) { TrueSyncStats statsComponent = gameObject.AddComponent <TrueSyncStats>(); statsComponent.Lockstep = m_Lockstep; } // Add player on lockestep. if (isOnline) { List <PhotonPlayer> photonPlayers = new List <PhotonPlayer>(PhotonNetwork.playerList); photonPlayers.Sort(UnityUtils.playerComparer); for (int photonPlayerIndex = 0; photonPlayerIndex < photonPlayers.Count; ++photonPlayerIndex) { PhotonPlayer photonPlayer = photonPlayers[photonPlayerIndex]; m_Lockstep.AddPlayer((byte)photonPlayer.ID, photonPlayer.NickName, photonPlayer.IsLocal); } } else { m_Lockstep.AddPlayer(0, "Local_Player", true); } // Fill behaviours per player dictionary. foreach (TSPlayer player in m_Lockstep.Players.Values) { List <TrueSyncManagedBehaviour> behaviours = new List <TrueSyncManagedBehaviour>(); m_BehavioursPerPlayer.Add(player.ID, behaviours); } // Initialize Physics Manager. PhysicsManager.Initialize(worldManager); PhysicsManager.OnRemoveBody(OnRemovedRigidbody); }
void Start() { instance = this; Application.runInBackground = true; //离线状态 就AbstractLockstep的OnEventDataReceived接收网络数据不会执行 //更换网络接口 可以从ICommunicator接口 入手 ICommunicator communicator = null; if (!PhotonNetwork.connected || !PhotonNetwork.inRoom) { Debug.LogWarning("You are not connected to Photon. TrueSync will start in offline mode."); } else { communicator = new PhotonTrueSyncCommunicator(PhotonNetwork.networkingPeer); } TrueSyncConfig activeConfig = ActiveConfig; lockstep = AbstractLockstep.NewInstance( lockedTimeStep.AsFloat(), communicator, PhysicsManager.instance, activeConfig.syncWindow, activeConfig.panicWindow, activeConfig.rollbackWindow, OnGameStarted, OnGamePaused, OnGameUnPaused, OnGameEnded, OnPlayerDisconnection, OnStepUpdate, GetLocalData, ProvideInputData ); if (ReplayRecord.replayMode == ReplayMode.LOAD_REPLAY) { ReplayPicker.replayToLoad.Load(); ReplayRecord replayRecord = ReplayRecord.replayToLoad; if (replayRecord == null) { Debug.LogError("Replay Record can't be loaded"); gameObject.SetActive(false); return; } else { lockstep.ReplayRecord = replayRecord; } } if (activeConfig.showStats) { this.gameObject.AddComponent <TrueSyncStats>().Lockstep = lockstep; } scheduler = new CoroutineScheduler(lockstep); if (ReplayRecord.replayMode != ReplayMode.LOAD_REPLAY) { if (communicator == null) { lockstep.AddPlayer(0, "Local_Player", true); } else { List <PhotonPlayer> players = new List <PhotonPlayer>(PhotonNetwork.playerList); players.Sort(UnityUtils.playerComparer); for (int index = 0, length = players.Count; index < length; index++) { PhotonPlayer p = players[index]; lockstep.AddPlayer((byte)p.ID, p.NickName, p.IsLocal); //更新players activePlayers } } } //搜寻场景预先挂载的TrueSyncBehaviour脚本,为它生成TrueSyncManagedBehaviour脚本 //可能这部分脚本有些属于玩家 有些属于公共部分,都根据OwnerIndex来区分 TrueSyncBehaviour[] behavioursArray = FindObjectsOfType <TrueSyncBehaviour>(); for (int index = 0, length = behavioursArray.Length; index < length; index++) { generalBehaviours.Add(NewManagedBehavior(behavioursArray[index]));//一个TrueSyncBehaviour对应TrueSyncManagedBehaviour } initBehaviors(); //初始化玩家prefab和挂在prefab上的TrueSyncBehaviour initGeneralBehaviors(generalBehaviours, false); //公用和玩家的区分,公用分给generalBehaviours,玩家分给behaviorsByPlayer PhysicsManager.instance.OnRemoveBody(OnRemovedRigidBody); startState = StartState.BEHAVIOR_INITIALIZED;//初始化完毕状态 }