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

144 lines
4.0 KiB
C#
Raw Normal View History

2022-01-29 04:49:07 +00:00
using Cysharp.Threading.Tasks;
using Mirror;
using OWML.Common;
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-06-19 12:22:05 +00:00
using System.Collections.Generic;
2021-04-12 09:37:20 +00:00
using System.Linq;
2022-01-29 04:49:07 +00:00
using System.Threading;
2021-04-12 09:37:20 +00:00
using UnityEngine;
2022-03-03 03:46:33 +00:00
namespace QSB.ShipSync;
internal class ShipManager : WorldObjectManager
2021-04-12 09:37:20 +00:00
{
2022-03-03 03:46:33 +00:00
public override WorldObjectScene WorldObjectScene => WorldObjectScene.SolarSystem;
2022-03-03 03:46:33 +00:00
public static ShipManager Instance;
2021-04-12 09:37:20 +00:00
2022-03-03 03:46:33 +00:00
public InteractZone HatchInteractZone;
public HatchController HatchController;
public ShipTractorBeamSwitch ShipTractorBeam;
public ShipCockpitController CockpitController;
public ShipElectricalComponent ShipElectricalComponent;
private GameObject _shipCustomAttach;
public uint CurrentFlyer
{
get => _currentFlyer;
set
2021-04-12 09:37:20 +00:00
{
2022-03-03 03:46:33 +00:00
if (_currentFlyer != uint.MaxValue && value != uint.MaxValue)
2021-04-12 09:37:20 +00:00
{
2022-03-03 03:46:33 +00:00
DebugLog.ToConsole($"Warning - Trying to set current flyer while someone is still flying? Current:{_currentFlyer}, New:{value}", MessageType.Warning);
}
2022-03-03 03:46:33 +00:00
_currentFlyer = value;
2021-04-12 09:37:20 +00:00
}
2022-03-03 03:46:33 +00:00
}
2021-04-13 16:25:00 +00:00
2022-03-03 03:46:33 +00:00
private readonly List<PlayerInfo> _playersInShip = new();
2021-06-19 12:22:05 +00:00
2022-03-03 03:46:33 +00:00
private uint _currentFlyer = uint.MaxValue;
2021-04-13 16:25:00 +00:00
2022-03-03 03:46:33 +00:00
public void Start()
=> Instance = this;
2021-04-13 20:09:26 +00:00
2022-03-03 03:46:33 +00:00
public override async UniTask BuildWorldObjects(OWScene scene, CancellationToken ct)
{
var shipBody = Locator.GetShipBody();
if (shipBody == null)
{
2022-03-03 03:46:33 +00:00
DebugLog.ToConsole($"Error - Couldn't find ship!", MessageType.Error);
return;
}
2022-03-03 03:46:33 +00:00
HatchController = shipBody.GetComponentInChildren<HatchController>();
if (HatchController == null)
{
DebugLog.ToConsole($"Error - Couldn't find hatch controller!", MessageType.Error);
return;
}
2022-03-03 03:46:33 +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
2022-03-03 03:46:33 +00:00
var sphereShape = HatchController.GetComponent<SphereShape>();
sphereShape.radius = 2.5f;
sphereShape.center = new Vector3(0, 0, 1);
2021-04-16 10:40:13 +00:00
2022-03-03 03:46:33 +00:00
if (QSBCore.IsHost)
{
if (ShipTransformSync.LocalInstance != null)
2021-04-16 10:40:13 +00:00
{
2022-03-03 03:46:33 +00:00
if (ShipTransformSync.LocalInstance.gameObject == null)
{
2022-03-03 03:46:33 +00:00
DebugLog.ToConsole($"Warning - ShipTransformSync's LocalInstance is not null, but it's gameobject is null!", MessageType.Warning);
return;
}
2021-06-18 21:38:32 +00:00
2022-03-03 03:46:33 +00:00
NetworkServer.Destroy(ShipTransformSync.LocalInstance.gameObject);
}
2022-02-01 00:33:11 +00:00
2022-03-03 03:46:33 +00:00
if (QSBPlayerManager.LocalPlayer.TransformSync == null)
{
DebugLog.ToConsole($"Error - Tried to spawn ship, but LocalPlayer's TransformSync is null!", MessageType.Error);
}
2022-02-01 00:33:11 +00:00
2022-03-03 03:46:33 +00:00
Instantiate(QSBNetworkManager.singleton.ShipPrefab).SpawnWithServerAuthority();
}
2021-06-19 12:22:05 +00:00
2022-03-03 03:46:33 +00:00
QSBWorldSync.Init<QSBShipComponent, ShipComponent>();
QSBWorldSync.Init<QSBShipHull, ShipHull>();
2022-03-03 03:46:33 +00:00
_shipCustomAttach = new GameObject(nameof(ShipCustomAttach));
_shipCustomAttach.transform.SetParent(shipBody.transform, false);
_shipCustomAttach.AddComponent<ShipCustomAttach>();
}
2022-03-03 03:46:33 +00:00
public override void UnbuildWorldObjects() => Destroy(_shipCustomAttach);
2022-03-03 03:46:33 +00:00
public void AddPlayerToShip(PlayerInfo player)
{
2022-03-06 09:01:02 +00:00
DebugLog.DebugWrite($"{player} enter ship.");
2022-03-03 03:46:33 +00:00
_playersInShip.Add(player);
UpdateElectricalComponent();
}
2022-03-03 03:46:33 +00:00
public void RemovePlayerFromShip(PlayerInfo player)
{
2022-03-06 09:01:02 +00:00
DebugLog.DebugWrite($"{player} leave ship.");
2022-03-03 03:46:33 +00:00
_playersInShip.Remove(player);
UpdateElectricalComponent();
}
2022-03-03 03:46:33 +00: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)
{
2022-03-03 03:46:33 +00:00
DebugLog.DebugWrite($"No players left in ship - turning off electricals.");
electricalSystem.SetPowered(false);
}
2022-03-03 03:46:33 +00:00
}
else
{
if (!damaged)
{
2022-03-03 03:46:33 +00:00
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
}
}