quantum-space-buddies/QSB/Anglerfish/Events/AnglerChangeStateEvent.cs
2021-12-05 11:03:09 +00:00

68 lines
1.8 KiB
C#

using QSB.Anglerfish.WorldObjects;
using QSB.Events;
using QSB.Player;
using QSB.WorldSync;
using UnityEngine;
namespace QSB.Anglerfish.Events
{
public class AnglerChangeStateEvent : QSBEvent<AnglerChangeStateMessage>
{
public override bool RequireWorldObjectsReady => true;
public override void SetupListener()
=> GlobalMessenger<QSBAngler>.AddListener(EventNames.QSBAnglerChangeState, Handler);
public override void CloseListener()
=> GlobalMessenger<QSBAngler>.RemoveListener(EventNames.QSBAnglerChangeState, Handler);
private void Handler(QSBAngler qsbAngler) => SendEvent(CreateMessage(qsbAngler));
private AnglerChangeStateMessage CreateMessage(QSBAngler qsbAngler) => new()
{
ObjectId = qsbAngler.ObjectId,
EnumValue = qsbAngler.AttachedObject._currentState,
TargetId = TargetToId(qsbAngler.TargetTransform),
LocalDisturbancePos = qsbAngler.AttachedObject._localDisturbancePos
};
public override void OnReceiveRemote(bool isHost, AnglerChangeStateMessage message)
{
var qsbAngler = QSBWorldSync.GetWorldFromId<QSBAngler>(message.ObjectId);
qsbAngler.TargetTransform = IdToTarget(message.TargetId);
qsbAngler.AttachedObject._localDisturbancePos = message.LocalDisturbancePos;
qsbAngler.AttachedObject.ChangeState(message.EnumValue);
}
private static uint TargetToId(Transform transform)
{
if (transform == null)
{
return uint.MaxValue;
}
if (transform == Locator.GetShipTransform())
{
return uint.MaxValue - 1;
}
return QSBPlayerManager.LocalPlayerId;
}
private static Transform IdToTarget(uint id)
{
if (id == uint.MaxValue)
{
return null;
}
if (id == uint.MaxValue - 1)
{
return Locator.GetShipTransform();
}
return QSBPlayerManager.GetPlayer(id).Body.transform;
}
}
}