forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayerListBufferEntry.cs
More file actions
41 lines (38 loc) · 1.63 KB
/
Copy pathPlayerListBufferEntry.cs
File metadata and controls
41 lines (38 loc) · 1.63 KB
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
using System;
using Unity.Entities;
namespace Unity.NetCode.Samples.PlayerList
{
/// <summary>
/// Similar to <see cref="PlayerListEntry"/>, except a <see cref="IBufferElementData"/>, allowing the client to store this data in a buffer.
/// Stores all players and their state (username, ping etc).
/// Index mapped to NetworkId - 1.
/// </summary>
/// <remarks>
/// This is therefore automatically sorted and deterministic.
/// Will contain disconnected players whose NetworkId has not been re-used.
/// Will also contain default entries as the list is resized with some spare capacity.
/// Depends on the implicit rules of NetworkId's.
/// A list is used to allow implicit resizing without having to dispose.
/// </remarks>
public struct PlayerListBufferEntry : IBufferElementData
{
/// <summary>Stores the last received RPC for this player.</summary>
public PlayerListEntry.ChangedRpc State;
public bool IsCreated => State.NetworkId != default;
/// <summary>
/// Returns the number of connected players.
/// Why? The buffer can also contain disconnected players.
/// </summary>
/// <param name="playerListBufferEntries"></param>
/// <returns></returns>
public static int CountNumConnectedPlayers(DynamicBuffer<PlayerListBufferEntry> playerListBufferEntries)
{
var count = 0;
foreach (var entry in playerListBufferEntries)
{
if (entry.IsCreated && entry.State.IsConnected) count++;
}
return count;
}
}
}