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

62 lines
1.5 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
{
2022-01-27 04:35:43 +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)
{
if (PlayerTransformSync.LocalInstance)
{
new FlyShipMessage(flying).Send();
}
}
2022-01-27 05:00:12 +00:00
private FlyShipMessage(bool flying) => Value = 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, Value);
public override void OnReceiveRemote()
{
SetCurrentFlyer(From, Value);
var shipCockpitController = GameObject.Find("ShipCockpitController").GetComponent<ShipCockpitController>();
if (Value)
{
shipCockpitController._interactVolume.DisableInteraction();
}
else
{
shipCockpitController._interactVolume.EnableInteraction();
}
}
private static void SetCurrentFlyer(uint id, bool isFlying)
{
ShipManager.Instance.CurrentFlyer = isFlying
? id
: uint.MaxValue;
if (QSBCore.IsHost)
{
2022-01-15 06:31:23 +00:00
ShipTransformSync.LocalInstance.netIdentity.SetAuthority(isFlying
2021-12-24 06:40:37 +00:00
? id
: QSBPlayerManager.LocalPlayerId);
}
}
}
}