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

54 lines
1.4 KiB
C#
Raw Normal View History

2020-11-04 09:34:01 +00:00
using OWML.Common;
using QSB.Utility;
using QSB.WorldSync;
2020-08-21 14:04:13 +01: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-02 21:23:01 +00:00
private void Awake()
{
Instance = this;
QSBSceneManager.OnUniverseSceneLoaded += (OWScene scene) => RebuildSectors();
DebugLog.DebugWrite("Sector Manager ready.", MessageType.Success);
}
private void OnDestroy()
{
QSBSceneManager.OnUniverseSceneLoaded -= (OWScene scene) => RebuildSectors();
}
2020-12-02 21:23:01 +00:00
public void RebuildSectors()
{
DebugLog.DebugWrite("Rebuilding sectors...", MessageType.Warning);
WorldRegistry.RemoveObjects<QSBSector>();
var sectors = Resources.FindObjectsOfTypeAll<Sector>().ToList();
for (var id = 0; id < sectors.Count; id++)
{
var qsbSector = WorldRegistry.GetObject<QSBSector>(id) ?? new QSBSector();
qsbSector.Init(sectors[id], id);
WorldRegistry.AddObject(qsbSector);
}
IsReady = WorldRegistry.GetObjects<QSBSector>().Any();
}
2020-12-02 21:23:01 +00:00
public QSBSector GetClosestSector(Transform trans)
{
return WorldRegistry.GetObjects<QSBSector>()
.Where(sector => sector.Sector != null && !_sectorBlacklist.Contains(sector.Type))
.OrderBy(sector => Vector3.Distance(sector.Position, trans.position))
.First();
}
}
}