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

119 lines
2.5 KiB
C#
Raw Normal View History

2021-03-09 19:45:00 +00:00
using OWML.Common;
using QSB.SectorSync.WorldObjects;
2021-02-19 12:42:37 +00:00
using QSB.Utility;
2021-03-09 19:45:00 +00:00
using QSB.WorldSync;
using System.Collections.Generic;
2020-08-22 16:08:56 +00:00
using System.Linq;
2020-08-21 13:04:13 +00:00
using UnityEngine;
2020-02-13 19:23:26 +00:00
2022-03-03 03:46:33 +00:00
namespace QSB.SectorSync;
public class QSBSectorDetector : MonoBehaviour
2020-02-15 19:48:02 +00:00
{
2022-03-03 03:46:33 +00:00
public readonly List<QSBSector> SectorList = new();
2021-02-19 12:54:48 +00:00
2022-03-03 03:46:33 +00:00
private SectorDetector _sectorDetector;
2021-03-09 19:45:00 +00:00
2022-03-03 03:46:33 +00:00
public void Init(SectorDetector detector)
{
if (_sectorDetector)
2020-12-02 21:23:01 +00:00
{
2022-03-03 03:46:33 +00:00
return;
2020-12-02 21:23:01 +00:00
}
2020-02-15 19:48:02 +00:00
2022-03-03 03:46:33 +00:00
if (!detector)
{
DebugLog.ToConsole($"Error - Trying to init QSBSectorDetector {name} with null SectorDetector!", MessageType.Error);
2022-03-03 03:46:33 +00:00
return;
}
_sectorDetector = detector;
_sectorDetector.OnEnterSector += AddSector;
_sectorDetector.OnExitSector += RemoveSector;
2022-03-03 03:46:33 +00:00
_sectorDetector._sectorList.ForEach(AddSector);
}
2022-03-03 03:46:33 +00:00
public void Uninit()
{
if (!_sectorDetector)
{
return;
}
2022-03-03 03:46:33 +00:00
_sectorDetector.OnEnterSector -= AddSector;
_sectorDetector.OnExitSector -= RemoveSector;
_sectorDetector = null;
SectorList.Clear();
}
private void AddSector(Sector sector)
{
if (!sector)
2020-12-02 21:23:01 +00:00
{
2022-03-03 03:46:33 +00:00
// wtf
DebugLog.ToConsole($"Warning - Trying to add null sector for QSBSectorDetector {name}", MessageType.Error);
2022-03-03 03:46:33 +00:00
return;
2021-03-09 19:45:00 +00:00
}
2022-03-03 03:46:33 +00:00
var worldObject = sector.GetWorldObject<QSBSector>();
if (worldObject == null)
2021-03-09 19:45:00 +00:00
{
2022-03-03 03:46:33 +00:00
DebugLog.ToConsole($"Error - Can't find QSBSector for sector {sector.name}!", MessageType.Error);
return;
2020-12-02 21:23:01 +00:00
}
2020-08-05 19:59:50 +00:00
SectorList.SafeAdd(worldObject);
2022-03-03 03:46:33 +00:00
}
2021-03-09 21:56:20 +00:00
2022-03-03 03:46:33 +00:00
private void RemoveSector(Sector sector)
{
if (!sector)
{
// wtf
DebugLog.ToConsole($"Warning - Trying to remove null sector for QSBSectorDetector {name}", MessageType.Error);
2022-03-03 03:46:33 +00:00
return;
}
2021-12-18 10:33:18 +00:00
2022-03-03 03:46:33 +00:00
var worldObject = sector.GetWorldObject<QSBSector>();
if (worldObject == null)
{
DebugLog.ToConsole($"Error - Can't find QSBSector for sector {sector.name}!", MessageType.Error);
return;
}
2021-06-18 21:38:32 +00:00
SectorList.QuickRemove(worldObject);
2022-03-03 03:46:33 +00:00
}
2021-12-18 10:33:18 +00:00
2022-03-03 03:46:33 +00:00
/// <summary>
/// called only by the sector manager
/// </summary>
public QSBSector GetClosestSector()
{
var type = _sectorDetector._occupantType;
2021-12-18 20:18:41 +00:00
var validSectors = SectorList
.Where(x => x.ShouldSyncTo(type))
.ToList();
2021-12-18 10:33:18 +00:00
if (validSectors.Count == 0)
2022-03-03 03:46:33 +00:00
{
validSectors = QSBWorldSync.GetWorldObjects<QSBSector>()
.Where(x =>
// we only wanna sync to the major ones when far away
x.Type != Sector.Name.Unnamed &&
x.ShouldSyncTo(type))
2022-03-03 03:46:33 +00:00
.ToList();
}
2021-03-13 10:19:02 +00:00
2022-03-03 03:46:33 +00:00
if (validSectors.Count == 0)
{
return null;
2021-03-09 21:56:20 +00:00
}
return validSectors
2022-03-20 07:50:17 +00:00
.MinBy(x => x.GetScore(_sectorDetector._attachedRigidbody));
2022-03-03 03:46:33 +00:00
}
}