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

140 lines
4.6 KiB
C#
Raw Normal View History

2020-08-22 17:08:25 +00:00
using OWML.Common;
2020-11-03 21:33:48 +00:00
using QSB.Player.Events;
2021-04-11 16:05:02 +00:00
using QSB.Player.TransformSync;
using QSB.Tools;
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;
2021-02-09 17:18:01 +00:00
using System.Diagnostics;
2020-08-21 13:04:13 +00:00
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)
{
DebugLog.ToConsole($"Error - Trying to get LocalPlayerId when the local PlayerTransformSync instance is null.", MessageType.Error);
2021-02-15 10:58:21 +00:00
return uint.MaxValue;
}
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;
}
return localInstance.NetIdentity.NetId.Value;
}
}
2021-03-23 13:18:29 +00:00
public static Action<uint> OnRemovePlayer;
2020-12-02 21:29:53 +00:00
public static PlayerInfo LocalPlayer => GetPlayer(LocalPlayerId);
public static List<PlayerInfo> PlayerList { get; } = new List<PlayerInfo>();
2020-12-14 20:48:41 +00:00
private static readonly List<PlayerSyncObject> PlayerSyncObjects = new List<PlayerSyncObject>();
2020-12-02 21:29:53 +00:00
public static PlayerInfo GetPlayer(uint id)
{
2021-02-09 17:18:01 +00:00
if (!QSBNetworkManager.Instance.IsReady)
{
var method = new StackTrace().GetFrame(1).GetMethod();
DebugLog.ToConsole($"Warning - GetPlayer() (id<{id}>) called when Network Manager not ready! Is a Player Sync Object still active? " +
2021-02-09 17:18:01 +00:00
$"{Environment.NewLine} Called from {method.DeclaringType.Name}.{method.Name}", MessageType.Warning);
}
2020-12-02 21:29:53 +00:00
if (id == uint.MaxValue || id == 0U)
{
return default;
}
var player = PlayerList.FirstOrDefault(x => x.PlayerId == id);
if (player != null)
{
return player;
}
2021-02-09 17:18:01 +00:00
var trace = new StackTrace().GetFrame(1).GetMethod();
DebugLog.DebugWrite($"Create Player : id<{id}> (Called from {trace.DeclaringType.Name}.{trace.Name})", MessageType.Info);
2020-12-02 21:29:53 +00:00
player = new PlayerInfo(id);
PlayerList.Add(player);
return player;
}
2020-07-29 21:04:50 +00:00
2020-12-02 21:29:53 +00:00
public static void RemovePlayer(uint id)
{
2021-02-09 17:18:01 +00:00
var trace = new StackTrace().GetFrame(1).GetMethod();
DebugLog.DebugWrite($"Remove Player : id<{id}> (Called from {trace.DeclaringType.Name}.{trace.Name})", 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 void HandleFullStateMessage(PlayerStateMessage message)
{
var player = GetPlayer(message.AboutId);
player.Name = message.PlayerName;
player.IsReady = message.PlayerReady;
2021-04-18 17:42:22 +00:00
player.PlayerStates = message.PlayerState;
2020-12-14 21:20:53 +00:00
if (LocalPlayer.IsReady)
{
player.UpdateStateObjects();
}
}
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
2020-12-02 21:29:53 +00:00
public static bool IsBelongingToLocalPlayer(uint id)
{
return id == LocalPlayerId ||
2020-12-14 20:48:41 +00:00
PlayerSyncObjects.Any(x => x != null && x.AttachedNetId == id && x.IsLocalPlayer);
2020-12-02 21:29:53 +00:00
}
2020-12-22 09:13:08 +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
}
2020-12-22 09:13:08 +00:00
return cameraList;
}
public static Tuple<Flashlight, IEnumerable<QSBFlashlight>> GetPlayerFlashlights()
=> new Tuple<Flashlight, IEnumerable<QSBFlashlight>>(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-03-01 09:44:36 +00:00
if (player.Body == null)
{
DebugLog.ToConsole($"Warning - Player {playerId} has a null body!", MessageType.Warning);
return;
}
foreach (var renderer in player.Body.GetComponentsInChildren<Renderer>())
2021-02-28 14:43:05 +00:00
{
renderer.enabled = visible;
}
}
2020-12-02 21:29:53 +00:00
}
2020-12-03 08:28:05 +00:00
}