Sunday 30 April 2017

TrafficControl: Refactoring the Train::move() function - Train State Model

To handle the logic for the train movements, I've defined some states that the trains will always be in. The trains will typicallly move clockwise in the state model:
The transitions that are reprosented by green arrows are more complex and may require their own methods.
The states are:
  • Opening: Train is at a station and is opening the doors.
  • Loading: Train is at a station with doors open and letting passengers enter and leave the train.
  • Closing:  Train is at a station and is closing the doors.
  • Waiting: Train is at a station with closed doors and is waiting for the departure time.
  • Ready: Train is at a track or station and is waiting for permission to leave.
  • Running: Train is moving on a track.
  • Passing: Train is moving on a track and has permission to enter the next track
  • Emergency: Train must stop immediately (or at next station, if in a tunnel).
  • Stopped: Train is not allowed to move anymore (end of TravelPlan or after Emergency state).
Each state will have a corresponding method.


n is a counter that each train is assigned every tick from the trafficClock class. It counts down and is returned from the functions. Once it reaches zero, the train will wait for the next tick from the trafficClock. 

I'll describe the trafficClock in a late blog post.

For the transitions between the states, I'll define specific methods if they are needed. In the picture above, the arrows are representing the transitions.

No comments:

Post a Comment