quantum-space-buddies/QSB/ShipSync/Events/FlyShipEvent.cs

84 lines
2.4 KiB
C#
Raw Normal View History

2021-04-12 09:37:20 +00:00
using OWML.Utils;
using QSB.Events;
using QSB.Messaging;
using QSB.Player;
2021-04-16 10:40:13 +00:00
using QSB.ShipSync.TransformSync;
using QSB.Utility;
using QuantumUNET;
using System.Linq;
2021-04-12 09:37:20 +00:00
using UnityEngine;
namespace QSB.ShipSync.Events
{
2021-05-15 20:31:29 +00:00
internal class FlyShipEvent : QSBEvent<BoolMessage>
2021-04-12 09:37:20 +00:00
{
public override QSB.Events.EventType Type => QSB.Events.EventType.FlyShip;
public override void SetupListener()
{
GlobalMessenger<OWRigidbody>.AddListener(EventNames.EnterFlightConsole, (OWRigidbody rigidbody) => Handler(true));
GlobalMessenger.AddListener(EventNames.ExitFlightConsole, () => Handler(false));
}
public override void CloseListener()
{
GlobalMessenger<OWRigidbody>.RemoveListener(EventNames.EnterFlightConsole, (OWRigidbody rigidbody) => Handler(true));
GlobalMessenger.RemoveListener(EventNames.ExitFlightConsole, () => Handler(false));
}
private void Handler(bool flying) => SendEvent(CreateMessage(flying));
private BoolMessage CreateMessage(bool flying) => new BoolMessage
{
AboutId = LocalPlayerId,
Value = flying
};
2021-05-15 20:31:29 +00:00
public override void OnReceiveLocal(bool server, BoolMessage message) => SetCurrentFlyer(message.Value, message.AboutId);
2021-04-12 09:37:20 +00:00
public override void OnReceiveRemote(bool server, BoolMessage message)
{
SetCurrentFlyer(message.Value, message.AboutId);
var shipCockpitController = GameObject.Find("ShipCockpitController").GetComponent<ShipCockpitController>();
2021-04-12 10:31:21 +00:00
var interactVolume = shipCockpitController.GetValue<SingleInteractionVolume>("_interactVolume");
2021-04-12 09:37:20 +00:00
if (message.Value)
{
interactVolume.DisableInteraction();
}
else
{
interactVolume.EnableInteraction();
}
}
private void SetCurrentFlyer(bool isFlying, uint id)
{
2021-06-19 10:26:05 +00:00
ShipManager.Instance.CurrentFlyer = isFlying
? id
2021-04-12 09:37:20 +00:00
: uint.MaxValue;
2021-04-16 10:40:13 +00:00
2021-08-08 18:53:55 +00:00
if (QSBCore.IsHost)
2021-04-16 10:40:13 +00:00
{
var newAuthority = ShipManager.Instance.CurrentFlyer == uint.MaxValue
2021-04-16 10:40:13 +00:00
? QNetworkServer.connections.First(x => x.GetPlayerId() == QSBPlayerManager.LocalPlayerId)
: QNetworkServer.connections.First(x => x.GetPlayerId() == id);
2021-05-10 13:30:38 +00:00
var ship = ShipTransformSync.LocalInstance;
var shipNetId = ship.NetIdentity;
2021-04-16 10:40:13 +00:00
if (shipNetId.ClientAuthorityOwner == newAuthority)
2021-04-16 10:40:13 +00:00
{
return;
}
if (shipNetId.ClientAuthorityOwner != null && shipNetId.ClientAuthorityOwner != newAuthority)
2021-04-16 10:40:13 +00:00
{
shipNetId.RemoveClientAuthority(shipNetId.ClientAuthorityOwner);
2021-04-16 10:40:13 +00:00
}
2021-06-18 21:38:32 +00:00
shipNetId.AssignClientAuthority(newAuthority);
2021-04-16 10:40:13 +00:00
}
2021-04-12 09:37:20 +00:00
}
}
}