quantum-space-buddies/QSB/ShipSync/ShipManager.cs

140 lines
3.9 KiB
C#
Raw Normal View History

2021-04-12 09:37:20 +00:00
using OWML.Common;
using OWML.Utils;
2021-06-19 12:22:05 +00:00
using QSB.Player;
using QSB.ShipSync.TransformSync;
using QSB.ShipSync.WorldObjects;
2021-04-12 09:37:20 +00:00
using QSB.Utility;
using QSB.WorldSync;
2021-04-16 10:40:13 +00:00
using QuantumUNET;
2021-06-19 12:22:05 +00:00
using System.Collections.Generic;
2021-04-12 09:37:20 +00:00
using System.Linq;
using UnityEngine;
namespace QSB.ShipSync
{
internal class ShipManager : WorldObjectManager
2021-04-12 09:37:20 +00:00
{
public override WorldObjectType WorldObjectType => WorldObjectType.SolarSystem;
2021-04-12 09:37:20 +00:00
public static ShipManager Instance;
2021-04-13 17:50:15 +00:00
public InteractZone HatchInteractZone;
public HatchController HatchController;
public ShipTractorBeamSwitch ShipTractorBeam;
public ShipCockpitController CockpitController;
public ShipElectricalComponent ShipElectricalComponent;
public bool HasAuthority
=> ShipTransformSync.LocalInstance.HasAuthority;
2021-04-12 09:37:20 +00: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 21:38:32 +00:00
2021-04-12 09:37:20 +00:00
_currentFlyer = value;
}
}
2021-04-13 16:25:00 +00:00
2021-11-20 19:49:50 +00:00
private List<PlayerInfo> _playersInShip = new();
2021-06-19 12:22:05 +00:00
private uint _currentFlyer = uint.MaxValue;
2021-04-13 16:25:00 +00:00
public void Start()
=> Instance = this;
2021-04-13 20:09:26 +00:00
protected override void RebuildWorldObjects(OWScene scene)
{
2021-04-13 20:09:26 +00:00
var shipTransform = GameObject.Find("Ship_Body");
if (shipTransform == null)
{
DebugLog.ToConsole($"Error - Couldn't find ship!", MessageType.Error);
return;
}
2021-04-13 20:09:26 +00:00
HatchController = shipTransform.GetComponentInChildren<HatchController>();
if (HatchController == null)
{
DebugLog.ToConsole($"Error - Couldn't find hatch controller!", MessageType.Error);
return;
}
2021-04-13 20:09:26 +00:00
HatchInteractZone = HatchController.GetComponent<InteractZone>();
ShipTractorBeam = QSBWorldSync.GetUnityObjects<ShipTractorBeamSwitch>().First();
CockpitController = QSBWorldSync.GetUnityObjects<ShipCockpitController>().First();
ShipElectricalComponent = QSBWorldSync.GetUnityObjects<ShipElectricalComponent>().First();
2021-04-13 16:25:00 +00:00
2021-04-13 17:50:15 +00:00
var sphereShape = HatchController.GetComponent<SphereShape>();
2021-04-13 16:25:00 +00:00
sphereShape.radius = 2.5f;
sphereShape.center = new Vector3(0, 0, 1);
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
{
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;
}
2021-08-22 16:17:46 +00:00
QNetworkServer.Destroy(ShipTransformSync.LocalInstance.gameObject);
}
2021-06-18 21:38:32 +00:00
if (QSBPlayerManager.LocalPlayer.TransformSync == null)
{
DebugLog.ToConsole($"Error - Tried to spawn ship, but LocalPlayer's TransformSync is null!", MessageType.Error);
}
2021-08-19 15:37:29 +00:00
Instantiate(QSBNetworkManager.Instance.ShipPrefab).SpawnWithServerAuthority();
2021-04-16 10:40:13 +00:00
}
2021-06-18 15:40:05 +00:00
QSBWorldSync.Init<QSBShipComponent, ShipComponent>();
QSBWorldSync.Init<QSBShipHull, ShipHull>();
2021-06-18 15:40:05 +00:00
}
2021-06-19 12:22:05 +00:00
public void AddPlayerToShip(PlayerInfo player)
{
DebugLog.DebugWrite($"{player.PlayerId} enter ship.");
2021-06-19 12:22:05 +00:00
_playersInShip.Add(player);
UpdateElectricalComponent();
2021-06-19 12:22:05 +00:00
}
public void RemovePlayerFromShip(PlayerInfo player)
{
DebugLog.DebugWrite($"{player.PlayerId} leave ship.");
2021-06-19 12:22:05 +00:00
_playersInShip.Remove(player);
UpdateElectricalComponent();
}
2021-08-19 22:15:20 +00:00
public bool IsPlayerInShip(PlayerInfo player)
=> _playersInShip.Contains(player);
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 12:22:05 +00:00
}
2021-04-12 09:37:20 +00:00
}
}