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

50 lines
1.6 KiB
C#
Raw Normal View History

2021-03-17 17:04:57 +00:00
using OWML.Common;
using QSB.Events;
using QSB.QuantumSync.WorldObjects;
2021-03-17 17:04:57 +00:00
using QSB.Utility;
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;
}
2021-02-24 10:45:25 +00:00
var qsbObj = QSBWorldSync.GetWorldFromId<QSBMultiStateQuantumObject>(message.ObjectId);
2020-12-24 15:57:25 +00:00
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;
}
2021-02-24 10:45:25 +00:00
var qsbObj = QSBWorldSync.GetWorldFromId<QSBMultiStateQuantumObject>(message.ObjectId);
2021-03-17 17:04:57 +00:00
if (qsbObj.ControllingPlayer != message.FromId)
{
DebugLog.ToConsole($"Error - Got MultiStateChangeEvent for {qsbObj.Name} from {message.FromId}, but it's currently controlled by {qsbObj.ControllingPlayer}!", MessageType.Error);
return;
}
2021-01-26 14:07:17 +00:00
qsbObj.ChangeState(message.StateIndex);
2020-12-23 11:26:47 +00:00
}
}
}