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