2020-07-28 13:59:24 +00:00
|
|
|
|
using QSB.Messaging;
|
2020-08-07 23:08:44 +00:00
|
|
|
|
using QSB.Utility;
|
2020-05-19 17:31:54 +00:00
|
|
|
|
using UnityEngine.Networking;
|
|
|
|
|
|
|
|
|
|
namespace QSB.Events
|
|
|
|
|
{
|
2020-08-09 20:46:51 +00:00
|
|
|
|
public class PlayerState : NetworkBehaviour
|
2020-05-19 17:31:54 +00:00
|
|
|
|
{
|
2020-08-09 20:46:51 +00:00
|
|
|
|
public static PlayerState LocalInstance { get; private set; }
|
2020-07-27 23:13:43 +00:00
|
|
|
|
|
2020-08-09 20:46:51 +00:00
|
|
|
|
private MessageHandler<PlayerStateMessage> _messageHandler;
|
2020-05-19 17:31:54 +00:00
|
|
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
|
{
|
2020-08-09 20:46:51 +00:00
|
|
|
|
_messageHandler = new MessageHandler<PlayerStateMessage>(MessageType.FullState);
|
2020-05-19 17:31:54 +00:00
|
|
|
|
_messageHandler.OnClientReceiveMessage += OnClientReceiveMessage;
|
2020-07-27 23:13:43 +00:00
|
|
|
|
|
|
|
|
|
LocalInstance = this;
|
2020-05-19 17:31:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-09 20:46:51 +00:00
|
|
|
|
private void OnClientReceiveMessage(PlayerStateMessage message)
|
2020-05-19 17:31:54 +00:00
|
|
|
|
{
|
2020-08-17 17:21:57 +00:00
|
|
|
|
if (message.FromId == PlayerRegistry.LocalPlayerId)
|
2020-08-09 13:26:33 +00:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-08-17 15:51:56 +00:00
|
|
|
|
UnityHelper.Instance.RunWhen(() => PlayerRegistry.GetTransformSync(message.AboutId) != null,
|
2020-08-09 20:46:51 +00:00
|
|
|
|
() => PlayerRegistry.HandleFullStateMessage(message));
|
2020-05-19 17:31:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Send()
|
|
|
|
|
{
|
2020-07-28 13:59:24 +00:00
|
|
|
|
foreach (var player in PlayerRegistry.PlayerList)
|
2020-05-19 17:31:54 +00:00
|
|
|
|
{
|
2020-08-09 20:46:51 +00:00
|
|
|
|
var message = new PlayerStateMessage
|
2020-07-27 23:13:43 +00:00
|
|
|
|
{
|
2020-08-17 15:51:56 +00:00
|
|
|
|
FromId = PlayerRegistry.LocalPlayerId,
|
|
|
|
|
AboutId = player.NetId,
|
2020-07-27 23:13:43 +00:00
|
|
|
|
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
|
2020-07-27 23:13:43 +00:00
|
|
|
|
};
|
2020-05-19 17:31:54 +00:00
|
|
|
|
|
2020-07-27 23:13:43 +00:00
|
|
|
|
_messageHandler.SendToAll(message);
|
|
|
|
|
}
|
2020-05-19 17:31:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|