2020-11-04 09:34:01 +00:00
|
|
|
|
using OWML.Common;
|
2020-12-31 12:10:55 +00:00
|
|
|
|
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 13:04:13 +00:00
|
|
|
|
using System.Linq;
|
2020-08-16 15:15:36 +00:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
2020-11-03 22:29:23 +00:00
|
|
|
|
namespace QSB.SectorSync
|
2020-08-16 15:15:36 +00:00
|
|
|
|
{
|
2020-12-02 21:23:01 +00:00
|
|
|
|
public class QSBSectorManager : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
public static QSBSectorManager Instance { get; private set; }
|
2020-08-16 15:15:36 +00:00
|
|
|
|
|
2020-12-02 21:23:01 +00:00
|
|
|
|
public bool IsReady { get; private set; }
|
2020-08-16 15:15:36 +00:00
|
|
|
|
|
2020-12-02 21:23:01 +00:00
|
|
|
|
private readonly Sector.Name[] _sectorBlacklist =
|
|
|
|
|
{
|
|
|
|
|
Sector.Name.Ship
|
|
|
|
|
};
|
2020-08-16 15:15:36 +00:00
|
|
|
|
|
2021-02-27 09:56:07 +00:00
|
|
|
|
private List<QSBSector> _sectorList = new List<QSBSector>();
|
|
|
|
|
|
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
|
|
|
|
|
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-08-16 15:15:36 +00:00
|
|
|
|
|
2020-12-02 21:23:01 +00:00
|
|
|
|
public void RebuildSectors()
|
|
|
|
|
{
|
|
|
|
|
DebugLog.DebugWrite("Rebuilding sectors...", MessageType.Warning);
|
2020-12-24 08:41:38 +00:00
|
|
|
|
QSBWorldSync.RemoveWorldObjects<QSBSector>();
|
2020-12-23 23:14:32 +00:00
|
|
|
|
QSBWorldSync.Init<QSBSector, Sector>();
|
2021-02-27 09:56:07 +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
|
|
|
|
}
|
2020-08-16 15:15:36 +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);
|
|
|
|
|
}
|
|
|
|
|
if (_sectorList.Contains(worldObject))
|
|
|
|
|
{
|
|
|
|
|
DebugLog.ToConsole($"Warning - Trying to add {sector.name}, but is already in list", MessageType.Warning);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_sectorList.Add(worldObject);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
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 09:56:07 +00:00
|
|
|
|
_sectorList.Remove(worldObject);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public QSBSector GetClosestSector(Transform trans) // trans rights \o/
|
|
|
|
|
{
|
2021-02-27 10:07:29 +00:00
|
|
|
|
var listToSearch = _sectorList.Count == 0 ? QSBWorldSync.GetWorldObjects<QSBSector>() : _sectorList;
|
|
|
|
|
return listToSearch
|
|
|
|
|
.Where(sector => sector.AttachedObject != null
|
|
|
|
|
&& !_sectorBlacklist.Contains(sector.Type))
|
|
|
|
|
.OrderBy(sector => Vector3.Distance(sector.Position, trans.position))
|
|
|
|
|
.First();
|
2020-12-02 21:23:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-03 08:28:05 +00:00
|
|
|
|
}
|