コード例 #1
0
 public void TransferValuesFrom(Transform2DNetworkData stateToSend)
 {
     this.Timestamp = stateToSend.Timestamp;
     this.Position  = stateToSend.Position;
     this.Rotation  = stateToSend.Rotation;
     this.Velocity  = stateToSend.Velocity;
 }
コード例 #2
0
        private void TransmitState_AtAllClients(Transform2DNetworkData data)
        {
            if (stateBuffer.Length > 1 && data.Timestamp <= stateBuffer[0].Timestamp)
            {
                return;
            }

            if (stateTimeOffsets[0] == 0f)
            {
                stateTimeOffsets.RemoveAt(0);
            }
            stateTimeOffsets.Add(data.Timestamp - GetWorldTime());
            if (stateTimeOffsets.Count > StateAmount)
            {
                stateTimeOffsets.RemoveAt(0);
            }

            for (int i = stateBuffer.Length - 1; i >= 1; i--)
            {
                stateBuffer[i] = stateBuffer[i - 1];
            }
            // Array.Copy(stateBuffer, 0, stateBuffer, 1, stateBuffer.Length - 1);
            stateBuffer[0] = data;

            // TODO: implement correction of position
            // first store last second of states every 0.1s
            // use timestamp to get a past state, either use lerping or just take the closest state.
            // test if it is correct, if not, then reset all and world.step() with time offset


            OnPositionReceived?.Invoke(data);

            // DebugLog.Warning($"[SyncedTransform2D-{EntityId}: {data.Position.ToString()}");
            // DebugLog.Warning($"[SyncedTransform2D-{EntityId}: {string.Join(", ", stateBuffer.Select((state, i) => state.Position.ToString()))}");
        }
コード例 #3
0
        public SyncedTransform2D(int entityId, IGame game, PhysicsBody body = null)
        {
            this.EntityId     = entityId;
            tickTriggerAmount = 1f / tickRate;
            this.game         = game;
            this.body         = body;

            stateBuffer = new Transform2DNetworkData[StateAmount];
            stateTimeOffsets.Add(0f);
            stateBuffer[0] = new Transform2DNetworkData()
            {
                Timestamp = 0.0000000001f, Position = new Vec2(0, 0), Rotation = 0f
            };
            stateBuffer[1] = new Transform2DNetworkData()
            {
                Timestamp = 0f, Position = new Vec2(0, 0), Rotation = 0f
            };
            transmitState = NetworkMagic.RegisterAtAllClients <Transform2DNetworkData>(0, TransmitState_AtAllClients, entityId);
        }