quantum-space-buddies/QSB/ConversationSync/Messages/DialogueConditionMessage.cs
2021-12-26 18:55:54 -08:00

61 lines
1.4 KiB
C#

using QSB.Messaging;
using QSB.Player.TransformSync;
using QSB.WorldSync;
using QuantumUNET.Transport;
namespace QSB.ConversationSync.Messages
{
public class DialogueConditionMessage : QSBMessage
{
static DialogueConditionMessage() => GlobalMessenger<string, bool>.AddListener(OWEvents.DialogueConditionChanged, Handler);
private static void Handler(string name, bool state)
{
if (PlayerTransformSync.LocalInstance)
{
new DialogueConditionMessage(name, state).Send();
}
}
private string ConditionName;
private bool ConditionState;
public DialogueConditionMessage(string name, bool state)
{
ConditionName = name;
ConditionState = state;
}
public override void Serialize(QNetworkWriter writer)
{
base.Serialize(writer);
writer.Write(ConditionName);
writer.Write(ConditionState);
}
public override void Deserialize(QNetworkReader reader)
{
base.Deserialize(reader);
ConditionName = reader.ReadString();
ConditionState = reader.ReadBoolean();
}
public override void OnReceiveRemote()
{
if (QSBCore.IsHost)
{
QSBWorldSync.SetDialogueCondition(ConditionName, ConditionState);
}
DialogueConditionManager.SharedInstance.SetConditionState(ConditionName, ConditionState);
}
public override void OnReceiveLocal()
{
if (QSBCore.IsHost)
{
QSBWorldSync.SetDialogueCondition(ConditionName, ConditionState);
}
}
}
}