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

71 lines
2.3 KiB
C#

using System.Collections;
using QSB.Messaging;
using QSB.TransformSync;
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(EventType eventType)
{
StartCoroutine(SendEvent(eventType));
}
private IEnumerator SendEvent(EventType eventType)
{
yield return new WaitUntil(() => PlayerTransformSync.LocalInstance != null);
var message = new EventMessage
{
EventType = (int)eventType,
SenderId = PlayerTransformSync.LocalInstance.netId.Value
};
_eventHandler.SendToServer(message);
}
private void OnServerReceiveMessage(EventMessage message)
{
_eventHandler.SendToAll(message);
}
private void OnClientReceiveMessage(EventMessage message)
{
if (message.SenderId == PlayerTransformSync.LocalInstance.netId.Value)
{
return;
}
switch ((EventType)message.EventType)
{
case EventType.TurnOnFlashlight:
PlayerRegistry.GetPlayerFlashlight(message.SenderId).TurnOn();
PlayerRegistry.UpdateState(message.SenderId, State.Flashlight, true);
break;
case EventType.TurnOffFlashlight:
PlayerRegistry.GetPlayerFlashlight(message.SenderId).TurnOff();
PlayerRegistry.UpdateState(message.SenderId, State.Flashlight, false);
break;
case EventType.SuitUp:
PlayerRegistry.UpdateState(message.SenderId, State.Suit, true);
break;
case EventType.RemoveSuit:
PlayerRegistry.UpdateState(message.SenderId, State.Suit, false);
break;
}
}
}
}