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

163 lines
5.2 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;
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;
2020-11-03 21:33:48 +00:00
namespace QSB.Player
{
2020-12-02 21:29:53 +00:00
public static class QSBPlayerManager
{
2021-02-15 10:58:21 +00:00
public static uint LocalPlayerId
{
get
{
var localInstance = PlayerTransformSync.LocalInstance;
if (localInstance == null)
{
2021-05-19 10:34:59 +00:00
DebugLog.ToConsole($"Error - Trying to get LocalPlayerId when the local PlayerTransformSync instance is null." +
2021-07-11 15:18:47 +00:00
$"{Environment.NewLine} Stacktrace : {Environment.StackTrace} ", MessageType.Error);
2021-02-15 10:58:21 +00:00
return uint.MaxValue;
}
2021-06-18 21:38:32 +00:00
2021-02-15 10:58:21 +00:00
if (localInstance.NetIdentity == null)
{
DebugLog.ToConsole($"Error - Trying to get LocalPlayerId when the local PlayerTransformSync instance's QNetworkIdentity is null.", MessageType.Error);
2021-02-15 10:58:21 +00:00
return uint.MaxValue;
}
2021-06-18 21:38:32 +00:00
2021-02-15 10:58:21 +00:00
return localInstance.NetIdentity.NetId.Value;
}
}
2021-03-23 13:18:29 +00:00
public static Action<uint> OnRemovePlayer;
2021-08-28 20:53:35 +00:00
public static Action<uint> OnAddPlayer;
2021-03-23 13:18:29 +00:00
2020-12-02 21:29:53 +00:00
public static PlayerInfo LocalPlayer => GetPlayer(LocalPlayerId);
public static List<PlayerInfo> PlayerList { get; } = new List<PlayerInfo>();
2021-11-20 19:49:50 +00:00
private static readonly List<PlayerSyncObject> PlayerSyncObjects = new();
2020-12-02 21:29:53 +00:00
public static PlayerInfo GetPlayer(uint id)
{
2021-11-20 19:49:50 +00:00
if (id is uint.MaxValue or 0U)
2020-12-02 21:29:53 +00:00
{
return default;
}
2021-06-18 21:38:32 +00:00
2020-12-02 21:29:53 +00:00
var player = PlayerList.FirstOrDefault(x => x.PlayerId == id);
2021-12-12 01:55:36 +00:00
if (player == null)
2020-12-02 21:29:53 +00:00
{
2021-12-12 01:55:36 +00: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 21:38:32 +00:00
2021-12-12 01:55:36 +00:00
return player;
}
public static void AddPlayer(uint id)
{
2021-11-13 19:01:42 +00:00
DebugLog.DebugWrite($"Create Player : id<{id}>", MessageType.Info);
2021-12-12 01:55:36 +00:00
var player = new PlayerInfo(id);
2020-12-02 21:29:53 +00:00
PlayerList.Add(player);
2021-08-28 20:53:35 +00:00
OnAddPlayer?.Invoke(id);
2020-12-02 21:29:53 +00:00
}
2020-07-29 21:04:50 +00:00
2020-12-02 21:29:53 +00:00
public static void RemovePlayer(uint id)
{
2021-11-13 19:01:42 +00:00
DebugLog.DebugWrite($"Remove Player : id<{id}>", MessageType.Info);
2021-03-09 16:43:41 +00:00
PlayerList.RemoveAll(x => x.PlayerId == id);
2020-12-02 21:29:53 +00:00
}
2020-07-29 21:04:50 +00:00
2020-12-14 21:20:53 +00:00
public static bool PlayerExists(uint id) =>
id != uint.MaxValue && PlayerList.Any(x => x.PlayerId == id);
2020-08-18 20:29:31 +00:00
2020-12-14 21:20:53 +00:00
public static IEnumerable<T> GetSyncObjects<T>() where T : PlayerSyncObject =>
PlayerSyncObjects.OfType<T>().Where(x => x != null);
2020-12-14 21:20:53 +00:00
public static T GetSyncObject<T>(uint id) where T : PlayerSyncObject =>
GetSyncObjects<T>().FirstOrDefault(x => x != null && x.AttachedNetId == id);
2020-08-14 18:54:15 +00:00
2020-12-19 18:53:26 +00:00
public static void AddSyncObject(PlayerSyncObject obj) => PlayerSyncObjects.Add(obj);
2020-12-14 19:23:24 +00:00
2020-12-19 18:53:26 +00:00
public static void RemoveSyncObject(PlayerSyncObject obj) => PlayerSyncObjects.Remove(obj);
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
2021-03-25 20:56:26 +00: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
}
else if (includeLocalCamera && (LocalPlayer == default || LocalPlayer.Camera == null))
{
if (LocalPlayer == default)
{
DebugLog.ToConsole($"Error - LocalPlayer is null.", MessageType.Error);
return cameraList;
}
2021-10-24 09:47:25 +00:00
DebugLog.ToConsole($"Error - LocalPlayer.Camera is null.", MessageType.Error);
LocalPlayer.Camera = Locator.GetPlayerCamera();
}
2021-06-18 21:38:32 +00:00
2020-12-22 09:13:08 +00:00
return cameraList;
}
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()
=> PlayerList.Where(x => x != LocalPlayer).ToList().ForEach(x => ChangePlayerVisibility(x.PlayerId, true));
2021-02-28 15:29:09 +00:00
public static void HideAllPlayers()
2021-02-28 14:43:05 +00:00
=> PlayerList.Where(x => x != LocalPlayer).ToList().ForEach(x => ChangePlayerVisibility(x.PlayerId, false));
public static void ChangePlayerVisibility(uint playerId, bool visible)
{
var player = GetPlayer(playerId);
2021-11-13 19:01:42 +00:00
player.Visible = visible;
2021-03-01 09:44:36 +00:00
if (player.Body == null)
{
2021-05-07 18:17:09 +00:00
DebugLog.ToConsole($"Warning - Player {playerId} has a null player model!", MessageType.Warning);
2021-03-01 09:44:36 +00:00
return;
}
2021-06-18 21:38:32 +00:00
2021-03-01 09:44:36 +00:00
foreach (var renderer in player.Body.GetComponentsInChildren<Renderer>())
2021-02-28 14:43:05 +00:00
{
renderer.enabled = visible;
}
}
2021-04-26 13:30:21 +00:00
2021-07-12 21:02:50 +00:00
public static PlayerInfo GetClosestPlayerToWorldPoint(Vector3 worldPoint, bool includeLocalPlayer) => includeLocalPlayer
2021-04-26 13:30:21 +00:00
? GetClosestPlayerToWorldPoint(PlayerList, worldPoint)
: GetClosestPlayerToWorldPoint(PlayerList.Where(x => x != LocalPlayer).ToList(), worldPoint);
public static PlayerInfo GetClosestPlayerToWorldPoint(List<PlayerInfo> playerList, Vector3 worldPoint)
{
2021-10-24 09:47:25 +00:00
if (playerList == null)
{
DebugLog.ToConsole($"Error - Cannot get closest player from null player list.", MessageType.Error);
return null;
}
2021-04-26 13:30:21 +00:00
if (playerList.Count == 0)
{
2021-06-23 20:46:52 +00:00
DebugLog.ToConsole($"Error - Cannot get closest player from empty player list.", MessageType.Error);
2021-04-26 13:30:21 +00:00
return null;
}
2021-06-18 21:38:32 +00: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 13:30:21 +00:00
}
2021-10-30 15:14:38 +00: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
}