コード例 #1
0
        public void UpdateInput(UdpConnector connector)
        {
            current = default(InputData);
            if (mIndex % mCmdOverTick == 0)
            {
                if (mChoke > 0)
                {
                    --mChoke;
                    return;
                }
            }

            current.index    = mIndex;
            current.keyboard = (byte)(GetKey(KeyCode.W) << 3 | GetKey(KeyCode.A) << 2 | GetKey(KeyCode.S) << 1 | GetKey(KeyCode.D));
            Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (current.mouseHasHit = Physics.Raycast(ray, out hit, 100f, (1 << Layers.Ground)))
            {
                current.mouseHit = hit.point;
            }
            mInputQueue.Add(current);

            if ((mIndex++) % mCmdOverTick == 0)
            {
                using (var builder = MessageBuilder.Get())
                {
                    FlatBufferBuilder fbb = builder.fbb;
                    try
                    {
                        var array = OffsetArrayPool.Alloc <Protocol.InputData>((int)mCmdOverTick);
                        for (int i = 0; i < mCmdOverTick; ++i)
                        {
                            InputData d = mInputQueue[mInputQueue.Count - (int)mCmdOverTick + i];
                            Protocol.InputData.StartInputData(fbb);
                            Protocol.InputData.AddIndex(fbb, d.index);
                            Protocol.InputData.AddKeyboard(fbb, d.keyboard);
                            Protocol.InputData.AddMouseHasHit(fbb, d.mouseHasHit);
                            if (d.mouseHasHit)
                            {
                                Protocol.InputData.AddMouseHit(fbb, Protocol.Vec3.CreateVec3(fbb, d.mouseHit.x, d.mouseHit.y, d.mouseHit.z));
                            }
                            array.offsets[array.position++] = Protocol.InputData.EndInputData(fbb);
                        }
                        Protocol.Msg_CS_InputDataArray.StartInputDataVector(fbb, array.position);
                        var offset = Helpers.SetVector(fbb, array);
                        fbb.Finish(Protocol.Msg_CS_InputDataArray.CreateMsg_CS_InputDataArray(fbb, offset).Value);
                        connector.SendMessage(
                            connector.CreateMessage(Protocol.MessageID.Msg_CS_InputDataArray, fbb),
                            NetDeliveryMethod.UnreliableSequenced,
                            1);
                    }
                    catch (Exception e)
                    {
                        TCLog.Exception(e);
                    }
                }
            }
        }
コード例 #2
0
        public void AddDelta(uint tick, ByteBuffer byteBuffer, NetIncomingMessage msg)
        {
            if (!mHasFullUpdated || tick <= mServerTick)
            {
                TCLog.WarnFormat("drop delta update, hasFullUpdated:{0}, tick:{1}, current serverTick:{2}", mHasFullUpdated, tick, mServerTick);
            }
            else
            {
                mCachedSnapshots.Enqueue(byteBuffer);
                int choke = msg.ReadInt32();
                InputManager.Instance.UpdateChoke(choke);
                for (int i = 0; i < Game.Instance.snapshotOverTick; ++i)
                {
                    InputManager.Instance.AckInput(msg.ReadUInt32());
                }

                Msg_SC_Snapshot snapshot = InstancePool.Get <Msg_SC_Snapshot>();
                Msg_SC_Snapshot.GetRootAsMsg_SC_Snapshot(byteBuffer, snapshot);
                mTickObjects.ApplyDeltaForPredict(new TickObjectDictionary.TickObjectEnumerator(snapshot));
            }
        }