예제 #1
0
        void Update()
        {
            if (!TickEngineSettings.single.enableTickEngine)
            {
                return;
            }

            if (simulationHasRun)
            {
                PostSimulate();
            }

            DoubleTime.SnapUpdate();

            NormTimeSinceFixed = (Time.time - Time.fixedTime) / Time.fixedDeltaTime;

            NetMasterCallbacks.OnPreUpdateCallbacks();

            float t = (Time.time - lastSentTickTime) / (TickEngineSettings.netTickInterval);

            // Interpolate NetObjects
            NetObject.NetObjDictsLocked = true;
            foreach (var no in NetObject.activeUncontrolledNetObjs.Values)
            {
                no.OnInterpolate(_prevFrameId, _currFrameId, t);
            }
            NetObject.NetObjDictsLocked = false;

            // Interpolate Others
            NetMasterCallbacks.OnInterpolateCallbacks(_prevFrameId, _currFrameId, t);
        }
예제 #2
0
        private void Awake()
        {
            rb                  = GetComponentInParent <Rigidbody>();
            rb2d                = GetComponentInParent <Rigidbody2D>();
            _hasRigidBody       = rb || rb2d;
            useRbForces         = rb && !rb.isKinematic;
            useRb2dForces       = rb2d && !rb2d.isKinematic;
            useRBGravity        = (rb && rb.useGravity) || (rb2d && rb.useGravity);
            needsSnapshot       = !_hasRigidBody || (rb && rb.isKinematic) || (rb2d && rb2d.isKinematic);
            localContactTrigger = GetComponent <IContactTrigger>();
            /// Register timing callbacks with Master.
            /// TODO: We likely should slave timings off of the owner
            if (needsSnapshot)
            {
                NetMasterCallbacks.RegisterCallbackInterfaces(this);
            }

            /// No need for the interpolation callback if we are using forces.
            if (_hasRigidBody)
            {
                NetMasterCallbacks.onPreUpdates.Remove(this);
            }

            /// Find interfaces for termination callbacks
            GetComponents(onHit);
            GetComponents(onTerminate);
        }
예제 #3
0
 protected virtual void OnDisable()
 {
     if (preventRepeats)
     {
         NetMasterCallbacks.RegisterCallbackInterfaces(this, false, true);
     }
 }
예제 #4
0
        private void LateUpdate()
        {
            if (!TickEngineSettings.single.enableTickEngine)
            {
                return;
            }

            NetMasterCallbacks.OnPostLateUpdateCallbacks();
        }
예제 #5
0
        private void FixedUpdate()
        {
            if (!TickEngineSettings.single.enableTickEngine)
            {
                return;
            }

            NetMasterCallbacks.OnPreSimulateCallbacks(NetMaster.CurrentFrameId, NetMaster.CurrentSubFrameId);
        }
예제 #6
0
        protected virtual void OnEnable()
        {
            if (preventRepeats)
            {
                NetMasterCallbacks.RegisterCallbackInterfaces(this);

                triggeringHitscans.Clear();
                triggeringEnters.Clear();
                triggeringStays.Clear();
            }
        }
예제 #7
0
        /// <summary>
        /// Unity lacks a PostPhysX/PostSimulation callback, so this is the closest we can get to creating one.
        /// If this happens during Update, it is important to sample values from the simulaion (such as rb.position, or your own tick based sim results)
        /// RATHER than from the scene objects, which may be interpolated.
        /// </summary>
        void PostSimulate()
        {
            bool isNetTick = _currSubFrameId == TickEngineSettings.sendEveryXTick - 1;

            NetMasterCallbacks.OnPostSimulateCallbacks(_currFrameId, _currSubFrameId, isNetTick);

            if (isNetTick)
            {
                SerializeAllAndSend();
            }

            IncrementFrameId();

            simulationHasRun = false;
        }
예제 #8
0
        private void LateUpdate()
        {
            if (!TickEngineSettings.single.enableTickEngine)
            {
                return;
            }

            /// Disable Simple if no NetObjects exist.
            if (NetObject.activeControlledNetObjs.Count == 0 && NetObject.activeUncontrolledNetObjs.Count == 0)
            {
                return;
            }

            NetMasterCallbacks.OnPostLateUpdateCallbacks();
        }
예제 #9
0
        private void FixedUpdate()
        {
            if (!TickEngineSettings.single.enableTickEngine)
            {
                return;
            }

            /// Disable Simple if no NetObjects exist.
            if (NetObject.activeControlledNetObjs.Count == 0 && NetObject.activeUncontrolledNetObjs.Count == 0)
            {
                return;
            }

            NetMasterCallbacks.OnPreSimulateCallbacks(NetMaster.CurrentFrameId, NetMaster.CurrentSubFrameId);
        }
예제 #10
0
        /// <summary>
        /// Increment the current frameId. If we are sending every X simulation tics, the Subframe only gets incremented.
        /// Frames are serialized and sent just before CurrentFrameId is incremented (after all subframes have been simulated).
        /// </summary>
        private static void IncrementFrameId()
        {
            _prevSubFrameId = _currSubFrameId;
            _currSubFrameId++;

            if (_currSubFrameId >= TickEngineSettings.sendEveryXTick)
            {
                _currSubFrameId = 0;
                _prevFrameId    = _currFrameId;

                _currFrameId++;
                if (_currFrameId >= TickEngineSettings.frameCount)
                {
                    _currFrameId = 0;
                }
            }

            NetMasterCallbacks.OnIncrementFrameCallbacks(_currFrameId, _currSubFrameId, _prevFrameId, _prevSubFrameId);

            if (_currSubFrameId == 0)
            {
                ///  Insert pre snapshot tick manager test for number of snaps needed per connection.
                TickManager.PreSnapshot(_currFrameId);

                // Snapshot NetObjects
                NetObject.NetObjDictsLocked = true;
                foreach (var no in NetObject.activeUncontrolledNetObjs.Values)
                {
                    no.OnSnapshot(_currFrameId);
                }
                NetObject.NetObjDictsLocked = false;

                // Snapshot Others
                NetMasterCallbacks.OnSnapshotCallbacks(_currFrameId);

                TickManager.PostSnapshot(_currFrameId);

                lastSentTickTime = Time.fixedTime;
            }
        }
예제 #11
0
        private static void SerializeAllAndSend()
        {
            byte[] buffer      = NetMsgSends.reusableBuffer;
            int    bitposition = 0;

            SerializationFlags writeFlags;
            SerializationFlags flags;

            if (TickManager.needToSendInitialForNewConn)
            {
                writeFlags = SerializationFlags.NewConnection | SerializationFlags.ForceReliable | SerializationFlags.Force;
                flags      = SerializationFlags.HasContent;
                TickManager.needToSendInitialForNewConn = false;
            }
            else
            {
                writeFlags = SerializationFlags.None;
                flags      = SerializationFlags.None;
            }

            /// Write frameId
            buffer.Write((uint)_currFrameId, ref bitposition, TickEngineSettings.frameCountBits);

            NetMasterCallbacks.OnPreSerializeTickCallbacks(_currFrameId, buffer, ref bitposition);

            #region NetObject Serialization

            /// Loop through owned NetObjects
            NetObject.NetObjDictsLocked = true;

            NonAllocDictionary <int, NetObject> controlledObjs = NetObject.activeControlledNetObjs;

            SerializeNetObjDict(controlledObjs, buffer, ref bitposition, ref flags, writeFlags);

            //NonAllocDictionary<int, NetObject> ownedButNotControlledObjs = NetObject.activeOwnedNetObjs;

            //if (ownedButNotControlledObjs.Count > 0)
            //    Debug.Log("LIMBO COUNT " + ownedButNotControlledObjs.Count);

            //SerializeNetObjDict(ownedButNotControlledObjs, buffer, ref bitposition, ref flags, writeFlags);

            //foreach (var no in ownedObjs)
            //{

            //    /// Not end of netobjs write bool
            //    int holdStartPos = bitposition;

            //    /// Write viewID
            //    buffer.WritePackedBytes((uint)no.ViewID, ref bitposition, 32);

            //    /// Write hadData bool
            //    int holdHasDataPos = bitposition;
            //    buffer.WriteBool(true, ref bitposition);

            //    /// Log the data size write position and write a placeholder.
            //    int holdDataSizePos = bitposition;
            //    bitposition += BITS_FOR_NETOBJ_SIZE;

            //    var objflags = no.OnNetSerialize(_currFrameId, buffer, ref bitposition, writeFlags);

            //    /// Skip netobjs if they had nothing to say
            //    if (objflags == SerializationFlags.None)
            //    {
            //        /// Rewind if this is a no-data write.
            //        if (no.SkipWhenEmpty)
            //        {
            //            bitposition = holdStartPos;
            //        }
            //        else
            //        {
            //            bitposition = holdHasDataPos;
            //            buffer.WriteBool(false, ref bitposition);
            //        }
            //    }
            //    else
            //    {
            //        /// Revise the data size now that we know it.
            //        flags |= objflags;
            //        int bitcount = bitposition - holdDataSizePos;
            //        buffer.Write((uint)bitcount, ref holdDataSizePos, BITS_FOR_NETOBJ_SIZE);
            //    }

            //    //Debug.Log(objflags + " / flg: " + (onNetSerialize[i]  as Component).name + " " + flags);
            //}

            NetObject.NetObjDictsLocked = false;

            #endregion

            // Any deferrered Ownership changes from SyncState happen here
            while (NetMasterCallbacks.postSerializationActions.Count > 0)
            {
                NetMasterCallbacks.postSerializationActions.Dequeue().Invoke();
            }

            if (flags == SerializationFlags.None)
            {
                return;
            }

            /// End of NetObject write bool
            buffer.WritePackedBytes(0, ref bitposition, 32);

            NetMsgSends.Send(buffer, bitposition, null, flags, true);
        }
예제 #12
0
 private void LateUpdate()
 {
     NetMasterCallbacks.OnPreLateUpdateCallbacks();
 }
예제 #13
0
        private void OnApplicationQuit()
        {
            isShuttingDown = true;

            NetMasterCallbacks.OnPreQuitCallbacks();
        }
예제 #14
0
 private void OnDestroy()
 {
     NetMasterCallbacks.RegisterCallbackInterfaces(this, false, true);
 }