quantum-space-buddies/QSB/SectorSync/WorldObjects/QSBSector.cs
2021-06-18 22:38:32 +01:00

88 lines
2.2 KiB
C#

using OWML.Common;
using OWML.Utils;
using QSB.Utility;
using QSB.WorldSync;
using System.Linq;
using UnityEngine;
namespace QSB.SectorSync.WorldObjects
{
public class QSBSector : WorldObject<Sector>
{
public Sector.Name Type => AttachedObject.GetName();
public Transform Transform => AttachedObject.transform;
public Vector3 Position => Transform.position;
public bool IsFakeSector => AttachedObject.GetType() == typeof(FakeSector);
public override void Init(Sector sector, int id)
{
ObjectId = id;
AttachedObject = sector;
if (IsFakeSector)
{
QSBSectorManager.Instance.FakeSectors.Add(this);
}
}
public override void OnRemoval()
{
if (IsFakeSector)
{
QSBSectorManager.Instance.FakeSectors.Remove(this);
}
}
public bool ShouldSyncTo(TargetType targetType)
{
if (AttachedObject == null)
{
DebugLog.ToConsole($"Warning - AttachedObject for sector id:{ObjectId} is null!", MessageType.Warning);
return false;
}
if (!AttachedObject.gameObject.activeInHierarchy)
{
return false;
}
if (targetType == TargetType.Ship && Type == Sector.Name.Ship)
{
return false;
}
if (AttachedObject.name == "Sector_Shuttle" || AttachedObject.name == "Sector_NomaiShuttleInterior")
{
if (QSBSceneManager.CurrentScene == OWScene.SolarSystem)
{
var shuttleController = AttachedObject.gameObject.GetComponentInParent<NomaiShuttleController>();
if (shuttleController == null)
{
DebugLog.ToConsole($"Warning - Expected to find a NomaiShuttleController for {AttachedObject.name}!", MessageType.Warning);
return false;
}
if (!shuttleController.IsPlayerInside())
{
return false;
}
}
else if (QSBSceneManager.CurrentScene == OWScene.EyeOfTheUniverse)
{
var shuttleController = Resources.FindObjectsOfTypeAll<EyeShuttleController>().First();
if (shuttleController == null)
{
DebugLog.ToConsole($"Warning - Expected to find a EyeShuttleController for {AttachedObject.name}!", MessageType.Warning);
return false;
}
if (!shuttleController.GetValue<bool>("_isPlayerInside"))
{
return false;
}
}
}
return true;
}
}
}