This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
/
RateLimitedPositionSendSystem.cs
54 lines (46 loc) · 1.86 KB
/
RateLimitedPositionSendSystem.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
using Improbable.Gdk.Core;
using Unity.Entities;
using UnityEngine;
namespace Improbable.Gdk.TransformSynchronization
{
[DisableAutoCreation]
[UpdateAfter(typeof(DefaultUpdateLatestTransformSystem))]
[UpdateAfter(typeof(RateLimitedTransformSendSystem))]
[UpdateInGroup(typeof(SpatialOSUpdateGroup))]
public class RateLimitedPositionSendSystem : ComponentSystem
{
private EntityQuery positionGroup;
protected override void OnCreate()
{
base.OnCreate();
positionGroup = GetEntityQuery(
ComponentType.ReadWrite<LastPositionSentData>(),
ComponentType.ReadWrite<Position.Component>(),
ComponentType.ReadOnly<TransformInternal.Component>(),
ComponentType.ReadOnly<RateLimitedSendConfig>(),
ComponentType.ReadOnly<Position.ComponentAuthority>()
);
positionGroup.SetFilter(Position.ComponentAuthority.Authoritative);
}
protected override void OnUpdate()
{
Entities.With(positionGroup).ForEach((RateLimitedSendConfig config, ref Position.Component position,
ref TransformInternal.Component transformInternal, ref LastPositionSentData lastPositionSent) =>
{
lastPositionSent.TimeSinceLastUpdate += Time.deltaTime;
if (lastPositionSent.TimeSinceLastUpdate < 1.0f / config.MaxPositionUpdateRateHz)
{
return;
}
var coords = transformInternal.Location.ToCoordinates();
if (coords == position.Coords)
{
return;
}
position.Coords = coords;
lastPositionSent.TimeSinceLastUpdate = 0.0f;
lastPositionSent.Position = position;
});
}
}
}