public bool Move(float distance) { //Move the most forward axle first if (direction == Direction.Forward) { if (axleFront.Move(distance)) { axleRear.Move(distance); return(true); } } else { if (axleRear.Move(distance)) { axleFront.Move(distance); return(true); } } return(false); }
//Warning: Recursive public bool Move(float distance) { bool hasMoved = false; float newPosition = position + distance * (int)direction; if (currentTrackSection.GetPositionOnTrack(newPosition) != null) { //Do the thing position = newPosition; hasMoved = true; } else { TrackSection newSection = (direction == Direction.Forward) ? TrackCollection.instance.Get(currentTrackSection.NextSectionIndex) : TrackCollection.instance.Get(currentTrackSection.PreviousSectionIndex); if (newSection != null) { //Check to see if we need to switch direction Direction newDirection = direction; float distanceOnNewSection = 0.0f; float positionOnNewSection = 0.0f; if (direction == Direction.Forward) { distanceOnNewSection = newPosition - currentTrackSection.length; //TODO: Make a function to determine the correct direction in the TrackSection class //Might be needed when working with switches, so we can override it in that case if (newSection.CheckNextSection(currentTrackSection)) { newDirection = Direction.Reverse; positionOnNewSection = newSection.length; } } else { distanceOnNewSection = -newPosition; positionOnNewSection = newSection.length; if (newSection.CheckPreviousSection(currentTrackSection)) { newDirection = Direction.Forward; positionOnNewSection = 0.0f; } } //TODO: Try to get a stack overflow by calling move with a really high distance value Traveler traveler = new Traveler(newSection, newDirection, positionOnNewSection); hasMoved = traveler.Move(distanceOnNewSection); this.direction = traveler.direction; this.currentTrackSection = traveler.currentTrackSection; this.position = traveler.position; } else { //There was no new section! //TODO: Derail, move to the end or something else? UnityEngine.Debug.Log("Traveler got stuck at track section " + currentTrackSection.index); Derail(); } } return(hasMoved); }