Example #1
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);
                    }
                }
            }
        }