quantum-space-buddies/QSB/PlayerRegistry.cs

69 lines
2.3 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using System.Linq;
using QSB.Events;
using QSB.TransformSync;
using QSB.Animation;
using QSB.Utility;
namespace QSB
{
public static class PlayerRegistry
{
public static PlayerInfo LocalPlayer => GetPlayer(PlayerTransformSync.LocalInstance.netId.Value);
public static List<PlayerInfo> PlayerList { get; } = new List<PlayerInfo>();
2020-08-05 19:59:50 +00:00
public static List<TransformSync.TransformSync> TransformSyncs { get; } = new List<TransformSync.TransformSync>();
2020-08-05 19:59:50 +00:00
public static List<TransformSync.TransformSync> LocalTransformSyncs => TransformSyncs.Where(t => t.hasAuthority).ToList();
public static List<AnimationSync> AnimationSyncs { get; } = new List<AnimationSync>();
public static PlayerInfo CreatePlayer(uint id)
{
DebugLog.ToConsole($"Creating player {id}");
2020-07-28 13:59:24 +00:00
if (PlayerExists(id))
{
return null;
}
var player = new PlayerInfo(id);
2020-07-28 13:59:24 +00:00
PlayerList.Add(player);
return player;
}
public static PlayerInfo GetPlayer(uint id)
{
return PlayerList.FirstOrDefault(x => x.NetId == id);
2020-07-29 21:04:50 +00:00
}
2020-07-29 21:04:50 +00:00
public static bool PlayerExists(uint id)
{
return GetPlayer(id) != null;
}
public static void RemovePlayer(uint id)
{
PlayerList.Remove(GetPlayer(id));
2020-07-29 21:04:50 +00:00
}
public static void HandleFullStateMessage(FullStateMessage message)
{
var player = GetPlayer(message.SenderId) ?? CreatePlayer(message.SenderId);
DebugLog.ToConsole($"Player Id {message.SenderId} ----------------", OWML.Common.MessageType.Info);
player.Name = message.PlayerName;
DebugLog.ToConsole($"Name : {player.Name}", OWML.Common.MessageType.Info);
player.IsReady = message.IsReady;
DebugLog.ToConsole($"IsReady : {player.IsReady}", OWML.Common.MessageType.Info);
}
public static TransformSync.TransformSync GetTransformSync(uint id)
{
return TransformSyncs.Single(x => x.netId.Value == id);
}
public static AnimationSync GetAnimationSync(uint id)
{
return AnimationSyncs.SingleOrDefault(x => x.netId.Value == id);
}
}
}