116 lines
2.8 KiB
C#
Raw Normal View History

using OWML.Common;
2021-03-26 20:56:57 +00:00
using QSB.Utility;
2021-03-13 19:54:36 +00:00
using QSB.WorldSync;
2021-10-25 10:42:25 +01:00
using System;
2021-03-13 21:21:27 +00:00
using System.Linq;
using UnityEngine;
2022-03-02 19:46:33 -08:00
namespace QSB.SectorSync.WorldObjects;
public class QSBSector : WorldObject<Sector>
{
public Sector.Name Type => AttachedObject.GetName();
2022-03-02 19:46:33 -08:00
public Transform Transform
2020-12-02 21:23:01 +00:00
{
2022-03-02 19:46:33 -08:00
get
2021-10-25 10:42:25 +01:00
{
2022-03-02 19:46:33 -08:00
if (AttachedObject == null)
2021-10-25 10:42:25 +01:00
{
2022-03-02 19:46:33 -08:00
DebugLog.ToConsole($"Error - Tried to get Transform from QSBSector {ObjectId} with null AttachedObject!\r\n{Environment.StackTrace}", MessageType.Error);
return null;
}
2022-03-02 19:46:33 -08:00
return AttachedObject.transform;
2021-10-25 10:42:25 +01:00
}
2022-03-02 19:46:33 -08:00
}
2021-03-12 21:46:02 +00:00
2022-03-02 19:46:33 -08:00
public override void SendInitialState(uint to) { }
2022-03-02 19:46:33 -08:00
public bool ShouldSyncTo(DynamicOccupant occupantType)
{
if (occupantType == DynamicOccupant.Ship && Type == Sector.Name.Ship)
2022-03-02 19:46:33 -08:00
{
return false;
}
2021-03-31 15:43:55 +01:00
if (AttachedObject == null)
{
DebugLog.ToConsole($"Warning - AttachedObject for sector id:{ObjectId} is null!", MessageType.Warning);
return false;
}
if (!AttachedObject.gameObject.activeInHierarchy)
2022-03-02 19:46:33 -08:00
{
return false;
}
2021-04-20 08:36:07 +01:00
2022-03-02 19:46:33 -08:00
if (AttachedObject.name is "Sector_Shuttle" or "Sector_NomaiShuttleInterior")
{
if (QSBSceneManager.CurrentScene == OWScene.SolarSystem)
2021-03-12 21:46:02 +00:00
{
2022-03-02 19:46:33 -08:00
var shuttleController = AttachedObject.gameObject.GetComponentInParent<NomaiShuttleController>();
if (shuttleController == null)
2021-03-13 19:54:36 +00:00
{
2022-03-02 19:46:33 -08:00
DebugLog.ToConsole($"Warning - Expected to find a NomaiShuttleController for {AttachedObject.name}!", MessageType.Warning);
return false;
}
2021-06-18 22:38:32 +01:00
2022-03-02 19:46:33 -08:00
if (!shuttleController.IsPlayerInside())
{
return false;
2021-03-13 19:54:36 +00:00
}
2022-03-02 19:46:33 -08:00
}
else if (QSBSceneManager.CurrentScene == OWScene.EyeOfTheUniverse)
{
2022-04-01 19:08:03 +01:00
// TODO : Optimize this out.
2022-03-02 19:46:33 -08:00
var shuttleController = QSBWorldSync.GetUnityObjects<EyeShuttleController>().First();
if (shuttleController == null)
2021-03-13 19:54:36 +00:00
{
2022-03-02 19:46:33 -08:00
DebugLog.ToConsole($"Warning - Expected to find a EyeShuttleController for {AttachedObject.name}!", MessageType.Warning);
return false;
}
2021-06-18 22:38:32 +01:00
2022-03-02 19:46:33 -08:00
if (!shuttleController._isPlayerInside)
{
return false;
2021-03-13 19:54:36 +00:00
}
2021-03-12 21:46:02 +00:00
}
}
2022-03-02 19:46:33 -08:00
return true;
2020-12-02 21:23:01 +00:00
}
public float GetScore(OWRigidbody rigidbody)
{
var sqrDistance = (Transform.position - rigidbody.GetPosition()).sqrMagnitude;
var radius = GetRadius();
var sqrVelocity = GetSqrVelocity(rigidbody);
return sqrDistance + radius * radius + sqrVelocity;
}
private float GetRadius()
{
// TODO : make this work for other stuff, not just shaped triggervolumes
var trigger = AttachedObject.GetTriggerVolume();
if (trigger && trigger.GetShape())
{
return trigger.GetShape().CalcWorldBounds().radius;
}
return 0f;
}
private float GetSqrVelocity(OWRigidbody rigidbody)
{
var sectorRigidbody = AttachedObject.GetOWRigidbody();
if (sectorRigidbody && rigidbody)
{
var relativeVelocity = rigidbody.GetVelocity() - sectorRigidbody.GetPointVelocity(rigidbody.GetPosition());
return relativeVelocity.sqrMagnitude;
}
return 0;
}
}