2021-04-12 10:37:20 +01:00
|
|
|
|
using OWML.Common;
|
2021-06-19 15:35:40 +01:00
|
|
|
|
using OWML.Utils;
|
2021-06-19 13:22:05 +01:00
|
|
|
|
using QSB.Player;
|
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-04-16 11:40:13 +01:00
|
|
|
|
using QuantumUNET;
|
2021-06-19 13:22:05 +01:00
|
|
|
|
using System.Collections.Generic;
|
2021-04-12 10:37:20 +01:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
|
|
namespace QSB.ShipSync
|
|
|
|
|
{
|
2021-06-15 18:56:51 +01:00
|
|
|
|
internal class ShipManager : WorldObjectManager
|
2021-04-12 10:37:20 +01:00
|
|
|
|
{
|
|
|
|
|
public static ShipManager Instance;
|
|
|
|
|
|
2021-04-13 18:50:15 +01:00
|
|
|
|
public InteractZone HatchInteractZone;
|
|
|
|
|
public HatchController HatchController;
|
|
|
|
|
public ShipTractorBeamSwitch ShipTractorBeam;
|
2021-06-15 18:56:51 +01:00
|
|
|
|
public ShipCockpitController CockpitController;
|
2021-06-19 15:35:40 +01:00
|
|
|
|
public ShipElectricalComponent ShipElectricalComponent;
|
2021-06-15 18:56:51 +01:00
|
|
|
|
public bool HasAuthority
|
|
|
|
|
=> ShipTransformSync.LocalInstance.HasAuthority;
|
2021-04-12 10:37:20 +01:00
|
|
|
|
public uint CurrentFlyer
|
|
|
|
|
{
|
|
|
|
|
get => _currentFlyer;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (_currentFlyer != uint.MaxValue && value != uint.MaxValue)
|
|
|
|
|
{
|
|
|
|
|
DebugLog.ToConsole($"Warning - Trying to set current flyer while someone is still flying? Current:{_currentFlyer}, New:{value}", MessageType.Warning);
|
|
|
|
|
}
|
2021-06-18 22:38:32 +01:00
|
|
|
|
|
2021-04-12 10:37:20 +01:00
|
|
|
|
_currentFlyer = value;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-13 17:25:00 +01:00
|
|
|
|
|
2021-06-19 13:22:05 +01:00
|
|
|
|
private List<PlayerInfo> _playersInShip = new List<PlayerInfo>();
|
|
|
|
|
|
2021-06-15 18:56:51 +01:00
|
|
|
|
private uint _currentFlyer = uint.MaxValue;
|
2021-04-13 17:25:00 +01:00
|
|
|
|
|
2021-06-15 18:56:51 +01:00
|
|
|
|
public void Start()
|
|
|
|
|
=> Instance = this;
|
2021-04-13 21:09:26 +01:00
|
|
|
|
|
2021-06-15 18:56:51 +01:00
|
|
|
|
protected override void RebuildWorldObjects(OWScene scene)
|
|
|
|
|
{
|
2021-04-13 21:09:26 +01:00
|
|
|
|
var shipTransform = GameObject.Find("Ship_Body");
|
2021-07-16 14:06:34 +01:00
|
|
|
|
if (shipTransform == null)
|
|
|
|
|
{
|
|
|
|
|
DebugLog.ToConsole($"Error - Couldn't find ship!", MessageType.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 21:09:26 +01:00
|
|
|
|
HatchController = shipTransform.GetComponentInChildren<HatchController>();
|
2021-07-16 14:06:34 +01:00
|
|
|
|
if (HatchController == null)
|
|
|
|
|
{
|
|
|
|
|
DebugLog.ToConsole($"Error - Couldn't find hatch controller!", MessageType.Error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 21:09:26 +01:00
|
|
|
|
HatchInteractZone = HatchController.GetComponent<InteractZone>();
|
|
|
|
|
ShipTractorBeam = Resources.FindObjectsOfTypeAll<ShipTractorBeamSwitch>().First();
|
2021-06-15 18:56:51 +01:00
|
|
|
|
CockpitController = Resources.FindObjectsOfTypeAll<ShipCockpitController>().First();
|
2021-06-19 15:35:40 +01:00
|
|
|
|
ShipElectricalComponent = Resources.FindObjectsOfTypeAll<ShipElectricalComponent>().First();
|
2021-04-13 17:25:00 +01:00
|
|
|
|
|
2021-04-13 18:50:15 +01:00
|
|
|
|
var sphereShape = HatchController.GetComponent<SphereShape>();
|
2021-04-13 17:25:00 +01:00
|
|
|
|
sphereShape.radius = 2.5f;
|
|
|
|
|
sphereShape.center = new Vector3(0, 0, 1);
|
2021-04-16 11:40:13 +01:00
|
|
|
|
|
2021-08-08 19:53:55 +01:00
|
|
|
|
if (QSBCore.IsHost)
|
2021-04-16 11:40:13 +01:00
|
|
|
|
{
|
2021-06-15 18:56:51 +01:00
|
|
|
|
if (ShipTransformSync.LocalInstance != null)
|
|
|
|
|
{
|
2021-07-16 14:06:34 +01:00
|
|
|
|
if (ShipTransformSync.LocalInstance.gameObject == null)
|
|
|
|
|
{
|
|
|
|
|
DebugLog.ToConsole($"Warning - ShipTransformSync's LocalInstance is not null, but it's gameobject is null!", MessageType.Warning);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-06-15 18:56:51 +01:00
|
|
|
|
QNetworkServer.Destroy(ShipTransformSync.LocalInstance.gameObject);
|
|
|
|
|
}
|
2021-06-18 22:38:32 +01:00
|
|
|
|
|
2021-07-16 14:06:34 +01:00
|
|
|
|
if (QSBPlayerManager.LocalPlayer.TransformSync == null)
|
|
|
|
|
{
|
|
|
|
|
DebugLog.ToConsole($"Error - Tried to spawn ship, but LocalPlayer's TransformSync is null!", MessageType.Error);
|
|
|
|
|
}
|
2021-08-19 16:37:29 +01:00
|
|
|
|
|
|
|
|
|
Instantiate(QSBNetworkManager.Instance.ShipPrefab).SpawnWithServerAuthority();
|
2021-04-16 11:40:13 +01:00
|
|
|
|
}
|
2021-06-18 16:40:05 +01:00
|
|
|
|
|
2021-06-20 09:48:37 +01:00
|
|
|
|
QSBWorldSync.Init<QSBShipComponent, ShipComponent>();
|
|
|
|
|
QSBWorldSync.Init<QSBShipHull, ShipHull>();
|
2021-06-18 16:40:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-19 13:22:05 +01:00
|
|
|
|
public void AddPlayerToShip(PlayerInfo player)
|
|
|
|
|
{
|
2021-06-19 15:35:40 +01:00
|
|
|
|
DebugLog.DebugWrite($"{player.PlayerId} enter ship.");
|
2021-06-19 13:22:05 +01:00
|
|
|
|
_playersInShip.Add(player);
|
2021-06-19 15:35:40 +01:00
|
|
|
|
UpdateElectricalComponent();
|
2021-06-19 13:22:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RemovePlayerFromShip(PlayerInfo player)
|
|
|
|
|
{
|
2021-06-19 15:35:40 +01:00
|
|
|
|
DebugLog.DebugWrite($"{player.PlayerId} leave ship.");
|
2021-06-19 13:22:05 +01:00
|
|
|
|
_playersInShip.Remove(player);
|
2021-06-19 15:35:40 +01:00
|
|
|
|
UpdateElectricalComponent();
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 23:15:20 +01:00
|
|
|
|
public bool IsPlayerInShip(PlayerInfo player)
|
|
|
|
|
=> _playersInShip.Contains(player);
|
|
|
|
|
|
2021-06-19 15:35:40 +01:00
|
|
|
|
private void UpdateElectricalComponent()
|
|
|
|
|
{
|
|
|
|
|
var electricalSystem = ShipElectricalComponent.GetValue<ElectricalSystem>("_electricalSystem");
|
|
|
|
|
var damaged = ShipElectricalComponent.GetValue<bool>("_damaged");
|
|
|
|
|
|
|
|
|
|
if (_playersInShip.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
if (!damaged)
|
|
|
|
|
{
|
|
|
|
|
DebugLog.DebugWrite($"No players left in ship - turning off electricals.");
|
|
|
|
|
electricalSystem.SetPowered(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (!damaged)
|
|
|
|
|
{
|
|
|
|
|
DebugLog.DebugWrite($"Player in ship - turning on electricals.");
|
|
|
|
|
electricalSystem.SetPowered(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-19 13:22:05 +01:00
|
|
|
|
}
|
2021-04-12 10:37:20 +01:00
|
|
|
|
}
|
|
|
|
|
}
|