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

60 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;
2022-03-03 03:46:33 +00:00
namespace QSB.ShipSync.Messages;
internal class FlyShipMessage : QSBMessage<bool>
2021-12-24 06:40:37 +00:00
{
2022-03-03 03:46:33 +00:00
static FlyShipMessage()
2021-12-24 06:40:37 +00:00
{
2022-03-03 03:46:33 +00:00
GlobalMessenger<OWRigidbody>.AddListener(OWEvents.EnterFlightConsole, _ => Handler(true));
GlobalMessenger.AddListener(OWEvents.ExitFlightConsole, () => Handler(false));
}
2021-12-24 06:40:37 +00:00
2022-03-03 03:46:33 +00:00
private static void Handler(bool flying)
{
if (PlayerTransformSync.LocalInstance)
2021-12-24 06:40:37 +00:00
{
2022-03-03 03:46:33 +00:00
new FlyShipMessage(flying).Send();
2021-12-24 06:40:37 +00:00
}
2022-03-03 03:46:33 +00:00
}
2021-12-24 06:40:37 +00:00
2022-03-11 01:57:50 +00:00
private FlyShipMessage(bool flying) : base(flying) { }
2021-12-24 06:40:37 +00:00
2022-03-03 03:46:33 +00:00
public override bool ShouldReceive => QSBWorldSync.AllObjectsReady;
2021-12-24 06:40:37 +00:00
2022-03-03 03:46:33 +00:00
public override void OnReceiveLocal() => SetCurrentFlyer(From, Data);
2021-12-24 06:40:37 +00:00
2022-03-03 03:46:33 +00:00
public override void OnReceiveRemote()
{
SetCurrentFlyer(From, Data);
var shipCockpitController = GameObject.Find("ShipCockpitController").GetComponent<ShipCockpitController>();
if (Data)
{
shipCockpitController._interactVolume.DisableInteraction();
}
else
2021-12-24 06:40:37 +00:00
{
2022-03-03 03:46:33 +00:00
shipCockpitController._interactVolume.EnableInteraction();
2021-12-24 06:40:37 +00:00
}
2022-03-03 03:46:33 +00:00
}
private static void SetCurrentFlyer(uint id, bool isFlying)
{
ShipManager.Instance.CurrentFlyer = isFlying
? id
: uint.MaxValue;
2021-12-24 06:40:37 +00:00
2022-03-03 03:46:33 +00:00
if (QSBCore.IsHost)
2021-12-24 06:40:37 +00:00
{
2022-03-03 03:46:33 +00:00
ShipTransformSync.LocalInstance.netIdentity.SetAuthority(isFlying
2021-12-24 06:40:37 +00:00
? id
2022-03-03 03:46:33 +00:00
: QSBPlayerManager.LocalPlayerId);
2021-12-24 06:40:37 +00:00
}
}
}