quantum-space-buddies/QSB/Events/FullStateRequest.cs
AmazingAlek 18a520bc78
cleanup (#99)
* cleanup
2020-07-28 14:59:24 +01:00

44 lines
1.2 KiB
C#

using System.Collections;
using QSB.Messaging;
using QSB.TransformSync;
using UnityEngine;
using UnityEngine.Networking;
namespace QSB.Events
{
public class FullStateRequest : NetworkBehaviour
{
public static FullStateRequest LocalInstance { get; private set; }
private MessageHandler<StateRequestMessage> _stateRequestHandler;
private void Awake()
{
_stateRequestHandler = new MessageHandler<StateRequestMessage>();
_stateRequestHandler.OnServerReceiveMessage += OnServerReceiveMessage;
LocalInstance = this;
}
public void Request()
{
StartCoroutine(SendRequest());
}
private IEnumerator SendRequest()
{
yield return new WaitUntil(() => PlayerTransformSync.LocalInstance != null);
var message = new StateRequestMessage
{
SenderId = PlayerTransformSync.LocalInstance.netId.Value
};
_stateRequestHandler.SendToServer(message);
}
private void OnServerReceiveMessage(StateRequestMessage message)
{
GameState.LocalInstance.Send();
}
}
}