11using MLAPI . Data ;
22using MLAPI . NetworkingManagerComponents . Core ;
3+ using System ;
34using System . Collections . Generic ;
45using UnityEngine ;
56
@@ -15,7 +16,7 @@ namespace MLAPI.MonoBehaviours.Core
1516 public class TrackedObject : MonoBehaviour
1617 {
1718 internal Dictionary < float , TrackedPointData > FrameData = new Dictionary < float , TrackedPointData > ( ) ;
18- internal LinkedList < float > Framekeys = new LinkedList < float > ( ) ;
19+ internal FixedQueue < float > Framekeys ;
1920 private Vector3 savedPosition ;
2021 private Quaternion savedRotation ;
2122
@@ -37,38 +38,49 @@ public float AvgTimeBetweenPointsMs
3738 {
3839 get
3940 {
40- if ( Framekeys . First == null || Framekeys . Last == null )
41+ if ( Framekeys . Count < 2 )
4142 return 0 ;
42- float totalSpan = Framekeys . Last . Value - Framekeys . First . Value ;
43+ float totalSpan = Framekeys . ElementAt ( Framekeys . Count - 1 ) - Framekeys . ElementAt ( 0 ) ;
4344 return ( totalSpan / Framekeys . Count ) * 1000f ;
4445 }
4546 }
4647
48+ public float TotalTimeHistory
49+ {
50+ get
51+ {
52+ return Framekeys . ElementAt ( Framekeys . Count - 1 ) - Framekeys . ElementAt ( 0 ) ;
53+ }
54+ }
55+
56+ private int maxPoints
57+ {
58+ get
59+ {
60+ return ( int ) ( NetworkingManager . singleton . NetworkConfig . SecondsHistory / ( 1f / NetworkingManager . singleton . NetworkConfig . EventTickrate ) ) ;
61+ }
62+ }
63+
4764 internal void ReverseTransform ( float secondsAgo )
4865 {
4966 savedPosition = transform . position ;
5067 savedRotation = transform . rotation ;
68+
5169 float currentTime = NetworkingManager . singleton . NetworkTime ;
5270 float targetTime = currentTime - secondsAgo ;
53- float previousTime = 0 ;
54- float nextTime = 0 ;
55- LinkedListNode < float > node = Framekeys . First ;
56- float previousValue = 0f ;
57- while ( node != null )
71+
72+ float previousTime = 0f ;
73+ float nextTime = 0f ;
74+ for ( int i = 0 ; i < Framekeys . Count ; i ++ )
5875 {
59- if ( previousValue <= targetTime && node . Value >= targetTime )
76+ if ( previousTime <= targetTime && Framekeys . ElementAt ( i ) >= targetTime )
6077 {
61- previousTime = previousValue ;
62- nextTime = node . Value ;
78+ nextTime = Framekeys . ElementAt ( i ) ;
6379 break ;
6480 }
6581 else
66- {
67- previousValue = node . Value ;
68- node = node . Next ;
69- }
82+ previousTime = Framekeys . ElementAt ( i ) ;
7083 }
71-
7284 float timeBetweenFrames = nextTime - previousTime ;
7385 float timeAwayFromPrevious = currentTime - previousTime ;
7486 float lerpProgress = timeAwayFromPrevious / timeBetweenFrames ;
@@ -84,35 +96,27 @@ internal void ResetStateTransform()
8496
8597 void Start ( )
8698 {
87- Framekeys . AddFirst ( 0 ) ;
99+ Framekeys = new FixedQueue < float > ( maxPoints ) ;
100+ Framekeys . Enqueue ( 0 ) ;
88101 LagCompensationManager . simulationObjects . Add ( this ) ;
89102 }
90103
91104 void OnDestroy ( )
92105 {
93- Framekeys . Clear ( ) ;
94- FrameData . Clear ( ) ;
95106 LagCompensationManager . simulationObjects . Remove ( this ) ;
96107 }
97108
98109 internal void AddFrame ( )
99110 {
100- float currentTime = NetworkingManager . singleton . NetworkTime ;
101- LinkedListNode < float > node = Framekeys . First ;
102- LinkedListNode < float > nextNode = node . Next ;
103- while ( node != null && currentTime - node . Value >= NetworkingManager . singleton . NetworkConfig . SecondsHistory )
104- {
105- nextNode = node . Next ;
106- FrameData . Remove ( node . Value ) ;
107- Framekeys . RemoveFirst ( ) ;
108- node = nextNode ;
109- }
111+ if ( Framekeys . Count == maxPoints )
112+ FrameData . Remove ( Framekeys . Dequeue ( ) ) ;
113+
110114 FrameData . Add ( NetworkingManager . singleton . NetworkTime , new TrackedPointData ( )
111115 {
112116 position = transform . position ,
113117 rotation = transform . rotation
114118 } ) ;
115- Framekeys . AddLast ( NetworkingManager . singleton . NetworkTime ) ;
119+ Framekeys . Enqueue ( NetworkingManager . singleton . NetworkTime ) ;
116120 }
117121 }
118122}
0 commit comments