/// <summary> /// Creates the route of the car /// </summary> /// <param name="startingLane"></param> /// <returns></returns> public List <TrafficLane> CreateRoute(TrafficLane startingLane) { bool finished = false; Random rnd = new Random(); List <TrafficLane> tempRoute = new List <TrafficLane>(); List <TrafficLane> temp = new List <TrafficLane>(); TrafficLane tmp; tempRoute.Add(startingLane); temp = startingLane.Lanes; while (!finished) { tmp = temp.ElementAt(rnd.Next(temp.Count)); tempRoute.Add(tmp); if (tmp.LaneType == false) { finished = true; } temp = tmp.Lanes; } return(tempRoute); }
/// <summary> /// Is triggered when the car is at the last point in the lane in which /// it is removed from the current lane and added to the next lane and /// point if the first point is availible. /// </summary> void SwitchLane() { if (Route.Count > 1) { TrafficLane temp = this.Route.ElementAt(1); this.CurrentLane = temp; this.Route.ElementAt(0).Cars.Remove(this); this.Route.RemoveAt(0); this.CurrentLane.Cars.Add(this); if (this.CurrentLane.IsFirstPointEmpty()) { this.CurPoint = Point.Empty; } else { this.CurPoint = this.CurrentLane.Points.First(); } } else { this.CurrentLane.Cars.Remove(this); TrafficSimulatorGUI.count--; } }
/// <summary> /// Get the coordinates to draw the traffic lights /// </summary> /// <param name="lane">Traffic Lane</param> /// <param name="state">Current state of the traffic light</param> /// <returns>Point with the correct coordinates</returns> private Point getCoordinatesForTrafficLight(TrafficLane l, bool state) { Crossing c = l.Parent; bool isVertical = false; int x = 0; int y = 0; if (c is Crossing_A) { x = LIGHT_STRUCT_X_SPOTS_CROSSING_A[l.ID - 4] + 1; y = LIGHT_STRUCT_Y_SPOTS_CROSSING_A[l.ID - 4] + 1; switch (l.ID) { case 4: case 5: case 8: case 9: isVertical = true; break; default: isVertical = false; break; } } else { x = LIGHT_STRUCT_X_SPOTS_CROSSING_B[l.ID - 4] + 1; y = LIGHT_STRUCT_Y_SPOTS_CROSSING_B[l.ID - 4] + 1; switch (l.ID) { case 4: case 7: isVertical = true; break; default: isVertical = false; break; } } if (state) { if (isVertical) { y += TRAFFIC_LIGHT_BOX_HEIGHT * 2; } else { x += TRAFFIC_LIGHT_BOX_WIDTH * 2; } } return(new Point(x, y)); }
/// <summary> /// Constructor /// </summary> /// <param name="carId"></param> /// <param name="color"></param> /// <param name="startingLane"></param> public Car(Color color, TrafficLane startingLane) { this.Color = color; this.CurrentLane = startingLane; if (CurrentLane.Cars.Count == 0) { this.CurPoint = CurrentLane.Points.First(); } this.Route = CreateRoute(startingLane); CurrentLane.Cars.Add(this); }
/// <summary> /// Draw traffic lights /// </summary> /// <param name="lane">Traffic Lane</param> /// <param name="state">Current state of the traffic light</param> public void drawTrafficLight(TrafficLane lane, bool state) { Rectangle r; SolidBrush brush = new SolidBrush(Color.Red); if (state) brush.Color = Color.Green; Point point = getCoordinatesForTrafficLight(lane, state); r = new Rectangle(point.X, point.Y, TRAFFIC_LIGHT_WIDTH, TRAFFIC_LIGHT_HEIGHT); painter.Graphics.FillRectangle(brush, r); }
/// <summary> /// Draw traffic lights /// </summary> /// <param name="lane">Traffic Lane</param> /// <param name="state">Current state of the traffic light</param> public void drawTrafficLight(TrafficLane lane, bool state) { Rectangle r; SolidBrush brush = new SolidBrush(Color.Red); if (state) { brush.Color = Color.Green; } Point point = getCoordinatesForTrafficLight(lane, state); r = new Rectangle(point.X, point.Y, TRAFFIC_LIGHT_WIDTH, TRAFFIC_LIGHT_HEIGHT); painter.Graphics.FillRectangle(brush, r); }
/// <summary> /// Creates the route of the car /// </summary> /// <param name="startingLane"></param> /// <returns></returns> public List<TrafficLane> CreateRoute(TrafficLane startingLane) { bool finished = false; Random rnd = new Random(); List<TrafficLane> tempRoute = new List<TrafficLane>(); List<TrafficLane> temp = new List<TrafficLane>(); TrafficLane tmp; tempRoute.Add(startingLane); temp = startingLane.Lanes; while (!finished) { tmp = temp.ElementAt(rnd.Next(temp.Count)); tempRoute.Add(tmp); if (tmp.LaneType == false) { finished = true; } temp = tmp.Lanes; } return tempRoute; }
/// <summary> /// Get the coordinates to draw the traffic lights /// </summary> /// <param name="lane">Traffic Lane</param> /// <param name="state">Current state of the traffic light</param> /// <returns>Point with the correct coordinates</returns> private Point getCoordinatesForTrafficLight(TrafficLane l, bool state) { Crossing c = l.Parent; bool isVertical = false; int x = 0; int y = 0; if (c is Crossing_A) { x = LIGHT_STRUCT_X_SPOTS_CROSSING_A[l.ID - 4] + 1; y = LIGHT_STRUCT_Y_SPOTS_CROSSING_A[l.ID - 4] + 1; switch (l.ID) { case 4: case 5: case 8: case 9: isVertical = true; break; default: isVertical = false; break; } } else { x = LIGHT_STRUCT_X_SPOTS_CROSSING_B[l.ID - 4] + 1; y = LIGHT_STRUCT_Y_SPOTS_CROSSING_B[l.ID - 4] + 1; switch (l.ID) { case 4: case 7: isVertical = true; break; default: isVertical = false; break; } } if (state) { if (isVertical) { y += TRAFFIC_LIGHT_BOX_HEIGHT * 2; } else { x += TRAFFIC_LIGHT_BOX_WIDTH * 2; } } return new Point(x, y); }