This repository has been archived by the owner on Jan 18, 2022. It is now read-only.
/
SetKinematicFromAuthoritySystem.cs
98 lines (86 loc) · 3.66 KB
/
SetKinematicFromAuthoritySystem.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
using Improbable.Gdk.Core;
using Improbable.Transform;
using Improbable.Worker.CInterop;
using Unity.Entities;
using UnityEngine;
namespace Improbable.Gdk.TransformSynchronization
{
[DisableAutoCreation]
[AlwaysUpdateSystem]
[UpdateInGroup(typeof(SpatialOSUpdateGroup))]
[UpdateBefore(typeof(ResetForAuthorityGainedSystem))]
public class SetKinematicFromAuthoritySystem : ComponentSystem
{
private ComponentUpdateSystem updateSystem;
private ComponentGroup newEntityGroup;
private ComponentGroup authChangeGroup;
protected override void OnCreateManager()
{
base.OnCreateManager();
updateSystem = World.GetExistingManager<ComponentUpdateSystem>();
newEntityGroup = GetComponentGroup(
ComponentType.Create<KinematicStateWhenAuth>(),
ComponentType.ReadOnly<Rigidbody>(),
ComponentType.ReadOnly<NewlyAddedSpatialOSEntity>(),
ComponentType.ReadOnly<TransformInternal.ComponentAuthority>()
);
newEntityGroup.SetFilter(TransformInternal.ComponentAuthority.NotAuthoritative);
authChangeGroup = GetComponentGroup(
ComponentType.Create<KinematicStateWhenAuth>(),
ComponentType.ReadOnly<Rigidbody>(),
ComponentType.ReadOnly<SpatialEntityId>(),
ComponentType.Subtractive<NewlyAddedSpatialOSEntity>()
);
}
protected override void OnUpdate()
{
UpdateNewEntityGroup();
UpdateAuthChangeGroup();
}
private void UpdateNewEntityGroup()
{
var rigidbodyArray = newEntityGroup.GetComponentArray<Rigidbody>();
var kinematicStateWhenAuthArray = newEntityGroup.GetComponentDataArray<KinematicStateWhenAuth>();
for (int i = 0; i < rigidbodyArray.Length; ++i)
{
var rigidbody = rigidbodyArray[i];
kinematicStateWhenAuthArray[i] = new KinematicStateWhenAuth
{
KinematicWhenAuthoritative = rigidbody.isKinematic
};
rigidbody.isKinematic = true;
}
}
private void UpdateAuthChangeGroup()
{
var rigidbodyArray = authChangeGroup.GetComponentArray<Rigidbody>();
var kinematicStateWhenAuthArray = authChangeGroup.GetComponentDataArray<KinematicStateWhenAuth>();
var spatialEntityIdArray = authChangeGroup.GetComponentDataArray<SpatialEntityId>();
for (int i = 0; i < rigidbodyArray.Length; ++i)
{
var rigidbody = rigidbodyArray[i];
var changes = updateSystem.GetAuthorityChangesReceived(spatialEntityIdArray[i].EntityId,
TransformInternal.ComponentId);
if (changes.Count == 0)
{
continue;
}
var auth = changes[changes.Count - 1];
switch (auth.Authority)
{
case Authority.NotAuthoritative:
kinematicStateWhenAuthArray[i] = new KinematicStateWhenAuth
{
KinematicWhenAuthoritative = rigidbody.isKinematic
};
rigidbody.isKinematic = true;
break;
case Authority.Authoritative:
case Authority.AuthorityLossImminent:
rigidbody.isKinematic = kinematicStateWhenAuthArray[i].KinematicWhenAuthoritative;
break;
}
}
}
}
}