Tuesday 6 March 2018

TrafficControl: TrafficClock

TrafficControl is using a thread that makes the network act on a specific pace, according to the user's preferences.

A QThread called clockThread is created and trafficClock is assigned to that thread. The thread loops as long as isAlive is true and does the following:
  • Calculate the time of the next tick
  • If the boolean variable simulationIsRunning is true, the stepTimeSignal is emitted, letting all trains move one second on the network and updating tracks and stations.
  • The thread sleeps until the next tick
The following table illustrates this:

System Time Simulated Time Speed Factor Time Interval
10:00:00 00:00:00 2x 500 ms
10:00:01 00:00:00.5 2x 500 ms
10:00:02 00:00:01.0 3x 333 ms
10:00:03 00:00:01.333 4x 250 ms
10:00:04 00:00:01.583 4x 250 ms

At 10:00:02, the user is increasing the speed factor to 3x. One second later, the user is increasing it to 4x.


I'll do some changes of the code in the next blog post. This is needed to simplify the user interface and refactor the time handling code.

No comments:

Post a Comment