quantum-space-buddies/QSB/Events/PlayerState.cs

49 lines
1.4 KiB
C#
Raw Normal View History

2020-07-28 13:59:24 +00:00
using QSB.Messaging;
2020-08-09 07:37:32 +00:00
using QSB.TransformSync;
using QSB.Utility;
using UnityEngine.Networking;
namespace QSB.Events
{
2020-08-09 20:46:51 +00:00
public class PlayerState : NetworkBehaviour
{
2020-08-09 20:46:51 +00:00
public static PlayerState LocalInstance { get; private set; }
2020-08-09 20:46:51 +00:00
private MessageHandler<PlayerStateMessage> _messageHandler;
private void Awake()
{
2020-08-09 20:46:51 +00:00
_messageHandler = new MessageHandler<PlayerStateMessage>(MessageType.FullState);
_messageHandler.OnClientReceiveMessage += OnClientReceiveMessage;
LocalInstance = this;
}
2020-08-09 20:46:51 +00:00
private void OnClientReceiveMessage(PlayerStateMessage message)
{
2020-08-09 13:26:33 +00:00
if (message.SenderId == PlayerTransformSync.LocalInstance.netId.Value)
{
return;
}
2020-08-09 20:46:51 +00:00
UnityHelper.Instance.RunWhen(() => PlayerRegistry.GetTransformSync(message.SenderId) != null,
() => PlayerRegistry.HandleFullStateMessage(message));
}
public void Send()
{
2020-07-28 13:59:24 +00:00
foreach (var player in PlayerRegistry.PlayerList)
{
2020-08-09 20:46:51 +00:00
var message = new PlayerStateMessage
{
2020-08-09 20:46:51 +00:00
SenderId = player.NetId,
PlayerName = player.Name,
2020-08-10 11:12:51 +00:00
PlayerReady = player.IsReady,
2020-08-09 20:46:51 +00:00
PlayerState = player.State
};
_messageHandler.SendToAll(message);
}
}
}
}