2022-03-20 00:13:40 -07:00
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 ;
2020-08-16 17:15:36 +02:00
using UnityEngine ;
2022-03-02 19:46:33 -08:00
namespace QSB.SectorSync.WorldObjects ;
public class QSBSector : WorldObject < Sector >
2020-08-16 17:15:36 +02:00
{
2022-03-20 02:36:14 -07:00
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-02-27 04:40:44 -08:00
}
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-01-25 21:08:04 -08:00
2022-03-02 19:46:33 -08:00
public bool ShouldSyncTo ( DynamicOccupant occupantType )
{
2022-03-20 00:02:48 -07:00
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
2022-03-20 00:02:48 -07: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-02-27 04:40:44 -08:00
}
2022-03-02 19:46:33 -08:00
return true ;
2020-12-02 21:23:01 +00:00
}
2022-03-19 21:44:44 -07:00
2022-03-20 00:13:40 -07:00
public float GetScore ( OWRigidbody rigidbody )
2022-03-19 21:44:44 -07:00
{
2022-03-20 00:13:40 -07:00
var sqrDistance = ( Transform . position - rigidbody . GetPosition ( ) ) . sqrMagnitude ;
2022-03-19 21:44:44 -07:00
var radius = GetRadius ( ) ;
2022-03-20 00:13:40 -07:00
var sqrVelocity = GetSqrVelocity ( rigidbody ) ;
2022-03-19 21:44:44 -07:00
2022-03-20 00:13:40 -07:00
return sqrDistance + radius * radius + sqrVelocity ;
2022-03-19 21:44:44 -07:00
}
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 ;
}
2022-03-20 00:13:40 -07:00
private float GetSqrVelocity ( OWRigidbody rigidbody )
2022-03-19 21:44:44 -07:00
{
var sectorRigidbody = AttachedObject . GetOWRigidbody ( ) ;
if ( sectorRigidbody & & rigidbody )
{
var relativeVelocity = rigidbody . GetVelocity ( ) - sectorRigidbody . GetPointVelocity ( rigidbody . GetPosition ( ) ) ;
return relativeVelocity . sqrMagnitude ;
}
return 0 ;
}
}