/// <summary> /// 先检测远端玩家都准备好没,如果没有发送196事件 告诉所有远端玩家我已经准备好了 /// </summary> private void CheckGameStart() { //默认是ReplayMode.NO_REPLAY if (this.replayMode == ReplayMode.LOAD_REPLAY) { this.RunSimulation(false); } else { //检测所有玩家是否已经准备好了 bool flag = true; int i = 0; int count = this.activePlayers.Count; while (i < count) { flag &= this.activePlayers[i].sentSyncedStart; i++; } //所有玩家已经准备好了 if (flag) { this.RunSimulation(false); SyncedData.pool.FillStack(this.activePlayers.Count * (this.syncWindow + this.rollbackWindow)); } //发送196事件,告诉远端玩家 我已经准备好了 else { this.RaiseEvent(SYNCED_GAME_START_CODE, SyncedInfo.Encode(new SyncedInfo { playerId = this.localPlayer.ID })); } } }
// 检查游戏是否可以开始 private void CheckGameStart() { bool flag = this.replayMode == ReplayMode.LOAD_REPLAY; if (flag) // 加载回放模式 { this.RunSimulation(false); } else // 非加载回放模式 { bool flag2 = true; int i = 0; int count = this.activePlayers.Count; while (i < count) { flag2 &= this.activePlayers[i].sentSyncedStart; i++; } bool flag3 = flag2; if (flag3) { this.RunSimulation(false); SyncedData.pool.FillStack(this.activePlayers.Count * (this.syncWindow + this.rollbackWindow)); } else { this.RaiseEvent(196, SyncedInfo.Encode(new SyncedInfo { playerId = this.localPlayer.ID })); } } }
private void CheckGameStart() { bool flag = this.replayMode == ReplayMode.LOAD_REPLAY; if (flag) { this.RunSimulation(false); } else { bool flag2 = true; foreach (TSPlayer current in this.players.Values) { flag2 &= current.sentSyncedStart; } bool flag3 = flag2; if (flag3) { this.RunSimulation(false); } else { this.RaiseEvent(196, SyncedInfo.Encode(new SyncedInfo { playerId = this.localPlayer.ID })); } } }
public static byte[] Encode(SyncedInfo info) { List <byte> list = new List <byte>(); list.Add(info.playerId); if (info.checksum != null) { list.AddRange(BitConverter.GetBytes(info.tick)); list.AddRange(Encoding.ASCII.GetBytes(info.checksum)); } return(list.ToArray()); }
/// <summary> /// 对刚体body的位置和旋转数据的一个同步校验 /// </summary> /// <param name="tick"></param> private void SendInfoChecksum(int tick) { Debug.Log("SendInfoChecksum"); if (replayMode != ReplayMode.LOAD_REPLAY) { SyncedInfo syncedInfo = bufferSyncedInfo.Current(); syncedInfo.playerId = localPlayer.ID; syncedInfo.tick = tick; syncedInfo.checksum = GetChecksumForSyncedInfo(); bufferSyncedInfo.MoveNext(); RaiseEvent(CHECKSUM_CODE, SyncedInfo.Encode(syncedInfo)); } }
private void SendInfoChecksum(int tick) { bool flag = this.replayMode == ReplayMode.LOAD_REPLAY; if (!flag) { SyncedInfo syncedInfo = this.bufferSyncedInfo.Current(); syncedInfo.playerId = this.localPlayer.ID; syncedInfo.tick = tick; syncedInfo.checksum = this.GetChecksumForSyncedInfo(); this.bufferSyncedInfo.MoveNext(); this.RaiseEvent(198, SyncedInfo.Encode(syncedInfo)); } }
public static SyncedInfo Decode(byte[] infoBytes) { SyncedInfo syncedInfo = new SyncedInfo(); int num = 0; syncedInfo.playerId = infoBytes[num++]; if (num < infoBytes.Length) { syncedInfo.tick = BitConverter.ToInt32(infoBytes, num); num += 4; syncedInfo.checksum = Encoding.ASCII.GetString(infoBytes, num, 32); } return(syncedInfo); }
private void OnChecksumReceived(SyncedInfo syncedInfo) { bool dropped = this.players[syncedInfo.playerId].dropped; if (!dropped) { this.checksumOk = true; SyncedInfo[] buffer = this.bufferSyncedInfo.buffer; for (int i = 0; i < buffer.Length; i++) { SyncedInfo syncedInfo2 = buffer[i]; bool flag = syncedInfo2.tick == syncedInfo.tick && syncedInfo2.checksum != syncedInfo.checksum; if (flag) { this.checksumOk = false; break; } } } }
private void CheckGameStart() { Debug.Log("CheckGameStart"); if (replayMode == ReplayMode.LOAD_REPLAY) { RunSimulation(false); } else { //检测所有玩家是否已经准备好了 bool flag = true; int i = 0; int count = activePlayers.Count; while (i < count) { flag &= activePlayers[i].sentSyncedStart; i++; } //所有玩家已经准备好了 if (flag) { RunSimulation(false); SyncedData.pool.FillStack(activePlayers.Count * (syncWindow + rollbackWindow)); } //其他玩家还没有准备好,发送196事件 else { RaiseEvent(SYNCED_GAME_START_CODE, SyncedInfo.Encode(new SyncedInfo { playerId = this.localPlayer.ID })); } } }
private void OnEventDataReceived(byte eventCode, object content) { bool flag = eventCode == 199; if (flag) // 接收操作数据 { byte[] data = content as byte[]; List <SyncedData> list = SyncedData.Decode(data); // 解码 bool flag2 = list.Count > 0; if (flag2) // 有数据 { TSPlayer tSPlayer = this.players[list[0].inputData.ownerID]; // 找到对应的玩家 bool flag3 = !tSPlayer.dropped; if (flag3) // 该玩家没有掉线 { this.OnSyncedDataReceived(tSPlayer, list); // 处理同步数据 bool flag4 = list[0].dropPlayer && tSPlayer.ID != this.localPlayer.ID && !this.players[list[0].dropFromPlayerId].dropped; if (flag4) { tSPlayer.dropCount++; // 统计掉线玩家数量 } } else // 该玩家掉线了,回收数据对象 { int i = 0; int count = list.Count; while (i < count) { SyncedData.pool.GiveBack(list[i]); i++; } } SyncedData.poolList.GiveBack(list); } } else { bool flag5 = eventCode == 198; if (flag5) { byte[] infoBytes = content as byte[]; this.OnChecksumReceived(SyncedInfo.Decode(infoBytes)); } else { bool flag6 = eventCode == 197; if (flag6) { byte[] array = content as byte[]; bool flag7 = array.Length != 0; if (flag7) { bool flag8 = array[0] == 0; if (flag8) { this.Pause(); } else { bool flag9 = array[0] == 1; if (flag9) { this.Run(); } else { bool flag10 = array[0] == 3; if (flag10) { this.End(); } } } } } else { bool flag11 = eventCode == 196; if (flag11) { byte[] infoBytes2 = content as byte[]; SyncedInfo syncedInfo = SyncedInfo.Decode(infoBytes2); this.players[syncedInfo.playerId].sentSyncedStart = true; } } } } }
private void OnEventDataReceived(byte eventCode, object content) { // 199 bool flag = eventCode == SEND_CODE; if (flag) { byte[] data = content as byte[]; List <SyncedData> list = SyncedData.Decode(data); bool flag2 = list.Count > 0; if (flag2) { TSPlayer tSPlayer = this.players[list[0].inputData.ownerID]; bool flag3 = !tSPlayer.dropped; if (flag3) { this.OnSyncedDataReceived(tSPlayer, list); bool flag4 = list[0].dropPlayer && tSPlayer.ID != this.localPlayer.ID && !this.players[list[0].dropFromPlayerId].dropped; if (flag4) { tSPlayer.dropCount++; } } else { int i = 0; int count = list.Count; while (i < count) { SyncedData.pool.GiveBack(list[i]); i++; } } SyncedData.poolList.GiveBack(list); } } else { // 198 bool flag5 = eventCode == CHECKSUM_CODE; if (flag5) { byte[] infoBytes = content as byte[]; this.OnChecksumReceived(SyncedInfo.Decode(infoBytes)); } else { // 197 bool flag6 = eventCode == SIMULATION_CODE; if (flag6) { byte[] array = content as byte[]; bool flag7 = array.Length != 0; if (flag7) { bool flag8 = array[0] == 0; if (flag8) { this.Pause(); } else { bool flag9 = array[0] == 1; if (flag9) { this.Run(); } else { bool flag10 = array[0] == 3; if (flag10) { this.End(); } } } } } else { // 196 bool flag11 = eventCode == SYNCED_GAME_START_CODE; if (flag11) { byte[] infoBytes2 = content as byte[]; SyncedInfo syncedInfo = SyncedInfo.Decode(infoBytes2); this.players[syncedInfo.playerId].sentSyncedStart = true; } } } } }
/// <summary> /// 接收网络数据 /// </summary> /// <param name="eventCode"></param> /// <param name="content"></param> private void OnEventDataReceived(byte eventCode, object content) { if (eventCode == SEND_CODE) { byte[] data = content as byte[]; //一次只接收一个玩家的数据 List <SyncedData> list = SyncedData.Decode(data); //目前该list的容量是1,并且该list是由SyncedData.poolList.GetNew();来创建的,所以后面要用SyncedData.pool.GiveBack来销毁 //注意list是网络数据 if (list.Count > 0) { //取出对应的远端玩家数据来更新 TSPlayer tSPlayer = this.players[list[0].inputData.ownerID]; if (!tSPlayer.dropped) { this.OnSyncedDataReceived(tSPlayer, list); //调用TSPlayer.controls添加数据 //满足三个条件,dropCount才可以增加 //网络数据的dropPlayer必须为true,网络过来的玩家不是本地玩家,提取网络数据中的dropFromPlayerId,查找players,其对应的dropped为false //这里有dropPlayer dropped dropFromPlayerId 需要理解区分 if (list[0].dropPlayer && tSPlayer.ID != this.localPlayer.ID && !this.players[list[0].dropFromPlayerId].dropped) { tSPlayer.dropCount++; } } else { int i = 0; int count = list.Count; while (i < count) { SyncedData.pool.GiveBack(list[i]); //掉线直接回收 i++; } } SyncedData.poolList.GiveBack(list); } } else { bool flag5 = eventCode == CHECKSUM_CODE; if (flag5) { byte[] infoBytes = content as byte[]; this.OnChecksumReceived(SyncedInfo.Decode(infoBytes)); } else { //197事件,内容只有3种:0:pause,1:Run,3:End if (eventCode == SIMULATION_CODE) { byte[] array = content as byte[]; if (array.Length != 0) { switch (array[0]) { case 0: this.Pause(); break; case 1: Run(); break; case 3: End(); break; } } } //196事件 远程玩家准备好事件 else { //远程玩家准备好事件 if (eventCode == SYNCED_GAME_START_CODE) { byte[] infoBytes2 = content as byte[]; SyncedInfo syncedInfo = SyncedInfo.Decode(infoBytes2); this.players[syncedInfo.playerId].sentSyncedStart = true; //玩家准备好标志 } } } } }
private void OnEventDataReceived(byte eventCode, object content) { bool flag = eventCode == 199; if (flag) { byte[] data = content as byte[]; SyncedData[] array = SyncedData.Decode(data); bool flag2 = array.Length != 0; if (flag2) { TSPlayer tSPlayer = this.players[array[0].inputData.ownerID]; bool flag3 = !tSPlayer.dropped; if (flag3) { this.OnSyncedDataReceived(tSPlayer, array); bool flag4 = array[0].dropPlayer && tSPlayer.ID != this.localPlayer.ID && !this.players[array[0].dropFromPlayerId].dropped; if (flag4) { tSPlayer.dropCount++; } } } } else { bool flag5 = eventCode == 198; if (flag5) { byte[] infoBytes = content as byte[]; this.OnChecksumReceived(SyncedInfo.Decode(infoBytes)); } else { bool flag6 = eventCode == 197; if (flag6) { byte[] array2 = content as byte[]; bool flag7 = array2.Length != 0; if (flag7) { bool flag8 = array2[0] == 0; if (flag8) { this.Pause(); } else { bool flag9 = array2[0] == 1; if (flag9) { this.Run(); } else { bool flag10 = array2[0] == 3; if (flag10) { this.End(); } } } } } else { bool flag11 = eventCode == 196; if (flag11) { byte[] infoBytes2 = content as byte[]; SyncedInfo syncedInfo = SyncedInfo.Decode(infoBytes2); this.players[syncedInfo.playerId].sentSyncedStart = true; } } } } }
private void OnEventDataReceived(byte eventCode, object content) { Debug.LogFormat("OnEventDataReceived eventCode is {0}", eventCode); if (eventCode == SEND_CODE) { byte[] data = content as byte[]; List <SyncedData> list = SyncedData.Decode(data);//只有list[0]是携带玩家ownerID数据,剩余的list都是该玩家的数据 if (list.Count > 0) { TSPlayer tSPlayer = players[list[0].inputData.ownerID]; if (!tSPlayer.dropped) { OnSyncedDataReceived(tSPlayer, list);//调用TSPlayer.addData添加数据 //满足三个条件,dropCount才可以增加 //网络数据的dropPlayer必须为true,网络过来的玩家不是本地玩家,提取网络数据中的dropFromPlayerId,查找players,其对应的dropped为false //这里有dropPlayer dropped dropFromPlayerId 需要理解区分 if (list[0].dropPlayer && tSPlayer.ID != localPlayer.ID && !players[list[0].dropFromPlayerId].dropped) { tSPlayer.dropCount++; } } else { int i = 0; int count = list.Count; while (i < count) { SyncedData.pool.GiveBack(list[i]); i++; } } SyncedData.poolList.GiveBack(list); } } else { if (eventCode == CHECKSUM_CODE) { byte[] infoBytes = content as byte[]; this.OnChecksumReceived(SyncedInfo.Decode(infoBytes)); } else { if (eventCode == SIMULATION_CODE) { byte[] array = content as byte[]; if (array.Length != 0) { if (array[0] == 0) { Pause(); } else { if (array[0] == 1) { Run(); } else { if (array[0] == 3) { End(); } } } } } else { if (eventCode == SYNCED_GAME_START_CODE) { byte[] infoBytes2 = content as byte[]; SyncedInfo syncedInfo = SyncedInfo.Decode(infoBytes2); players[syncedInfo.playerId].sentSyncedStart = true; } } } } }