quantum-space-buddies/QSB/SectorSync/QSBSectorManager.cs

48 lines
1.3 KiB
C#
Raw Normal View History

2020-11-04 09:34:01 +00:00
using OWML.Common;
using QSB.SectorSync.WorldObjects;
2020-11-04 09:34:01 +00:00
using QSB.Utility;
using QSB.WorldSync;
2020-08-21 13:04:13 +00:00
using System.Linq;
using UnityEngine;
2020-11-03 22:29:23 +00:00
namespace QSB.SectorSync
{
2020-12-02 21:23:01 +00:00
public class QSBSectorManager : MonoBehaviour
{
public static QSBSectorManager Instance { get; private set; }
2020-12-02 21:23:01 +00:00
public bool IsReady { get; private set; }
2020-12-02 21:23:01 +00:00
private readonly Sector.Name[] _sectorBlacklist =
{
Sector.Name.Ship
};
2020-12-14 20:41:56 +00:00
public void Awake()
2020-12-02 21:23:01 +00:00
{
Instance = this;
QSBSceneManager.OnUniverseSceneLoaded += (OWScene scene) => RebuildSectors();
DebugLog.DebugWrite("Sector Manager ready.", MessageType.Success);
}
2020-12-03 08:28:05 +00:00
2020-12-18 20:28:22 +00:00
public void OnDestroy() => QSBSceneManager.OnUniverseSceneLoaded -= (OWScene scene) => RebuildSectors();
2020-12-02 21:23:01 +00:00
public void RebuildSectors()
{
DebugLog.DebugWrite("Rebuilding sectors...", MessageType.Warning);
QSBWorldSync.RemoveWorldObjects<QSBSector>();
2020-12-23 23:14:32 +00:00
QSBWorldSync.Init<QSBSector, Sector>();
2020-12-11 13:14:58 +00:00
IsReady = QSBWorldSync.GetWorldObjects<QSBSector>().Any();
2020-12-02 21:23:01 +00:00
}
2020-12-14 20:41:56 +00:00
public QSBSector GetClosestSector(Transform trans) // trans rights \o/
2020-12-14 21:20:53 +00:00
{
2020-12-11 13:14:58 +00:00
return QSBWorldSync.GetWorldObjects<QSBSector>()
2020-12-23 22:43:05 +00:00
.Where(sector => sector.AttachedObject != null
2020-12-20 09:45:50 +00:00
&& !_sectorBlacklist.Contains(sector.Type)
2020-12-20 09:42:57 +00:00
&& sector.Transform.gameObject.activeInHierarchy)
2020-12-02 21:23:01 +00:00
.OrderBy(sector => Vector3.Distance(sector.Position, trans.position))
.First();
}
}
2020-12-03 08:28:05 +00:00
}