quantum-space-buddies/QSB/ShipSync/Messages/FlyShipMessage.cs

61 lines
1.4 KiB
C#
Raw Normal View History

2021-12-24 06:40:37 +00:00
using QSB.AuthoritySync;
using QSB.Messaging;
using QSB.Player;
using QSB.Player.TransformSync;
using QSB.ShipSync.TransformSync;
using QSB.WorldSync;
using UnityEngine;
namespace QSB.ShipSync.Messages
2021-12-24 06:40:37 +00:00
{
internal class FlyShipMessage : QSBMessage<bool>
2021-12-24 06:40:37 +00:00
{
static FlyShipMessage()
{
GlobalMessenger<OWRigidbody>.AddListener(OWEvents.EnterFlightConsole, _ => Handler(true));
GlobalMessenger.AddListener(OWEvents.ExitFlightConsole, () => Handler(false));
}
2021-12-24 06:40:37 +00:00
private static void Handler(bool flying)
2021-12-24 06:40:37 +00:00
{
if (PlayerTransformSync.LocalInstance)
{
new FlyShipMessage(flying).Send();
}
2021-12-24 06:40:37 +00:00
}
private FlyShipMessage(bool flying) => Data = flying;
2021-12-24 06:40:37 +00:00
public override bool ShouldReceive => QSBWorldSync.AllObjectsReady;
2021-12-24 06:40:37 +00:00
public override void OnReceiveLocal() => SetCurrentFlyer(From, Data);
2021-12-24 06:40:37 +00:00
public override void OnReceiveRemote()
2021-12-24 06:40:37 +00:00
{
SetCurrentFlyer(From, Data);
var shipCockpitController = GameObject.Find("ShipCockpitController").GetComponent<ShipCockpitController>();
if (Data)
{
shipCockpitController._interactVolume.DisableInteraction();
}
else
{
shipCockpitController._interactVolume.EnableInteraction();
}
2021-12-24 06:40:37 +00:00
}
private static void SetCurrentFlyer(uint id, bool isFlying)
2021-12-24 06:40:37 +00:00
{
ShipManager.Instance.CurrentFlyer = isFlying
2021-12-24 06:40:37 +00:00
? id
: uint.MaxValue;
if (QSBCore.IsHost)
{
ShipTransformSync.LocalInstance.netIdentity.SetAuthority(isFlying
? id
: QSBPlayerManager.LocalPlayerId);
}
2021-12-24 06:40:37 +00:00
}
}
}