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

73 lines
2.1 KiB
C#
Raw Normal View History

2020-08-22 18:08:25 +01:00
using OWML.Common;
2020-11-03 21:33:48 +00:00
using QSB.Player.Events;
2020-08-21 14:04:13 +01:00
using QSB.TransformSync;
using QSB.Utility;
2020-08-21 14:04:13 +01: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>();
public static List<PlayerSyncObject> PlayerSyncObjects { get; } = 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;
}
DebugLog.DebugWrite($"Creating player id {id}", MessageType.Info);
player = new PlayerInfo(id);
PlayerList.Add(player);
return player;
}
2020-12-02 21:29:53 +00:00
public static void RemovePlayer(uint id)
{
DebugLog.DebugWrite($"Removing player {GetPlayer(id).Name} id {id}", MessageType.Info);
PlayerList.Remove(GetPlayer(id));
}
2020-07-29 22:04:50 +01:00
2020-12-02 21:29:53 +00:00
public static void RemoveAllPlayers()
{
DebugLog.DebugWrite($"Removing all players.", MessageType.Info);
PlayerList.Clear();
}
2020-10-22 21:21:41 +01:00
2020-12-02 21:29:53 +00:00
public static bool PlayerExists(uint id)
2020-12-14 16:24:52 +00:00
=> id != uint.MaxValue && PlayerList.Any(x => x.PlayerId == id);
2020-08-18 21:29:31 +01:00
2020-12-02 21:29:53 +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-02 21:29:53 +00:00
public static IEnumerable<T> GetSyncObjects<T>() where T : PlayerSyncObject
2020-12-14 16:24:52 +00:00
=> PlayerSyncObjects.OfType<T>().Where(x => x != null);
2020-12-02 21:29:53 +00:00
public static T GetSyncObject<T>(uint id) where T : PlayerSyncObject
2020-12-14 16:24:52 +00:00
=> GetSyncObjects<T>().FirstOrDefault(x => x != null && x.AttachedNetId == id);
2020-08-14 20:54:15 +02:00
2020-12-02 21:29:53 +00:00
public static bool IsBelongingToLocalPlayer(uint id)
{
return id == LocalPlayerId ||
PlayerSyncObjects.Any(x => x != null && x.AttachedNetId == id && x.IsLocalPlayer);
}
}
2020-12-03 08:28:05 +00:00
}