コード例 #1
0
 public void OnReceiveSyncStart(NetSCSynchronizateStartMsg msg)
 {
     //GameMain.instance.EnableSync = true;
     //Random.InitState(10000);
 }
コード例 #2
0
        static void ServerSyncThread()
        {
            NetBuff writeBuffer = new NetBuff();

            //使用心跳检查客户端是否存活
            while (true)
            {
                //发送同步消息
                if (!isStart)
                {
                    if (inputChar == 'a' || inputChar == 'A')
                    {
                        NetSCSynchronizateStartMsg msg = new NetSCSynchronizateStartMsg();
                        SendMsg(writeBuffer, msg);
                        isStart = true;
                    }

                    Thread.Sleep(10);
                    continue;
                }

                if (playerList.Count == 0)
                {
                    isStart = false;
                    Thread.Sleep(10);
                    continue;
                }

                bool isOk          = true && playerList.Count > 0;
                bool isWaitSomeOne = false;
                for (int i = 0; i < playerList.Count; i++)
                {
                    if (!playerList[i].IsDisconnected && !playerList[i].IsOperationReceived)
                    {
                        isOk = false;
                    }

                    if (curFrame - playerList[i].CurFrame >= FORWARD_FRAME_COUNT)
                    {
                        isWaitSomeOne = true;
                    }
                }

                if (!isWaitSomeOne && (isOk || !forceSync))
                {
                    //开始一帧
                    curFrame++;
                    Console.WriteLine("start frame " + curFrame);

                    //生成消息
                    var frameData = new FrameData();
                    frameData.Frame = curFrame;
                    for (int i = 0; i < playerList.Count; i++)
                    {
                        if (!playerList[i].IsDisconnected && playerList[i].OperationData != null)
                        {
                            frameData.OperationDatas.Add(playerList[i].OperationData);
                            playerList[i].OperationData       = null;
                            playerList[i].IsOperationReceived = false;
                        }
                    }

                    historyFrameDatas.Add(frameData);
                    if (historyFrameDatas.Count >= 200)
                    {
                        historyFrameDatas.RemoveAt(0);
                    }

                    SendMsg(writeBuffer, frameData);

                    Console.WriteLine("send msg finish");
                }

                Thread.Sleep(DELTA_TIME);
            }
        }