Example #1
0
        /**
         * @brief Instantiates a new prefab in a deterministic way.
         *
         * @param prefab GameObject's prefab to instantiate.
         * @param position Position to place the new GameObject.
         * @param rotation Rotation to set in the new GameObject.
         **/
        public static GameObject SyncedInstantiate(GameObject prefab, TSVector position, TSQuaternion rotation)
        {
            if (instance != null && instance.lockstep != null)
            {
                GameObject go = GameObject.Instantiate(prefab, position.ToVector(), rotation.ToQuaternion()) as GameObject;

                if (ReplayRecord.replayMode != ReplayMode.LOAD_REPLAY)
                {
                    AddGameObjectOnSafeMap(go);
                }

                MonoBehaviour[] monoBehaviours = go.GetComponentsInChildren <MonoBehaviour>();
                for (int index = 0, length = monoBehaviours.Length; index < length; index++)
                {
                    MonoBehaviour bh = monoBehaviours[index];

                    if (bh is ITrueSyncBehaviour)
                    {
                        instance.queuedBehaviours.Add(instance.NewManagedBehavior((ITrueSyncBehaviour)bh));
                    }
                }

                InitializeGameObject(go, position, rotation);

                return(go);
            }

            return(null);
        }
Example #2
0
        /// <summary>
        ///@brief Instantiates a new prefab in a deterministic way.
        ///
        ///@param prefab GameObject's prefab to instantiate.
        ///@param position Position to place the new GameObject.
        ///@param rotation Rotation to set in the new GameObject.
        /// </summary>
        public static GameObject SyncedInstantiate(GameObject prefab, TSVector position, TSQuaternion rotation)
        {
            if (instance != null && instance.lockstep != null)
            {
                //先实例化一个GameObject
                GameObject go = GameObject.Instantiate(prefab, position.ToVector(), rotation.ToQuaternion()) as GameObject;

                if (ReplayRecord.replayMode != ReplayMode.LOAD_REPLAY)
                {
                    //非录像模式将该对象添加到帧记录里。AddGameObjectOnSafeMap(go);
                    AddGameObjectOnSafeMap(go);
                }

                MonoBehaviour[] monoBehaviours = go.GetComponentsInChildren <MonoBehaviour>();
                for (int index = 0, length = monoBehaviours.Length; index < length; index++)
                {
                    MonoBehaviour bh = monoBehaviours[index];

                    if (bh is ITrueSyncBehaviour)
                    {
                        //将该对象的帧行为添加到queuedBehaviours,等待帧更新的时候分配拥有者和调度初始化方法
                        instance.queuedBehaviours.Add(instance.NewManagedBehavior((ITrueSyncBehaviour)bh));
                    }
                }

                //该对象上组件的初始化方法(ICollider注册到物理管理器里PhysicsManager, TSTransform, TSTransform2D)。
                InitializeGameObject(go, position, rotation);

                return(go);
            }

            return(null);
        }
Example #3
0
        private static void InitializeGameObject(GameObject go, TSVector position, TSQuaternion rotation)
        {
            ICollider[] tsColliders = go.GetComponentsInChildren <ICollider>();
            if (tsColliders != null)
            {
                for (int index = 0, length = tsColliders.Length; index < length; index++)
                {
                    PhysicsManager.instance.AddBody(tsColliders[index]);
                }
            }

            TSTransform rootTSTransform = go.GetComponent <TSTransform>();

            if (rootTSTransform != null)
            {
                rootTSTransform.Initialize();

                rootTSTransform.position = position;
                rootTSTransform.rotation = rotation;
            }

            TSTransform[] tsTransforms = go.GetComponentsInChildren <TSTransform>();
            if (tsTransforms != null)
            {
                for (int index = 0, length = tsTransforms.Length; index < length; index++)
                {
                    TSTransform tsTransform = tsTransforms[index];

                    if (tsTransform != rootTSTransform)
                    {
                        tsTransform.Initialize();
                    }
                }
            }

            TSTransform2D rootTSTransform2D = go.GetComponent <TSTransform2D>();

            if (rootTSTransform2D != null)
            {
                rootTSTransform2D.Initialize();

                rootTSTransform2D.position = new TSVector2(position.x, position.y);
                rootTSTransform2D.rotation = rotation.ToQuaternion().eulerAngles.z;
            }

            TSTransform2D[] tsTransforms2D = go.GetComponentsInChildren <TSTransform2D>();
            if (tsTransforms2D != null)
            {
                for (int index = 0, length = tsTransforms2D.Length; index < length; index++)
                {
                    TSTransform2D tsTransform2D = tsTransforms2D[index];

                    if (tsTransform2D != rootTSTransform2D)
                    {
                        tsTransform2D.Initialize();
                    }
                }
            }
        }
Example #4
0
        /**
         * @brief Instantiates a new prefab in a deterministic way.
         *
         * @param prefab GameObject's prefab to instantiate.
         * @param position Position to place the new GameObject.
         * @param rotation Rotation to set in the new GameObject.
         **/
        public static GameObject SyncedInstantiate(GameObject prefab, TSVector position, TSQuaternion rotation)
        {
            if (instance != null && instance.lockstep != null && (prefab.GetComponentInChildren <TSTransform>() != null || prefab.GetComponentInChildren <TSTransform2D>() != null))
            {
                GameObject go = GameObject.Instantiate(prefab, position.ToVector(), rotation.ToQuaternion()) as GameObject;

                foreach (MonoBehaviour bh in go.GetComponentsInChildren <MonoBehaviour>())
                {
                    if (bh is ITrueSyncBehaviour)
                    {
                        instance.queuedBehaviours.Add(instance.NewManagedBehavior((ITrueSyncBehaviour)bh));
                    }
                }

                InitializeGameObject(go, position, rotation);

                return(go);
            }

            return(null);
        }
        /// <summary>
        /// 初始化物体身上的Ts脚本
        /// </summary>
        /// <param name="go"></param>
        /// <param name="position"></param>
        /// <param name="rotation"></param>
        private static void InitializeGameObject(GameObject go, TSVector position, TSQuaternion rotation)
        {
            //将物体身上所有Collider注册到物理管理器
            var tsColliders = go.GetComponentsInChildren <ICollider>();

            if (tsColliders != null)
            {
                for (int index = 0, length = tsColliders.Length; index < length; index++)
                {
                    PhysicsManager.instance.AddBody(tsColliders[index]);
                }
            }

            //初始化物体和子物体身上所有tsTransform
            var rootTSTransform = go.GetComponent <TSTransform>();

            if (rootTSTransform != null)
            {
                rootTSTransform.Initialize(position, rotation);
            }

            var tsTransforms = go.GetComponentsInChildren <TSTransform>();

            if (tsTransforms != null)
            {
                for (int index = 0, length = tsTransforms.Length; index < length; index++)
                {
                    var tsTransform = tsTransforms[index];

                    if (tsTransform != rootTSTransform)
                    {
                        tsTransform.Initialize();
                    }
                }
            }

            //初始化物体和子物体身上所有tsTransform2D
            var rootTSTransform2D = go.GetComponent <TSTransform2D>();

            if (rootTSTransform2D != null)
            {
                rootTSTransform2D.Initialize();

                rootTSTransform2D.position = new TSVector2(position.x, position.y);
                rootTSTransform2D.rotation = rotation.ToQuaternion().eulerAngles.z;
            }

            var tsTransforms2D = go.GetComponentsInChildren <TSTransform2D>();

            if (tsTransforms2D != null)
            {
                for (int index = 0, length = tsTransforms2D.Length; index < length; index++)
                {
                    var tsTransform2D = tsTransforms2D[index];

                    if (tsTransform2D != rootTSTransform2D)
                    {
                        tsTransform2D.Initialize();
                    }
                }
            }
        }