public override void OnValidate() { // always >= 0 maxConnections = Mathf.Max(maxConnections, 0); // always <= maxConnections minPlayers = Mathf.Min(minPlayers, maxConnections); // always >= 0 minPlayers = Mathf.Max(minPlayers, 0); if (roomPlayerPrefab != null) { NetworkIdentity identity = roomPlayerPrefab.GetComponent <NetworkIdentity>(); if (identity == null) { roomPlayerPrefab = null; Debug.LogError("RoomPlayer prefab must have a NetworkIdentity component."); } } if (!allowLocalPlayers) { // client itself will count as a local player, may rise some complication on the way maxLocalPlayers = 1; } else { maxLocalPlayers = 4; // or more... } base.OnValidate(); }
/// <summary> /// /// </summary> /// <param name="conn">Connection of the client</param> /// <param name="extraMessage"></param> public override void OnServerAddPlayer(NetworkConnection conn, AddPlayerMessage extraMessage) { if (SceneManager.GetActiveScene().name != RoomScene) { return; } if (roomSlots.Count == maxConnections) { return; } allPlayersReady = false; if (LogFilter.Debug) { Debug.LogFormat("NetworkRoomManager.OnServerAddPlayer playerPrefab:{0}", roomPlayerPrefab.name); } GameObject newRoomGameObject = OnRoomServerCreateRoomPlayer(conn); if (newRoomGameObject == null) { newRoomGameObject = (GameObject)Instantiate(roomPlayerPrefab.gameObject, Vector3.zero, Quaternion.identity); } NetworkRoomPlayer newRoomPlayer = newRoomGameObject.GetComponent <NetworkRoomPlayer>(); roomSlots.Add(newRoomPlayer); RecalculateRoomPlayerIndices(); NetworkServer.AddPlayerForConnection(conn, newRoomGameObject); }
/// <summary> /// /// </summary> /// <param name="conn">Connection of the client</param> public override void OnServerDisconnect(NetworkConnection conn) { if (conn.identity != null) { NetworkRoomPlayer player = conn.identity.GetComponent <NetworkRoomPlayer>(); if (player != null) { roomSlots.Remove(player); } } allPlayersReady = false; foreach (NetworkRoomPlayer player in roomSlots) { if (player != null) { player.GetComponent <NetworkRoomPlayer>().readyToBegin = false; } } if (SceneManager.GetActiveScene().name == RoomScene) { RecalculateRoomPlayerIndices(); } base.OnServerDisconnect(conn); OnRoomServerDisconnect(conn); }
/// <summary> /// Called on the server when a client disconnects. /// <para>This is called on the Server when a Client disconnects from the Server. Use an override to decide what should happen when a disconnection is detected.</para> /// </summary> /// <param name="conn">Connection from client.</param> public override void OnServerDisconnect(NetworkConnection conn) { if (conn.identity != null) { NetworkRoomPlayer player = conn.identity.GetComponent <NetworkRoomPlayer>(); if (player != null) { player.RpcDisconnectRoomPlayer(); roomSlots.Remove(player); } } /* * allPlayersReady = false; * * foreach (NetworkRoomPlayer player in roomSlots) * { * if (player != null) * player.GetComponent<NetworkRoomPlayer>().readyToBegin = false; * } */ if (IsSceneActive(RoomScene)) { RecalculateRoomPlayerIndices(); } base.OnServerDisconnect(conn); OnRoomServerDisconnect(conn); }
// for users to apply settings from their room player object to their in-game player object /// <summary> /// This is called on the server when it is told that a client has finished switching from the room scene to a game player scene. /// <para>When switching from the room, the room-player is replaced with a game-player object. This callback function gives an opportunity to apply state from the room-player to the game-player object.</para> /// </summary> /// <param name="roomPlayer">The room player object.</param> /// <param name="gamePlayer">The game player object.</param> /// <returns>False to not allow this player to replace the room player.</returns> public virtual bool OnRoomServerSceneLoadedForPlayer(GameObject roomPlayer, GameObject gamePlayer) { // pass data from roomPlayer to gamePlayer // this will hapened only on server, i need to do it also on all clients !!! NetworkRoomPlayer networkRoomPlayer = roomPlayer.GetComponent <NetworkRoomPlayer>(); GamePlayer _gamePlayer = gamePlayer.GetComponent <GamePlayer>(); _gamePlayer.clientIndex = networkRoomPlayer.clientIndex; _gamePlayer.localPlayerClientCount = networkRoomPlayer.localPlayerCount; gamePlayer.name = "gamePlayer_" + _gamePlayer.clientIndex; return(true); }
/// <summary> /// Called on the server when a client disconnects. /// <para>This is called on the Server when a Client disconnects from the Server. Use an override to decide what should happen when a disconnection is detected.</para> /// </summary> /// <param name="conn">Connection from client.</param> public override void OnServerDisconnect(NetworkConnection conn) { if (conn.identity != null) { NetworkRoomPlayer roomPlayer = conn.identity.GetComponent <NetworkRoomPlayer>(); if (roomPlayer != null) { roomSlots.Remove(roomPlayer); } foreach (NetworkIdentity clientOwnedObject in conn.clientOwnedObjects) { roomPlayer = clientOwnedObject.GetComponent <NetworkRoomPlayer>(); if (roomPlayer != null) { roomSlots.Remove(roomPlayer); } } } allPlayersReady = false; foreach (NetworkRoomPlayer player in roomSlots) { if (player != null) { player.GetComponent <NetworkRoomPlayer>().readyToBegin = false; } } if (IsSceneActive(RoomScene)) { RecalculateRoomPlayerIndices(); } base.OnServerDisconnect(conn); OnRoomServerDisconnect(conn); }
public override void OnValidate() { // always >= 0 maxConnections = Mathf.Max(maxConnections, 0); // always <= maxConnections minPlayers = Mathf.Min(minPlayers, maxConnections); // always >= 0 minPlayers = Mathf.Max(minPlayers, 0); if (roomPlayerPrefab != null) { NetworkIdentity identity = roomPlayerPrefab.GetComponent <NetworkIdentity>(); if (identity == null) { roomPlayerPrefab = null; Debug.LogError("RoomPlayer prefab must have a NetworkIdentity component."); } } base.OnValidate(); }
private void mainLoop() { //Check if we are ready if (!readyToStart) { return; } //We are now ready and in the main game loop //Check if we have a president if (!server.havePres()) { //If we don't have a president get one int nextPresIndex = getNewPres(); //We have found a suitable president Mirror.NetworkRoomPlayer x = server.roomSlots[nextPresIndex]; customLobby.RoomPlayer nextPres = (customLobby.RoomPlayer)x; nextPres.CmdChangeRole("President"); //Check if president was assigned if (nextPres.role != "President") { return; } //Add the new president to the prevPresidents list prevPresidents.Add(nextPresIndex); //Remove the old president so that they can be president again if (prevPresidents.Count > 1) { prevPresidents.RemoveAt(0); } } //Wait for president to select a chancellor int cnt = 0; foreach (customLobby.RoomPlayer player in server.roomSlots) { if (player.role == "Chancellor") { cnt++; } } //No chancellor so return if (cnt == 0) { return; } //We have a chancellor //TODO: Implement our card draw functions here Debug.Log("GAMELOOP"); }