mirror of
https://github.com/misternebula/quantum-space-buddies.git
synced 2025-01-18 13:23:05 +00:00
49 lines
1.4 KiB
C#
49 lines
1.4 KiB
C#
using QSB.Messaging;
|
|
using QSB.TransformSync;
|
|
using QSB.Utility;
|
|
using UnityEngine.Networking;
|
|
|
|
namespace QSB.Events
|
|
{
|
|
public class PlayerState : NetworkBehaviour
|
|
{
|
|
public static PlayerState LocalInstance { get; private set; }
|
|
|
|
private MessageHandler<PlayerStateMessage> _messageHandler;
|
|
|
|
private void Awake()
|
|
{
|
|
_messageHandler = new MessageHandler<PlayerStateMessage>(MessageType.FullState);
|
|
_messageHandler.OnClientReceiveMessage += OnClientReceiveMessage;
|
|
|
|
LocalInstance = this;
|
|
}
|
|
|
|
private void OnClientReceiveMessage(PlayerStateMessage message)
|
|
{
|
|
if (message.SenderId == PlayerTransformSync.LocalInstance.netId.Value)
|
|
{
|
|
return;
|
|
}
|
|
UnityHelper.Instance.RunWhen(() => PlayerRegistry.GetTransformSync(message.SenderId) != null,
|
|
() => PlayerRegistry.HandleFullStateMessage(message));
|
|
}
|
|
|
|
public void Send()
|
|
{
|
|
foreach (var player in PlayerRegistry.PlayerList)
|
|
{
|
|
var message = new PlayerStateMessage
|
|
{
|
|
SenderId = player.NetId,
|
|
PlayerName = player.Name,
|
|
PlayerReady = player.IsReady,
|
|
PlayerState = player.State
|
|
};
|
|
|
|
_messageHandler.SendToAll(message);
|
|
}
|
|
}
|
|
}
|
|
}
|