/// <summary> /// Creates the room. /// </summary> /// <param name="roomName">Room name to use</param> /// <param name="joinDuringGame">If set to <c>true</c> players can join /// after the game has started.</param> /// <param name="autoconnect">If set to <c>true</c> players are automatically /// connected to the room, otherwise they need to be explicitly accepted.</param> internal void CreateRoom( string roomName, bool joinDuringGame, bool autoconnect) { NearbyRoom room = NearbyRoom.CreateRoom(roomName); room.AutoJoin = autoconnect; room.AlwaysOpen = joinDuringGame; // Store off the room in with the players. The game manager // will pick up the room and start processing messages. GameManager.Instance.Room = room; // if you can join in progress and it is autoconnect, setup the // room and start it, no need to wait. if (joinDuringGame && autoconnect) { Debug.Log("Starting Game!"); PlayerInfo.AddPendingPlayer( new NearbyPlayer(localPlayerName), localAvatarIndex); GameManager.Instance.StartPlaying(GameManager.GameType.MultiplayerLocal); } else { statusMsg = "Waiting for players..."; GameManager.Instance.Room.WaitForPlayers(OnPlayerFound); Debug.Log(statusMsg); ShowLobbyPanel(); } }
/// <summary> /// Called when the nearby connection response from /// sending Remote connection request to the room advertiser. /// </summary> /// <param name="response">Response.</param> internal void OnRoomJoined(ConnectionResponse response) { Debug.Log("OnRoomJoined Called status: " + response.ResponseStatus); if (response.ResponseStatus == ConnectionResponse.Status.Accepted) { // if we are connected, stop looking for rooms. NearbyRoom.StopRoomDiscovery(); // the first payload is sent with the response so we can initialize // the game scene. OnMessageReceived(Room.Address, response.Payload); connected = true; } else if (response.ResponseStatus == ConnectionResponse.Status.ErrorAlreadyConnected) { // cleanup the old connection and join again. Room.Disconnect(); PlayerInfo localPlayer = PlayerInfo.LocalPlayer; Room.JoinRoom( localPlayer.Player, localPlayer.SerializedData, OnRoomJoined); } else { GameOver("Error joining room: " + response.ResponseStatus); } }
/// <summary> /// Starts the discovery of rooms. /// </summary> internal void StartDiscovery() { statusMsg = "Looking for Games..."; PlayerInfo.AddPendingPlayer( new NearbyPlayer(localPlayerName), localAvatarIndex); NearbyRoom.FindRooms(OnRoomFound); ShowLobbyPanel(); }
public void OnEndpointLost(string lostEndpointId) { Debug.Log("Endpoint lost: " + lostEndpointId); if (roomDiscoveredCallback != null) { NearbyRoom room = NearbyRoom.LookupRoom(lostEndpointId); if (room != null) { roomDiscoveredCallback.Invoke(room, false); } } }
/// <summary> /// Stops the playing. This cleans up the internal data structures /// and stops the nearby communications. /// </summary> public void StopPlaying() { levelmanager.DestroyBoard(); if (Room != null) { NearbyRoom.StopAll(); Room = null; PlayerInfo.ClearAllPlayers(); gameState.LevelData.Clear(); } // 0 is the main menu scene. SceneManager.LoadScene(0); }
public void OnEndpointFound(EndpointDetails discoveredEndpoint) { Debug.Log("Found Endpoint!"); NearbyRoom room = new NearbyRoom( discoveredEndpoint.DeviceId, discoveredEndpoint.EndpointId, discoveredEndpoint.Name); if (roomDiscoveredCallback != null) { Debug.Log("Invoking roomCallback."); roomDiscoveredCallback.Invoke(room, true); } else { Debug.Log("No roomCallback configured."); } }
/// <summary> /// called when a room is discovered. /// </summary> /// <param name="room">Room.</param> /// <param name="available">If set to <c>true</c> available.</param> internal void OnRoomFound(NearbyRoom room, bool available) { if (available) { GameObject obj = Instantiate(itemChoicePrefab) as GameObject; obj.transform.SetParent(lobbyListArea.transform, false); obj.GetComponentInChildren <Text>().text = room.Name; Toggle t = obj.GetComponentInChildren <Toggle>(); t.gameObject.name = room.EndpointId; t.group = lobbyPanel.GetComponent <ToggleGroup>(); Debug.Log("Room found: " + room.Name); discoveredRooms.Add(room.EndpointId, obj); } else { if (discoveredRooms.ContainsKey(room.EndpointId)) { GameObject obj = discoveredRooms[room.EndpointId]; DestroyObject(obj); discoveredRooms.Remove(room.EndpointId); } } }
/// <summary> /// Enters the room from the lobby. /// </summary> public void EnterRoom() { if (joining) { // get the room that we should join. Toggle[] rooms = lobbyListArea.GetComponentsInChildren <Toggle>(); foreach (Toggle t in rooms) { if (t.isOn) { GameManager.Instance.Room = NearbyRoom.LookupRoomByEndpoint(t.name); break; } } if (GameManager.Instance.Room != null) { GameManager.Instance.StartPlaying(GameManager.GameType.MultiplayerRemote); } else { statusMsg = "You must select a room to start!"; } return; } else { if (!GameManager.Instance.Room.AutoJoin) { // loop over players and remove unchecked players Toggle[] players = lobbyListArea.GetComponentsInChildren <Toggle>(); Debug.Log("Checking " + players.Length + " players"); foreach (Toggle t in players) { if (!t.isOn) { Debug.Log("Removing " + t.gameObject.name); PlayerInfo.RemovePendingPlayer(t.gameObject.name); } else { Debug.Log("Accepting " + t.gameObject.name); PlayerInfo p = PlayerInfo.GetPlayer(t.gameObject.name); if (p != null) { GameManager.Instance.Room.AcceptRequest(p.Player.EndpointId); } else { Debug.LogError("Cannot find NearbyPlayer for " + t.gameObject.name); } } } } if (!GameManager.Instance.Room.AlwaysOpen) { GameManager.Instance.Room.CloseRoom(); } Debug.Log("Starting Game!"); GameManager.Instance.StartPlaying(GameManager.GameType.MultiplayerLocal); } }
public void CancelMultiplayer() { GameManager.Instance.Room = null; NearbyRoom.StopAll(); GameManager.Instance.StopPlaying(); }
/// <summary> /// called when a room is discovered. /// </summary> /// <param name="room">Room.</param> /// <param name="available">If set to <c>true</c> available.</param> internal void OnRoomFound(NearbyRoom room, bool available) { if (available) { GameObject obj = Instantiate(itemChoicePrefab) as GameObject; obj.transform.SetParent(lobbyListArea.transform, false); obj.GetComponentInChildren<Text>().text = room.Name; Toggle t = obj.GetComponentInChildren<Toggle>(); t.gameObject.name = room.EndpointId; t.group = lobbyPanel.GetComponent<ToggleGroup>(); Debug.Log("Room found: " + room.Name); discoveredRooms.Add(room.EndpointId, obj); } else { if (discoveredRooms.ContainsKey(room.EndpointId)) { GameObject obj = discoveredRooms[room.EndpointId]; DestroyObject(obj); discoveredRooms.Remove(room.EndpointId); } } }
/// <summary> /// Stops the playing. This cleans up the internal data structures /// and stops the nearby communications. /// </summary> public void StopPlaying() { levelmanager.DestroyBoard(); if (Room != null) { NearbyRoom.StopAll(); Room = null; PlayerInfo.ClearAllPlayers(); gameState.LevelData.Clear(); } // 0 is the main menu scene. Application.LoadLevel(0); }