/// <summary>
        /// Call TakeOverObservable() to take over the ownership of an observable you don't own
        /// <para>For TakeOverObservable() to work, the observable must have the squattingAllowed boolean sat to true</para>
        /// </summary>
        /// <param name="observableView">The ObservableView component attached to the Synced GameObject
        /// <para>
        /// All Synced GameObjects have ObservableView component added at runtime
        /// </para>
        /// </param>
        public void TakeOverObservable(ObservableView observableView)
        {
            EdgeMultiplayObserver currentOwner = EdgeManager.observers.Find(observer => observer.networkedPlayer.playerId == observableView.ownerId);

            if (currentOwner == null)
            {
                Debug.LogWarning("EdgeMultiplay: Couldn't find the owner");
                return;
            }

            if (currentOwner.networkedPlayer.playerId == networkedPlayer.playerId)
            {
                Debug.LogWarning("EdgeMultiplay: No ownership change needed you already own the observable object");
                return;
            }

            Observable observable = currentOwner.observables.Find(obs => obs.observableIndex == observableView.observableIndex);

            if (observable == null)
            {
                Debug.LogWarning("EdgeMultiplay: Couldn't find the observable");
                return;
            }

            if (!observable.squattingAllowed)
            {
                Debug.LogWarning("EdgeMultiplay: Couldn't TakeOver Observable, Squatting is not Allowed");
                return;
            }
            else
            {
                EdgeManager.MessageSender.SendGamePlayEvent(new GamePlayEvent()
                {
                    eventName  = "TakeOverObservable",
                    stringData = new string[1] {
                        observableView.ownerId
                    },
                    integerData = new int[1] {
                        observableView.observableIndex
                    }
                });
            }
        }
Exemple #2
0
        /// <summary>
        /// Creates synced GameObject in runtime in the local player's world
        /// and sends an event to all players in the room to create the observable in their worlds
        /// </summary>
        /// <param name="prefabName">The name of your prefab (Game Object) stored in Resources Folder without extensions ex. 'Ball' </param>
        /// <param name="startPosition"> the inital position of the spawning </param>
        /// <param name="startRotation">  the inital rotation of the spawning </param>
        /// <param name="syncOption"> Which Synchronization option will be applied </param>
        /// <param name="interpolatePosition"> Set to true if you want to smoothen the tracked position if you have network lag </param>
        /// <param name="interpolateRotation"> Set to true if you want to smoothen the tracked rotation if you have network lag</param>
        /// <param name="interpolationFactor"> Set Interpolation factor between 0.1 and 1 </param>
        /// <returns> The created Observer object </returns>
        public Observable CreateObservableObject(string prefabName, Vector3 startPosition, Quaternion startRotation, SyncOptions syncOption, bool interpolatePosition = false, bool interpolateRotation = false, float interpolationFactor = 0)
        {
            GameObject prefab = Resources.Load <GameObject>(prefabName);
            GameObject syncedObject;

            syncedObject = Instantiate(prefab, startPosition, startRotation);
            if (!observer)
            {
                observer = gameObject.AddComponent <EdgeMultiplayObserver>();
            }
            Observable newObservable = new Observable(syncedObject.transform, syncOption, interpolatePosition, interpolateRotation, Mathf.Clamp(interpolationFactor, 0.1f, 1f), observer.observables.Count);

            observer.observables.Add(newObservable);
            newObservable.SetupObservable(this);
            observer.UpdateObservables();
            if (isLocalPlayer)
            {
                GamePlayEvent newObserverEvent = new GamePlayEvent
                {
                    eventName   = "NewObservableCreated",
                    booleanData = new bool[2] {
                        interpolatePosition, interpolateRotation
                    },
                    stringData = new string[2] {
                        playerId, prefabName
                    },
                    integerData = new int[1] {
                        (int)syncOption
                    },
                    floatData = new float[7] {
                        startPosition.x, startPosition.y, startPosition.z, startRotation.eulerAngles.x, startRotation.eulerAngles.y, startRotation.eulerAngles.z, interpolationFactor
                    },
                };
                edgeManager.SendGamePlayEvent(newObserverEvent);
            }
            return(newObservable);
        }