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

93 lines
2.8 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 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
};
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();
Locator.GetPlayerSectorDetector().OnEnterSector -= AddSector;
Locator.GetPlayerSectorDetector().OnExitSector -= RemoveSector;
}
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>();
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
Locator.GetPlayerSectorDetector().OnEnterSector += AddSector;
Locator.GetPlayerSectorDetector().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);
}
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/
{
// default to sync to the sun sector if the player isn't in any sector right now
return _sectorList.Count == 0
? QSBWorldSync.GetWorldObjects<QSBSector>().First(x => x.AttachedObject.GetName() == Sector.Name.Sun)
: _sectorList
.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
}