more precise leave/join events

This commit is contained in:
_nebula 2023-08-02 11:36:09 +01:00
parent 0e01fb8bc8
commit e59575011e
2 changed files with 66 additions and 7 deletions

View File

@ -38,15 +38,37 @@ public interface IQSBAPI
uint[] GetPlayerIDs();
/// <summary>
/// Invoked when a player joins the game.
/// Invoked when any player (local or remote) joins the game.
/// </summary>
UnityEvent<uint> OnPlayerJoin();
/// <summary>
/// Invoked when a player leaves the game.
/// Invoked when any player (local or remote) leaves the game.
/// </summary>
UnityEvent<uint> OnPlayerLeave();
/// <summary>
/// Invoked when the local client joins a game,
/// either through connecting to an external game or hosting their own game.
/// </summary>
UnityEvent OnLocalJoin();
/// <summary>
/// Invoked when a client (but not the local player) joins the game.
/// </summary>
UnityEvent<uint> OnPeerJoin();
/// <summary>
/// Invoked when the local client leaves a game,
/// either through disconnecting from an external game or shutting down the hosted server.
/// </summary>
UnityEvent OnLocalLeave();
/// <summary>
/// Invoked when a client (but not the local player) leaves the game.
/// </summary>
UnityEvent<uint> OnPeerLeave();
/// <summary>
/// Sets some arbitrary data for a given player.
/// </summary>

View File

@ -24,9 +24,14 @@ public class QSBAPI : IQSBAPI
public uint[] GetPlayerIDs() => QSBPlayerManager.PlayerList.Select(x => x.PlayerId).ToArray();
public UnityEvent<uint> OnPlayerJoin() => QSBAPIEvents.OnPlayerJoinEvent;
public UnityEvent<uint> OnPlayerLeave() => QSBAPIEvents.OnPlayerLeaveEvent;
public UnityEvent OnLocalJoin() => QSBAPIEvents.OnLocalJoinEvent;
public UnityEvent OnLocalLeave() => QSBAPIEvents.OnLocalLeaveEvent;
public UnityEvent<uint> OnPeerJoin() => QSBAPIEvents.OnPeerJoinEvent;
public UnityEvent<uint> OnPeerLeave() => QSBAPIEvents.OnPeerLeaveEvent;
public void SetCustomData<T>(uint playerId, string key, T data) => QSBPlayerManager.GetPlayer(playerId).SetCustomData(key, data);
public T GetCustomData<T>(uint playerId, string key) => QSBPlayerManager.GetPlayer(playerId).GetCustomData<T>(key);
@ -41,11 +46,43 @@ internal static class QSBAPIEvents
{
static QSBAPIEvents()
{
QSBPlayerManager.OnAddPlayer += player => OnPlayerJoinEvent.Invoke(player.PlayerId);
QSBPlayerManager.OnRemovePlayer += player => OnPlayerLeaveEvent.Invoke(player.PlayerId);
QSBPlayerManager.OnAddPlayer += (player) =>
{
OnPlayerJoinEvent.Invoke(player.PlayerId);
if (player.IsLocalPlayer)
{
OnLocalJoinEvent.Invoke();
}
else
{
OnPeerJoinEvent.Invoke(player.PlayerId);
}
};
QSBPlayerManager.OnRemovePlayer += (player) =>
{
OnPlayerLeaveEvent.Invoke(player.PlayerId);
if (player.IsLocalPlayer)
{
OnLocalLeaveEvent.Invoke();
}
else
{
OnPeerLeaveEvent.Invoke(player.PlayerId);
}
};
}
internal class PlayerEvent : UnityEvent<uint> { }
internal static PlayerEvent OnPlayerJoinEvent = new PlayerEvent();
internal static PlayerEvent OnPlayerLeaveEvent = new PlayerEvent();
internal static PlayerEvent OnPlayerJoinEvent = new();
internal static PlayerEvent OnPlayerLeaveEvent = new();
internal static UnityEvent OnLocalJoinEvent = new();
internal static UnityEvent OnLocalLeaveEvent = new();
internal static PlayerEvent OnPeerJoinEvent = new();
internal static PlayerEvent OnPeerLeaveEvent = new();
}