-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathUpdatePlayerRankSystem.cs
82 lines (72 loc) · 2.76 KB
/
UpdatePlayerRankSystem.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
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.NetCode;
using System.Collections.Generic;
using Unity.MegacityMetro.UI;
using Unity.NetCode.Extensions;
namespace Unity.MegacityMetro.Gameplay
{
/// <summary>
/// System to update the score ranking.
/// </summary>
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation)]
public partial struct UpdateScoreRankingSystem : ISystem, ISystemStartStop
{
private EntityQuery m_PlayersQuery;
private EntityQuery m_LocalPlayerQuery;
public void OnCreate(ref SystemState state)
{
m_LocalPlayerQuery = state.GetEntityQuery(ComponentType.ReadOnly<GhostOwnerIsLocal>());
m_PlayersQuery = state.GetEntityQuery(ComponentType.ReadOnly<PlayerScore>(), ComponentType.ReadOnly<PlayerName>());
state.RequireForUpdate(m_PlayersQuery);
state.RequireForUpdate(m_LocalPlayerQuery);
}
public void OnStartRunning(ref SystemState state)
{
HUD.Instance.Leaderboard.Show();
}
public void OnStopRunning(ref SystemState state)
{
HUD.Instance.Leaderboard.Hide();
}
public void OnUpdate(ref SystemState state)
{
if (m_LocalPlayerQuery.CalculateEntityCount() <= 0)
return;
var localEntityPlayer = m_LocalPlayerQuery.ToEntityArray(Allocator.Temp)[0];
var playerCount = m_PlayersQuery.CalculateEntityCount();
var ranking = CollectionHelper.CreateNativeArray<PlayerScoreAspect>(playerCount, state.WorldUpdateAllocator);
var index = 0;
foreach (var player in SystemAPI.Query<PlayerScoreAspect>())
{
player.SetIsLocalPlayer(ref localEntityPlayer);
ranking[index] = player;
index++;
}
ranking.Sort(new SortableScoreComparer());
for (int i = 0; i < ranking.Length; i++)
{
var isTopRanked = i == 0;
PlayerInfoController.Instance.UpdateBadge(ranking[i].Self, isTopRanked);
}
HUD.Instance?.Leaderboard?.SetRanking(ref ranking);
ranking.Dispose(state.Dependency);
}
}
/// <summary>
/// Executes comparison between rank parameters
/// </summary>
[BurstCompile]
public struct SortableScoreComparer : IComparer<PlayerScoreAspect>
{
public int Compare(PlayerScoreAspect x, PlayerScoreAspect y)
{
if(!y.Value.Equals(x.Value))
return y.Value.CompareTo(x.Value);
if (!y.Kills.Equals(x.Kills))
return y.Kills.CompareTo(x.Kills);
return x.NetworkId.CompareTo(y.NetworkId);
}
}
}