Skip to content

Commit

Permalink
feat: adding OnRoomServerPlayersNotReady to NetworkRoomManager that i…
Browse files Browse the repository at this point in the history
…s called when player ready changes and atleast 1 player is not ready (#1921)

* adding virtual function for Players not ready

* moving function calls to property

* making if check easier to read

* setting field

* making field private

* removed blank line

* Added override to template

* doc: Updated ChangeLog

Co-authored-by: Chris Langsenkamp <chris@clevertech.net>
  • Loading branch information
James-Frowen and Chris Langsenkamp committed May 27, 2020
1 parent 64533ca commit 9ae7fa2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 7 deletions.
47 changes: 40 additions & 7 deletions Assets/Mirror/Components/NetworkRoomManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public struct PendingPlayer
/// True when all players have submitted a Ready message
/// </summary>
[Tooltip("Diagnostic flag indicating all players are ready to play")]
public bool allPlayersReady;
[FormerlySerializedAs("allPlayersReady")]
[SerializeField] bool _allPlayersReady;

/// <summary>
/// These slots track players that enter the room.
Expand All @@ -78,6 +79,30 @@ public struct PendingPlayer
[Tooltip("List of Room Player objects")]
public List<NetworkRoomPlayer> roomSlots = new List<NetworkRoomPlayer>();

public bool allPlayersReady
{
get => _allPlayersReady;
set
{
bool wasReady = _allPlayersReady;
bool nowReady = value;

if (wasReady != nowReady)
{
_allPlayersReady = value;

if (nowReady)
{
OnRoomServerPlayersReady();
}
else
{
OnRoomServerPlayersNotReady();
}
}
}
}

public override void OnValidate()
{
// always >= 0
Expand Down Expand Up @@ -183,15 +208,17 @@ public void CheckReadyToBegin()
if (!IsSceneActive(RoomScene))
return;

if (minPlayers > 0 && NetworkServer.connections.Count(conn => conn.Value != null && conn.Value.identity.gameObject.GetComponent<NetworkRoomPlayer>().readyToBegin) < minPlayers)
int numberOfReadyPlayers = NetworkServer.connections.Count(conn => conn.Value != null && conn.Value.identity.gameObject.GetComponent<NetworkRoomPlayer>().readyToBegin);
bool enoughReadyPlayers = minPlayers <= 0 || numberOfReadyPlayers >= minPlayers;
if (enoughReadyPlayers)
{
pendingPlayers.Clear();
allPlayersReady = true;
}
else
{
allPlayersReady = false;
return;
}

pendingPlayers.Clear();
allPlayersReady = true;
OnRoomServerPlayersReady();
}

void CallOnClientEnterRoom()
Expand Down Expand Up @@ -604,6 +631,12 @@ public virtual void OnRoomServerPlayersReady()
ServerChangeScene(GameplayScene);
}

/// <summary>
/// This is called on the server when CheckReadyToBegin finds that players are not ready
/// <para>May be called multiple times while not ready players are joining</para>
/// </summary>
public virtual void OnRoomServerPlayersNotReady() { }

#endregion

#region room client virtuals
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ public class #SCRIPTNAME# : NetworkRoomManager
base.OnRoomServerPlayersReady();
}

/// <summary>
/// This is called on the server when CheckReadyToBegin finds that players are not ready
/// <para>May be called multiple times while not ready players are joining</para>
/// </summary>
public override void OnRoomServerPlayersNotReady() { }

#endregion

#region Client Callbacks
Expand Down
1 change: 1 addition & 0 deletions doc/General/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Mirror uses semantic versioning, and the versions shown here are those that were
## Version 14.x.x -- In Progress
- Added: [NetworkLogSettings](../Components/NetworkLogSettings.md) component and Log Settings Window.
- Added: SyncLists now support `AddRange`, `InsertRange`, and `RemoveAll`.
- Added: Network Room Manager now has a virtual `OnRoomServerPlayersNotReady` that fires on server from `CheckReadyToBegin`.
- Added: Network Room Player template now includes base Network Behaviour overrides.
- Added: Network Room Player now has a virtual hook for the index SyncVar, and the override is in the template.
- Fixed: Network Room Manager.minPlayers is now protected so it's available for derived classes.
Expand Down

0 comments on commit 9ae7fa2

Please sign in to comment.