-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathGrabPassOwnership.cs
142 lines (125 loc) · 4.67 KB
/
GrabPassOwnership.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
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
// Copyright (c) Meta Platforms, Inc. and affiliates.
using Fusion;
using Oculus.Interaction;
using Oculus.Interaction.HandGrab;
using UnityEngine;
namespace CrypticCabinet.Utils
{
/// <summary>
/// Establishes the state authority ownership transfer to grant a grabbing action.
/// This is to guarantee that whenever a player tries to grab an object, that player will be the only
/// one able to grab that object assuming the state authority was acquired.
/// When another player tries to grab an object, the state authority change is requested before allowing
/// the grab of the object for that other player.
/// </summary>
[RequireComponent(typeof(Grabbable))]
public class GrabPassOwnership : NetworkBehaviour, IStateAuthorityChanged
{
private Grabbable m_grabbable;
private NetworkObject m_networkObject;
[SerializeField] private Renderer m_debugRenderer;
[SerializeField] private bool m_changeColorForAuthority;
[SerializeField] private Collider m_grabbableCollider;
[SerializeField] private Collider[] m_additionalGrabbableColliders;
[SerializeField] private bool m_shouldDisableOnGrab = true;
[SerializeField] private bool m_shouldEnableOnRelease = true;
[SerializeField] private Rigidbody m_rigidbody;
private bool m_originalKinematicState;
private void Start()
{
if (m_rigidbody == null)
{
m_rigidbody = GetComponent<Rigidbody>();
}
m_grabbable = GetComponent<Grabbable>();
m_networkObject = GetComponent<NetworkObject>();
if (m_networkObject == null)
{
m_networkObject = GetComponentInParent<NetworkObject>();
}
if (m_grabbableCollider == null)
{
m_grabbableCollider = GetComponentInChildren<Collider>();
}
m_grabbable.WhenPointerEventRaised += GrabbableOnWhenPointerEventRaised;
}
private void OnDestroy()
{
if (m_grabbable != null)
{
m_grabbable.WhenPointerEventRaised -= GrabbableOnWhenPointerEventRaised;
}
}
public override void Spawned()
{
base.Spawned();
if (m_changeColorForAuthority && m_debugRenderer != null && m_debugRenderer.material != null)
{
m_debugRenderer.material.color = HasStateAuthority ? Color.green : Color.red;
}
}
public void StateAuthorityChanged()
{
if (m_changeColorForAuthority && m_debugRenderer != null && m_debugRenderer.material != null)
{
m_debugRenderer.material.color = HasStateAuthority ? Color.green : Color.red;
}
}
private void GrabbableOnWhenPointerEventRaised(PointerEvent obj)
{
if (obj.Type == PointerEventType.Select)
{
if (obj.Data is GrabInteractor or HandGrabInteractor or TouchHandGrabInteractor)
{
m_networkObject.RequestStateAuthority();
if (m_shouldDisableOnGrab)
{
RpcBeingHeld(true);
}
}
}
else if (obj.Type == PointerEventType.Unselect)
{
if (obj.Data is GrabInteractor or HandGrabInteractor or TouchHandGrabInteractor)
{
if (m_shouldEnableOnRelease)
{
RpcBeingHeld(false);
}
}
}
}
[Rpc(sources: RpcSources.All, targets: RpcTargets.All)]
private void RpcBeingHeld(bool held, RpcInfo info = default)
{
if (info.Source == PlayerRef.None || info.Source.PlayerId == Runner.LocalPlayer.PlayerId)
{
return;
}
m_grabbableCollider.enabled = !held;
if (m_additionalGrabbableColliders != null)
{
foreach (var c in m_additionalGrabbableColliders)
{
if (c != null)
{
c.enabled = !held;
}
}
}
if (m_rigidbody == null)
{
return;
}
if (held)
{
m_originalKinematicState = m_rigidbody.isKinematic;
m_rigidbody.isKinematic = true;
}
else
{
m_rigidbody.isKinematic = m_originalKinematicState;
}
}
}
}