quantum-space-buddies/QSB/ModelShip/Messages/UseFlightConsoleMessage.cs

83 lines
2.2 KiB
C#
Raw Normal View History

2023-05-08 11:30:59 -07:00
using QSB.Messaging;
2022-05-28 20:39:42 +01:00
using QSB.ModelShip.TransformSync;
2023-05-08 11:30:59 -07:00
using QSB.OwnershipSync;
2022-05-28 20:39:42 +01:00
using QSB.Player;
using QSB.Player.TransformSync;
using QSB.Utility;
2022-05-28 20:39:42 +01:00
using QSB.WorldSync;
namespace QSB.ModelShip.Messages;
2023-07-28 19:30:57 +01:00
public class UseFlightConsoleMessage : QSBMessage<bool>
2022-05-28 20:39:42 +01:00
{
2022-06-07 22:07:39 -07:00
static UseFlightConsoleMessage()
2022-05-28 20:39:42 +01:00
{
GlobalMessenger<OWRigidbody>.AddListener(OWEvents.EnterRemoteFlightConsole, _ => Handler(true));
GlobalMessenger.AddListener(OWEvents.ExitRemoteFlightConsole, () => Handler(false));
}
private static void Handler(bool active)
{
if (PlayerTransformSync.LocalInstance != null)
{
2022-06-07 22:07:39 -07:00
new UseFlightConsoleMessage(active).Send();
2022-05-28 20:39:42 +01:00
}
}
2022-06-07 22:07:39 -07:00
private UseFlightConsoleMessage(bool active) : base(active) { }
2022-05-28 20:39:42 +01:00
public override void OnReceiveLocal() => SetCurrentFlyer(From, Data);
2022-05-28 20:39:42 +01:00
public override void OnReceiveRemote()
{
var console = QSBWorldSync.GetUnityObject<RemoteFlightConsole>();
SetCurrentFlyer(From, Data);
2022-05-28 20:39:42 +01:00
if (Data)
{
2022-06-07 19:59:22 -07:00
console._modelShipBody.Unsuspend();
2022-05-28 20:39:42 +01:00
console._interactVolume.ResetInteraction();
console._interactVolume.DisableInteraction();
}
else
{
console._interactVolume.ResetInteraction();
if (console._modelShipBody == null)
{
console._interactVolume.DisableInteraction();
return;
}
console._modelShipBody.Suspend(console._suspensionBody);
console._interactVolume.EnableInteraction();
}
QSBWorldSync.GetUnityObject<ModelShipController>()._detector.SetActive(Data);
QSBWorldSync.GetUnityObjects<ModelShipLandingSpot>().ForEach(x => x._owCollider.SetActivation(Data));
}
private void SetCurrentFlyer(uint flyer, bool isFlying)
{
ModelShipManager.Instance.CurrentFlyer = isFlying
? flyer
: uint.MaxValue;
2022-05-28 20:39:42 +01:00
if (QSBCore.IsHost)
{
ModelShipTransformSync.LocalInstance.netIdentity.SetOwner(isFlying
? flyer
: QSBPlayerManager.LocalPlayerId); // Host gets ownership when its not in use
2022-05-28 20:39:42 +01:00
}
// Client messes up its position when they start flying it
// We can just recall it immediately so its in the right place.
var console = QSBWorldSync.GetUnityObject<RemoteFlightConsole>();
2022-11-19 14:09:09 -08:00
if (console._modelShipBody) // for when model ship is destroyed
{
console.RespawnModelShip(false);
}
2022-05-28 20:39:42 +01:00
}
}