quantum-space-buddies/QSB/QuantumSync/Events/MultiStateChangeEvent.cs

55 lines
1.7 KiB
C#
Raw Normal View History

2020-12-24 10:06:17 +00:00
using OWML.Utils;
using QSB.Events;
using QSB.QuantumSync.WorldObjects;
2020-12-24 10:06:17 +00:00
using QSB.WorldSync;
2020-12-23 11:26:47 +00:00
namespace QSB.QuantumSync.Events
{
public class MultiStateChangeEvent : QSBEvent<MultiStateChangeMessage>
{
2021-01-03 09:52:34 +00:00
public override EventType Type => EventType.MultiStateChange;
2020-12-23 11:26:47 +00:00
public override void SetupListener() => GlobalMessenger<int, int>.AddListener(EventNames.QSBMultiStateChange, Handler);
public override void CloseListener() => GlobalMessenger<int, int>.RemoveListener(EventNames.QSBMultiStateChange, Handler);
private void Handler(int objid, int stateIndex) => SendEvent(CreateMessage(objid, stateIndex));
private MultiStateChangeMessage CreateMessage(int objid, int stateIndex) => new MultiStateChangeMessage
{
AboutId = LocalPlayerId,
ObjectId = objid,
StateIndex = stateIndex
};
2020-12-24 15:57:25 +00:00
public override void OnReceiveLocal(bool server, MultiStateChangeMessage message)
{
if (!QSBCore.DebugMode)
{
return;
}
var qsbObj = QSBWorldSync.GetWorldObject<QSBMultiStateQuantumObject>(message.ObjectId);
qsbObj.DebugBoxText.text = message.StateIndex.ToString();
}
2020-12-23 11:26:47 +00:00
public override void OnReceiveRemote(bool server, MultiStateChangeMessage message)
{
if (!QSBCore.HasWokenUp)
{
return;
}
2020-12-24 10:06:17 +00:00
var qsbObj = QSBWorldSync.GetWorldObject<QSBMultiStateQuantumObject>(message.ObjectId);
var unityObj = qsbObj.AttachedObject;
var currentStateIndex = unityObj.GetValue<int>("_stateIndex");
if (currentStateIndex != -1)
{
qsbObj.QuantumStates[currentStateIndex].SetVisible(false);
}
qsbObj.QuantumStates[message.StateIndex].SetVisible(true);
unityObj.SetValue("_stateIndex", message.StateIndex);
2020-12-24 15:57:25 +00:00
if (QSBCore.DebugMode)
{
qsbObj.DebugBoxText.text = message.StateIndex.ToString();
}
2020-12-23 11:26:47 +00:00
}
}
}