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

86 lines
2.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;
2020-08-21 13:04:13 +00:00
using QSB.TransformSync;
using QSB.Utility;
2020-08-21 13:04:13 +00:00
using System.Collections.Generic;
using System.Linq;
2020-11-03 21:33:48 +00:00
namespace QSB.Player
{
2020-12-02 21:29:53 +00:00
public static class QSBPlayerManager
{
2020-12-12 18:14:04 +00:00
public static uint LocalPlayerId => PlayerTransformSync.LocalInstance.NetIdentity?.NetId.Value ?? uint.MaxValue;
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)
{
if (id == uint.MaxValue || id == 0U)
{
return default;
}
var player = PlayerList.FirstOrDefault(x => x.PlayerId == id);
if (player != null)
{
return player;
}
2020-12-14 19:23:24 +00:00
DebugLog.DebugWrite($"Create Player : id<{id}>", 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)
{
2020-12-14 19:23:24 +00:00
DebugLog.DebugWrite($"Remove Player : id<{id}>", MessageType.Info);
2020-12-02 21:29:53 +00:00
PlayerList.Remove(GetPlayer(id));
}
2020-07-29 21:04:50 +00:00
2020-12-02 21:29:53 +00:00
public static void RemoveAllPlayers()
{
2020-12-14 19:23:24 +00:00
DebugLog.DebugWrite($"Remove All Players", MessageType.Info);
2020-12-02 21:29:53 +00:00
PlayerList.Clear();
}
2020-10-22 20:21:41 +00:00
2020-12-14 20:41:56 +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 20:41:56 +00:00
public static void HandleFullStateMessage(PlayerStateMessage message)
{
var player = GetPlayer(message.AboutId);
player.Name = message.PlayerName;
player.IsReady = message.PlayerReady;
player.State = message.PlayerState;
if (LocalPlayer.IsReady)
{
player.UpdateStateObjects();
}
}
2020-12-14 20:41:56 +00:00
public static IEnumerable<T> GetSyncObjects<T>() where T : PlayerSyncObject =>
PlayerSyncObjects.OfType<T>().Where(x => x != null);
2020-12-14 20:41:56 +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-14 19:23:24 +00:00
public static void AddSyncObject(PlayerSyncObject obj)
{
DebugLog.DebugWrite($"SyncObject Add : type<{obj.GetType().Name}>, netid<{obj.NetId}>");
2020-12-14 20:48:41 +00:00
PlayerSyncObjects.Add(obj);
2020-12-14 19:23:24 +00:00
}
public static void RemoveSyncObject(PlayerSyncObject obj)
{
DebugLog.DebugWrite($"SyncObject Remove : type<{obj.GetType().Name}>, netid<{obj.NetId}>");
2020-12-14 20:48:41 +00:00
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-03 08:28:05 +00:00
}