2026-07-12 01:15:51 +01:00
using System ;
using System.Collections.Generic ;
2026-07-15 04:18:13 +01:00
using System.Linq ;
2026-07-12 01:15:51 +01:00
using UnityEngine ;
#nullable enable
namespace GameCult.Eve.UnityScene
{
public sealed class EveUnityShotTrajectoryRenderer : MonoBehaviour
{
[SerializeField] private float width = 0.2f ;
[SerializeField] private float minimumDurationSeconds = 0.03f ;
[SerializeField] private float lingerSeconds = 0.08f ;
[SerializeField] private Color hitColor = new Color ( 1f , 0.35f , 0.12f , 1f ) ;
[SerializeField] private Color missColor = new Color ( 0.35f , 0.8f , 1f , 0.8f ) ;
private readonly List < ActiveTrajectory > _active = new List < ActiveTrajectory > ( ) ;
private EveUnityPlayableWorldClientHost ? _host ;
2026-07-12 01:24:45 +01:00
private IEveUnityGameObjectAssetProvider ? _assetProvider ;
2026-07-12 01:15:51 +01:00
private Material ? _material ;
public int ActiveTrajectoryCount = > _active . Count ;
2026-07-15 04:18:13 +01:00
public int ActiveFallbackTrajectoryCount = > _active . Count ( trajectory = > trajectory . Line ! = null ) ;
2026-07-12 01:15:51 +01:00
2026-07-12 01:24:45 +01:00
public void Bind ( EveUnityPlayableWorldClientHost host , IEveUnityGameObjectAssetProvider ? assetProvider = null )
2026-07-12 01:15:51 +01:00
{
if ( host = = null ) throw new ArgumentNullException ( nameof ( host ) ) ;
2026-07-12 01:24:45 +01:00
if ( ReferenceEquals ( _host , host ) & & ReferenceEquals ( _assetProvider , assetProvider ) ) return ;
2026-07-12 01:15:51 +01:00
Unbind ( ) ;
_host = host ;
2026-07-12 01:24:45 +01:00
_assetProvider = assetProvider ;
2026-07-12 01:15:51 +01:00
_host . ShotAvailable + = Present ;
}
public void Unbind ( )
{
if ( _host ! = null ) _host . ShotAvailable - = Present ;
_host = null ;
2026-07-12 01:24:45 +01:00
_assetProvider = null ;
2026-07-12 01:15:51 +01:00
}
public void Present ( EveUnityShotReceipt receipt )
{
if ( receipt = = null ) throw new ArgumentNullException ( nameof ( receipt ) ) ;
var shot = new GameObject ( "Eve shot " + receipt . ShotId ) ;
shot . transform . SetParent ( transform , false ) ;
var origin = Vector ( receipt . Origin ) ;
2026-07-12 01:20:05 +01:00
var endpoint = Vector ( receipt . Endpoint ) ;
2026-07-12 01:24:45 +01:00
var effectPrefab = ResolveEffectPrefab ( receipt ) ;
2026-07-15 04:18:13 +01:00
LineRenderer ? line = null ;
2026-07-12 01:24:45 +01:00
if ( effectPrefab ! = null )
{
var effect = Instantiate ( effectPrefab , shot . transform ) ;
effect . transform . position = origin ;
var direction = endpoint - origin ;
if ( direction . sqrMagnitude > 0.0001f ) effect . transform . rotation = Quaternion . LookRotation ( direction . normalized ) ;
2026-07-12 01:27:17 +01:00
var lightning = effect . GetComponentInChildren < LightningCompute > ( ) ;
if ( lightning ! = null )
{
lightning . StartPosition = origin ;
lightning . EndPosition = endpoint ;
lightning . Animate = true ;
lightning . StartAnimation ( ) ;
}
2026-07-12 01:24:45 +01:00
}
2026-07-15 04:18:13 +01:00
else
{
line = shot . AddComponent < LineRenderer > ( ) ;
line . useWorldSpace = true ;
line . positionCount = 2 ;
var intensityScale = Mathf . Clamp ( Mathf . Sqrt ( ( float ) receipt . PresentationIntensity ) , 0.5f , 4f ) ;
line . startWidth = Math . Max ( 0.01f , width * intensityScale ) ;
line . endWidth = Math . Max ( 0.01f , width * 0.5f * intensityScale ) ;
line . material = SharedMaterial ( ) ;
line . startColor = line . endColor = receipt . Hit ? hitColor : missColor ;
line . SetPosition ( 0 , origin ) ;
}
2026-07-12 01:24:45 +01:00
var impactPrefab = ResolveRolePrefab ( "effect.impact." + receipt . ImpactKind ) ;
2026-07-12 01:20:05 +01:00
var travels = string . Equals ( receipt . PresentationKind , "bolt" , StringComparison . Ordinal ) | |
string . Equals ( receipt . PresentationKind , "guided" , StringComparison . Ordinal ) ;
2026-07-15 04:18:13 +01:00
if ( line ! = null )
line . SetPosition ( 1 , travels ? origin : endpoint ) ;
2026-07-12 01:15:51 +01:00
_active . Add ( new ActiveTrajectory (
shot ,
line ,
origin ,
2026-07-12 01:20:05 +01:00
endpoint ,
2026-07-12 01:15:51 +01:00
Time . unscaledTime ,
2026-07-12 01:20:05 +01:00
Math . Max ( minimumDurationSeconds , ( float ) receipt . DurationSeconds ) ,
2026-07-12 01:24:45 +01:00
travels ,
impactPrefab ) ) ;
2026-07-12 01:15:51 +01:00
}
private void Update ( )
{
var now = Time . unscaledTime ;
for ( var index = _active . Count - 1 ; index > = 0 ; index - - )
{
var trajectory = _active [ index ] ;
var progress = Mathf . Clamp01 ( ( now - trajectory . StartedAt ) / trajectory . Duration ) ;
2026-07-15 04:18:13 +01:00
if ( trajectory . Travels & & trajectory . Line ! = null )
2026-07-12 01:20:05 +01:00
trajectory . Line . SetPosition ( 1 , Vector3 . Lerp ( trajectory . Origin , trajectory . Endpoint , progress ) ) ;
2026-07-12 01:24:45 +01:00
if ( progress > = 1 & & ! trajectory . ImpactPresented )
{
trajectory . ImpactPresented = true ;
if ( trajectory . ImpactPrefab ! = null )
{
2026-07-15 04:30:21 +01:00
var impact = Instantiate ( trajectory . ImpactPrefab , trajectory . Root . transform ) ;
2026-07-12 01:24:45 +01:00
impact . transform . position = trajectory . Endpoint ;
}
}
2026-07-12 01:15:51 +01:00
if ( progress < 1 | | now < trajectory . StartedAt + trajectory . Duration + lingerSeconds ) continue ;
Destroy ( trajectory . Root ) ;
_active . RemoveAt ( index ) ;
}
}
private Material SharedMaterial ( )
{
if ( _material ! = null ) return _material ;
var shader = Shader . Find ( "Sprites/Default" ) ? ? Shader . Find ( "Universal Render Pipeline/Unlit" ) ;
if ( shader = = null ) throw new InvalidOperationException ( "EveUnity requires an unlit shader for shot trajectories." ) ;
_material = new Material ( shader ) { name = "EveUnity shot trajectory" } ;
return _material ;
}
2026-07-12 01:24:45 +01:00
private GameObject ? ResolveEffectPrefab ( EveUnityShotReceipt receipt )
{
if ( ! string . IsNullOrWhiteSpace ( receipt . ItemKey ) )
{
var itemPrefab = ResolveRolePrefab ( "effect.shot.item." + receipt . ItemKey ) ;
if ( itemPrefab ! = null ) return itemPrefab ;
}
return ResolveRolePrefab ( "effect.shot." + receipt . PresentationKind ) ;
}
private GameObject ? ResolveRolePrefab ( string role ) = > _assetProvider ? . ResolvePrefab (
new EveUnityPlayableWorldAssetBinding ( "" , role , "provider-asset-ref" ) ) ;
2026-07-12 01:15:51 +01:00
private void OnDestroy ( )
{
Unbind ( ) ;
foreach ( var trajectory in _active )
if ( trajectory . Root ! = null ) Destroy ( trajectory . Root ) ;
_active . Clear ( ) ;
if ( _material ! = null ) Destroy ( _material ) ;
}
private static Vector3 Vector ( EveUnityShotVector3 value ) = >
new Vector3 ( ( float ) value . X , ( float ) value . Y , ( float ) value . Z ) ;
private sealed class ActiveTrajectory
{
2026-07-15 04:18:13 +01:00
public ActiveTrajectory ( GameObject root , LineRenderer ? line , Vector3 origin , Vector3 endpoint , float startedAt , float duration , bool travels , GameObject ? impactPrefab )
2026-07-12 01:15:51 +01:00
{
Root = root ;
Line = line ;
Origin = origin ;
Endpoint = endpoint ;
StartedAt = startedAt ;
Duration = duration ;
2026-07-12 01:20:05 +01:00
Travels = travels ;
2026-07-12 01:24:45 +01:00
ImpactPrefab = impactPrefab ;
2026-07-12 01:15:51 +01:00
}
public GameObject Root { get ; }
2026-07-15 04:18:13 +01:00
public LineRenderer ? Line { get ; }
2026-07-12 01:15:51 +01:00
public Vector3 Origin { get ; }
public Vector3 Endpoint { get ; }
public float StartedAt { get ; }
public float Duration { get ; }
2026-07-12 01:20:05 +01:00
public bool Travels { get ; }
2026-07-12 01:24:45 +01:00
public GameObject ? ImpactPrefab { get ; }
public bool ImpactPresented { get ; set ; }
2026-07-12 01:15:51 +01:00
}
}
}