Exemplo n.º 1
0
 /// <summary>
 /// Serialize the snapshot without compression.
 /// </summary>
 /// <param name="writer"></param>
 public override void Serialize(NetworkWriter writer)
 {
     writer.Write(NetworkMatch.m_match_elapsed_seconds);
     writer.Write((byte)m_num_snapshots);
     for (int i = 0; i < m_num_snapshots; i++)
     {
         writer.Write(m_snapshots[i].m_net_id);
         writer.Write(m_snapshots[i].m_pos.x);
         writer.Write(m_snapshots[i].m_pos.y);
         writer.Write(m_snapshots[i].m_pos.z);
         writer.Write(NetworkCompress.CompressQuaternion(m_snapshots[i].m_rot));
         writer.Write(HalfHelper.Compress(m_snapshots[i].m_vel.x));
         writer.Write(HalfHelper.Compress(m_snapshots[i].m_vel.y));
         writer.Write(HalfHelper.Compress(m_snapshots[i].m_vel.z));
         writer.Write(HalfHelper.Compress(m_snapshots[i].m_vrot.x));
         writer.Write(HalfHelper.Compress(m_snapshots[i].m_vrot.y));
         writer.Write(HalfHelper.Compress(m_snapshots[i].m_vrot.z));
     }
 }
        /// <summary>
        /// Serialize the message over the network.
        /// </summary>
        /// <remarks>
        /// Only sends what it needs and compresses floats if you chose to.
        /// </remarks>
        /// <param name="writer">The NetworkWriter to write to.</param>
        override public void Serialize(NetworkWriter writer)
        {
            bool sendPosition, sendRotation, sendScale, sendVelocity, sendAngularVelocity;

            // If is a server trying to relay client information back out to other clients.
            if (NetworkServer.active && !smoothSync.hasAuthority)
            {
                sendPosition        = state.serverShouldRelayPosition;
                sendRotation        = state.serverShouldRelayRotation;
                sendScale           = state.serverShouldRelayScale;
                sendVelocity        = state.serverShouldRelayVelocity;
                sendAngularVelocity = state.serverShouldRelayAngularVelocity;
            }
            else // If is a server or client trying to send owned object information across the network.
            {
                sendPosition        = smoothSync.sendPosition;
                sendRotation        = smoothSync.sendRotation;
                sendScale           = smoothSync.sendScale;
                sendVelocity        = smoothSync.sendVelocity;
                sendAngularVelocity = smoothSync.sendAngularVelocity;
            }
            // Only set last sync States on clients here because the server needs to send multiple Serializes.
            if (!NetworkServer.active)
            {
                if (sendPosition)
                {
                    smoothSync.lastPositionWhenStateWasSent = state.position;
                }
                if (sendRotation)
                {
                    smoothSync.lastRotationWhenStateWasSent = state.rotation;
                }
                if (sendScale)
                {
                    smoothSync.lastScaleWhenStateWasSent = state.scale;
                }
                if (sendVelocity)
                {
                    smoothSync.lastVelocityWhenStateWasSent = state.velocity;
                }
                if (sendAngularVelocity)
                {
                    smoothSync.lastAngularVelocityWhenStateWasSent = state.angularVelocity;
                }
            }

            writer.Write(encodeSyncInformation(sendPosition, sendRotation, sendScale,
                                               sendVelocity, sendAngularVelocity));
            writer.Write(smoothSync.netId);
            writer.WritePackedUInt32((uint)smoothSync.syncIndex);
            writer.WritePackedUInt32((uint)state.ownerTimestamp);

            // Write position.
            if (sendPosition)
            {
                if (smoothSync.isPositionCompressed)
                {
                    if (smoothSync.isSyncingXPosition)
                    {
                        writer.Write(HalfHelper.Compress(state.position.x));
                    }
                    if (smoothSync.isSyncingYPosition)
                    {
                        writer.Write(HalfHelper.Compress(state.position.y));
                    }
                    if (smoothSync.isSyncingZPosition)
                    {
                        writer.Write(HalfHelper.Compress(state.position.z));
                    }
                }
                else
                {
                    if (smoothSync.isSyncingXPosition)
                    {
                        writer.Write(state.position.x);
                    }
                    if (smoothSync.isSyncingYPosition)
                    {
                        writer.Write(state.position.y);
                    }
                    if (smoothSync.isSyncingZPosition)
                    {
                        writer.Write(state.position.z);
                    }
                }
            }
            // Write rotation.
            if (sendRotation)
            {
                Vector3 rot = state.rotation.eulerAngles;
                if (smoothSync.isRotationCompressed)
                {
                    if (smoothSync.isSyncingXRotation)
                    {
                        writer.Write(HalfHelper.Compress(rot.x));
                    }
                    if (smoothSync.isSyncingYRotation)
                    {
                        writer.Write(HalfHelper.Compress(rot.y));
                    }
                    if (smoothSync.isSyncingZRotation)
                    {
                        writer.Write(HalfHelper.Compress(rot.z));
                    }
                }
                else
                {
                    if (smoothSync.isSyncingXRotation)
                    {
                        writer.Write(rot.x);
                    }
                    if (smoothSync.isSyncingYRotation)
                    {
                        writer.Write(rot.y);
                    }
                    if (smoothSync.isSyncingZRotation)
                    {
                        writer.Write(rot.z);
                    }
                }
            }
            // Write scale.
            if (sendScale)
            {
                if (smoothSync.isScaleCompressed)
                {
                    if (smoothSync.isSyncingXScale)
                    {
                        writer.Write(HalfHelper.Compress(state.scale.x));
                    }
                    if (smoothSync.isSyncingYScale)
                    {
                        writer.Write(HalfHelper.Compress(state.scale.y));
                    }
                    if (smoothSync.isSyncingZScale)
                    {
                        writer.Write(HalfHelper.Compress(state.scale.z));
                    }
                }
                else
                {
                    if (smoothSync.isSyncingXScale)
                    {
                        writer.Write(state.scale.x);
                    }
                    if (smoothSync.isSyncingYScale)
                    {
                        writer.Write(state.scale.y);
                    }
                    if (smoothSync.isSyncingZScale)
                    {
                        writer.Write(state.scale.z);
                    }
                }
            }
            // Write velocity.
            if (sendVelocity)
            {
                if (smoothSync.isVelocityCompressed)
                {
                    if (smoothSync.isSyncingXVelocity)
                    {
                        writer.Write(HalfHelper.Compress(state.velocity.x));
                    }
                    if (smoothSync.isSyncingYVelocity)
                    {
                        writer.Write(HalfHelper.Compress(state.velocity.y));
                    }
                    if (smoothSync.isSyncingZVelocity)
                    {
                        writer.Write(HalfHelper.Compress(state.velocity.z));
                    }
                }
                else
                {
                    if (smoothSync.isSyncingXVelocity)
                    {
                        writer.Write(state.velocity.x);
                    }
                    if (smoothSync.isSyncingYVelocity)
                    {
                        writer.Write(state.velocity.y);
                    }
                    if (smoothSync.isSyncingZVelocity)
                    {
                        writer.Write(state.velocity.z);
                    }
                }
            }
            // Write angular velocity.
            if (sendAngularVelocity)
            {
                if (smoothSync.isAngularVelocityCompressed)
                {
                    if (smoothSync.isSyncingXAngularVelocity)
                    {
                        writer.Write(HalfHelper.Compress(state.angularVelocity.x));
                    }
                    if (smoothSync.isSyncingYAngularVelocity)
                    {
                        writer.Write(HalfHelper.Compress(state.angularVelocity.y));
                    }
                    if (smoothSync.isSyncingZAngularVelocity)
                    {
                        writer.Write(HalfHelper.Compress(state.angularVelocity.z));
                    }
                }
                else
                {
                    if (smoothSync.isSyncingXAngularVelocity)
                    {
                        writer.Write(state.angularVelocity.x);
                    }
                    if (smoothSync.isSyncingYAngularVelocity)
                    {
                        writer.Write(state.angularVelocity.y);
                    }
                    if (smoothSync.isSyncingZAngularVelocity)
                    {
                        writer.Write(state.angularVelocity.z);
                    }
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Serialize the message over the network.
        /// </summary>
        /// <remarks>
        /// Only sends what it needs and compresses floats if you chose to.
        /// </remarks>
        /// <param name="writer">The NetworkWriter to write to.</param>
        public void Serialize(BinaryWriter writer)
        {
            bool sendPosition, sendRotation, sendScale, sendVelocity, sendAngularVelocity, sendAtPositionalRestTag, sendAtRotationalRestTag;

            //// If is a server trying to relay client information back out to other clients.
            //if (NetworkServer.active && !smoothSync.photonView.IsMine)//!smoothSync.hasAuthority)
            //{
            //    sendPosition = state.serverShouldRelayPosition;
            //    sendRotation = state.serverShouldRelayRotation;
            //    sendScale = state.serverShouldRelayScale;
            //    sendVelocity = state.serverShouldRelayVelocity;
            //    sendAngularVelocity = state.serverShouldRelayAngularVelocity;
            //    sendTeleportTag = state.teleport;
            //    sendAtPositionalRestTag = state.atPositionalRest;
            //    sendAtRotationalRestTag = state.atRotationalRest;
            //}
            //else // If is a server or client trying to send owned object information across the network.
            {
                sendPosition            = smoothSync.sendPosition;
                sendRotation            = smoothSync.sendRotation;
                sendScale               = smoothSync.sendScale;
                sendVelocity            = smoothSync.sendVelocity;
                sendAngularVelocity     = smoothSync.sendAngularVelocity;
                sendAtPositionalRestTag = smoothSync.sendAtPositionalRestMessage;
                sendAtRotationalRestTag = smoothSync.sendAtRotationalRestMessage;
            }
            //// Only set last sync States on clients here because the server needs to send multiple Serializes.
            //if (!NetworkServer.active)
            {
                if (sendPosition)
                {
                    smoothSync.lastPositionWhenStateWasSent = state.position;
                }
                if (sendRotation)
                {
                    smoothSync.lastRotationWhenStateWasSent = state.rotation;
                }
                if (sendScale)
                {
                    smoothSync.lastScaleWhenStateWasSent = state.scale;
                }
                if (sendVelocity)
                {
                    smoothSync.lastVelocityWhenStateWasSent = state.velocity;
                }
                if (sendAngularVelocity)
                {
                    smoothSync.lastAngularVelocityWhenStateWasSent = state.angularVelocity;
                }
            }

            writer.Write(encodeSyncInformation(sendPosition, sendRotation, sendScale,
                                               sendVelocity, sendAngularVelocity, sendAtPositionalRestTag, sendAtRotationalRestTag));
            //writer.Write(smoothSync.netID);
            writer.Write((uint)smoothSync.syncIndex);
            writer.Write(state.ownerTimestamp);

            // Write position.
            if (sendPosition)
            {
                if (smoothSync.isPositionCompressed)
                {
                    if (smoothSync.isSyncingXPosition)
                    {
                        writer.Write(HalfHelper.Compress(state.position.x));
                    }
                    if (smoothSync.isSyncingYPosition)
                    {
                        writer.Write(HalfHelper.Compress(state.position.y));
                    }
                    if (smoothSync.isSyncingZPosition)
                    {
                        writer.Write(HalfHelper.Compress(state.position.z));
                    }
                }
                else
                {
                    if (smoothSync.isSyncingXPosition)
                    {
                        writer.Write(state.position.x);
                    }
                    if (smoothSync.isSyncingYPosition)
                    {
                        writer.Write(state.position.y);
                    }
                    if (smoothSync.isSyncingZPosition)
                    {
                        writer.Write(state.position.z);
                    }
                }
            }
            // Write rotation.
            if (sendRotation)
            {
                Vector3 rot = state.rotation.eulerAngles;
                if (smoothSync.isRotationCompressed)
                {
                    // Convert to radians for more accurate Half numbers
                    if (smoothSync.isSyncingXRotation)
                    {
                        writer.Write(HalfHelper.Compress(rot.x * Mathf.Deg2Rad));
                    }
                    if (smoothSync.isSyncingYRotation)
                    {
                        writer.Write(HalfHelper.Compress(rot.y * Mathf.Deg2Rad));
                    }
                    if (smoothSync.isSyncingZRotation)
                    {
                        writer.Write(HalfHelper.Compress(rot.z * Mathf.Deg2Rad));
                    }
                }
                else
                {
                    if (smoothSync.isSyncingXRotation)
                    {
                        writer.Write(rot.x);
                    }
                    if (smoothSync.isSyncingYRotation)
                    {
                        writer.Write(rot.y);
                    }
                    if (smoothSync.isSyncingZRotation)
                    {
                        writer.Write(rot.z);
                    }
                }
            }
            // Write scale.
            if (sendScale)
            {
                if (smoothSync.isScaleCompressed)
                {
                    if (smoothSync.isSyncingXScale)
                    {
                        writer.Write(HalfHelper.Compress(state.scale.x));
                    }
                    if (smoothSync.isSyncingYScale)
                    {
                        writer.Write(HalfHelper.Compress(state.scale.y));
                    }
                    if (smoothSync.isSyncingZScale)
                    {
                        writer.Write(HalfHelper.Compress(state.scale.z));
                    }
                }
                else
                {
                    if (smoothSync.isSyncingXScale)
                    {
                        writer.Write(state.scale.x);
                    }
                    if (smoothSync.isSyncingYScale)
                    {
                        writer.Write(state.scale.y);
                    }
                    if (smoothSync.isSyncingZScale)
                    {
                        writer.Write(state.scale.z);
                    }
                }
            }
            // Write velocity.
            if (sendVelocity)
            {
                if (smoothSync.isVelocityCompressed)
                {
                    if (smoothSync.isSyncingXVelocity)
                    {
                        writer.Write(HalfHelper.Compress(state.velocity.x));
                    }
                    if (smoothSync.isSyncingYVelocity)
                    {
                        writer.Write(HalfHelper.Compress(state.velocity.y));
                    }
                    if (smoothSync.isSyncingZVelocity)
                    {
                        writer.Write(HalfHelper.Compress(state.velocity.z));
                    }
                }
                else
                {
                    if (smoothSync.isSyncingXVelocity)
                    {
                        writer.Write(state.velocity.x);
                    }
                    if (smoothSync.isSyncingYVelocity)
                    {
                        writer.Write(state.velocity.y);
                    }
                    if (smoothSync.isSyncingZVelocity)
                    {
                        writer.Write(state.velocity.z);
                    }
                }
            }
            // Write angular velocity.
            if (sendAngularVelocity)
            {
                if (smoothSync.isAngularVelocityCompressed)
                {
                    // Convert to radians for more accurate Half numbers
                    if (smoothSync.isSyncingXAngularVelocity)
                    {
                        writer.Write(HalfHelper.Compress(state.angularVelocity.x * Mathf.Deg2Rad));
                    }
                    if (smoothSync.isSyncingYAngularVelocity)
                    {
                        writer.Write(HalfHelper.Compress(state.angularVelocity.y * Mathf.Deg2Rad));
                    }
                    if (smoothSync.isSyncingZAngularVelocity)
                    {
                        writer.Write(HalfHelper.Compress(state.angularVelocity.z * Mathf.Deg2Rad));
                    }
                }
                else
                {
                    if (smoothSync.isSyncingXAngularVelocity)
                    {
                        writer.Write(state.angularVelocity.x);
                    }
                    if (smoothSync.isSyncingYAngularVelocity)
                    {
                        writer.Write(state.angularVelocity.y);
                    }
                    if (smoothSync.isSyncingZAngularVelocity)
                    {
                        writer.Write(state.angularVelocity.z);
                    }
                }
            }
            //// Only the server sends out owner information.
            //if (smoothSync.isSmoothingAuthorityChanges && NetworkServer.active)
            //{
            //    writer.Write((byte)smoothSync.ownerChangeIndicator);
            //}

            if (smoothSync.automaticallyResetTime)
            {
                writer.Write((byte)state.localTimeResetIndicator);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Serialize the message over the network
        /// </summary>
        /// <remarks>
        /// Only sends what it needs. Optionally compresses floats depending on the settings on the SmoothSync object.
        /// </remarks>
        /// <param name="writer">The NetworkWriter to write to</param>
        override public void Serialize(NetworkWriter writer)
        {
            // If haven't changed enough since last sent update, we don't want to send out or assign things
            bool sendPosition        = smoothSync.shouldSendPosition();
            bool sendVelocity        = smoothSync.shouldSendVelocity();
            bool sendAngularVelocity = smoothSync.shouldSendAngularVelocity();
            bool sendRotation        = smoothSync.shouldSendRotation();

            // only set last sync states on clients here because server needs to send multiple serializes
            if (!NetworkServer.active)
            {
                if (sendPosition)
                {
                    smoothSync.lastPositionWhenStateWasSent = state.position;
                }
                if (sendVelocity)
                {
                    smoothSync.lastVelocityWhenStateWasSent = state.velocity;
                }
                if (sendAngularVelocity)
                {
                    smoothSync.lastAngularVelocityWhenStateWasSent = state.angularVelocity;
                }
                if (sendRotation)
                {
                    smoothSync.lastRotationWhenStateWasSent = state.rotation;
                }
            }

            writer.Write(encodeSyncInformation(sendPosition, sendRotation, sendVelocity, sendAngularVelocity));
            writer.Write(smoothSync.netId);
            writer.WritePackedUInt32((uint)smoothSync.syncIndex);
            writer.WritePackedUInt32((uint)state.ownerTimestamp);

            // write position
            if (sendPosition)
            {
                if (smoothSync.isPositionCompressed)
                {
                    if (smoothSync.isSyncingXPosition)
                    {
                        writer.Write(HalfHelper.Compress(state.position.x));
                    }
                    if (smoothSync.isSyncingYPosition)
                    {
                        writer.Write(HalfHelper.Compress(state.position.y));
                    }
                    if (smoothSync.isSyncingZPosition)
                    {
                        writer.Write(HalfHelper.Compress(state.position.z));
                    }
                }
                else
                {
                    if (smoothSync.isSyncingXPosition)
                    {
                        writer.Write(state.position.x);
                    }
                    if (smoothSync.isSyncingYPosition)
                    {
                        writer.Write(state.position.y);
                    }
                    if (smoothSync.isSyncingZPosition)
                    {
                        writer.Write(state.position.z);
                    }
                }
            }
            // write velocity
            if (sendVelocity)
            {
                if (smoothSync.isVelocityCompressed)
                {
                    if (smoothSync.isSyncingXVelocity)
                    {
                        writer.Write(HalfHelper.Compress(state.velocity.x));
                    }
                    if (smoothSync.isSyncingYVelocity)
                    {
                        writer.Write(HalfHelper.Compress(state.velocity.y));
                    }
                    if (smoothSync.isSyncingZVelocity)
                    {
                        writer.Write(HalfHelper.Compress(state.velocity.z));
                    }
                }
                else
                {
                    if (smoothSync.isSyncingXVelocity)
                    {
                        writer.Write(state.velocity.x);
                    }
                    if (smoothSync.isSyncingYVelocity)
                    {
                        writer.Write(state.velocity.y);
                    }
                    if (smoothSync.isSyncingZVelocity)
                    {
                        writer.Write(state.velocity.z);
                    }
                }
            }
            // write rotation
            if (sendRotation)
            {
                Vector3 rot = state.rotation.eulerAngles;
                if (smoothSync.isRotationCompressed)
                {
                    if (smoothSync.isSyncingXRotation)
                    {
                        writer.Write(HalfHelper.Compress(rot.x));
                    }
                    if (smoothSync.isSyncingYRotation)
                    {
                        writer.Write(HalfHelper.Compress(rot.y));
                    }
                    if (smoothSync.isSyncingZRotation)
                    {
                        writer.Write(HalfHelper.Compress(rot.z));
                    }
                }
                else
                {
                    if (smoothSync.isSyncingXRotation)
                    {
                        writer.Write(rot.x);
                    }
                    if (smoothSync.isSyncingYRotation)
                    {
                        writer.Write(rot.y);
                    }
                    if (smoothSync.isSyncingZRotation)
                    {
                        writer.Write(rot.z);
                    }
                }
            }
            // write angular velocity
            if (sendAngularVelocity)
            {
                if (smoothSync.isAngularVelocityCompressed)
                {
                    if (smoothSync.isSyncingXAngularVelocity)
                    {
                        writer.Write(HalfHelper.Compress(state.angularVelocity.x));
                    }
                    if (smoothSync.isSyncingYAngularVelocity)
                    {
                        writer.Write(HalfHelper.Compress(state.angularVelocity.y));
                    }
                    if (smoothSync.isSyncingZAngularVelocity)
                    {
                        writer.Write(HalfHelper.Compress(state.angularVelocity.z));
                    }
                }
                else
                {
                    if (smoothSync.isSyncingXAngularVelocity)
                    {
                        writer.Write(state.angularVelocity.x);
                    }
                    if (smoothSync.isSyncingYAngularVelocity)
                    {
                        writer.Write(state.angularVelocity.y);
                    }
                    if (smoothSync.isSyncingZAngularVelocity)
                    {
                        writer.Write(state.angularVelocity.z);
                    }
                }
            }
        }