2022-01-28 20:49:07 -08:00
|
|
|
|
using Cysharp.Threading.Tasks;
|
|
|
|
|
using Mirror;
|
2022-01-14 22:54:18 -08:00
|
|
|
|
using OWML.Common;
|
2022-05-17 22:28:49 +01:00
|
|
|
|
using QSB.Animation.Player.Thrusters;
|
2022-05-05 15:42:07 +01:00
|
|
|
|
using QSB.Messaging;
|
2021-06-19 13:22:05 +01:00
|
|
|
|
using QSB.Player;
|
2022-05-05 15:42:07 +01:00
|
|
|
|
using QSB.ShipSync.Messages;
|
2021-06-15 18:56:51 +01:00
|
|
|
|
using QSB.ShipSync.TransformSync;
|
2021-06-20 09:48:37 +01:00
|
|
|
|
using QSB.ShipSync.WorldObjects;
|
2021-04-12 10:37:20 +01:00
|
|
|
|
using QSB.Utility;
|
2021-06-15 18:56:51 +01:00
|
|
|
|
using QSB.WorldSync;
|
2021-06-19 13:22:05 +01:00
|
|
|
|
using System.Collections.Generic;
|
2021-04-12 10:37:20 +01:00
|
|
|
|
using System.Linq;
|
2022-01-28 20:49:07 -08:00
|
|
|
|
using System.Threading;
|
2021-04-12 10:37:20 +01:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
namespace QSB.ShipSync;
|
|
|
|
|
|
|
|
|
|
internal class ShipManager : WorldObjectManager
|
2021-04-12 10:37:20 +01:00
|
|
|
|
{
|
2022-03-02 19:46:33 -08:00
|
|
|
|
public override WorldObjectScene WorldObjectScene => WorldObjectScene.SolarSystem;
|
2021-12-20 18:41:12 -08:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
public static ShipManager Instance;
|
2021-04-12 10:37:20 +01:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
public InteractZone HatchInteractZone;
|
|
|
|
|
public HatchController HatchController;
|
|
|
|
|
public ShipTractorBeamSwitch ShipTractorBeam;
|
|
|
|
|
public ShipCockpitController CockpitController;
|
|
|
|
|
public ShipElectricalComponent ShipElectricalComponent;
|
2022-04-16 10:08:59 +01:00
|
|
|
|
public ShipCockpitUI ShipCockpitUI;
|
2022-03-02 19:46:33 -08:00
|
|
|
|
private GameObject _shipCustomAttach;
|
|
|
|
|
public uint CurrentFlyer
|
|
|
|
|
{
|
|
|
|
|
get => _currentFlyer;
|
|
|
|
|
set
|
2021-04-12 10:37:20 +01:00
|
|
|
|
{
|
2022-03-02 19:46:33 -08:00
|
|
|
|
if (_currentFlyer != uint.MaxValue && value != uint.MaxValue)
|
2021-04-12 10:37:20 +01:00
|
|
|
|
{
|
2022-03-02 19:46:33 -08:00
|
|
|
|
DebugLog.ToConsole($"Warning - Trying to set current flyer while someone is still flying? Current:{_currentFlyer}, New:{value}", MessageType.Warning);
|
2022-02-27 04:40:44 -08:00
|
|
|
|
}
|
2022-03-02 19:46:33 -08:00
|
|
|
|
|
|
|
|
|
_currentFlyer = value;
|
2021-04-12 10:37:20 +01:00
|
|
|
|
}
|
2022-03-02 19:46:33 -08:00
|
|
|
|
}
|
2022-08-16 16:45:13 +01:00
|
|
|
|
public bool IsShipWrecked => _shipDestroyed || ShipCockpitUI._shipDamageCtrlr.IsDestroyed();
|
2021-04-13 17:25:00 +01:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
private readonly List<PlayerInfo> _playersInShip = new();
|
2021-06-19 13:22:05 +01:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
private uint _currentFlyer = uint.MaxValue;
|
2022-08-16 16:45:13 +01:00
|
|
|
|
private bool _shipDestroyed;
|
2021-04-13 17:25:00 +01:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
public void Start()
|
2022-05-05 15:42:07 +01:00
|
|
|
|
{
|
|
|
|
|
Instance = this;
|
|
|
|
|
QSBPlayerManager.OnRemovePlayer += OnRemovePlayer;
|
2022-08-16 16:45:13 +01:00
|
|
|
|
GlobalMessenger.AddListener("ShipDestroyed", OnShipDestroyed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnDestroy()
|
|
|
|
|
{
|
|
|
|
|
GlobalMessenger.RemoveListener("ShipDestroyed", OnShipDestroyed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnShipDestroyed()
|
|
|
|
|
{
|
|
|
|
|
_shipDestroyed = true;
|
2022-05-05 15:42:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnRemovePlayer(PlayerInfo player)
|
|
|
|
|
{
|
|
|
|
|
if (QSBCore.IsHost && player.PlayerId == CurrentFlyer)
|
|
|
|
|
{
|
|
|
|
|
new FlyShipMessage(false).Send();
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-13 21:09:26 +01:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
public override async UniTask BuildWorldObjects(OWScene scene, CancellationToken ct)
|
|
|
|
|
{
|
2022-08-16 16:45:13 +01:00
|
|
|
|
_shipDestroyed = false;
|
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
var shipBody = Locator.GetShipBody();
|
|
|
|
|
if (shipBody == null)
|
2021-06-15 18:56:51 +01:00
|
|
|
|
{
|
2022-03-02 19:46:33 -08:00
|
|
|
|
DebugLog.ToConsole($"Error - Couldn't find ship!", MessageType.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-07-16 14:06:34 +01:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
HatchController = shipBody.GetComponentInChildren<HatchController>();
|
|
|
|
|
if (HatchController == null)
|
|
|
|
|
{
|
|
|
|
|
DebugLog.ToConsole($"Error - Couldn't find hatch controller!", MessageType.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-07-16 14:06:34 +01:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
HatchInteractZone = HatchController.GetComponent<InteractZone>();
|
2022-05-03 08:48:24 +01:00
|
|
|
|
ShipTractorBeam = QSBWorldSync.GetUnityObject<ShipTractorBeamSwitch>();
|
|
|
|
|
CockpitController = QSBWorldSync.GetUnityObject<ShipCockpitController>();
|
|
|
|
|
ShipElectricalComponent = QSBWorldSync.GetUnityObject<ShipElectricalComponent>();
|
2022-05-14 09:01:14 +01:00
|
|
|
|
ShipCockpitUI = QSBWorldSync.GetUnityObject<ShipCockpitUI>();
|
2021-04-13 17:25:00 +01:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
var sphereShape = HatchController.GetComponent<SphereShape>();
|
|
|
|
|
sphereShape.radius = 2.5f;
|
|
|
|
|
sphereShape.center = new Vector3(0, 0, 1);
|
2021-04-16 11:40:13 +01:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
if (QSBCore.IsHost)
|
|
|
|
|
{
|
|
|
|
|
if (QSBPlayerManager.LocalPlayer.TransformSync == null)
|
|
|
|
|
{
|
|
|
|
|
DebugLog.ToConsole($"Error - Tried to spawn ship, but LocalPlayer's TransformSync is null!", MessageType.Error);
|
|
|
|
|
}
|
2022-01-31 16:33:11 -08:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
Instantiate(QSBNetworkManager.singleton.ShipPrefab).SpawnWithServerAuthority();
|
2022-02-27 04:40:44 -08:00
|
|
|
|
}
|
2021-06-19 13:22:05 +01:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
QSBWorldSync.Init<QSBShipComponent, ShipComponent>();
|
|
|
|
|
QSBWorldSync.Init<QSBShipHull, ShipHull>();
|
2021-06-19 15:35:40 +01:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
_shipCustomAttach = new GameObject(nameof(ShipCustomAttach));
|
|
|
|
|
_shipCustomAttach.transform.SetParent(shipBody.transform, false);
|
|
|
|
|
_shipCustomAttach.AddComponent<ShipCustomAttach>();
|
2022-04-16 21:31:01 +01:00
|
|
|
|
|
2022-08-04 22:21:46 -07:00
|
|
|
|
QSBWorldSync.Init<QSBShipLight, ShipLight>(new[]
|
2022-04-16 21:31:01 +01:00
|
|
|
|
{
|
|
|
|
|
CockpitController._headlight,
|
|
|
|
|
CockpitController._landingLight,
|
|
|
|
|
ShipCockpitUI._altimeterLight,
|
|
|
|
|
ShipCockpitUI._landingCamScreenLight,
|
|
|
|
|
ShipCockpitUI._minimapLight,
|
|
|
|
|
ShipCockpitUI._minimapNorthPoleLight,
|
|
|
|
|
ShipCockpitUI._minimapProbeLight,
|
|
|
|
|
ShipCockpitUI._minimapShipLight,
|
|
|
|
|
ShipCockpitUI._minimapSouthPoleLight,
|
|
|
|
|
ShipCockpitUI._probeLauncherScreenLight,
|
|
|
|
|
ShipCockpitUI._sigScopeScreenLight
|
|
|
|
|
});
|
2022-05-14 12:18:48 +01:00
|
|
|
|
|
|
|
|
|
QSBWorldSync.Init<QSBShipDetachableModule, ShipDetachableModule>();
|
|
|
|
|
QSBWorldSync.Init<QSBShipDetachableLeg, ShipDetachableLeg>();
|
2022-03-02 19:46:33 -08:00
|
|
|
|
}
|
2021-06-19 15:35:40 +01:00
|
|
|
|
|
2022-08-04 22:21:46 -07:00
|
|
|
|
public override void UnbuildWorldObjects()
|
|
|
|
|
{
|
|
|
|
|
if (QSBCore.IsHost)
|
|
|
|
|
{
|
|
|
|
|
if (ShipTransformSync.LocalInstance != null)
|
|
|
|
|
{
|
|
|
|
|
if (ShipTransformSync.LocalInstance.gameObject == null)
|
|
|
|
|
{
|
|
|
|
|
DebugLog.ToConsole($"Warning - ShipTransformSync's LocalInstance is not null, but it's gameobject is null!", MessageType.Warning);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NetworkServer.Destroy(ShipTransformSync.LocalInstance.gameObject);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Destroy(_shipCustomAttach);
|
|
|
|
|
}
|
2022-02-24 22:04:54 -08:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
public void AddPlayerToShip(PlayerInfo player)
|
|
|
|
|
{
|
|
|
|
|
_playersInShip.Add(player);
|
|
|
|
|
UpdateElectricalComponent();
|
|
|
|
|
}
|
2022-02-24 22:04:54 -08:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
public void RemovePlayerFromShip(PlayerInfo player)
|
|
|
|
|
{
|
|
|
|
|
_playersInShip.Remove(player);
|
|
|
|
|
UpdateElectricalComponent();
|
|
|
|
|
}
|
2022-02-27 04:40:44 -08:00
|
|
|
|
|
2022-03-02 19:46:33 -08:00
|
|
|
|
public bool IsPlayerInShip(PlayerInfo player)
|
|
|
|
|
=> _playersInShip.Contains(player);
|
|
|
|
|
|
|
|
|
|
private void UpdateElectricalComponent()
|
|
|
|
|
{
|
|
|
|
|
var electricalSystem = ShipElectricalComponent._electricalSystem;
|
|
|
|
|
var damaged = ShipElectricalComponent._damaged;
|
|
|
|
|
|
|
|
|
|
if (_playersInShip.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
if (!damaged)
|
2021-06-19 15:35:40 +01:00
|
|
|
|
{
|
2022-03-02 19:46:33 -08:00
|
|
|
|
electricalSystem.SetPowered(false);
|
2021-06-19 15:35:40 +01:00
|
|
|
|
}
|
2022-03-02 19:46:33 -08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (!damaged)
|
2021-06-19 15:35:40 +01:00
|
|
|
|
{
|
2022-03-02 19:46:33 -08:00
|
|
|
|
electricalSystem.SetPowered(true);
|
2021-06-19 15:35:40 +01:00
|
|
|
|
}
|
2021-06-19 13:22:05 +01:00
|
|
|
|
}
|
2021-04-12 10:37:20 +01:00
|
|
|
|
}
|
2022-04-16 10:08:59 +01:00
|
|
|
|
|
|
|
|
|
public void UpdateSignalscope(bool equipped)
|
|
|
|
|
{
|
|
|
|
|
ShipCockpitUI._displaySignalscopeScreen = equipped;
|
|
|
|
|
ShipCockpitUI._shipAudioController.PlaySigScopeSlide();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void UpdateProbeLauncher(bool equipped)
|
|
|
|
|
{
|
|
|
|
|
ShipCockpitUI._displayProbeLauncherScreen = equipped;
|
|
|
|
|
ShipCockpitUI._shipAudioController.PlayProbeScreenMotor();
|
2022-04-16 21:31:01 +01:00
|
|
|
|
}
|
2022-04-16 10:08:59 +01:00
|
|
|
|
|
2022-04-16 21:31:01 +01:00
|
|
|
|
public void UpdateLandingCamera(bool on)
|
|
|
|
|
{
|
|
|
|
|
if (on)
|
2022-04-16 10:08:59 +01:00
|
|
|
|
{
|
2022-04-16 21:31:01 +01:00
|
|
|
|
EnterLandingView();
|
|
|
|
|
return;
|
2022-04-16 10:08:59 +01:00
|
|
|
|
}
|
2022-04-16 21:31:01 +01:00
|
|
|
|
|
|
|
|
|
ExitLandingView();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void EnterLandingView()
|
|
|
|
|
{
|
|
|
|
|
if (CockpitController._landingCam.mode == LandingCamera.Mode.Double)
|
|
|
|
|
{
|
|
|
|
|
CockpitController._landingCam.enabled = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (CockpitController._landingCamComponent.isDamaged)
|
|
|
|
|
{
|
|
|
|
|
CockpitController._shipAudioController.PlayLandingCamOn(AudioType.ShipCockpitLandingCamStatic_LP);
|
|
|
|
|
CockpitController._shipAudioController.PlayLandingCamStatic(0.25f);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CockpitController._shipAudioController.PlayLandingCamOn(AudioType.ShipCockpitLandingCamAmbient_LP);
|
|
|
|
|
CockpitController._shipAudioController.PlayLandingCamAmbient(0.25f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ExitLandingView()
|
|
|
|
|
{
|
|
|
|
|
CockpitController._landingCam.enabled = false;
|
|
|
|
|
CockpitController._shipAudioController.PlayLandingCamOff();
|
2022-04-16 10:08:59 +01:00
|
|
|
|
}
|
2022-08-04 22:21:46 -07:00
|
|
|
|
}
|