forked from tmokmss/com.unity.multiplayer.samples.coop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetworkCharSelection.cs
108 lines (90 loc) · 3.74 KB
/
NetworkCharSelection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System;
using Unity.BossRoom.Gameplay.Configuration;
using Unity.BossRoom.Utils;
using Unity.Netcode;
namespace Unity.BossRoom.Gameplay.GameState
{
/// <summary>
/// Common data and RPCs for the CharSelect stage.
/// </summary>
public class NetworkCharSelection : NetworkBehaviour
{
public enum SeatState : byte
{
Inactive,
Active,
LockedIn,
}
/// <summary>
/// Describes one of the players in the lobby, and their current character-select status.
/// </summary>
public struct LobbyPlayerState : INetworkSerializable, IEquatable<LobbyPlayerState>
{
public ulong ClientId;
private FixedPlayerName m_PlayerName; // I'm sad there's no 256Bytes fixed list :(
public int PlayerNumber; // this player's assigned "P#". (0=P1, 1=P2, etc.)
public int SeatIdx; // the latest seat they were in. -1 means none
public float LastChangeTime;
public SeatState SeatState;
public LobbyPlayerState(ulong clientId, string name, int playerNumber, SeatState state, int seatIdx = -1, float lastChangeTime = 0)
{
ClientId = clientId;
PlayerNumber = playerNumber;
SeatState = state;
SeatIdx = seatIdx;
LastChangeTime = lastChangeTime;
m_PlayerName = new FixedPlayerName();
PlayerName = name;
}
public string PlayerName
{
get => m_PlayerName;
private set => m_PlayerName = value;
}
public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
{
serializer.SerializeValue(ref ClientId);
serializer.SerializeValue(ref m_PlayerName);
serializer.SerializeValue(ref PlayerNumber);
serializer.SerializeValue(ref SeatState);
serializer.SerializeValue(ref SeatIdx);
serializer.SerializeValue(ref LastChangeTime);
}
public bool Equals(LobbyPlayerState other)
{
return ClientId == other.ClientId &&
m_PlayerName.Equals(other.m_PlayerName) &&
PlayerNumber == other.PlayerNumber &&
SeatIdx == other.SeatIdx &&
LastChangeTime.Equals(other.LastChangeTime) &&
SeatState == other.SeatState;
}
}
private NetworkList<LobbyPlayerState> m_LobbyPlayers;
public Avatar[] AvatarConfiguration;
private void Awake()
{
m_LobbyPlayers = new NetworkList<LobbyPlayerState>();
}
/// <summary>
/// Current state of all players in the lobby.
/// </summary>
public NetworkList<LobbyPlayerState> LobbyPlayers => m_LobbyPlayers;
/// <summary>
/// When this becomes true, the lobby is closed and in process of terminating (switching to gameplay).
/// </summary>
public NetworkVariable<bool> IsLobbyClosed { get; } = new NetworkVariable<bool>(false);
/// <summary>
/// Server notification when a client requests a different lobby-seat, or locks in their seat choice
/// </summary>
public event Action<ulong, int, bool> OnClientChangedSeat;
/// <summary>
/// RPC to notify the server that a client has chosen a seat.
/// </summary>
[ServerRpc(RequireOwnership = false)]
public void ChangeSeatServerRpc(ulong clientId, int seatIdx, bool lockedIn)
{
OnClientChangedSeat?.Invoke(clientId, seatIdx, lockedIn);
}
}
}