This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
/
ResetForAuthorityGainedSystem.cs
110 lines (98 loc) · 5.1 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
108
109
110
using Improbable.Gdk.Core;
using Improbable.Transform;
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 ComponentGroup rigidbodyGroup;
private ComponentGroup transformGroup;
protected override void OnCreateManager()
{
base.OnCreateManager();
worker = World.GetExistingManager<WorkerSystem>();
updateSystem = World.GetExistingManager<ComponentUpdateSystem>();
rigidbodyGroup = GetComponentGroup(
ComponentType.ReadOnly<Rigidbody>(),
ComponentType.ReadOnly<TransformInternal.Component>(),
ComponentType.ReadOnly<SpatialEntityId>(),
ComponentType.Create<TicksSinceLastTransformUpdate>(),
ComponentType.Create<BufferedTransform>(),
ComponentType.Subtractive<NewlyAddedSpatialOSEntity>(),
ComponentType.ReadOnly<TransformInternal.ComponentAuthority>()
);
rigidbodyGroup.SetFilter(TransformInternal.ComponentAuthority.Authoritative);
transformGroup = GetComponentGroup(
ComponentType.ReadOnly<UnityEngine.Transform>(),
ComponentType.ReadOnly<TransformInternal.Component>(),
ComponentType.ReadOnly<SpatialEntityId>(),
ComponentType.Create<TicksSinceLastTransformUpdate>(),
ComponentType.Create<BufferedTransform>(),
ComponentType.Subtractive<NewlyAddedSpatialOSEntity>(),
ComponentType.Subtractive<Rigidbody>(),
ComponentType.ReadOnly<TransformInternal.ComponentAuthority>()
);
transformGroup.SetFilter(TransformInternal.ComponentAuthority.Authoritative);
}
protected override void OnUpdate()
{
UpdateRigidbodyData();
UpdateTransformData();
}
private void UpdateTransformData()
{
var ticksSinceLastUpdateArray = rigidbodyGroup.GetComponentDataArray<TicksSinceLastTransformUpdate>();
var transformComponentArray = rigidbodyGroup.GetComponentDataArray<TransformInternal.Component>();
var bufferedTransformArray = rigidbodyGroup.GetBufferArray<BufferedTransform>();
var rigidbodyArray = rigidbodyGroup.GetComponentArray<Rigidbody>();
var spatialEntityIdArray = rigidbodyGroup.GetComponentDataArray<SpatialEntityId>();
for (int i = 0; i < transformComponentArray.Length; ++i)
{
// todo this is not a correct constraint. Needs a the auth loss temporary exposed to correctly do this
// alternatively this needs an authority changed component that is filled at the beginning of the tick
if (updateSystem
.GetAuthorityChangesReceived(spatialEntityIdArray[i].EntityId, TransformInternal.ComponentId)
.Count == 0)
{
continue;
}
var t = transformComponentArray[i];
var rigidbody = rigidbodyArray[i];
rigidbody.MovePosition(t.Location.ToUnityVector3() + worker.Origin);
rigidbody.MoveRotation(t.Rotation.ToUnityQuaternion());
rigidbody.AddForce(t.Velocity.ToUnityVector3() - rigidbody.velocity, ForceMode.VelocityChange);
bufferedTransformArray[i].Clear();
ticksSinceLastUpdateArray[i] = new TicksSinceLastTransformUpdate();
}
}
private void UpdateRigidbodyData()
{
var ticksSinceLastUpdateArray = transformGroup.GetComponentDataArray<TicksSinceLastTransformUpdate>();
var transformComponentArray = transformGroup.GetComponentDataArray<TransformInternal.Component>();
var bufferedTransformArray = transformGroup.GetBufferArray<BufferedTransform>();
var unityTransformArray = transformGroup.GetComponentArray<UnityEngine.Transform>();
var spatialEntityIdArray = transformGroup.GetComponentDataArray<SpatialEntityId>();
for (int i = 0; i < transformComponentArray.Length; ++i)
{
if (updateSystem
.GetAuthorityChangesReceived(spatialEntityIdArray[i].EntityId, TransformInternal.ComponentId)
.Count == 0)
{
continue;
}
var t = transformComponentArray[i];
var unityTransform = unityTransformArray[i];
unityTransform.position = t.Location.ToUnityVector3() + worker.Origin;
unityTransform.rotation = t.Rotation.ToUnityQuaternion();
bufferedTransformArray[i].Clear();
ticksSinceLastUpdateArray[i] = new TicksSinceLastTransformUpdate();
}
}
}
}