2020-07-28 13:59:24 +00:00
|
|
|
|
using QSB.Messaging;
|
2020-08-09 07:37:32 +00:00
|
|
|
|
using QSB.TransformSync;
|
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-07-28 13:59:24 +00:00
|
|
|
|
public class GameState : NetworkBehaviour
|
2020-05-19 17:31:54 +00:00
|
|
|
|
{
|
2020-07-27 23:13:43 +00:00
|
|
|
|
public static GameState LocalInstance { get; private set; }
|
|
|
|
|
|
2020-05-19 17:31:54 +00:00
|
|
|
|
private MessageHandler<FullStateMessage> _messageHandler;
|
|
|
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
|
{
|
2020-08-09 06:58:54 +00:00
|
|
|
|
_messageHandler = new MessageHandler<FullStateMessage>(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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnClientReceiveMessage(FullStateMessage message)
|
|
|
|
|
{
|
2020-08-09 13:26:33 +00:00
|
|
|
|
if (message.SenderId == PlayerTransformSync.LocalInstance.netId.Value)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
DebugLog.ToConsole($"Received game state for id {message.SenderId}");
|
2020-07-27 23:13:43 +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-07-28 13:59:24 +00:00
|
|
|
|
var message = new FullStateMessage
|
2020-07-27 23:13:43 +00:00
|
|
|
|
{
|
|
|
|
|
PlayerName = player.Name,
|
|
|
|
|
SenderId = player.NetId
|
|
|
|
|
};
|
2020-05-19 17:31:54 +00:00
|
|
|
|
|
2020-07-27 23:13:43 +00:00
|
|
|
|
_messageHandler.SendToAll(message);
|
2020-08-07 23:08:44 +00:00
|
|
|
|
DebugLog.ToConsole($"* Sent state for {player.NetId}");
|
2020-07-27 23:13:43 +00:00
|
|
|
|
}
|
2020-05-19 17:31:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|