@@ -10,7 +10,7 @@ namespace MLAPI.MonoBehaviours.Core
1010 public class TrackedObject : MonoBehaviour
1111 {
1212 internal Dictionary < float , TrackedPointData > FrameData = new Dictionary < float , TrackedPointData > ( ) ;
13- internal List < float > Framekeys = new List < float > ( ) { 0 } ;
13+ internal LinkedList < float > Framekeys = new LinkedList < float > ( ) ;
1414 private Vector3 savedPosition ;
1515 private Quaternion savedRotation ;
1616
@@ -22,15 +22,21 @@ internal void ReverseTransform(float secondsAgo)
2222 float targetTime = currentTime - secondsAgo ;
2323 float previousTime = 0 ;
2424 float nextTime = 0 ;
25- for ( int i = 1 ; i < Framekeys . Count ; i ++ )
25+ LinkedListNode < float > node = Framekeys . First ;
26+ float previousValue = 0f ;
27+ while ( node != null )
2628 {
27- if ( Framekeys [ i - 1 ] <= targetTime && Framekeys [ i ] >= targetTime )
29+ if ( previousValue <= targetTime && node . Value >= targetTime )
2830 {
29- previousTime = Framekeys [ i ] ;
30- nextTime = Framekeys [ i + 1 ] ;
31+ previousTime = previousValue ;
32+ nextTime = node . Value ;
3133 break ;
3234 }
33-
35+ else
36+ {
37+ previousValue = node . Value ;
38+ node = node . Next ;
39+ }
3440 }
3541
3642 float timeBetweenFrames = nextTime - previousTime ;
@@ -48,35 +54,35 @@ internal void ResetStateTransform()
4854
4955 void Start ( )
5056 {
57+ Framekeys . AddFirst ( 0 ) ;
5158 LagCompensationManager . SimulationObjects . Add ( this ) ;
5259 }
5360
5461 void OnDestroy ( )
5562 {
63+ Framekeys . Clear ( ) ;
64+ FrameData . Clear ( ) ;
5665 LagCompensationManager . SimulationObjects . Remove ( this ) ;
5766 }
5867
5968 internal void AddFrame ( )
6069 {
6170 float currentTime = Time . time ;
62- for ( int i = 0 ; i < Framekeys . Count ; i ++ )
71+ LinkedListNode < float > node = Framekeys . First ;
72+ LinkedListNode < float > nextNode = node . Next ;
73+ while ( currentTime - node . Value >= NetworkingManager . singleton . NetworkConfig . SecondsHistory )
6374 {
64- if ( currentTime - Framekeys [ i ] >= NetworkingManager . singleton . NetworkConfig . SecondsHistory )
65- {
66- for ( int j = 0 ; j < i ; j ++ )
67- {
68- FrameData . Remove ( Framekeys [ 0 ] ) ;
69- //This is not good for performance. Other datatypes should be concidered.
70- Framekeys . RemoveAt ( 0 ) ;
71- }
72- }
75+ nextNode = node . Next ;
76+ FrameData . Remove ( node . Value ) ;
77+ Framekeys . RemoveFirst ( ) ;
78+ node = nextNode ;
7379 }
7480 FrameData . Add ( Time . time , new TrackedPointData ( )
7581 {
7682 position = transform . position ,
7783 rotation = transform . rotation
7884 } ) ;
79- Framekeys . Add ( Time . time ) ;
85+ Framekeys . AddLast ( Time . time ) ;
8086 }
8187 }
8288}
0 commit comments