-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathLocationManager.cs
More file actions
197 lines (169 loc) · 7.77 KB
/
LocationManager.cs
File metadata and controls
197 lines (169 loc) · 7.77 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
// 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.Generic;
using System.Linq;
using Meta.Decommissioned.Game.MiniGames;
using Meta.Decommissioned.Lobby;
using Meta.Decommissioned.Player;
using Meta.Multiplayer.Networking;
using Meta.Multiplayer.PlayerManagement;
using Meta.Utilities;
using Meta.XR.Samples;
using Unity.Netcode;
using UnityEngine;
namespace Meta.Decommissioned.Game
{
/// <summary>
/// This singleton manages the movement of players from place to place during the game; from here, we can access what
/// players are in which rooms and move them between specific <see cref="GamePosition"/>s.
/// </summary>
[MetaCodeSample("Decommissioned")]
public class LocationManager : NetworkSingleton<LocationManager>
{
private GamePosition[] m_gamePositions = Array.Empty<GamePosition>();
public event Action<NetworkObject, MiniGameRoom> OnPlayerJoinedRoom;
public bool EnableLogging;
private new void Awake()
{
base.Awake();
m_gamePositions = FindObjectsByType<GamePosition>(FindObjectsInactive.Include, FindObjectsSortMode.None);
}
public void InvokeOnPlayerJoinedRoom(NetworkObject playerObject, MiniGameRoom room) =>
OnPlayerJoinedRoom?.Invoke(playerObject, room);
public void UpdateLocalPosition()
{
foreach (var pos in m_gamePositions)
{
// if the occupying player is rejoining, they'll need to teleport immediately
var occupyingPlayer = pos.OccupyingPlayer;
if (occupyingPlayer != null && occupyingPlayer.NetworkObjectId != default)
{
_ = StartCoroutine(
new WaitUntil(() =>
pos.NetworkManager.SpawnManager.GetLocalPlayerObject() != null &&
pos.OccupyingPlayer != null)
.Then(() => pos.OnOccupyingPlayerHasChanged(default, pos.OccupyingPlayer)));
}
}
}
#region GamePosition Retrieval
public GamePosition[] GetAllGamePositions() => m_gamePositions;
private GamePosition GetNextUnOccupiedGamePosition(MiniGameRoom room) => m_gamePositions
.Where(x => x.MiniGameRoom == room).OrderBy(x => x.PositionIndex).FirstOrDefault(x => !x.IsOccupied);
public GamePosition GetGamePositionByIndex(MiniGameRoom room, int index) => m_gamePositions
.FirstOrDefault(x => x.MiniGameRoom == room && x.PositionIndex == index);
public GamePosition[] GetGamePositionByType(MiniGameRoom room) =>
m_gamePositions.Where(x => x.MiniGameRoom == room).ToArray();
public GamePosition GetGamePositionByPlayer(NetworkObject player) => player != null
? m_gamePositions
.FirstOrDefault(x => x.IsOccupied && x.OccupyingPlayer.NetworkObjectId == player.NetworkObjectId)
: null;
public GamePosition GetGamePositionByPlayerId(PlayerId playerId) => m_gamePositions.FirstOrDefault(x =>
x.IsOccupied && x.OccupyingPlayerId is { } id && id == playerId);
public IEnumerable<NetworkObject> GetPlayersInRoom(MiniGameRoom room) =>
m_gamePositions.Where(x => x.MiniGameRoom == room && x.OccupyingPlayer != null)
.Select(location => location.OccupyingPlayer);
public GamePosition[] GetAssignedMiniGamePositions() =>
m_gamePositions.Where(x => x.MiniGameRoom != MiniGameRoom.None && x.IsOccupied).ToArray();
public IEnumerable<NetworkObject> GetPlayersNotInRoom(MiniGameRoom room) =>
m_gamePositions.Where(x => x.MiniGameRoom != room && x.OccupyingPlayer != null)
.Select(location => location.OccupyingPlayer);
public string GetFriendlyMiniGameRoomName(MiniGameRoom room) => room.ToString();
#endregion
#region Teleportation methods
#if UNITY_EDITOR
[ServerRpc(RequireOwnership = false)]
public void TeleportPlayer_ServerRpc(NetworkObjectReference playerObject, MiniGameRoom room)
{
_ = TeleportPlayer(playerObject, room);
}
#endif
public void TeleportToMainRoom(NetworkObject playerObject)
{
var playerSpawns = PlayerSpawns.GetByPlayerObject(playerObject);
if (playerSpawns != null)
{
if (playerSpawns.MainSpawn == null)
{
var nextOpenPosition = GetNextUnOccupiedGamePosition(MiniGameRoom.None);
playerSpawns.SetMainSpawn(nextOpenPosition);
}
if (EnableLogging)
{
Debug.Log($"Teleporting {playerObject.name} to spawn {playerSpawns.MainSpawn.name}");
}
_ = TeleportPlayer(playerObject, playerSpawns.MainSpawn);
}
else
{
if (EnableLogging)
{
Debug.Log($"Teleporting {playerObject.name} to random spawn");
}
_ = TeleportPlayer(playerObject, MiniGameRoom.None);
}
}
public GamePosition TeleportPlayer(NetworkObject playerObject, MiniGameRoom room)
{
foreach (var location in m_gamePositions.Where(x => x.OccupyingPlayer == playerObject))
{
location.ClearPosition();
}
var target = m_gamePositions.FirstOrDefault(x => x.MiniGameRoom == room && x.IsOccupied is false);
if (target != null)
{
if (EnableLogging)
{
Debug.Log($"Teleporting {playerObject} to {room} location {target}", playerObject);
}
target.OccupyPosition(playerObject);
}
else
{
Debug.LogError($"Could not find {room} to teleport {playerObject}.", playerObject);
}
return target;
}
public GamePosition TeleportPlayer(NetworkObject playerObject, MiniGameRoom room, int spawnIndex)
{
foreach (var location in m_gamePositions.Where(x => x.OccupyingPlayer == playerObject))
{
location.ClearPosition();
}
var target = m_gamePositions.FirstOrDefault(x =>
x.MiniGameRoom == room && x.IsOccupied is false && x.PositionIndex == spawnIndex);
if (target != null)
{
if (EnableLogging)
{
Debug.Log($"Teleporting {playerObject} to {room} location {target}", playerObject);
}
target.OccupyPosition(playerObject);
}
else
{
Debug.LogError($"Could not find {room} with spawn number {spawnIndex} to teleport {playerObject} " +
"to. Trying with generic spawning.", playerObject);
return TeleportPlayer(playerObject, room);
}
return target;
}
public GamePosition TeleportPlayer(NetworkObject playerObject, GamePosition position)
{
foreach (var location in m_gamePositions.Where(x => x.OccupyingPlayer == playerObject))
{
location.ClearPosition();
}
var target = position;
if (EnableLogging)
{
Debug.Log($"Teleporting {playerObject} to {target.MiniGameRoom} location {target}", playerObject);
}
target.OccupyPosition(playerObject);
return target;
}
#endregion
}
}