public void EndSimulation(uint tick) { bool update = HasSimulationChangedLast(); if (this[lastIndex].tick < tick && update) { Momentum2D[] segment = momentums[lastIndex.segment]; if (lastIndex.momentum + 1 < segment.Length) { lastIndex = new TrailIndex( lastIndex.segment, lastIndex.momentum + 1); } else { momentums.Add(new Momentum2D[segmentSize]); lastIndex = new TrailIndex( lastIndex.segment + 1, 0 ); } } if (update) { this[lastIndex] = ExtractMomentum(tick); } if (currentIndex != lastIndex) { ClearContacts(); ApplyMomentum(this[currentIndex]); } }
public void BeginSimulation(uint tick) { lastIndex = MomentumIndexForTick(tick); if (!lastIndex.valid) { lastIndex = TrailIndex.zero; } if (currentIndex != lastIndex) { ClearContacts(); ApplyMomentum(this[lastIndex]); } if (lastIndex.segment < momentums.Count - 1) { momentums.RemoveRange( lastIndex.segment + 1, momentums.Count - (lastIndex.segment + 1)); } int nextMomI = lastIndex.momentum + 1; while (nextMomI < momentums[lastIndex.segment].Length && momentums[lastIndex.segment][nextMomI] != null) { Momentum2D.Release(momentums[lastIndex.segment][nextMomI++]); momentums[lastIndex.segment][nextMomI++] = null; } }
public Momentum2D MomentumForTick(uint tick) { TrailIndex i = MomentumIndexForTick(tick); if (!i.valid) { throw new ArgumentOutOfRangeException( $"{nameof(tick)} should be higher than the oldest {nameof(tick)} of {nameof(MomentumTrail2D)}" ); } return(this[i]); }
bool GoToIndex(TrailIndex i) { if (!i.valid) { return(false); } if (i != currentIndex) { ClearContacts(); currentIndex = i; ApplyMomentum(this[currentIndex]); } return(true); }
public bool GoToTime(uint tick, uint keepTick = 0) { TrailIndex i = MomentumIndexForTick(tick); bool success = GoToIndex(i); uint pastTick = Math.Max(tick - keepTick, 0); int pastSegI = 0; while (pastSegI + 1 <= i.segment && momentums[pastSegI + 1][0].tick <= pastTick) { pastSegI++; } if (pastSegI > 0) { currentIndex = new TrailIndex( currentIndex.segment - pastSegI, currentIndex.momentum ); lastIndex = new TrailIndex( lastIndex.segment - pastSegI, lastIndex.momentum ); if (!Momentum2D.IsPoolFull()) { for (int segI = 0; segI < pastSegI; segI++) { Momentum2D.Release(momentums[segI]); if (Momentum2D.IsPoolFull()) { break; } } } momentums.RemoveRange(0, pastSegI); } return(success); }
public IEnumerable <Momentum2D> MomentumIterator(uint startTick = 0, uint endTick = uint.MaxValue) { TrailIndex startIndex = startTick > 0 ? MomentumIndexForTick(startTick) : TrailIndex.zero; if (!startIndex.valid) { yield break; } bool checkEnd = endTick != uint.MaxValue; int segI = startIndex.segment; int momI = startIndex.momentum; Momentum2D[] segment = momentums[segI]; while (segI < lastIndex.segment || (segI == lastIndex.segment && momI <= lastIndex.momentum)) { if (checkEnd && segment[momI].tick > endTick) { yield break; } yield return(segment[momI]); if (segI == lastIndex.segment && momI == lastIndex.momentum) { yield break; } if (momI < segment.Length - 1 && segment[momI + 1] != null) { momI++; } else { segment = momentums[++segI]; momI = 0; } } }
Momentum2D this[TrailIndex i] { get { return(momentums[i.segment][i.momentum]); } set { momentums[i.segment][i.momentum] = value; } }