quantum-space-buddies/QSB/PlayerRegistry.cs

68 lines
2.4 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using System.Linq;
using QSB.TransformSync;
using QSB.Animation;
2020-08-09 11:40:35 +00:00
using QSB.Messaging;
using System;
using QSB.Utility;
namespace QSB
{
public static class PlayerRegistry
{
2020-08-14 18:54:15 +00:00
public static uint LocalPlayerId => PlayerTransformSync.LocalInstance.netId.Value;
public static PlayerInfo LocalPlayer => GetPlayer(LocalPlayerId);
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-08 16:03:59 +00:00
public static List<TransformSync.TransformSync> LocalTransformSyncs => TransformSyncs.Where(t => t != null && t.hasAuthority).ToList();
public static List<AnimationSync> AnimationSyncs { get; } = new List<AnimationSync>();
2020-08-17 16:58:45 +00:00
public static PlayerInfo GetPlayer(uint id)
{
2020-08-17 16:58:45 +00:00
var player = PlayerList.FirstOrDefault(x => x.NetId == id);
if (player != null)
{
2020-08-17 16:58:45 +00:00
return player;
}
2020-08-17 16:58:45 +00:00
player = new PlayerInfo(id);
2020-07-28 13:59:24 +00:00
PlayerList.Add(player);
return player;
}
public static void RemovePlayer(uint id)
{
PlayerList.Remove(GetPlayer(id));
2020-07-29 21:04:50 +00:00
}
2020-08-09 20:46:51 +00:00
public static void HandleFullStateMessage(PlayerStateMessage message)
{
2020-08-17 17:25:28 +00:00
var player = GetPlayer(message.AboutId);
player.Name = message.PlayerName;
2020-08-10 10:45:24 +00:00
player.IsReady = message.PlayerReady;
player.State = message.PlayerState;
DebugLog.ToConsole($"Updating state of player {player.NetId} to : {Environment.NewLine}" +
$"{DebugLog.GenerateTable(Enum.GetNames(typeof(State)).ToList(), FlagsHelper.FlagsToListSet(player.State))}");
2020-08-13 17:25:12 +00:00
if (LocalPlayer.IsReady)
2020-08-09 20:46:51 +00:00
{
2020-08-10 10:45:24 +00:00
player.UpdateStateObjects();
}
}
public static TransformSync.TransformSync GetTransformSync(uint id)
{
2020-08-14 17:30:23 +00:00
return TransformSyncs.FirstOrDefault(x => x != null && x.netId.Value == id);
}
2020-08-14 18:54:15 +00:00
public static bool IsBelongingToLocalPlayer(uint id)
{
2020-08-18 18:47:17 +00:00
return id == LocalPlayerId || GetTransformSync(id)?.PlayerId == LocalPlayerId;
2020-08-14 18:54:15 +00:00
}
public static AnimationSync GetAnimationSync(uint id)
{
2020-08-08 16:03:59 +00:00
return AnimationSyncs.FirstOrDefault(x => x != null && x.netId.Value == id);
}
}
}