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

98 lines
2.9 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;
2021-02-27 09:56:07 +00:00
using System.Collections.Generic;
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; }
2021-02-27 10:30:15 +00:00
public List<QSBSector> SectorList = new List<QSBSector>();
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 21:41:56 +01: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
2021-02-27 09:56:07 +00:00
public void OnDestroy()
{
QSBSceneManager.OnUniverseSceneLoaded -= (OWScene scene) => RebuildSectors();
2021-02-27 10:07:29 +00:00
FindObjectOfType<PlayerSectorDetector>().OnEnterSector -= AddSector;
FindObjectOfType<PlayerSectorDetector>().OnExitSector -= RemoveSector;
2021-02-27 09:56:07 +00:00
}
2020-12-02 21:23:01 +00:00
public void RebuildSectors()
{
DebugLog.DebugWrite("Rebuilding sectors...", MessageType.Warning);
2020-12-23 23:14:32 +00:00
QSBWorldSync.Init<QSBSector, Sector>();
2021-02-27 10:30:15 +00:00
SectorList.Clear();
2020-12-11 13:14:58 +00:00
IsReady = QSBWorldSync.GetWorldObjects<QSBSector>().Any();
2021-02-27 09:56:07 +00:00
2021-02-27 10:07:29 +00:00
FindObjectOfType<PlayerSectorDetector>().OnEnterSector += AddSector;
FindObjectOfType<PlayerSectorDetector>().OnExitSector += RemoveSector;
2020-12-02 21:23:01 +00:00
}
2021-02-27 09:56:07 +00:00
private void AddSector(Sector sector)
{
var worldObject = QSBWorldSync.GetWorldFromUnity<QSBSector, Sector>(sector);
if (worldObject == null)
{
DebugLog.ToConsole($"Error - Can't find QSBSector for sector {sector.name}!", MessageType.Error);
}
2021-02-27 10:30:15 +00:00
if (SectorList.Contains(worldObject))
2021-02-27 09:56:07 +00:00
{
DebugLog.ToConsole($"Warning - Trying to add {sector.name}, but is already in list", MessageType.Warning);
return;
}
2021-02-27 10:30:15 +00:00
SectorList.Add(worldObject);
2021-02-27 09:56:07 +00:00
}
private void RemoveSector(Sector sector)
2020-12-14 21:20:53 +00:00
{
2021-02-27 09:56:07 +00:00
var worldObject = QSBWorldSync.GetWorldFromUnity<QSBSector, Sector>(sector);
if (worldObject == null)
{
DebugLog.ToConsole($"Error - Can't find QSBSector for sector {sector.name}!", MessageType.Error);
return;
}
2021-02-27 10:30:15 +00:00
if (!SectorList.Contains(worldObject))
2021-01-26 14:07:17 +00:00
{
2021-02-27 09:56:07 +00:00
DebugLog.ToConsole($"Warning - Trying to remove {sector.name}, but is not in list!", MessageType.Warning);
return;
2021-01-26 14:07:17 +00:00
}
2021-02-27 10:30:15 +00:00
SectorList.Remove(worldObject);
2021-02-27 09:56:07 +00:00
}
public QSBSector GetClosestSector(Transform trans) // trans rights \o/
{
2021-02-27 10:30:15 +00:00
if (!IsReady)
{
DebugLog.ToConsole($"Warning - Tried to get closest sector to {trans.name} before SectorManager was ready.", MessageType.Warning);
return null;
}
2021-02-27 14:25:54 +00:00
var listToCheck = SectorList.Count == 0
? QSBWorldSync.GetWorldObjects<QSBSector>()
2021-02-27 12:25:06 +00:00
: SectorList;
return listToCheck
.Where(sector => sector.AttachedObject != null
&& !_sectorBlacklist.Contains(sector.Type)
&& sector.Transform.gameObject.activeInHierarchy)
.OrderBy(sector => Vector3.Distance(sector.Position, trans.position))
2021-02-28 15:35:01 +00:00
.FirstOrDefault();
2020-12-02 21:23:01 +00:00
}
}
2020-12-03 08:28:05 +00:00
}