Advertisement
daixso

Traffic Light Simulator

May 29th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var northBound = document.getElementById("northBound");
  2. var southBound = document.getElementById("southBound");
  3. var eastBound = document.getElementById("eastBound");
  4. var westBound = document.getElementById("westBound");
  5.  
  6. var signalGreen = "light-green";
  7. var signalYellow = "light-yellow";
  8. var signalRed = "light-red";
  9.  
  10. var greenTimer = 10000;
  11. var yellowTimer = 3000;
  12.  
  13. var stopSimulation = false;
  14.  
  15.  
  16. // Lights work on two groups
  17. // North and South are one group
  18. // East and West are another group
  19. // Only one group can be active at a time
  20. var norSouActive = false;
  21.  
  22. function startSimulation() {
  23.     console.log('DEBUG: Triggered start simulation');
  24.     setupSimulation();
  25. };
  26.  
  27. function stopSimulation() {
  28.     stopSimulation = true;
  29.     console.log('stopping simulation...');
  30. };
  31.  
  32. function setupSimulation() {
  33.     // North and South traffic go first
  34.     norSouActive = true;
  35.     console.log('initializing norSouGroup to green');
  36.     switchNorthSouthGroup('g');
  37.  
  38.     // Ensure West and East are stopped
  39.     console.log('initializing easWesGroup to red');
  40.     switchEastWestGroup('r');
  41.  
  42.     runSimulation();
  43. };
  44.  
  45. function runSimulation() {
  46.     console.log('prepareForChange timer...');
  47.     setTimeout(prepareForChange, greenTimer);
  48.     console.log('changeGroup...');
  49.     changeGroup();
  50.     console.log('runSimulation');
  51.     runSimulation();
  52. };
  53.  
  54. function prepareForChange() {
  55.     if(norSouActive) {
  56.         switchNorthSouthGroup('y');
  57.         setTimeout(changeGroup(), yellowTimer);
  58.     } else {
  59.         switchEastWestGroup('y');
  60.         setTimeout(changeGroup(), yellowTimer);
  61.     }
  62. };
  63.  
  64. function changeGroup() {
  65.     if(norSouActive) {
  66.         switchNorthSouthGroup('r');
  67.         switchEastWestGroup('g');
  68.     } else {
  69.         switchEastWestGroup('r');
  70.         switchNorthSouthGroup('g');
  71.     }
  72. };
  73.  
  74. // g - green y - yellow r - red
  75. function switchEastWestGroup(status) {
  76.     switch(status) {
  77.         case "g":
  78.             westBound.className = "grid-item " + signalGreen;
  79.             eastBound.className = "grid-item " + signalGreen;
  80.             break;
  81.         case "y":
  82.             westBound.className = "grid-item " + signalYellow;
  83.             eastBound.className = "grid-item " + signalYellow;
  84.             break;
  85.         case "r":
  86.             westBound.className = "grid-item " + signalRed;
  87.             eastBound.className = "grid-item " + signalRed;
  88.             break;
  89.     }
  90. };
  91.  
  92. // g - green y - yellow r - red
  93. function switchNorthSouthGroup(status) {
  94.     switch(status) {
  95.         case "g":
  96.             northBound.className = "grid-item " + signalGreen;
  97.             southBound.className = "grid-item " + signalGreen;
  98.             break;
  99.         case "y":
  100.             northBound.className = "grid-item " + signalYellow;
  101.             southBound.className = "grid-item " + signalYellow;
  102.             break;
  103.         case "r":
  104.             northBound.className = "grid-item " + signalRed;
  105.             southBound.className = "grid-item " + signalRed;
  106.             break;
  107.     }
  108. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement