quantum-space-buddies/QSB/SectorSync/WorldObjects/QSBSector.cs

103 lines
2.4 KiB
C#
Raw Normal View History

2022-01-29 04:49:07 +00:00
using Cysharp.Threading.Tasks;
using OWML.Common;
2021-03-26 20:56:57 +00:00
using QSB.Utility;
2021-03-13 19:54:36 +00:00
using QSB.WorldSync;
2021-10-25 09:42:25 +00:00
using System;
2021-03-13 21:21:27 +00:00
using System.Linq;
2022-01-29 04:49:07 +00:00
using System.Threading;
using UnityEngine;
2022-03-03 03:46:33 +00:00
namespace QSB.SectorSync.WorldObjects;
public class QSBSector : WorldObject<Sector>
{
2022-03-03 03:46:33 +00:00
public Sector.Name Type => AttachedObject.GetName();
public Transform Transform
2020-12-02 21:23:01 +00:00
{
2022-03-03 03:46:33 +00:00
get
2021-10-25 09:42:25 +00:00
{
2022-03-03 03:46:33 +00:00
if (AttachedObject == null)
2021-10-25 09:42:25 +00:00
{
2022-03-03 03:46:33 +00:00
DebugLog.ToConsole($"Error - Tried to get Transform from QSBSector {ObjectId} with null AttachedObject!\r\n{Environment.StackTrace}", MessageType.Error);
return null;
}
2022-03-03 03:46:33 +00:00
return AttachedObject.transform;
2021-10-25 09:42:25 +00:00
}
2022-03-03 03:46:33 +00:00
}
public Vector3 Position => Transform.position;
2022-02-16 05:54:12 +00:00
2022-03-03 03:46:33 +00:00
public bool IsFakeSector => AttachedObject is FakeSector;
public FakeSector FakeSector => (FakeSector)AttachedObject;
2020-08-20 18:31:10 +00:00
2022-03-03 03:46:33 +00:00
public override async UniTask Init(CancellationToken ct)
{
if (IsFakeSector)
2020-12-02 21:23:01 +00:00
{
2022-03-03 03:46:33 +00:00
QSBSectorManager.Instance.FakeSectors.Add(this);
2021-03-11 20:02:23 +00:00
}
2022-03-03 03:46:33 +00:00
}
2021-03-11 20:02:23 +00:00
2022-03-03 03:46:33 +00:00
public override void OnRemoval()
{
if (IsFakeSector)
2021-03-11 20:02:23 +00:00
{
2022-03-03 03:46:33 +00:00
QSBSectorManager.Instance.FakeSectors.Remove(this);
2020-12-02 21:23:01 +00:00
}
2022-03-03 03:46:33 +00:00
}
2021-03-12 21:46:02 +00:00
2022-03-03 03:46:33 +00:00
public override void SendInitialState(uint to) { }
2022-03-03 03:46:33 +00:00
public bool ShouldSyncTo(DynamicOccupant occupantType)
{
if (AttachedObject == null)
2021-03-12 21:46:02 +00:00
{
2022-03-03 03:46:33 +00:00
DebugLog.ToConsole($"Warning - AttachedObject for sector id:{ObjectId} is null!", MessageType.Warning);
return false;
}
2021-03-31 14:16:03 +00:00
2022-03-03 03:46:33 +00:00
if (!AttachedObject.gameObject.activeInHierarchy)
{
return false;
}
2021-03-31 14:43:55 +00:00
2022-03-03 03:46:33 +00:00
if (occupantType == DynamicOccupant.Ship && Type == Sector.Name.Ship)
{
return false;
}
2021-04-20 07:36:07 +00:00
2022-03-03 03:46:33 +00:00
if (AttachedObject.name is "Sector_Shuttle" or "Sector_NomaiShuttleInterior")
{
if (QSBSceneManager.CurrentScene == OWScene.SolarSystem)
2021-03-12 21:46:02 +00:00
{
2022-03-03 03:46:33 +00:00
var shuttleController = AttachedObject.gameObject.GetComponentInParent<NomaiShuttleController>();
if (shuttleController == null)
2021-03-13 19:54:36 +00:00
{
2022-03-03 03:46:33 +00:00
DebugLog.ToConsole($"Warning - Expected to find a NomaiShuttleController for {AttachedObject.name}!", MessageType.Warning);
return false;
}
2021-06-18 21:38:32 +00:00
2022-03-03 03:46:33 +00:00
if (!shuttleController.IsPlayerInside())
{
return false;
2021-03-13 19:54:36 +00:00
}
2022-03-03 03:46:33 +00:00
}
else if (QSBSceneManager.CurrentScene == OWScene.EyeOfTheUniverse)
{
var shuttleController = QSBWorldSync.GetUnityObjects<EyeShuttleController>().First();
if (shuttleController == null)
2021-03-13 19:54:36 +00:00
{
2022-03-03 03:46:33 +00:00
DebugLog.ToConsole($"Warning - Expected to find a EyeShuttleController for {AttachedObject.name}!", MessageType.Warning);
return false;
}
2021-06-18 21:38:32 +00:00
2022-03-03 03:46:33 +00:00
if (!shuttleController._isPlayerInside)
{
return false;
2021-03-13 19:54:36 +00:00
}
2021-03-12 21:46:02 +00:00
}
}
2022-03-03 03:46:33 +00:00
return true;
2020-12-02 21:23:01 +00:00
}
}