void Start() { // We won't immediately have connection, so at the start of the lobby we will set the connection status to show this connectionStatusText.text = "No Connection..."; GS.GameSparksAvailable += (isAvailable) => { if (isAvailable) { connectionStatusText.text = "GameSparks Connected..."; } else { connectionStatusText.text = "GameSparks Disconnected..."; } }; // we won't start with a user logged in so lets show this also userIdText.text = "No User Logged In..."; // Login button listener. So we don't need to create extra methods // loginButton.onClick.AddListener(() => { MultiplayerNetworkingManager.Instance().AuthenticateUser(usernameInput.text, passwordInput.text, OnRegistration, OnAuthentication); }); }
private void OnMatchFound(GameSparks.Api.Messages.MatchFoundMessage _message) { Debug.Log("GM| Match Found!..."); // Store the MatchFoundMessage data and have MultiplayerNetworkingManager start a RT session // Since MatchFoundMessage only happens once after sending a MatchmakingRequest (to start a new game), a new RT session instance is created each time. tempRTSessionInfo = new RTSessionInfo(_message); MultiplayerNetworkingManager.Instance().StartNewRTSession(tempRTSessionInfo); }
private bool DetermineIsUser(int _peerId) { // If it's the local user if (_peerId == MultiplayerNetworkingManager.Instance().GetRTSession().PeerId) { return(true); } return(false); }
// Use this for intialization. Called by MultiplayerNetworkingManager when the RT session is ready. public void StartGame() { // Loop through all the players in the RT session and add the player game objects into the scene. for (int i = 0; i < MultiplayerNetworkingManager.Instance().GetSessionInfo().GetPlayerList().Count; i++) { // Add the player to the game AddPlayer(MultiplayerNetworkingManager.Instance().GetSessionInfo().GetPlayerList()[i].peerId); } UIManager.Instance().ResetGameUI(); }
private Transform GetParentTransform(int _peerId) { // If it's the local user... if (_peerId == MultiplayerNetworkingManager.Instance().GetRTSession().PeerId) { // ... child the camera avatar to the camera, so the CameraTracker script can share the camera's positiona and rotation relative to the cloud anchor. return(Camera.main.transform); } // Otherwise, child the camera avatar to the cloud anchor so opponents share their localPosition (position relative to the cloud anchor transform) return(anchorTransform); }
void OnCollisionEnter(Collision _col) { Debug.Log(_col.gameObject.name); using (RTData data = RTData.Get()) { data.SetInt(1, ownerPeerId); MultiplayerNetworkingManager.Instance().GetRTSession().SendData(6, GameSparks.RT.GameSparksRT.DeliveryIntent.UNRELIABLE_SEQUENCED, data); Collide(); } }
private string GetUsername(int _peerId) { // Loop through all the players for (int i = 0; i < MultiplayerNetworkingManager.Instance().GetSessionInfo().GetPlayerList().Count; i++) { // Find the player with this peer Id if (_peerId == MultiplayerNetworkingManager.Instance().GetSessionInfo().GetPlayerList()[i].peerId) { // Return the player's username return(MultiplayerNetworkingManager.Instance().GetSessionInfo().GetPlayerList()[i].displayName); } } return(""); }
public void SetSpectatorMode() { for (int i = 0; i < playerList.Count; i++) { if (playerList[i].isUser) { using (RTData data = RTData.Get()) { // Data doesn't need to contain anything since we are just notifying the other peers that players has attacked MultiplayerNetworkingManager.Instance().GetRTSession().SendData(5, GameSparks.RT.GameSparksRT.DeliveryIntent.UNRELIABLE, data); // send the data at op-code 3 playerList[i].EnterSpectatorMode(); spectateButton.interactable = false; } break; } } }
// This function ensures that the player can only attack after a certain cooldown private IEnumerator AbilityCooldown() { canAttack = false; // Send a data packet to GameSparks to notify opponent that player has attacked using (RTData data = RTData.Get()) { // Data doesn't need to contain anything since we are just notifying the other peers that players has attacked MultiplayerNetworkingManager.Instance().GetRTSession().SendData(3, GameSparks.RT.GameSparksRT.DeliveryIntent.UNRELIABLE, data); // send the data at op-code 3 TriggerAbility(); } // Wait for the cooldown period before letting the player attack again yield return(new WaitForSeconds(coolDown)); canAttack = true; }
public void EndGame() { // End the RT session MultiplayerNetworkingManager.Instance().GetRTSession().Disconnect(); // Remove the players foreach (PlayerManager player in playerList) { // Remove the player from the list of players playerList.Remove(player); // Destroy the gameObject Destroy(player.gameObject); break; } // Enable the matchmaking UI UIManager.Instance().ResetMatchmakingUI(); }
private IEnumerator SendCameraMovement() { // Camera's position relative to the cloud anchor. camRelativePosition = anchorTransform.InverseTransformPoint(transform.position); // Camera's rotation relative to the cloud anchor. camRelativeRotation = Quaternion.Inverse(anchorTransform.rotation) * transform.rotation; // Construct a packet containing the camera's position and rotation relative to the cloud anchor using (RTData data = RTData.Get()) { data.SetVector3(1, camRelativePosition); data.SetVector3(2, camRelativeRotation.eulerAngles); // Send the packet with OpCode 2 MultiplayerNetworkingManager.Instance().GetRTSession().SendData(2, GameSparks.RT.GameSparksRT.DeliveryIntent.UNRELIABLE_SEQUENCED, data); } yield return(new WaitForSeconds(updateRate)); StartCoroutine(SendCameraMovement()); }
// Keeps the SessionInfo playerList updated with the current match participants. This is called each time the match is updated (e.g. a player joins or leaves the match). private void OnMatchUpdated(GameSparks.Api.Messages.MatchUpdatedMessage _message) { MultiplayerNetworkingManager.Instance().GetSessionInfo().UpdateRTSessionInfo(_message); }
public void FindMatch() { MultiplayerNetworkingManager.Instance().SendMatchmakingRequest(matchShortCode, 0, matchGroup); }
void Awake() { instance = this; // if not, give it a reference to this class DontDestroyOnLoad(this.gameObject); // and make this object persistent as we load new scenes }
void Update() { // Only the user can provide input to this Attack script if (isUser) { if (canAttack) { #region Touch Input if (Input.touchCount > 0) { Touch touch = Input.touches[0]; // Reset the timer once the user touches the screen if (touch.phase == TouchPhase.Began) { timer = 0f; isPressed = true; } if (isPressed) { timer += Time.deltaTime; // If the timer is a long press, then activate the shield if (timer > longPressCutoff) { if (!shieldActivated) { using (RTData data = RTData.Get()) { data.SetInt(1, 1); MultiplayerNetworkingManager.Instance().GetRTSession().SendData(4, GameSparks.RT.GameSparksRT.DeliveryIntent.UNRELIABLE, data); TriggerShield(true); } shieldActivated = true; } } } // Check the timer amount when the user lifts his finger off the screen if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled) { if (timer < longPressCutoff) { // Tap ended StartCoroutine(AbilityCooldown()); } else { // Long press ended if (shieldActivated) { using (RTData data = RTData.Get()) { data.SetInt(1, 2); MultiplayerNetworkingManager.Instance().GetRTSession().SendData(4, GameSparks.RT.GameSparksRT.DeliveryIntent.UNRELIABLE, data); TriggerShield(false); } shieldActivated = false; } } isPressed = false; } if (touch.phase == TouchPhase.Moved) { } } #endregion #region Keyboard Input // Reset the timer once the user touches the screen if (Input.GetKeyDown("space")) { timer = 0f; isPressed = true; } if (isPressed) { timer += Time.deltaTime; // If the timer is a long press, then activate the shield if (timer > longPressCutoff) { if (!shieldActivated) { using (RTData data = RTData.Get()) { data.SetInt(1, 1); MultiplayerNetworkingManager.Instance().GetRTSession().SendData(4, GameSparks.RT.GameSparksRT.DeliveryIntent.UNRELIABLE, data); TriggerShield(true); } shieldActivated = true; } } } // Check the timer amount when the user lifts his finger off the screen if (Input.GetKeyUp("space")) { if (timer < longPressCutoff) { // Tap ended StartCoroutine(AbilityCooldown()); } else { // Long press ended if (shieldActivated) { using (RTData data = RTData.Get()) { data.SetInt(1, 2); MultiplayerNetworkingManager.Instance().GetRTSession().SendData(4, GameSparks.RT.GameSparksRT.DeliveryIntent.UNRELIABLE, data); TriggerShield(false); } shieldActivated = false; } } isPressed = false; } #endregion } } }