/**
     * @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, FPVector position, FPQuaternion rotation)
    {
        if (instance != null /*&& instance.lockstep != null*/)
        {
            GameObject go = GameObject.Instantiate(prefab, position.ToVector(), rotation.ToQuaternion()) as GameObject;

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

                if (bh is IFrameSyncBehaviour)
                {
                    instance.mapManagedBehaviors.Add(new KeyValuePair <IFrameSyncBehaviour, FrameSyncManagedBehaviour>(
                                                         (IFrameSyncBehaviour)bh, instance.NewManagedBehavior((IFrameSyncBehaviour)bh)
                                                         ));
                }
            }

            InitializeGameObject(go, position, rotation);

            return(go);
        }

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

        FPTransform rootFPTransform = go.GetComponent <FPTransform>();

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

            rootFPTransform.position = position;
            rootFPTransform.rotation = rotation;
        }

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

                if (FPTransform != rootFPTransform)
                {
                    FPTransform.Initialize();
                }
            }
        }

        FPTransform2D rootFPTransform2D = go.GetComponent <FPTransform2D>();

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

            rootFPTransform2D.position = new FPVector2(position.x, position.y);
            rootFPTransform2D.rotation = rotation.ToQuaternion().eulerAngles.z;
        }

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

                if (FPTransform2D != rootFPTransform2D)
                {
                    FPTransform2D.Initialize();
                }
            }
        }
    }
示例#3
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, FPVector position, FPQuaternion rotation)
    {
        if (instance != null /*&& instance.lockstep != null*/)
        {
            GameObject go = GameObject.Instantiate(prefab, position.ToVector(), rotation.ToQuaternion()) as GameObject;
            AddSyncBehaviour(go);
            InitializeGameObject(go, position, rotation);
            return(go);
        }
        return(null);
    }