public void Init(List <RailSegment> lastSegments) { LastSegments = lastSegments; RailPosition = new RailPathPosition(LastSegments[0], 0f); UpcomingSegments = new List <RailSegment>(); UpcomingSegments.Add(RailPosition.Segment); // Init upcoming segments while (UpcomingSegments.Count < UpcomingSegmentsLength) { RailSegment furthest = null; RailSegment secondFurthest = null; if (UpcomingSegments.Count == 1) { furthest = RailPosition.Segment; secondFurthest = LastSegments[1]; } else { furthest = UpcomingSegments[UpcomingSegments.Count - 1]; secondFurthest = UpcomingSegments[UpcomingSegments.Count - 2]; } UpcomingSegments.Add(furthest.GetNextSegment(secondFurthest)); } UpdatePosition(); }
private void UpdateAdjacentSegments(RailSegment newSegment) { if (newSegment == RailPosition.Segment) { throw new System.Exception("UpdateAdjacentSegments was called even though the current segment didn't change!"); } if (UpcomingSegments.Contains(newSegment) && LastSegments.Contains(newSegment)) { throw new System.Exception("New segment found in both last and upcoming segments, that doesn't make any sense."); } if (!UpcomingSegments.Contains(newSegment) && !LastSegments.Contains(newSegment)) { throw new System.Exception("New segment was not found in either last or upcoming segments."); } // Segment is an upcoming one => train going forwards if (UpcomingSegments.Contains(newSegment)) { // Handle passed segments int segmentIndex = UpcomingSegments.IndexOf(newSegment); for (int i = 0; i < segmentIndex; i++) { UpcomingSegments.RemoveAt(0); LastSegments.Insert(0, UpcomingSegments[0]); } // Adjust length of last segments while (LastSegments.Count > LastSegmentsLength) { LastSegments.RemoveAt(LastSegments.Count - 1); } // Adjust length of upcoming segments while (UpcomingSegments.Count < UpcomingSegmentsLength) { RailSegment furthest = UpcomingSegments[UpcomingSegments.Count - 1]; RailSegment secondFurthest = UpcomingSegments[UpcomingSegments.Count - 2]; UpcomingSegments.Add(furthest.GetNextSegment(secondFurthest)); } } // Segment is a past one => train going backwards else if (LastSegments.Contains(newSegment)) { int segmentIndex = LastSegments.IndexOf(newSegment); for (int i = 0; i < segmentIndex; i++) { LastSegments.RemoveAt(0); UpcomingSegments.Insert(0, LastSegments[0]); } // Adjust length of last segments while (LastSegments.Count < LastSegmentsLength) { RailSegment last = LastSegments[LastSegments.Count - 1]; RailSegment secondLast = LastSegments[LastSegments.Count - 2]; LastSegments.Add(last.GetNextSegment(secondLast)); } // Adjust length of upcoming segments while (UpcomingSegments.Count > UpcomingSegmentsLength) { UpcomingSegments.RemoveAt(UpcomingSegments.Count - 1); } } }