2020-07-28 15:59:24 +02:00
|
|
|
|
using QSB.Messaging;
|
2020-05-19 19:31:54 +02:00
|
|
|
|
using UnityEngine.Networking;
|
|
|
|
|
|
|
|
|
|
namespace QSB.Events
|
|
|
|
|
{
|
2020-08-09 21:46:51 +01:00
|
|
|
|
public class PlayerState : NetworkBehaviour
|
2020-05-19 19:31:54 +02:00
|
|
|
|
{
|
2020-08-09 21:46:51 +01:00
|
|
|
|
public static PlayerState LocalInstance { get; private set; }
|
2020-07-28 00:13:43 +01:00
|
|
|
|
|
2020-08-09 21:46:51 +01:00
|
|
|
|
private MessageHandler<PlayerStateMessage> _messageHandler;
|
2020-05-19 19:31:54 +02:00
|
|
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
|
{
|
2020-08-22 18:08:25 +01:00
|
|
|
|
_messageHandler = new MessageHandler<PlayerStateMessage>(EventType.FullState);
|
2020-05-19 19:31:54 +02:00
|
|
|
|
_messageHandler.OnClientReceiveMessage += OnClientReceiveMessage;
|
2020-07-28 00:13:43 +01:00
|
|
|
|
|
|
|
|
|
LocalInstance = this;
|
2020-05-19 19:31:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-09 21:46:51 +01:00
|
|
|
|
private void OnClientReceiveMessage(PlayerStateMessage message)
|
2020-05-19 19:31:54 +02:00
|
|
|
|
{
|
2020-08-17 22:02:36 +02:00
|
|
|
|
if (message.AboutId == PlayerRegistry.LocalPlayerId)
|
2020-08-09 14:26:33 +01:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-08-21 22:31:42 +02:00
|
|
|
|
QSB.Helper.Events.Unity.RunWhen(
|
|
|
|
|
() => PlayerRegistry.GetSyncObject<TransformSync.TransformSync>(message.AboutId) != null,
|
2020-08-09 21:46:51 +01:00
|
|
|
|
() => PlayerRegistry.HandleFullStateMessage(message));
|
2020-05-19 19:31:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Send()
|
|
|
|
|
{
|
2020-07-28 15:59:24 +02:00
|
|
|
|
foreach (var player in PlayerRegistry.PlayerList)
|
2020-05-19 19:31:54 +02:00
|
|
|
|
{
|
2020-08-09 21:46:51 +01:00
|
|
|
|
var message = new PlayerStateMessage
|
2020-07-28 00:13:43 +01:00
|
|
|
|
{
|
2020-08-17 16:51:56 +01:00
|
|
|
|
AboutId = player.NetId,
|
2020-07-28 00:13:43 +01:00
|
|
|
|
PlayerName = player.Name,
|
2020-08-10 12:12:51 +01:00
|
|
|
|
PlayerReady = player.IsReady,
|
2020-08-09 21:46:51 +01:00
|
|
|
|
PlayerState = player.State
|
2020-07-28 00:13:43 +01:00
|
|
|
|
};
|
2020-05-19 19:31:54 +02:00
|
|
|
|
|
2020-07-28 00:13:43 +01:00
|
|
|
|
_messageHandler.SendToAll(message);
|
|
|
|
|
}
|
2020-05-19 19:31:54 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|