Sunday, 28 February 2016

PUN shooter #14 - Multimap

Now I have 4 maps but not sure how I want to make them available: If all users choose their own map, the probability that two users are in the same room is now 1/N where N is the number of maps.

: D

But then again, I need a simple way to open the maps for showcasing and testing. So, I implemented an option to join a specific room (numbered in the menu). Now I want players who just choose 'Survival' to join a non empty room if possible (otherwise, pick a random room)
It's possible to just try joining rooms (since the names are known in advance) but this isn't gonna play nice with my workflow where loading the target level and joining a room are the same thing.

So, I connect to the network from the game menu and check rooms.

Before starting a game, we need to disconnect from the server, or vary connection logic to avoid a redundant connection attempt.

using UnityEngine;
using System.Collections;
public class RoomSelectionHelper : MonoBehaviour {
public bool onMasterServer = false;
public bool inTheLobby = false;
public int roomCount = 0;
public int selectedIndex = -1;
void Start(){ PhotonNetwork.ConnectUsingSettings("v4.2"); }
void OnConnectedToMaster(){
onMasterServer = true;
PhotonNetwork.JoinLobby ();
}
// You could select a room here but it should be safer
// To keep checking since room state change over time.
void OnJoinedLobby(){ inTheLobby = true; }
void Update(){
inTheLobby = PhotonNetwork.insideLobby;
if (inTheLobby) SelectRoom ();
}
void SelectRoom(){
RoomInfo[] rooms = PhotonNetwork.GetRoomList ();
roomCount = rooms.Length;
foreach (RoomInfo room in rooms) {
if (room.playerCount > 0 && room.open) {
if (room.playerCount < room.maxPlayers || room.maxPlayers == 0) {
DoSelectRoom (room.name);
}
}
}
}
// This part is custom. In our case we parse
// the room name to extract the level index.
void DoSelectRoom(string name){
int i = name.IndexOf('-');
string indexString = name.Substring (i + 1);
selectedIndex = int.Parse (indexString);
GetComponent<MainMenuActions> ().survivalLevelIndex = selectedIndex;
}
}
Since there wouldn't be much benefit to showing these options at this stage, they are only available when SHIFT is pressed.

Try the latest build [here]

No comments:

Post a Comment