/// <summary> /// When an observerable object change its owner, OwnershipChangeEvent is sent from the owner to other players /// Changing ownership must occur at the owner world /// </summary> /// <param name="ownershipChangeEvent">OwnershipChangeEvent contains (oldOwnerId, the observerable Index and the newOwnerId)</param> void UpdateObserverOwnership(GamePlayEvent ownershipChangeEvent) { if (localPlayer.playerId == ownershipChangeEvent.senderId) { return; } NetworkedPlayer oldOwner = GetPlayer(ownershipChangeEvent.senderId); Observable observer; if (oldOwner.observer != null) { observer = oldOwner.observer.observables[ownershipChangeEvent.integerData[0]]; } else { Debug.LogError("Couldn't find old owner.observer"); return; } NetworkedPlayer newOwner = GetPlayer(ownershipChangeEvent.stringData[0]); if (newOwner.observer == null) { newOwner.gameObject.AddComponent <EdgeMultiplayObserver>(); } newOwner.observer.observables.Add(observer); newOwner.observer.UpdateObservables(); oldOwner.observer.observables.RemoveAt(ownershipChangeEvent.integerData[0]); }
/// <summary> /// Sends an event to all players in the room except the sender /// </summary> public void BroadcastMessage(string eventName, string[] stringData = null, int[] commandInts = null, float[] floatData = null, bool[] booleanData = null) { GamePlayEvent gamePlayEvent = new GamePlayEvent(this.roomId, playerId, eventName, stringData, commandInts, floatData, booleanData); edgeManager.SendGamePlayEvent(gamePlayEvent); }
public override void OnWebSocketEventReceived(GamePlayEvent gameplayEvent) { if (!isLocalPlayer) { if (gameplayEvent.senderId == playerId) { playerEvent(gameplayEvent); } } }
public override void OnUDPEventReceived(GamePlayEvent udpEvent) { if (!isLocalPlayer) { if (udpEvent.senderId == playerId) { playerEvent(udpEvent); } } }
/// <summary> /// Sends a UDP Message from local player to other room members, can be used only after the game starts (OnGameStart()) /// <para> /// on the player manager that inherits from NetworkedPlayer use /// <b>public override void OnMessageReceived</b> to get the forwarded message /// </para> /// <para> /// <b>UDP Messages are limited to 508 bytes</b> if you want to exceed that make sure you slice and reassemble your buffer /// </para> /// </summary> /// <param name="gameplayEvent">the GamePlay Event to be forwarded to other room members</param> public static void SendUDPMessage(GamePlayEvent gameplayEvent) { gameplayEvent.roomId = gameSession.roomId; gameplayEvent.senderId = gameSession.playerId; if (udpClient.run) { udpClient.Send(gameplayEvent.ToJson()); } else { Debug.LogError("EdgeMultiplay: Error in sending UDP Message"); } }
/// <summary> /// Sends an event to all players in the room (UDP) /// </summary> /// <param name="gamePlayEvent">The GamePlayEvent to send</param> public void SendGamePlayEventUDP(GamePlayEvent gamePlayEvent) { GamePlayEvent gameplayEvent = new GamePlayEvent() { eventName = gamePlayEvent.eventName, booleanData = gamePlayEvent.booleanData, stringData = gamePlayEvent.stringData, integerData = gamePlayEvent.integerData, floatData = gamePlayEvent.floatData, }; EdgeManager.SendUDPMessage(gameplayEvent); }
/// <summary> /// Sends an event to all players in the room except the sender /// </summary> public void BroadcastMessage(GamePlayEvent gamePlayEvent) { GamePlayEvent gameplayEvent = new GamePlayEvent() { eventName = gamePlayEvent.eventName, booleanData = gamePlayEvent.booleanData, stringData = gamePlayEvent.stringData, integerData = gamePlayEvent.integerData, floatData = gamePlayEvent.floatData, }; edgeManager.SendGamePlayEvent(gameplayEvent); }
/// <summary> /// If the LocalPlayer is observing any transforms, once there is any update to the observed transform /// the local player will send the updated transfrom to its clones in the other players' world. /// </summary> /// <param name="receivedEvent"> the received gameplay event contains (obsverable owner id, observerable index, syncOption, updated transform data) </param> void SyncObject(GamePlayEvent receivedEvent) { if (receivedEvent.senderId == localPlayer.playerId) { return; } NetworkedPlayer sourcePlayer = GetPlayer(receivedEvent.senderId); if (sourcePlayer.isLocalPlayer) { return; } int observerIndex; Observable observerObject; observerIndex = receivedEvent.integerData[1]; observerObject = sourcePlayer.observer.observables.Find(observer => observer.observerIndex == observerIndex); if (observerObject == null) { Debug.LogError("No observer found with this id " + receivedEvent.integerData[1]); return; } switch (receivedEvent.integerData[0]) { case (int)SyncOptions.SyncPosition: observerObject.observeredTransform.transform.position = Util.ConvertFloatArrayToVector3(receivedEvent.floatData, 0); break; case (int)SyncOptions.SyncRotation: observerObject.observeredTransform.transform.rotation = Quaternion.Euler(Util.ConvertFloatArrayToVector3(receivedEvent.floatData, 0)); break; case (int)SyncOptions.SyncPositionAndRotation: observerObject.observeredTransform.transform.position = Util.ConvertFloatArrayToVector3(receivedEvent.floatData, 0); observerObject.observeredTransform.transform.rotation = Quaternion.Euler(Util.ConvertFloatArrayToVector3(receivedEvent.floatData, 3)); break; case (int)SyncOptions.SyncLocalPosition: Util.SetLocalPostion(observerObject.observeredTransform.transform, Util.ConvertFloatArrayToVector3(receivedEvent.floatData, 0)); break; case (int)SyncOptions.SyncLocalRotation: Util.SetLocalRotation(observerObject.observeredTransform.transform, Util.ConvertFloatArrayToVector3(receivedEvent.floatData, 0)); break; case (int)SyncOptions.SyncLocalPositionAndRotation: Util.SetLocalPostion(observerObject.observeredTransform.transform, Util.ConvertFloatArrayToVector3(receivedEvent.floatData, 0)); Util.SetLocalRotation(observerObject.observeredTransform.transform, Util.ConvertFloatArrayToVector3(receivedEvent.floatData, 3)); break; } }
void TakeOverObservable(GamePlayEvent gamePlayEvent) { // check if the observable owner is the local player if (gamePlayEvent.stringData[0] == localPlayer.playerId) { //get the observable Observable observable = localPlayer.observer.observables .Find(obs => obs.observableIndex == gamePlayEvent.integerData[0]); if (observable == null) { Debug.LogWarning("EdgeMultiplay: couldn't find the observable"); return; } // change the observable ownership from the current owner to the sender observable.ChangeOwnership(gamePlayEvent.senderId); } }
void CreateObserverableObject(GamePlayEvent newObserverableEvent) { if (localPlayer.playerId == newObserverableEvent.stringData[0]) { return; } NetworkedPlayer playerCreatedObserver = GetPlayer(newObserverableEvent.stringData[0]); Observable observerable = playerCreatedObserver.CreateObservableObject( prefabName: newObserverableEvent.stringData[1], startPosition: Util.ConvertFloatArrayToVector3(newObserverableEvent.floatData, 0), startRotation: Quaternion.Euler(Util.ConvertFloatArrayToVector3(newObserverableEvent.floatData, 3)), syncOption: (SyncOptions)Enum.ToObject(typeof(SyncOptions), newObserverableEvent.integerData[0]), interpolatePosition: newObserverableEvent.booleanData[0], interpolateRotation: newObserverableEvent.booleanData[1], interpolationFactor: newObserverableEvent.floatData[6]); EdgeMultiplayCallbacks.newObservableCreated(observerable); }
public void SendDataToServer() { GamePlayEvent observerEvent = new GamePlayEvent(); observerEvent.eventName = "EdgeMultiplayObserver"; observerEvent.integerData = new int[2] { (int)syncOption, observableIndex }; switch (syncOption) { case SyncOptions.SyncPosition: observerEvent.floatData = Util.GetPositionData(observeredTransform.transform); lastPosition = observeredTransform.transform.position; break; case SyncOptions.SyncRotation: observerEvent.floatData = Util.GetRotationEulerData(observeredTransform.transform); lastRotation = observeredTransform.transform.rotation.eulerAngles; break; case SyncOptions.SyncPositionAndRotation: observerEvent.floatData = Util.GetPositionAndRotationData(observeredTransform.transform); lastRotation = observeredTransform.transform.rotation.eulerAngles; lastPosition = observeredTransform.transform.position; break; case SyncOptions.SyncLocalPosition: observerEvent.floatData = Util.GetLocalPositionData(observeredTransform.transform); lastPosition = observeredTransform.localPosition; break; case SyncOptions.SyncLocalRotation: observerEvent.floatData = Util.GetLocalRotationData(observeredTransform.transform); lastRotation = observeredTransform.transform.rotation.eulerAngles; break; case SyncOptions.SyncLocalPositionAndRotation: observerEvent.floatData = Util.GetLocalPositionAndRotationData(observeredTransform.transform); lastPosition = observeredTransform.transform.localRotation.eulerAngles; lastRotation = observeredTransform.transform.localPosition; break; } EdgeManager.SendUDPMessage(observerEvent); }
/// <summary> /// Changes the owner of an Observable object /// <para>ChangeOwnership() will change the owner in the local player's world and </para> /// <para>update all room members about Ownership change.</para> /// </summary> /// <param name="newOwnerId">The new owner player id</param> public void ChangeOwnership(string newOwnerId) { if (owner != null) { if (newOwnerId == owner.playerId) { Debug.LogWarning("EdgeMultiplay: No ownership change needed you already own the observable object"); return; } if (owner.observer != null) { owner.observer.observables.Remove(this); } owner = EdgeManager.GetPlayer(newOwnerId); if (owner == null) { throw new Exception("EdgeMultiplay: Couldn't find player with id: " + newOwnerId); } if (owner.observer == null) { owner.gameObject.AddComponent <EdgeMultiplayObserver>(); } owner.observer.observables.Add(this); owner.observer.UpdateObservables(); GamePlayEvent changeOwnershipEvent = new GamePlayEvent() { eventName = "ObservableOwnershipChange", stringData = new string[1] { newOwnerId }, integerData = new int[1] { observableIndex } }; EdgeManager.MessageSender.BroadcastMessage(changeOwnershipEvent); } else { Debug.LogError("EdgeMultiplay: Observer has no owner, Use observer.SetOwnership() first"); return; } }
void OwnershipRequestReceived(GamePlayEvent gamePlayEvent) { // check if the observable owner is the local player if (gamePlayEvent.stringData[0] == localPlayer.playerId) { //get the observable Observable observable = localPlayer.observer.observables .Find(obs => obs.observableIndex == gamePlayEvent.integerData[0]); if (observable == null) { Debug.LogWarning("EdgeMultiplay: couldn't find the observable"); return; } NetworkedPlayer requestee = GetPlayer(gamePlayEvent.senderId); // Inform the current owner about the ownership request // Triggers OnOwnershipRequestReceived() callback localPlayer.ownershipRequested(requestee, observable); } }
/// <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); }
void HandleUDPMessage(string message) { GamePlayEvent gamePlayEvent = JsonUtility.FromJson <GamePlayEvent>(message); switch (gamePlayEvent.type) { case "GamePlayEvent": if (gamePlayEvent.eventName == "EdgeMultiplayObserver") { SyncObject(gamePlayEvent); } else { // if used for other than that Syncing GameObjects Position & Rotation // it wil trigger OnUDPMessagesReceived() EdgeMultiplayCallbacks.udpEventReceived(gamePlayEvent); } break; default: Debug.LogError("Unknown UDP message arrived: " + message); break; } }
/// <summary> /// During GamePlay once a event received from another player (Websocket) /// </summary> /// <param name="gamePlayEvent">received GamePlayEvent</param> public virtual void OnWebSocketEventReceived(GamePlayEvent gamePlayEvent) { Debug.Log("WebSocket Event Received From Server : " + gamePlayEvent.eventName); }
/// <summary> /// Sends an event to all players in the room except the sender /// </summary> public void BroadcastMessage(string eventName, List <float> floatArray) { GamePlayEvent gameplayEvent = new GamePlayEvent(eventName, floatArray); edgeManager.SendGamePlayEvent(gameplayEvent); }
/// <summary> /// Sends an event to all players in the room except the sender /// </summary> public void BroadcastMessage(string eventName, List <int> integerArray) { GamePlayEvent gameplayEvent = new GamePlayEvent(eventName, integerArray); edgeManager.SendGamePlayEvent(gameplayEvent); }
/// <summary> /// Sends an event to all players in the room except the sender /// </summary> public void BroadcastMessage(string eventName, Vector3 position, Quaternion rotation) { GamePlayEvent gameplayEvent = new GamePlayEvent(eventName, position, rotation); edgeManager.SendGamePlayEvent(gameplayEvent); }
/// <summary> /// During GamePlay once a event received from another player (UDP) /// </summary> /// <param name="gamePlayEvent">received GamePlayEvent</param> public virtual void OnUDPEventReceived(GamePlayEvent gamePlayEvent) { //Debug.Log("UDP Msg Received Event From Server : " + gamePlayEvent.eventName); }
/// <summary> /// Called once GamePlay Events Received from the server /// </summary> /// <param name="gamePlayEvent">the received gamePlayEvent</param> public virtual void OnMessageReceived(GamePlayEvent gamePlayEvent) { Debug.Log("GamePlayEvent Received"); }
public void SendGamePlayEvent(GamePlayEvent mobiledgexEvent) { mobiledgexEvent.roomId = gameSession.roomId; mobiledgexEvent.senderId = gameSession.playerId; wsClient.Send(Messaging <GamePlayEvent> .Serialize(mobiledgexEvent)); }
void ReflectEvent(GamePlayEvent receivedEvent) { EdgeMultiplayCallbacks.eventReceived(receivedEvent); }
void HandleWebSocketMessage(string message) { var msg = MessageWrapper.UnWrapMessage(message); switch (msg.type) { case "register": Register register = Messaging <Register> .Deserialize(message); gameSession.sessionId = register.sessionId; gameSession.playerId = register.playerId; EdgeMultiplayCallbacks.registerEvent(); break; case "notification": Notification notification = Messaging <Notification> .Deserialize(message); switch (notification.notificationText) { case "left-room": gameSession.roomId = ""; EdgeMultiplayCallbacks.leftRoom(); break; case "join-room-faliure": EdgeMultiplayCallbacks.joinRoomFaliure(); break; case "new-room-created-in-lobby": EdgeMultiplayCallbacks.newRoomCreatedInLobby(); break; } EdgeMultiplayCallbacks.notificationEvent(notification); break; case "roomsList": RoomsList roomsList = Messaging <RoomsList> .Deserialize(message); EdgeMultiplayCallbacks.roomsList(roomsList.rooms); break; case "roomCreated": RoomCreated roomCreated = Messaging <RoomCreated> .Deserialize(message); gameSession.roomId = roomCreated.room.roomId; EdgeMultiplayCallbacks.roomCreated(roomCreated.room); break; case "roomJoin": RoomJoin roomJoin = Messaging <RoomJoin> .Deserialize(message); gameSession.roomId = roomJoin.room.roomId; EdgeMultiplayCallbacks.roomJoin(roomJoin.room); break; case "playerJoinedRoom": PlayerJoinedRoom playerJoinedRoom = Messaging <PlayerJoinedRoom> .Deserialize(message); EdgeMultiplayCallbacks.playerRoomJoined(playerJoinedRoom.room); break; case "gameStart": GameStart gameStart = Messaging <GameStart> .Deserialize(message); gameSession.currentPlayers = gameStart.room.roomMembers.ToArray(); foreach (Player player in gameStart.room.roomMembers) { if (player.playerId == gameSession.playerId) { gameSession.playerIndex = player.playerIndex; } } CreatePlayers(gameSession.currentPlayers); gameStarted = true; EdgeMultiplayCallbacks.gameStart(); if (useLocalHostServer) { udpClient = new MobiledgeXUDPClient(hostIPAddress, defaultEdgeMultiplayServerUDPPort); } else { udpClient = new MobiledgeXUDPClient(integration.GetHost(), integration.GetAppPort(LProto.L_PROTO_UDP).public_port); } SendUDPMessage(new GamePlayEvent() { eventName = "Start" }); break; case "GamePlayEvent": GamePlayEvent gamePlayEvent = Messaging <GamePlayEvent> .Deserialize(message); switch (gamePlayEvent.eventName) { case "NewObservableCreated": CreateObserverableObject(gamePlayEvent); break; case "ObservableOwnershipChange": UpdateObserverOwnership(gamePlayEvent); break; default: ReflectEvent(gamePlayEvent); break; } break; case "memberLeft": RoomMemberLeft playerLeft = Messaging <RoomMemberLeft> .Deserialize(message); EdgeMultiplayCallbacks.playerLeft(playerLeft); break; default: Debug.LogError("Unknown WebSocket message arrived: " + msg.type + ", message: " + message); break; } }