17 using System.Collections.Generic;
21 #if UNITY_2019_1_OR_NEWER && INPUTSYSTEM_AVAILABLE
22 using UnityEngine.InputSystem;
32 #region Private Fields
34 private Dictionary<PlayerIndex, PlayerCore>
players =
new Dictionary<PlayerIndex, PlayerCore>();
36 internal static bool scanningForPlayers =
false;
41 #region Public Functions
50 return playerIndex !=
PlayerIndex.None &&
Instance.players.TryGetValue(playerIndex, out var playerCore);
62 if (playerIndex ==
PlayerIndex.None || !
Instance.players.TryGetValue(playerIndex, out var playerCore))
68 friendlyName = playerCore.FriendlyName;
85 playerSettings =
null;
92 playerSettings = resultingPlayerSettings;
99 playerSettings = tiltFiveManager.playerSettings;
103 playerSettings =
null;
110 #region Internal Functions
118 if (scanningForPlayers)
123 if (playerSettings !=
null &&
Instance.players.TryGetValue(playerSettings.
PlayerIndex, out var playerCore))
128 if (!Glasses.IsConnected(playerCore.GlassesHandle))
131 Log.
Info($
"Player {playerIndex} was removed due to their glasses (handle: {playerCore.GlassesHandle}) being disconnected.");
132 Instance.players.Remove(playerIndex);
136 playerCore.Update(playerSettings, spectatorSettings);
144 internal static void Reset(PlayerSettings playerSettings, SpectatorSettings spectatorSettings)
146 var playerIndex = playerSettings.PlayerIndex;
148 if (playerSettings !=
null &&
Instance.players.TryGetValue(playerIndex, out var playerCore))
150 if (Glasses.IsConnected(playerCore.GlassesHandle))
152 playerCore.Reset(playerSettings, spectatorSettings);
156 Validate(playerSettings);
165 internal static void Validate(PlayerSettings playerSettings)
167 if (playerSettings ==
null)
172 var scaleSettings = playerSettings.scaleSettings;
173 scaleSettings.contentScaleRatio = Mathf.Clamp(scaleSettings.contentScaleRatio, ScaleSettings.MIN_CONTENT_SCALE_RATIO,
float.MaxValue);
175 playerSettings.Validate();
182 internal static void ScanForNewPlayers()
184 if (scanningForPlayers)
188 scanningForPlayers =
true;
192 scanningForPlayers =
false;
195 internal static bool TryGetGlassesHandle(
PlayerIndex playerIndex, out GlassesHandle glassesHandle)
199 glassesHandle =
new GlassesHandle();
202 var playerAvailable =
Instance.players.TryGetValue(playerIndex, out var playerCore);
203 glassesHandle = playerCore?.GlassesHandle ??
new GlassesHandle();
204 return playerAvailable;
207 internal static bool TryGetPlayerIndex(GlassesHandle glassesHandle, out
PlayerIndex playerIndex)
209 foreach (var keyValuePair
in Instance.players)
211 var currentPlayerIndex = keyValuePair.Key;
212 var currentPlayerCore = keyValuePair.Value;
213 if (currentPlayerCore.GlassesHandle == glassesHandle)
215 playerIndex = currentPlayerIndex;
223 internal static bool AllSupportedPlayersConnected()
225 if(!TiltFiveSingletonHelper.TryGetISceneInfo(out var sceneInfo))
229 var supportedPlayers = sceneInfo.GetSupportedPlayerCount();
247 internal static bool TryAddPlayer(GlassesHandle glassesHandle, out
PlayerIndex playerIndex)
250 if (
players.Count >= GlassesSettings.MAX_SUPPORTED_GLASSES_COUNT)
256 if(TryGetPlayerIndex(glassesHandle, out var existingPlayerIndex))
272 if (!
players.ContainsKey(currentPlayerIndex))
274 Glasses.TryGetFriendlyName(glassesHandle, out var friendlyName);
275 players[currentPlayerIndex] =
new PlayerCore(glassesHandle, friendlyName);
277 Log.
Info($
"Player {currentPlayerIndex} created. Glasses: {glassesHandle} (\"{friendlyName}\")");
284 bool lowestPlayerIndex =
false;
289 if (lowestPlayerIndex)
291 Glasses.SetDefaultGlassesHandle(glassesHandle);
294 playerIndex = currentPlayerIndex;
304 internal static void OnDisable()
310 internal static bool TryGetFirstConnectedPlayer(out
PlayerIndex playerIndex)
331 internal static void DrawGizmos(PlayerSettings playerSettings)
333 if (playerSettings ==
null)
338 var gameBoardSettings = playerSettings.gameboardSettings;
340 if (gameBoardSettings.currentGameBoard !=
null)
342 gameBoardSettings.currentGameBoard.DrawGizmo(playerSettings.scaleSettings, gameBoardSettings);
350 #region Private Classes
372 internal void Reset(PlayerSettings playerSettings, SpectatorSettings spectatorSettings)
374 Glasses.Reset(playerSettings.glassesSettings, spectatorSettings,
GlassesHandle);
398 #region Public Classes
The Glasses API and runtime.
static void Reset(GlassesSettings glassesSettings, SpectatorSettings spectatorSettings=null, PlayerIndex playerIndex=PlayerIndex.None)
Reset this T:TiltFive.Glasses.
static void Update(GlassesSettings glassesSettings, ScaleSettings scaleSettings, GameBoardSettings gameBoardSettings)
Updates this T:TiltFive.Glasses.
static bool Validate(GlassesSettings glassesSettings, SpectatorSettings spectatorSettings=null, PlayerIndex playerIndex=PlayerIndex.One)
Validates the specified glassesSettings with the specified glasses core.
static void Info(string m, params object[] list)
INFO logging function call.
readonly string FriendlyName
PlayerCore(GlassesHandle glassesHandle, string friendlyName)
readonly GlassesHandle GlassesHandle
void GetLatestPoseData(PlayerSettings playerSettings, SpectatorSettings spectatorSettings)
Obtains the latest pose for all trackable objects.
Suspends coroutine execution until the provided player has connected.
WaitUntilPlayerConnected(PlayerIndex playerIndex)
override bool keepWaiting
Provides access to player settings and functionality.
Dictionary< PlayerIndex, PlayerCore > players
static bool TryGetSettings(PlayerIndex playerIndex, out PlayerSettings playerSettings)
Obtains the PlayerSettings corresponding to the specified player.
static bool IsConnected(PlayerIndex playerIndex)
Determines whether the specified player has an associated pair of glasses connected.
static bool TryGetFriendlyName(PlayerIndex playerIndex, out string friendlyName)
Attempts to get the friendly name assigned to the specified player's glasses.
ScaleSettings scaleSettings
GameBoardSettings gameboardSettings
WandSettings leftWandSettings
GlassesSettings glassesSettings
WandSettings rightWandSettings
bool TryGetPlayerSettings(PlayerIndex playerIndex, out PlayerSettings playerSettings)
Gets the player settings for the specified player.
static bool TryGetISceneInfo(out ISceneInfo sceneInfo)
The Wand API and runtime.
static void Update(WandSettings wandSettings, ScaleSettings scaleSettings, GameBoardSettings gameBoardSettings, PlayerIndex playerIndex=PlayerIndex.One)
PlayerIndex
The Player index (e.g. Player One, Player Two, etc)