quantum-space-buddies/QSB/Events/EventHandler.cs
Mister_Nebula 1effae0bd3 event overhaul
i have no idea how i did this but it works
2020-08-08 00:08:44 +01:00

56 lines
1.6 KiB
C#

using System.Collections;
using System.Linq;
using QSB.Messaging;
using QSB.TransformSync;
using QSB.Utility;
using UnityEngine;
using UnityEngine.Networking;
namespace QSB.Events
{
public class EventHandler : NetworkBehaviour
{
public static EventHandler LocalInstance;
private MessageHandler<EventMessage> _eventHandler;
private void Awake()
{
LocalInstance = this;
_eventHandler = new MessageHandler<EventMessage>();
_eventHandler.OnClientReceiveMessage += OnClientReceiveMessage;
_eventHandler.OnServerReceiveMessage += OnServerReceiveMessage;
}
public void Send(EventMessage message)
{
StartCoroutine(SendEvent(message));
}
private IEnumerator SendEvent(EventMessage message)
{
yield return new WaitUntil(() => PlayerTransformSync.LocalInstance != null);
_eventHandler.SendToServer(message);
}
private void OnServerReceiveMessage(EventMessage message)
{
_eventHandler.SendToAll(message);
}
private void OnClientReceiveMessage(EventMessage message)
{
DebugLog.ToConsole($"Received event message of {message.EventType}");
var player = PlayerRegistry.GetPlayer(message.SenderId);
if (message.SenderId == PlayerRegistry.LocalPlayer.NetId)
{
return;
}
var _event = EventSender.EventList.First(x => x.Type == (EventType)message.EventType);
_event.OnReceive(message.SenderId, message.Data);
}
}
}