-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathGamePosition.cs
More file actions
222 lines (181 loc) · 7.56 KB
/
GamePosition.cs
File metadata and controls
222 lines (181 loc) · 7.56 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
221
222
// Copyright (c) Meta Platforms, Inc. and affiliates.
// Use of the material below is subject to the terms of the MIT License
// https://github.com/oculus-samples/Unity-Decommissioned/tree/main/Assets/Decommissioned/LICENSE
using System;
using System.Collections;
using Meta.Decommissioned.Game;
using Meta.Decommissioned.Game.MiniGames;
using Meta.Decommissioned.Interactables;
using Meta.Decommissioned.Occlusion;
using Meta.Decommissioned.Player;
using Meta.Decommissioned.ScriptableObjects;
using Meta.Multiplayer.Core;
using Meta.Multiplayer.Networking;
using Meta.Multiplayer.PlayerManagement;
using Meta.XR.Samples;
using NaughtyAttributes;
using Unity.Netcode;
using UnityEngine;
namespace Meta.Decommissioned.Lobby
{
/// <summary>
/// Class representing a specific position in the game that may or may not be currently occupied by a player during the
/// game. Players are moved between these positions by a <see cref="LocationManager"/>.
/// </summary>
[MetaCodeSample("Decommissioned")]
public class GamePosition : NetworkBehaviour
{
[field: Header("Game Location Options")]
[field: SerializeField]
public MiniGameRoom MiniGameRoom { get; private set; }
[field: SerializeField] public int PositionIndex { get; private set; }
[SerializeField] private PlayerAssignmentInterface m_playerAssignment;
[SerializeField] private MiniGameAssignmentButton[] m_assignmentButtons = new MiniGameAssignmentButton[5];
[SerializeField] private Transform m_playerAssignmentPosition;
public PlayerAssignmentDoor PlayerAssignmentDoor;
[field: SerializeField] public PlayerColorConfig.GameColor PositionColor { get; private set; }
[ShowNativeProperty] public bool IsOccupied => OccupyingPlayer != null;
[ShowNativeProperty]
public NetworkObject OccupyingPlayer =>
NetworkManager.Singleton != null &&
NetworkManager.Singleton.IsListening &&
m_occupyingPlayer.Value.TryGet(out var player)
? player
: null;
public PlayerId? OccupyingPlayerId => OccupyingPlayer.GetOwnerPlayerId();
private readonly NetworkVariable<NetworkObjectReference> m_occupyingPlayer = new();
[field: SerializeField] public bool IsInitialSpawnPoint { get; private set; }
public event Action<NetworkObject, NetworkObject> OnOccupyingPlayerChanged;
private static LocationManager LocationManager => LocationManager.Instance;
public void Awake()
{
_ = StartCoroutine(StartupRoutine());
IEnumerator StartupRoutine()
{
yield return new WaitUntil(() => NetworkManager.Singleton != null);
NetworkManager.Singleton.OnClientDisconnectCallback += OnClientDisconnectServerRpc;
CommanderCandidateManager.Instance.OnNewCommander += OnNewCommander;
}
}
public override void OnDestroy()
{
if (NetworkManager.Singleton != null)
{
NetworkManager.Singleton.OnClientDisconnectCallback -= OnClientDisconnectServerRpc;
}
base.OnDestroy();
}
public override void OnNetworkSpawn()
{
base.OnNetworkSpawn();
m_occupyingPlayer.OnValueChanged += OnOccupyingPlayerHasChanged;
}
/// <summary>
/// When a new commander is set, we ensure that all related components are in the proper state depending on whether
/// or not the Commander is currently occupying this position.
/// </summary>
private void OnNewCommander(PlayerId player)
{
if (!IsOccupied)
{
return;
}
var positionOwner = OccupyingPlayer.GetOwnerPlayerId();
if (m_playerAssignment == null || positionOwner != player)
{
return;
}
m_playerAssignment.transform.SetPositionAndRotation(m_playerAssignmentPosition.position,
m_playerAssignmentPosition.rotation);
if (m_playerAssignment.LocationButtons.Count > 0)
{
m_playerAssignment.DisableStationButtons();
}
m_playerAssignment.LocationButtons.Clear();
m_playerAssignment.LocationButtons.AddRange(m_assignmentButtons);
m_playerAssignment.AssignmentDoor = PlayerAssignmentDoor;
m_playerAssignment.SetInterfaceActive(positionOwner == player, player);
}
public void OnOccupyingPlayerHasChanged(NetworkObjectReference previousPlayer, NetworkObjectReference newPlayer)
{
_ = previousPlayer.TryGet(out var oldPlayer);
_ = newPlayer.TryGet(out var player);
if (!IsOccupied)
{
OnOccupyingPlayerChanged?.Invoke(oldPlayer, default);
return;
}
// If this player is the local one, we'll want to set their camera and occlusion based on their new position
if (player.IsLocalPlayer)
{
if (player.TryGetComponent(out ClientNetworkTransform clientNetworkTransform))
{
clientNetworkTransform.Teleport(transform.position, Quaternion.LookRotation(transform.forward, Vector3.up),
Vector3.one);
}
PlayerCamera.Instance.Refocus();
RoomOcclusionZoneManager.Instance.ApplyOcclusion(MiniGameRoom);
}
OnOccupyingPlayerChanged?.Invoke(oldPlayer, player);
LocationManager.InvokeOnPlayerJoinedRoom(player, MiniGameRoom);
}
[ServerRpc(RequireOwnership = false)]
private void OnClientDisconnectServerRpc(ulong clientId)
{
if (!IsOccupied)
{
return;
}
if (OccupyingPlayer.OwnerClientId == clientId)
{
ClearPosition();
}
}
public void OccupyPosition(NetworkObject playerObject)
{
if (!NetworkManager.Singleton.IsHost)
{
SubmitMarkOccupiedServerRpc(playerObject);
}
else
{
m_occupyingPlayer.Value = playerObject;
}
if (PositionColor == PlayerColorConfig.GameColor.None)
{
return;
}
var playerId = playerObject.GetOwnerPlayerId();
if (playerId == null)
{
Debug.LogError($"Could not retrieve PlayerId from a player at position {PositionIndex}", this);
return;
}
PlayerColor.GetByPlayerId(playerId.Value).SetPlayerColor(PositionColor);
}
public void ClearPosition()
{
if (!NetworkManager.Singleton.IsHost)
{
SubmitMarkUnoccupiedServerRpc();
return;
}
m_occupyingPlayer.Value = default;
}
[ServerRpc(RequireOwnership = false)]
private void SubmitMarkOccupiedServerRpc(NetworkObjectReference playerObject)
{
m_occupyingPlayer.Value = playerObject;
}
[ServerRpc(RequireOwnership = false)]
private void SubmitMarkUnoccupiedServerRpc()
{
m_occupyingPlayer.Value = default;
}
[ContextMenu("Occupy with local player")]
private void Debug__OccupyWithLocalPlayer()
{
_ = LocationManager.TeleportPlayer(PlayerManager.LocalPlayerId.Object, MiniGameRoom);
}
}
}