//Method name: westLigth //Method description: functionality from west light public void westLight(ref StopLight west, ref StopLight north) { //get west color west2 = west.getColor(); west.timer(west.count); //check if color changed if (string.Equals(west2, "Green") && string.Equals("Yellow", west.getColor())) { west.lightChanged = true; } //west changed to red set north to green if (string.Equals(west2, "Yellow") && string.Equals("Red", west.getColor())) { west.count = 0; north.setGreen(); west.lightChanged = true; north.count = 0; } //increase count if green or yellow if (string.Equals("Green", west.getColor()) || string.Equals("Yellow", west.getColor())) { west.count++; } }
//Method name: southLigth //Method description: functionality from south light public void southLight(ref StopLight south, ref StopLight east) { //get souths color south2 = south.getColor(); south.timer(south.count); //check to see if color changed if (string.Equals(south2, "Green") && string.Equals("Yellow", south.getColor())) { south.lightChanged = true; } //if south is red set east to green if (string.Equals(south2, "Yellow") && string.Equals("Red", south.getColor())) { south.count = 0; east.setGreen(); south.lightChanged = true; east.count = 0; } //increase count if south is green or yellow if (string.Equals("Green", south.getColor()) || string.Equals("Yellow", south.getColor())) { south.count++; } }
//Method name: eastLigth //Method description: functionality from east light public void eastLight(ref StopLight east, ref StopLight west) { //get easts color east2 = east.getColor(); east.timer(east.count); //check to see if color changed if (string.Equals(east2, "Green") && string.Equals("Yellow", east.getColor())) { east.lightChanged = true; } if (string.Equals(east2, "Yellow") && string.Equals("Red", east.getColor())) { east.lightChanged = true; east.count = 0; } //increase east count if green or yellow if (string.Equals("Green", east.getColor()) || string.Equals("Yellow", east.getColor())) { east.count++; } //set west to green if (east.count == 7) { west.setGreen(); west.lightChanged = true; west.count = 0; } }
protected List <StopLight> StopLightsOnPath() { List <StopLight> stopLights = new List <StopLight>(); float step = CurrentPath().length / approxPoints; Vector3 nextPoint = CurrentPath().GetPointAtDistance(DistanceTraveled()); for (float searchDistance = step; searchDistance <= CurrentPath().length; searchDistance += step) { Vector3 currentPoint = nextPoint; nextPoint = CurrentPath().GetPointAtDistance(DistanceTraveled() + searchDistance); Vector3 searchVec = nextPoint - currentPoint; RaycastHit hit; if (Physics.Raycast(currentPoint, searchVec, out hit, searchVec.magnitude, LayerMask.GetMask("Traffic"))) { if (hit.transform.gameObject.tag == "StopLight") { StopLight stopLight = hit.transform.GetComponent <StopLight>(); //If the path is a circle, don't count old stop lights if (stopLights.Contains(stopLight)) { break; } stopLights.Add(stopLight); } } } return(stopLights); }
//Method name: northLigth //Method description: functionality from north light public void northLight(ref StopLight north, ref StopLight south) { //get norths color before calling function north2 = north.getColor(); north.timer(north.count); //check to see if color has changed if (string.Equals(north2, "Green") && string.Equals("Yellow", north.getColor())) { north.lightChanged = true; } //light changed if (string.Equals(north2, "Yellow") && string.Equals("Red", north.getColor())) { north.lightChanged = true; count = 0; } //increase count if ligth is green or yellow if (string.Equals("Green", north.getColor()) || string.Equals("Yellow", north.getColor())) { count++; } if (count == 7) { south.setGreen(); south.lightChanged = true; south.count = 0; } }
{ public static void Main() { System.Console.WriteLine("Stoplight starts now"); StopLight stop = new StopLight(); Application.Run(stop); System.Console.WriteLine("End"); } //End of Main function
public void Test() { StopLight stopLight = new StopLight(); // green Assert.AreEqual <Color>(Color.Yellow, stopLight.ChangeColor()); Assert.AreEqual <Color>(Color.Red, stopLight.ChangeColor()); Assert.AreEqual <Color>(Color.Green, stopLight.ChangeColor()); }
public void Test() { var stopLight = new StopLight(); // green var values = (Color[])Enum.GetValues(typeof(Color)); for (int i = 0; i < 100; i++) { Assert.AreEqual(values[i % 3], stopLight.Next); } }
public void IncreaseStopLightCounter() { if (mapObject != null) { if (mapObject.mapObjectProperties.mapObjectType == MapObjectType.STOPLIGHT) { StopLight stopLight = (StopLight)mapObject.mapObjectProperties; stopLight.IncreaseVehicleCounter(); } } }
public Intersection GetIntersection() { MapObjectProperties mapObjectProperties = GetMapObjectProperties(); if (mapObjectProperties != null && mapObject.GetMapObjectType() == MapObjectType.STOPLIGHT) { StopLight stopLight = (StopLight)mapObjectProperties; return(stopLight.GetControllingIntersection()); } return(null); }
public int GetStopLightCounter() { if (mapObject != null) { if (mapObject.mapObjectProperties.mapObjectType == MapObjectType.STOPLIGHT) { StopLight stopLight = (StopLight)mapObject.mapObjectProperties; return(stopLight.vehicleCounter); } } return(0); }
private bool InFrontOfStopLight(StopLight s) { int rot = (int)Mathf.Floor(s.transform.rotation.y); if (s.transform.rotation.y == 0 && transform.position.z < s.transform.position.z || s.transform.rotation.y == 90 && transform.position.x < s.transform.position.x || s.transform.rotation.y == 180 && transform.position.z > s.transform.position.z || s.transform.rotation.y == 270 && transform.position.x > s.transform.position.x) { return(true); } return(false); }
//> -1 means a block public bool IsStopLightBlockDirection(int vehicleDirection, out bool isStopLight) { isStopLight = false; if (mapObject != null) { if (mapObject.mapObjectProperties.mapObjectType == MapObjectType.STOPLIGHT) { StopLight stopLight = (StopLight)mapObject.mapObjectProperties; if (vehicleDirection == stopLight.blockDirection) { isStopLight = true; if (stopLight.IsBlocked()) { return(true); } } } } return(false); }
static void Main(string[] args) { //setting north light green and all other lights red StopLight north = new StopLight("North", "Green"); StopLight south = new StopLight("South", "Red"); StopLight east = new StopLight("East", "Red"); StopLight west = new StopLight("West", "Red"); //creating stopwatch object and starting the stopwatch; Stopwatch stopWatch = new Stopwatch(); TimeSpan ts = stopWatch.Elapsed; //count that will have the seconds that have gone by int timeCount = 0; stopWatch.Start(); //lines to format the output Console.WriteLine("Current Time \t North Light \t South Light \t East Light \t West Light"); Console.WriteLine("------------- \t ------------ \t --------- \t -----------\t ----------"); //print the system with initial values Console.WriteLine(timeCount + "\t\t" + north.getColor() + "\t\t " + south.getColor() + "\t\t " + east.getColor() + "\t\t " + west.getColor()); north.count++; //while time is less than 60 seconds while (timeCount <= 60) { //get the time elapsed ts = stopWatch.Elapsed; //if one second has gone by if (ts.Seconds == 1) { //increase count by one timeCount++; //restart the stopwatch (it will always be between 0 and 1) //once it is 1 we know that one second has gone by so we increase timeCount by 1 //and restart the stopWatch stopWatch.Restart(); //if time is between 45 and 50, simulate an emergency vehicle going by if (timeCount >= 40 && timeCount <= 50) { //if time == 40 simulate vehicle coming by, it approaches east light which turns green //and all other lights go red //east count initialized again because it went green if (timeCount == 40) { east.isApproaching(); east.count = 1; north.ApproachedOther(); west.ApproachedOther(); south.ApproachedOther(); Console.WriteLine("There is a emergency vehicle approaching the {0} light", east.getName()); Console.WriteLine(timeCount + "\t\t" + north.getColor() + "\t\t" + south.getColor() + "\t\t " + east.getColor() + "\t\t" + west.getColor()); } //if time == 50 emergency vehicle left if (timeCount == 50) { Console.WriteLine("The emergency vehicle has left the area "); Console.WriteLine(timeCount + "\t\t" + north.getColor() + "\t\t " + south.getColor() + "\t\t " + east.getColor() + "\t\t " + west.getColor()); Console.WriteLine("\n"); } //continue so that it doesnt call the functions below continue; } //calling functions and parameters by reference north.northLight(ref north, ref south); south.southLight(ref south, ref east); east.eastLight(ref east, ref west); west.westLight(ref west, ref north); //if any of the lights has changed color, print //if(!string.Equals(north.north2,north.getColor()) || !string.Equals(south.south2, south.getColor()) || !string.Equals(east.east2, east.getColor()) || !string.Equals(west.west2, west.getColor())) if (north.lightChanged == true || west.lightChanged == true || south.lightChanged == true || east.lightChanged == true) { Console.WriteLine(timeCount + "\t\t" + north.getColor() + "\t\t " + south.getColor() + "\t\t " + east.getColor() + "\t\t " + west.getColor()); } north.lightChanged = false; west.lightChanged = false; east.lightChanged = false; south.lightChanged = false; } } //stop the watch after one minute has gone by stopWatch.Stop(); }
//should each stoplight be its own mapobject //its extra things to add to our draw list then private void AddIntersection(List <Intersection> intersectionList, Tile[,] tileGrid, ShrunkNode[,] shrunkMap, int shrunkX, int shrunkY, Point expandedPoint, Random rndExtras, int id) { Intersection intersection = new Intersection(rndExtras, expandedPoint.X, expandedPoint.Y, id); MapObject mapObject; Dictionary <Direction, StopLight> stopLightList = new Dictionary <Direction, StopLight>(); int tileX; int tileY; StopLight stopLight; int stageLeftRight = rndExtras.Next(0, 1) * 2; //should always be 0 or 2 int stageUpDown; if (stageLeftRight == 0) { stageUpDown = 2; } else { stageUpDown = 0; } //Road Down if (ShrunkWorldBuilder.IsRoad(new Point(shrunkX, shrunkY + 1), shrunkMap)) { tileX = expandedPoint.X; tileY = expandedPoint.Y + 1; stopLight = new StopLight(0, stageUpDown, intersection, 0); stopLightList.Add(Direction.DOWN, stopLight); mapObject = new MapObject(stopLight, tileX, tileY, Color.White, 1f); tileGrid[tileX, tileY].AddMapObject(mapObject, false, false, true, null, false); AddWhiteLine(tileGrid, tileX, tileY, 3); } // Road Right if (ShrunkWorldBuilder.IsRoad(new Point(shrunkX + 1, shrunkY), shrunkMap)) { tileX = expandedPoint.X + 1; tileY = expandedPoint.Y + 1; stopLight = new StopLight(6, stageLeftRight, intersection, 3); stopLightList.Add(Direction.RIGHT, stopLight); mapObject = new MapObject(stopLight, tileX, tileY, Color.White, 1f); tileGrid[tileX, tileY].AddMapObject(mapObject, false, false, true, null, false); AddWhiteLine(tileGrid, tileX, tileY, 2); } // Road Up if (ShrunkWorldBuilder.IsRoad(new Point(shrunkX, shrunkY - 1), shrunkMap)) { tileX = expandedPoint.X + 1; tileY = expandedPoint.Y; stopLight = new StopLight(4, stageUpDown, intersection, 2); stopLightList.Add(Direction.UP, stopLight); mapObject = new MapObject(stopLight, tileX, tileY, Color.White, 1f); tileGrid[tileX, tileY].AddMapObject(mapObject, false, false, true, null, false); AddWhiteLine(tileGrid, tileX, tileY, 1); } //Road Left if (ShrunkWorldBuilder.IsRoad(new Point(shrunkX - 1, shrunkY), shrunkMap)) { tileX = expandedPoint.X; tileY = expandedPoint.Y; stopLight = new StopLight(2, stageLeftRight, intersection, 1); stopLightList.Add(Direction.LEFT, stopLight); mapObject = new MapObject(stopLight, tileX, tileY, Color.White, 1f); tileGrid[tileX, tileY].AddMapObject(mapObject, false, false, true, null, false); AddWhiteLine(tileGrid, tileX, tileY, 0); } AddTurningArrows(tileGrid, expandedPoint, 0); intersection.SetStopLightList(stopLightList); intersectionList.Add(intersection); }
//Constructor public StopLightInterface() { Size = new Size((int)interfacewidth, (int)interfaceheight); //Colors BackColor = Color.Blue; topcontainer.BackColor = Color.White; bottomcontainer.BackColor = Color.LightBlue; //Top Container sizes topcontainerwidth = interfacewidth; topcontainerheight = 1 / 10.0 * interfaceheight; //Title sizes titlewidth = 1 / 3.0 * topcontainerwidth; titleheight = 1 / 5.0 * topcontainerheight; //Bottom Conatiner sizes bottomcontainerwidth = interfacewidth; bottomcontainerheight = 1 / 7.0 * interfaceheight; //Control Strip sizes controlstripwidth = 3 / 9.0 * bottomcontainerwidth; controlstripheight = 2 / 5.0 * bottomcontainerheight; radioButtonWidth = 1 / 3.0 * controlstripwidth; radioButtonHeight = 1 / 3.0 * controlstripheight; //Control Strip points controlstripx = 3 / 9.0 * bottomcontainerwidth; controlstripy = 1 / 5.0 * bottomcontainerheight; slowButtonx = 1 / 5.0 * controlstripwidth; slowButtony = 1 / 3.0 * controlstripheight; fastButtonx = 3 / 5.0 * controlstripwidth; fastButtony = slowButtony; //Button sizes buttonwidth = 1 / 9.0 * bottomcontainerwidth; buttonheight = 1 / 5.0 * bottomcontainerheight; //Bottom Conatiner x and y bottomcontainerx = 0; bottomcontainery = interfaceheight - bottomcontainerheight; //Start/Stop button x and y startandstopbuttonx = 1 / 9.0 * bottomcontainerwidth; startandstopbuttony = 2 / 5.0 * bottomcontainerheight; //Exit button x and y exitbuttonx = 7 / 9.0 * bottomcontainerwidth; exitbuttony = 2 / 5.0 * bottomcontainerheight; //Set light positions lightradius = 60; redlightx = (float)interfacewidth / 2; redlighty = (float)interfaceheight / 3 - (float)topcontainerheight; yellowlightx = redlightx; yellowlighty = (float)(2 / 3.0) * (float)interfaceheight - (float)topcontainerheight - lightradius; greenlightx = redlightx; greenlighty = (float)interfaceheight - (float)topcontainerheight - ((float)2 * lightradius); //Setup title title.Text = "Competent Traffic Light by Corey Stock"; title.TextAlign = ContentAlignment.TopCenter; title.Size = new Size((int)titlewidth, (int)titleheight); title.Left = (int)(interfacewidth - titlewidth) / 2; title.Top = topcontainer.Bottom / 2; title.ForeColor = Color.Green; //Setup topcontainer topcontainer.Size = new Size((int)topcontainerwidth, (int)topcontainerheight); topcontainer.Left = 0; topcontainer.Top = 0; //Setup start and stop button startandstopbutton.Text = "Start"; startandstopbutton.Size = new Size((int)buttonwidth, (int)buttonheight); startandstopbutton.Location = new Point((int)startandstopbuttonx, (int)startandstopbuttony); startandstopbutton.BackColor = Color.Green; //Setup exit button exitbutton.Text = "Exit"; exitbutton.Size = new Size((int)buttonwidth, (int)buttonheight); exitbutton.Location = new Point((int)exitbuttonx, (int)exitbuttony); exitbutton.BackColor = Color.Red; //Setup bottom group box bottomcontainer.Size = new Size((int)bottomcontainerwidth, (int)bottomcontainerheight); bottomcontainer.Location = new Point((int)bottomcontainerx, (int)bottomcontainery); //Setup radio button group controlstrip.Size = new Size((int)controlstripwidth, (int)controlstripheight); controlstrip.Location = new Point((int)controlstripx, (int)controlstripy); slowButton.Size = new Size((int)radioButtonWidth, (int)radioButtonHeight); slowButton.Location = new Point((int)slowButtonx, (int)slowButtony); fastButton.Size = new Size((int)radioButtonWidth, (int)radioButtonHeight); fastButton.Location = new Point((int)fastButtonx, (int)fastButtony); slowButton.Text = "Slow"; fastButton.Text = "Fast"; //Start on slow slowButton.Checked = true; //Create event handlers for buttons startandstopbutton.Click += new EventHandler(ControlLight); exitbutton.Click += new EventHandler(StopRunning); slowButton.Click += new EventHandler(selectSpeed); fastButton.Click += new EventHandler(selectSpeed); //Add UI elements to Form topcontainer.Controls.Add(title); Controls.Add(topcontainer); controlstrip.Controls.Add(slowButton); controlstrip.Controls.Add(fastButton); bottomcontainer.Controls.Add(startandstopbutton); bottomcontainer.Controls.Add(exitbutton); bottomcontainer.Controls.Add(controlstrip); Controls.Add(bottomcontainer); AcceptButton = startandstopbutton; //Set clock event lightClock.Elapsed += ClockEvent; //Create new stoplight sl = new StopLight(); }