-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathChessBoardHandlerMotif.cs
More file actions
220 lines (192 loc) · 7.46 KB
/
ChessBoardHandlerMotif.cs
File metadata and controls
220 lines (192 loc) · 7.46 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright (c) Meta Platforms, Inc. and affiliates.
#if FUSION2
using Fusion;
using UnityEngine;
using Oculus.Interaction;
using System.Collections.Generic;
using Meta.XR.MultiplayerBlocks.Fusion;
using Meta.XR.Samples;
namespace MRMotifs.SharedActivities.ChessSample
{
/// <summary>
/// Handles synchronization of chess piece positions and rotations across networked clients
/// and manages interaction events for selecting and moving chess pieces.
/// </summary>
[MetaCodeSample("MRMotifs-SharedActivities")]
public class ChessBoardHandlerMotif : NetworkBehaviour, IStateAuthorityChanged
{
/// <summary>
/// Networked array for storing chess piece positions.
/// </summary>
[Networked, Capacity(32)]
private NetworkArray<Vector3> ChessPiecePositions => default;
/// <summary>
/// Networked array for storing chess piece rotations.
/// </summary>
[Networked, Capacity(32)]
private NetworkArray<Quaternion> ChessPieceRotations => default;
private List<InteractableUnityEventWrapper> m_chessPieceInteractables = new();
private Grabbable m_grabbable;
private GameObject m_grabbedChessPiece;
private Vector3 m_pendingPosition;
private bool m_hasSpawned;
private bool m_isPieceBeingMoved;
public override void Spawned()
{
base.Spawned();
m_hasSpawned = true;
InitializeChessPieces();
FusionBBEvents.OnSceneLoadDone += SceneLoaded;
}
public override void Despawned(NetworkRunner runner, bool hasState)
{
base.Despawned(runner, hasState);
FusionBBEvents.OnSceneLoadDone -= SceneLoaded;
UnsubscribeChessPieceEvents();
}
/// <summary>
/// This is a callback of the <see cref="FusionBBEvents"/> OnSceneLoadDone event.
/// This method is called just once per experience, which means it is only called for the
/// first client that joins. It is used so the first client is populating the networked properties
/// with the chess piece positions and rotations for every client that joins later.
/// </summary>
private void SceneLoaded(NetworkRunner obj)
{
UpdateInitialChessPieceStates();
}
private void InitializeChessPieces()
{
var chessPieces = new List<InteractableUnityEventWrapper>();
var allChildren = GetComponentsInChildren<Transform>();
foreach (var child in allChildren)
{
if (child == transform)
{
continue;
}
var interactable = child.GetComponent<InteractableUnityEventWrapper>();
if (interactable != null)
{
chessPieces.Add(interactable);
}
}
m_chessPieceInteractables = chessPieces;
foreach (var interactable in m_chessPieceInteractables)
{
interactable.WhenSelect.AddListener(() => TogglePieceMoved(true));
interactable.WhenUnselect.AddListener(() => TogglePieceMoved(false));
}
}
private void UpdateInitialChessPieceStates()
{
SetChessPieceRigidbodyState(false);
for (var i = 0; i < ChessPiecePositions.Length; i++)
{
if (i < m_chessPieceInteractables.Count)
{
ChessPiecePositions.Set(i, m_chessPieceInteractables[i].transform.localPosition);
ChessPieceRotations.Set(i, m_chessPieceInteractables[i].transform.localRotation);
}
else
{
ChessPiecePositions.Set(i, Vector3.zero);
ChessPieceRotations.Set(i, Quaternion.identity);
}
}
}
private void TogglePieceMoved(bool isBeingMoved)
{
if (!Object.HasStateAuthority)
{
SetChessPieceRigidbodyState(false);
Object.RequestStateAuthority();
return;
}
m_isPieceBeingMoved = isBeingMoved;
if (!m_isPieceBeingMoved)
{
SendChessPieceOffset();
}
}
private void FixedUpdate()
{
if (!m_hasSpawned)
{
return;
}
if (Object.HasStateAuthority)
{
SendChessPieceOffset();
}
UpdateRemoteChessPieces();
}
private void SendChessPieceOffset()
{
for (var i = 0; i < m_chessPieceInteractables.Count; i++)
{
var chessPiece = m_chessPieceInteractables[i];
var localPosition = chessPiece.transform.localPosition;
var localRotation = chessPiece.transform.localRotation;
ChessPiecePositions.Set(i, localPosition);
ChessPieceRotations.Set(i, localRotation);
}
}
private void UpdateRemoteChessPieces()
{
for (var i = 0; i < m_chessPieceInteractables.Count; i++)
{
if (i >= ChessPiecePositions.Length)
{
continue;
}
var targetLocalPosition = ChessPiecePositions.Get(i);
var targetLocalRotation = ChessPieceRotations.Get(i);
var chessPieceTransform = m_chessPieceInteractables[i].transform;
if (!HasStateAuthority)
{
chessPieceTransform.localPosition = Vector3.Lerp(
chessPieceTransform.localPosition, targetLocalPosition,
Time.deltaTime * 10f);
chessPieceTransform.localRotation = Quaternion.Slerp(
chessPieceTransform.localRotation,
targetLocalRotation, Time.deltaTime * 10f);
}
else
{
chessPieceTransform.localPosition = targetLocalPosition;
chessPieceTransform.localRotation = targetLocalRotation;
}
}
}
private void UnsubscribeChessPieceEvents()
{
foreach (var interactable in m_chessPieceInteractables)
{
interactable.WhenSelect.RemoveListener(() => TogglePieceMoved(true));
interactable.WhenUnselect.RemoveListener(() => TogglePieceMoved(false));
}
}
/// <summary>
/// This method comes from the <see cref="IStateAuthorityChanged"/> interface and is called
/// when the State Authority has been changed. We use to execute commands immediately
/// after StateAuthority has been assigned to the client who tried to move a chess piece.
/// </summary>
public void StateAuthorityChanged()
{
if (!Object.HasStateAuthority)
{
SetChessPieceRigidbodyState(true);
}
}
private void SetChessPieceRigidbodyState(bool isKinematic)
{
foreach (var chessPiece in m_chessPieceInteractables)
{
var chessPieceRb = chessPiece.GetComponent<Rigidbody>();
chessPieceRb.isKinematic = isKinematic;
chessPieceRb.useGravity = !isKinematic;
}
}
}
}
#endif