mirror of
https://github.com/misternebula/quantum-space-buddies.git
synced 2025-01-09 12:54:51 +00:00
49227bf5c9
* Remove wake up dependency from transform sync * Remove wake up dependency for all synced transforms * Remove comments * Remove obsolete QSBBehaviour component * Remove wakeup dependency in RespawnOnDeath * Prevent errors on MessageHandler and RespawnOnDeath when network not initialized * Add RespawnOnDeath only after network is initialized * wip * Prevent trying to add animator components multiple times * Reset sectors on scene load * Reset transform sync on scene change * Remove comment * Remove wakeup event dependency from WakeUpSync * Sync loop count
63 lines
1.8 KiB
C#
63 lines
1.8 KiB
C#
using System;
|
|
using UnityEngine.Networking;
|
|
|
|
namespace QSB.Messaging
|
|
{
|
|
// Extend this to create new message handlers.
|
|
public class MessageHandler<T> where T : QSBMessage, new()
|
|
{
|
|
public event Action<T> OnClientReceiveMessage;
|
|
public event Action<T> OnServerReceiveMessage;
|
|
|
|
public MessageHandler()
|
|
{
|
|
if (QSBNetworkManager.IsReady)
|
|
{
|
|
Init();
|
|
}
|
|
else
|
|
{
|
|
QSBNetworkManager.OnNetworkManagerReady.AddListener(Init);
|
|
}
|
|
}
|
|
|
|
private void Init()
|
|
{
|
|
var message = (T)Activator.CreateInstance(typeof(T));
|
|
NetworkServer.RegisterHandler((short)message.MessageType, OnServerReceiveMessageHandler);
|
|
NetworkManager.singleton.client.RegisterHandler((short)message.MessageType, OnClientReceiveMessageHandler);
|
|
}
|
|
|
|
public void SendToAll(T message)
|
|
{
|
|
if (!QSBNetworkManager.IsReady)
|
|
{
|
|
return;
|
|
}
|
|
NetworkServer.SendToAll((short)message.MessageType, message);
|
|
}
|
|
|
|
public void SendToServer(T message)
|
|
{
|
|
if (!QSBNetworkManager.IsReady)
|
|
{
|
|
return;
|
|
}
|
|
NetworkManager.singleton.client.Send((short)message.MessageType, message);
|
|
}
|
|
|
|
private void OnClientReceiveMessageHandler(NetworkMessage netMsg)
|
|
{
|
|
var message = netMsg.ReadMessage<T>();
|
|
OnClientReceiveMessage?.Invoke(message);
|
|
}
|
|
|
|
private void OnServerReceiveMessageHandler(NetworkMessage netMsg)
|
|
{
|
|
var message = netMsg.ReadMessage<T>();
|
|
OnServerReceiveMessage?.Invoke(message);
|
|
}
|
|
|
|
}
|
|
}
|