-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathClientSimPositionSyncedHelperBase.cs
More file actions
151 lines (121 loc) · 4.44 KB
/
ClientSimPositionSyncedHelperBase.cs
File metadata and controls
151 lines (121 loc) · 4.44 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using UnityEngine;
namespace VRC.SDK3.ClientSim
{
[AddComponentMenu("")]
public abstract class ClientSimPositionSyncedHelperBase :
ClientSimBehaviour,
IClientSimSyncable,
IClientSimPositionSyncable,
IClientSimRespawnHandler
{
private int _ownerID = 1;
private Vector3 _originalPosition;
private Quaternion _originalRotation;
private Rigidbody _rigidbody;
private bool _useGravity;
private bool _isKinematic;
private IClientSimSyncedObjectManager _syncedObjectManager;
public bool SyncPosition { get; protected set; }
protected override void Awake()
{
base.Awake();
_originalPosition = transform.position;
_originalRotation = transform.rotation;
_rigidbody = GetComponent<Rigidbody>();
if (_rigidbody != null)
{
_isKinematic = _rigidbody.isKinematic;
_useGravity = _rigidbody.useGravity;
}
}
public virtual void Initialize(IClientSimSyncedObjectManager syncedObjectManager)
{
_syncedObjectManager = syncedObjectManager;
_syncedObjectManager.AddSyncedObject(this);
}
protected virtual void OnDestroy()
{
// Nullable needed for uninitialized case.
_syncedObjectManager?.RemoveSyncedObject(this);
}
public void TeleportTo(Vector3 position, Quaternion rotation)
{
this.Log($"Teleporting Object {Tools.GetGameObjectPath(gameObject)} to {position} and rotation {rotation.eulerAngles}");
FlagDiscontinuity();
transform.SetPositionAndRotation(position, rotation);
}
public void FlagDiscontinuity()
{
// TODO As of right now, ClientSim doesn't handle any actual sync.
}
#region IClientSimSyncable
public int GetOwner()
{
return _ownerID;
}
public void SetOwner(int ownerID)
{
_ownerID = ownerID;
}
#endregion
#region IClientSimRespawnable
public void Respawn()
{
this.Log($"Respawning Object {Tools.GetGameObjectPath(gameObject)}");
TeleportTo(_originalPosition, _originalRotation);
if (_rigidbody != null && !_rigidbody.isKinematic)
{
_rigidbody.velocity = Vector3.zero;
}
}
#endregion
#region IClientSimPositionSyncable
public void SetIsKinematic(bool value)
{
_isKinematic = value;
if (_rigidbody)
{
_rigidbody.isKinematic = value;
}
}
public void SetUseGravity(bool value)
{
_useGravity = value;
if (_rigidbody)
{
_rigidbody.useGravity = value;
}
}
public bool GetIsKinematic()
{
return _rigidbody && _rigidbody.isKinematic;
}
public bool GetUseGravity()
{
return _rigidbody && _rigidbody.useGravity;
}
public void UpdatePositionSync()
{
if (_rigidbody != null)
{
// TODO if user is not the owner, set useGravity to false, isKinematic to true.
// This would better simulate ownership, but also make testing awkward.
if (_rigidbody.isKinematic != _isKinematic)
{
_rigidbody.isKinematic = _isKinematic;
this.LogWarning($"Rigidbody.isKinematic was set outside of VRCObjectSync.SetKinematic method! {Tools.GetGameObjectPath(gameObject)}");
}
if (_rigidbody.useGravity != _useGravity)
{
_rigidbody.useGravity = _useGravity;
this.LogWarning($"Rigidbody.useGravity was set outside of VRCObjectSync.SetGravity method! {Tools.GetGameObjectPath(gameObject)}");
}
// VRChatBug: Modifying a collider's "is Trigger" property is also restricted when VRCObjectSync is on
// the same object, but this check will not be added.
}
}
public Transform GetTransform() => transform;
#endregion
}
}