private void initBehaviors() { behaviorsByPlayer = new Dictionary <byte, List <FrameSyncManagedBehaviour> >(); List <FPVector> playerPosition = new List <FPVector>(); playerPosition.Add(new FPVector(-5.0f, 0, 0)); playerPosition.Add(new FPVector(-5.0f, 0, 5.0f)); int playerIndex = 0; var playersEnum = lockstep.Players.GetEnumerator(); while (playersEnum.MoveNext()) { FPPlayer p = playersEnum.Current.Value; List <FrameSyncManagedBehaviour> behaviorsInstatiated = new List <FrameSyncManagedBehaviour>(); for (int index = 0, length = playerPrefabs.Length; index < length; index++) { GameObject prefab = playerPrefabs[index]; GameObject prefabInst = Instantiate(prefab); prefabInst.transform.position = playerPosition[playerIndex].ToVector(); InitializeGameObject(prefabInst, prefabInst.transform.position.ToFPVector(), prefabInst.transform.rotation.ToFPQuaternion()); FrameSyncBehaviour[] behaviours = prefabInst.GetComponentsInChildren <FrameSyncBehaviour>(); for (int index2 = 0, length2 = behaviours.Length; index2 < length2; index2++) { FrameSyncBehaviour behaviour = behaviours[index2]; behaviour.owner = p.playerInfo; behaviour.localOwner = lockstep.LocalPlayer.playerInfo; behaviour.numberOfPlayers = lockstep.Players.Count; FrameSyncManagedBehaviour tsmb = NewManagedBehavior(behaviour); tsmb.owner = behaviour.owner; tsmb.localOwner = behaviour.localOwner; behaviorsInstatiated.Add(tsmb); } } behaviorsByPlayer.Add(p.ID, behaviorsInstatiated); playerIndex++; } }
// 添加玩家 public void AddPlayer(byte playerId, string playerName, bool isLocal) { FPPlayer tSPlayer = new FPPlayer(playerId, playerName); this.players.Add(tSPlayer.ID, tSPlayer); this.activePlayers.Add(tSPlayer); if (isLocal) // 本地玩家 { this.localPlayer = tSPlayer; this.localPlayer.sentSyncedStart = true; } this.UpdateActivePlayers(); bool flag = this.replayMode == ReplayMode.RECORD_REPLAY; if (flag) { this.replayRecord.AddPlayer(tSPlayer); } }
private void DropLagPlayers() { List <FPPlayer> list = new List <FPPlayer>(); int refTick = this.GetRefTick(this.GetSyncedDataTick()); bool flag = refTick >= 0; if (flag) { int i = 0; int count = this.activePlayers.Count; while (i < count) { FPPlayer tSPlayer = this.activePlayers[i]; bool flag2 = !tSPlayer.IsDataReady(refTick); if (flag2) { tSPlayer.dropCount++; list.Add(tSPlayer); } i++; } } int j = 0; int count2 = list.Count; while (j < count2) { FPPlayer p = list[j]; this.CheckDrop(p); bool sendDataForDrop = list[j].GetSendDataForDrop(this.localPlayer.ID, this._syncedDataCacheDrop); if (sendDataForDrop) { this.communicator.OpRaiseEvent(199, SyncedData.Encode(this._syncedDataCacheDrop), true, null); SyncedData.pool.GiveBack(this._syncedDataCacheDrop[0]); } j++; } }
private static byte[] ToReplayBytes(ReplayRecord replay) { List <byte> list = new List <byte>(); Dictionary <byte, FPPlayer> .Enumerator enumerator = replay.players.GetEnumerator(); while (enumerator.MoveNext()) { KeyValuePair <byte, FPPlayer> current = enumerator.Current; FPPlayer value = current.Value; list.Add(value.playerInfo.id); Utils.GetBytes(value.playerInfo.name.Length, list); list.AddRange(Encoding.ASCII.GetBytes(value.playerInfo.name)); Utils.GetBytes(value.controls.Count, list); Dictionary <int, SyncedData> .Enumerator enumerator2 = value.controls.GetEnumerator(); while (enumerator2.MoveNext()) { KeyValuePair <int, SyncedData> current2 = enumerator2.Current; Utils.GetBytes(current2.Key, list); current2 = enumerator2.Current; current2.Value.inputData.Serialize(list); } } return(list.ToArray()); }
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) // 有数据 { FPPlayer 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; } } } } }
protected abstract void OnSyncedDataReceived(FPPlayer player, List <SyncedData> data);
internal void AddPlayer(FPPlayer player) { this.players[player.ID] = new FPPlayer(player.ID, player.playerInfo.name); }
protected override void OnSyncedDataReceived(FPPlayer player, List <SyncedData> data) { player.AddDataRollback(data); }