quantum-space-buddies/QSB/PlayerRegistry.cs

77 lines
2.5 KiB
C#
Raw Normal View History

using System.Collections.Generic;
using System.Linq;
using QSB.TransformSync;
using QSB.Animation;
using QSB.ElevatorSync;
2020-08-09 12:40:35 +01:00
using QSB.Messaging;
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 21:59:50 +02:00
public static List<TransformSync.TransformSync> TransformSyncs { get; } = new List<TransformSync.TransformSync>();
2020-08-08 18:03:59 +02:00
public static List<TransformSync.TransformSync> LocalTransformSyncs => TransformSyncs.Where(t => t != null && t.hasAuthority).ToList();
public static List<AnimationSync> AnimationSyncs { get; } = new List<AnimationSync>();
public static List<ElevatorController> ElevatorControllers { get; } = new List<ElevatorController>();
public static PlayerInfo CreatePlayer(uint id)
{
2020-07-28 15:59:24 +02:00
if (PlayerExists(id))
{
return null;
}
var player = new PlayerInfo(id);
2020-07-28 15:59:24 +02:00
PlayerList.Add(player);
return player;
}
public static PlayerInfo GetPlayer(uint id)
{
return PlayerList.FirstOrDefault(x => x.NetId == id);
2020-07-29 22:04:50 +01:00
}
2020-07-29 22:04:50 +01:00
public static bool PlayerExists(uint id)
{
return GetPlayer(id) != null;
}
public static void RemovePlayer(uint id)
{
PlayerList.Remove(GetPlayer(id));
2020-07-29 22:04:50 +01:00
}
2020-08-09 21:46:51 +01:00
public static void HandleFullStateMessage(PlayerStateMessage message)
{
var player = GetPlayer(message.SenderId) ?? CreatePlayer(message.SenderId);
player.Name = message.PlayerName;
2020-08-10 11:45:24 +01:00
player.IsReady = message.PlayerReady;
player.State = message.PlayerState;
2020-08-10 12:12:51 +01:00
2020-08-09 21:46:51 +01:00
if (LocalPlayer.IsReady == true)
{
2020-08-10 11:45:24 +01:00
player.UpdateStateObjects();
}
}
public static TransformSync.TransformSync GetTransformSync(uint id)
{
2020-08-08 18:03:59 +02:00
return TransformSyncs.First(x => x != null && x.netId.Value == id);
}
public static AnimationSync GetAnimationSync(uint id)
{
2020-08-08 18:03:59 +02:00
return AnimationSyncs.FirstOrDefault(x => x != null && x.netId.Value == id);
}
public static ElevatorController GetElevatorController(string name)
{
return ElevatorControllers.FirstOrDefault(x => x != null && x.ElevatorName == name);
}
}
}