This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
/
ResetForAuthorityGainedSystem.cs
107 lines (94 loc) · 4.7 KB
/
ResetForAuthorityGainedSystem.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
using Improbable.Gdk.Core;
using Unity.Entities;
using UnityEngine;
namespace Improbable.Gdk.TransformSynchronization
{
[DisableAutoCreation]
[UpdateBefore(typeof(DefaultUpdateLatestTransformSystem))]
[UpdateInGroup(typeof(SpatialOSUpdateGroup))]
public class ResetForAuthorityGainedSystem : ComponentSystem
{
private WorkerSystem worker;
private ComponentUpdateSystem updateSystem;
private EntityQuery rigidbodyGroup;
private EntityQuery transformGroup;
protected override void OnCreate()
{
base.OnCreate();
worker = World.GetExistingSystem<WorkerSystem>();
updateSystem = World.GetExistingSystem<ComponentUpdateSystem>();
// TODO: we need "auth loss" exposed to set the component group filters below correctly.
// Alternatively, we need an authority changed component that is filled at the beginning of the tick.
rigidbodyGroup = GetEntityQuery(
ComponentType.ReadOnly<Rigidbody>(),
ComponentType.ReadOnly<TransformInternal.Component>(),
ComponentType.ReadOnly<SpatialEntityId>(),
ComponentType.ReadWrite<TicksSinceLastTransformUpdate>(),
ComponentType.ReadWrite<BufferedTransform>(),
ComponentType.Exclude<NewlyAddedSpatialOSEntity>(),
ComponentType.ReadOnly<TransformInternal.ComponentAuthority>()
);
rigidbodyGroup.SetFilter(TransformInternal.ComponentAuthority.Authoritative);
transformGroup = GetEntityQuery(
ComponentType.ReadOnly<UnityEngine.Transform>(),
ComponentType.ReadOnly<TransformInternal.Component>(),
ComponentType.ReadOnly<SpatialEntityId>(),
ComponentType.ReadWrite<TicksSinceLastTransformUpdate>(),
ComponentType.ReadWrite<BufferedTransform>(),
ComponentType.Exclude<NewlyAddedSpatialOSEntity>(),
ComponentType.Exclude<Rigidbody>(),
ComponentType.ReadOnly<TransformInternal.ComponentAuthority>()
);
transformGroup.SetFilter(TransformInternal.ComponentAuthority.Authoritative);
}
protected override void OnUpdate()
{
UpdateRigidbodyData();
UpdateTransformData();
}
private void UpdateRigidbodyData()
{
Entities.With(rigidbodyGroup).ForEach(
(Entity entity, DynamicBuffer<BufferedTransform> buffer,
ref TicksSinceLastTransformUpdate ticksSinceLastTransformUpdate,
ref TransformInternal.Component transformInternal,
ref SpatialEntityId spatialEntityId) =>
{
if (updateSystem
.GetAuthorityChangesReceived(spatialEntityId.EntityId, TransformInternal.ComponentId)
.Count == 0)
{
return;
}
var rigidbody = EntityManager.GetComponentObject<Rigidbody>(entity);
rigidbody.MovePosition(TransformUtils.ToUnityVector3(transformInternal.Location) + worker.Origin);
rigidbody.MoveRotation(TransformUtils.ToUnityQuaternion(transformInternal.Rotation));
rigidbody.AddForce(TransformUtils.ToUnityVector3(transformInternal.Velocity) - rigidbody.velocity,
ForceMode.VelocityChange);
buffer.Clear();
ticksSinceLastTransformUpdate = new TicksSinceLastTransformUpdate();
});
}
private void UpdateTransformData()
{
Entities.With(transformGroup).ForEach((Entity entity,
DynamicBuffer<BufferedTransform> buffer,
ref TicksSinceLastTransformUpdate ticksSinceLastTransformUpdate,
ref TransformInternal.Component transformInternal,
ref SpatialEntityId spatialEntityId) =>
{
if (updateSystem
.GetAuthorityChangesReceived(spatialEntityId.EntityId, TransformInternal.ComponentId)
.Count == 0)
{
return;
}
var unityTransform = EntityManager.GetComponentObject<UnityEngine.Transform>(entity);
unityTransform.position = TransformUtils.ToUnityVector3(transformInternal.Location) + worker.Origin;
unityTransform.rotation = TransformUtils.ToUnityQuaternion(transformInternal.Rotation);
buffer.Clear();
ticksSinceLastTransformUpdate = new TicksSinceLastTransformUpdate();
});
}
}
}