예제 #1
0
        public void CopyFromVehicleSync(VehicleSync VehicleSyncScript)
        {
            ownerTimestamp = Time.realtimeSinceStartup;
            position       = VehicleSyncScript.CurrentPosition();
            rotation       = VehicleSyncScript.CurrentRotation();

            if (VehicleSyncScript.rb)
            {
                velocity        = VehicleSyncScript.rb.velocity;
                angularVelocity = VehicleSyncScript.rb.angularVelocity * Mathf.Rad2Deg;
            }
            else
            {
                velocity        = Vector3.zero;
                angularVelocity = Vector3.zero;
            }
        }
        override public void Deserialize(NetworkReader reader)
        {
            byte syncInfoByte        = reader.ReadByte();
            bool syncPosition        = (syncInfoByte & p) == p;
            bool syncRotation        = (syncInfoByte & r) == r;
            bool syncVelocity        = (syncInfoByte & v) == v;
            bool syncAngularVelocity = (syncInfoByte & av) == av;

            vehicleState.atPositionalRest = (syncInfoByte & apr) == apr;
            vehicleState.atRotationalRest = (syncInfoByte & arr) == arr;

            NetworkIdentity networkIdentity = reader.ReadNetworkIdentity();

            if (!networkIdentity)
            {
                Debug.LogWarning("Where is Network Identity on Vehicle?");
                return;
            }

            uint netID     = networkIdentity.netId;
            int  syncIndex = (int)reader.ReadPackedUInt32();

            vehicleState.ownerTimestamp = reader.ReadSingle();

            GameObject go = NetworkIdentity.spawned[netID].gameObject;

            if (!go)
            {
                Debug.LogWarning("Where is Vehicle?");
                return;
            }

            VehicleSync = go.GetComponent <VehicleSync>();

            if (NetworkServer.active && !VehicleSync.hasAuthorityOrUnownedOnServer)
            {
                vehicleState.serverShouldRelayPosition        = syncPosition;
                vehicleState.serverShouldRelayRotation        = syncRotation;
                vehicleState.serverShouldRelayVelocity        = syncVelocity;
                vehicleState.serverShouldRelayAngularVelocity = syncAngularVelocity;
            }

            for (int i = 0; i < VehicleSync.childObjectVehicleSyncs.Length; i++)
            {
                if (VehicleSync.childObjectVehicleSyncs[i].syncIndex == syncIndex)
                {
                    VehicleSync = VehicleSync.childObjectVehicleSyncs[i];
                }
            }

            if (VehicleSync.receivedStatesCounter < VehicleSync.sendRate)
            {
                VehicleSync.receivedStatesCounter++;
            }

            if (syncPosition)
            {
                if (VehicleSync.isSyncingPosition)
                {
                    vehicleState.position.x = reader.ReadSingle();
                    vehicleState.position.y = reader.ReadSingle();
                    vehicleState.position.z = reader.ReadSingle();
                }
            }
            else
            {
                if (VehicleSync.stateCount > 0)
                {
                    vehicleState.position = VehicleSync.stateBuffer[0].position;
                }

                else
                {
                    vehicleState.position = VehicleSync.CurrentPosition();
                }
            }

            if (syncRotation)
            {
                vehicleState.reusableRotationVector = Vector3.zero;

                if (VehicleSync.isSyncingRotation)
                {
                    vehicleState.reusableRotationVector.x = reader.ReadSingle();
                    vehicleState.reusableRotationVector.y = reader.ReadSingle();
                    vehicleState.reusableRotationVector.z = reader.ReadSingle();
                }

                vehicleState.rotation = Quaternion.Euler(vehicleState.reusableRotationVector);
            }
            else
            {
                if (VehicleSync.stateCount > 0)
                {
                    vehicleState.rotation = VehicleSync.stateBuffer[0].rotation;
                }

                else
                {
                    vehicleState.rotation = VehicleSync.CurrentRotation();
                }
            }

            if (syncVelocity)
            {
                if (VehicleSync.isSyncingVelocity)
                {
                    vehicleState.velocity.x = reader.ReadSingle();
                    vehicleState.velocity.y = reader.ReadSingle();
                    vehicleState.velocity.z = reader.ReadSingle();
                }

                VehicleSync.latestReceivedVelocity = vehicleState.velocity;
            }
            else
            {
                vehicleState.velocity = VehicleSync.latestReceivedVelocity;
            }

            if (syncAngularVelocity)
            {
                if (VehicleSync.isSyncingAngularVelocity)
                {
                    vehicleState.angularVelocity.x = reader.ReadSingle();
                    vehicleState.angularVelocity.y = reader.ReadSingle();
                    vehicleState.angularVelocity.z = reader.ReadSingle();
                }

                VehicleSync.latestReceivedAngularVelocity = vehicleState.angularVelocity;
            }
            else
            {
                vehicleState.angularVelocity = VehicleSync.latestReceivedAngularVelocity;
            }

            if (VehicleSync.isAuthorityChanged && !NetworkServer.active)
            {
                VehicleSync.ownerChangeIndicator = (int)reader.ReadByte();
            }
        }
 public void CopyFromVehicleSync(VehicleSync VehicleSyncScript)
 {
     this.VehicleSync = VehicleSyncScript;
     vehicleState.CopyFromVehicleSync(VehicleSyncScript);
 }