quantum-space-buddies/QSB/Player/QSBPlayerManager.cs

128 lines
4.0 KiB
C#
Raw Normal View History

2020-08-22 17:08:25 +00:00
using OWML.Common;
2021-11-01 15:49:00 +00:00
using QSB.ItemSync.WorldObjects.Items;
2021-04-11 16:05:02 +00:00
using QSB.Player.TransformSync;
using QSB.Tools.FlashlightTool;
2022-03-07 20:34:35 +00:00
using QSB.Tools.ProbeTool;
using QSB.Utility;
2021-02-09 17:18:01 +00:00
using System;
2020-08-21 13:04:13 +00:00
using System.Collections.Generic;
using System.Linq;
2021-02-28 14:43:05 +00:00
using UnityEngine;
2022-03-03 03:46:33 +00:00
namespace QSB.Player;
public static class QSBPlayerManager
{
2022-03-03 03:46:33 +00:00
public static PlayerInfo LocalPlayer
2020-12-02 21:29:53 +00:00
{
2022-03-03 03:46:33 +00:00
get
2021-02-15 10:58:21 +00:00
{
2022-03-03 03:46:33 +00:00
var localInstance = PlayerTransformSync.LocalInstance;
if (localInstance == null)
2021-02-15 10:58:21 +00:00
{
2022-03-03 03:46:33 +00:00
DebugLog.ToConsole("Error - Trying to get LocalPlayer when the local PlayerTransformSync instance is null." +
$"{Environment.NewLine} Stacktrace : {Environment.StackTrace} ", MessageType.Error);
return null;
2021-02-15 10:58:21 +00:00
}
2022-03-03 03:46:33 +00:00
return localInstance.Player;
2021-02-15 10:58:21 +00:00
}
2022-03-03 03:46:33 +00:00
}
public static uint LocalPlayerId => LocalPlayer?.PlayerId ?? uint.MaxValue;
2021-02-15 10:58:21 +00:00
2022-03-03 03:46:33 +00:00
/// <summary>
/// called right after player is added
/// </summary>
public static Action<PlayerInfo> OnAddPlayer;
/// <summary>
/// called right before player is removed
/// </summary>
public static Action<PlayerInfo> OnRemovePlayer;
2021-03-23 13:18:29 +00:00
2022-03-03 03:46:33 +00:00
public static readonly List<PlayerInfo> PlayerList = new();
2022-03-03 03:46:33 +00:00
public static PlayerInfo GetPlayer(uint id)
{
if (id is uint.MaxValue or 0)
2020-12-02 21:29:53 +00:00
{
2022-03-03 03:46:33 +00:00
return default;
}
2021-12-12 01:55:36 +00:00
2022-03-03 03:46:33 +00:00
var player = PlayerList.FirstOrDefault(x => x.PlayerId == id);
if (player == null)
{
DebugLog.ToConsole($"Error - Player with id {id} does not exist! Stacktrace : {Environment.StackTrace}", MessageType.Error);
return default;
}
2022-03-03 03:46:33 +00:00
return player;
}
public static bool PlayerExists(uint id) =>
id is not (uint.MaxValue or 0) && PlayerList.Any(x => x.PlayerId == id);
2020-12-14 19:23:24 +00:00
2022-03-03 03:46:33 +00:00
public static List<PlayerInfo> GetPlayersWithCameras(bool includeLocalCamera = true)
{
var cameraList = PlayerList.Where(x => x.Camera != null && x.PlayerId != LocalPlayerId).ToList();
if (includeLocalCamera
&& LocalPlayer != default
&& LocalPlayer.Camera != null)
{
2022-03-03 03:46:33 +00:00
cameraList.Add(LocalPlayer);
}
else if (includeLocalCamera && (LocalPlayer == default || LocalPlayer.Camera == null))
{
if (LocalPlayer == default)
{
2022-03-03 03:46:33 +00:00
DebugLog.ToConsole($"Error - LocalPlayer is null.", MessageType.Error);
return cameraList;
}
2021-06-18 21:38:32 +00:00
2022-03-03 03:46:33 +00:00
DebugLog.ToConsole($"Error - LocalPlayer.Camera is null.", MessageType.Error);
LocalPlayer.Camera = Locator.GetPlayerCamera();
2020-12-22 09:13:08 +00:00
}
2022-03-03 03:46:33 +00:00
return cameraList;
}
2021-02-28 14:43:05 +00:00
public static (Flashlight LocalFlashlight, IEnumerable<QSBFlashlight> RemoteFlashlights) GetPlayerFlashlights()
=> (Locator.GetFlashlight(), PlayerList.Where(x => x.FlashLight != null).Select(x => x.FlashLight));
2021-04-26 13:30:21 +00:00
public static (SurveyorProbe LocalProbe, IEnumerable<QSBProbe> RemoteProbes) GetPlayerProbes()
2022-03-07 21:25:41 +00:00
=> (Locator.GetProbe(), PlayerList.Where(x => x.Probe != null).Select(x => x.Probe));
public static IEnumerable<ThrusterLightTracker> GetThrusterLightTrackers()
=> PlayerList.Select(x => x.ThrusterLightTracker).Where(x => x != null);
2022-03-07 20:34:35 +00:00
2022-05-04 09:18:58 +00:00
public static void ShowAllPlayers(float time = 2f)
=> PlayerList.ForEach(x => x.SetVisible(true, time));
2021-04-26 13:30:21 +00:00
2022-05-04 09:18:58 +00:00
public static void HideAllPlayers(float time = 2f)
=> PlayerList.ForEach(x => x.SetVisible(false, time));
2021-10-24 09:47:25 +00:00
2022-03-03 03:46:33 +00:00
public static PlayerInfo GetClosestPlayerToWorldPoint(Vector3 worldPoint, bool includeLocalPlayer) => includeLocalPlayer
? GetClosestPlayerToWorldPoint(PlayerList, worldPoint)
: GetClosestPlayerToWorldPoint(PlayerList.Where(x => x != LocalPlayer).ToList(), worldPoint);
2022-03-03 03:46:33 +00:00
public static PlayerInfo GetClosestPlayerToWorldPoint(List<PlayerInfo> playerList, Vector3 worldPoint)
{
if (playerList == null)
{
DebugLog.ToConsole($"Error - Cannot get closest player from null player list.", MessageType.Error);
return null;
}
2021-06-18 21:38:32 +00:00
2022-03-03 03:46:33 +00:00
playerList = playerList.Where(x => x.IsReady && x.Body != null).ToList();
2022-03-03 03:46:33 +00:00
if (playerList.Count == 0)
{
DebugLog.ToConsole($"Error - Cannot get closest player from empty (ready) player list.", MessageType.Error);
return null;
2021-04-26 13:30:21 +00:00
}
2021-10-30 15:14:38 +00:00
2022-03-03 03:46:33 +00:00
return playerList.MinBy(x => Vector3.Distance(x.Body.transform.position, worldPoint));
2020-12-02 21:29:53 +00:00
}
2022-03-03 03:46:33 +00:00
public static IEnumerable<(PlayerInfo Player, IQSBItem HeldItem)> GetPlayerCarryItems()
=> PlayerList.Select(x => (x, x.HeldItem));
}