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

48 lines
1.3 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-07-28 13:59:24 +00:00
public class GameState : NetworkBehaviour
{
public static GameState LocalInstance { get; private set; }
private MessageHandler<FullStateMessage> _messageHandler;
private void Awake()
{
2020-08-09 06:58:54 +00:00
_messageHandler = new MessageHandler<FullStateMessage>(MessageType.FullState);
_messageHandler.OnClientReceiveMessage += OnClientReceiveMessage;
LocalInstance = this;
}
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}");
PlayerRegistry.HandleFullStateMessage(message);
}
public void Send()
{
2020-07-28 13:59:24 +00:00
foreach (var player in PlayerRegistry.PlayerList)
{
2020-07-28 13:59:24 +00:00
var message = new FullStateMessage
{
PlayerName = player.Name,
SenderId = player.NetId
};
_messageHandler.SendToAll(message);
DebugLog.ToConsole($"* Sent state for {player.NetId}");
}
}
}
}