private RoadUser pickCorrectRoadUser(RoadUser roadUser, int spawnX, int spawnY) { double maxSpeed = roadUser.MaxSpeed; bool redlight = roadUser.RedLight; if (roadUser is BlueCar) { return(new BlueCar(new Point(spawnX, spawnY), maxSpeed, redlight)); } else if (roadUser is BlueSportsCar) { return(new BlueSportsCar(new Point(spawnX, spawnY), maxSpeed, redlight)); } else if (roadUser is GreenSportsCar) { return(new GreenSportsCar(new Point(spawnX, spawnY), maxSpeed, redlight)); } else if (roadUser is Pedestrian) { return(new Pedestrian(new Point(spawnX, spawnY), maxSpeed)); } else { return(null); } }
/// <summary> /// Bepalen maakt een bocht op gegeven locatie /// </summary> /// <param name="roadUser"></param>de roaduser waar het om gaat</param> /// <param name="P"></param>het punt waar de roaduser zich bevind</param> /// <param name="X1"></param>de X van het punt waar de bocht plaatsvindt</param> /// <param name="Y1"></param>de Y van het punt waar de bocht plaatsvindt</param> /// <param name="X2"></param>de X van het punt waar de auto naar toe moet wijzen</param> /// <param name="Y2"></param>de Y van het punt waar de auto naar toe moet wijzen</param> protected virtual void Type4Turn(RoadUser roadUser, Point P, int turnLocationX, int turnLocationY, int faceX, int faceY) { if (P.X == turnLocationX && P.Y == turnLocationY) { roadUser.FaceTo(new Point(faceX, faceY)); } }
private void trainSpawnTimer_Tick(object sender, EventArgs e) { if (random.Next(0, 2) == 0) { lastTrain = new RedTrain(new Point(223, 418)); if (random.Next(5) == 0) { lastTrain = new GreenTrain(new Point(223, 418)); } lastTrain.FaceTo(new Point(223, 0)); } else { lastTrain = new RedTrain(new Point(174, 0)); if (random.Next(5) == 0) { lastTrain = new GreenTrain(new Point(174, 0)); } lastTrain.FaceTo(new Point(174, 418)); } Intersection.AddRoadUser(lastTrain); trainSpawnTimer.Stop(); }
protected override void Type4Turn(RoadUser roadUser, Point P, int X1, int Y1, int X2, int Y2) { if (P.X == X1 && P.Y <= Y1) { roadUser.FaceTo(new Point(X2, Y2)); } }
protected bool AddToTrafficLightQueue(LaneId laneId, RoadUser roadUser) { return((Intersection.GetTrafficLight(laneId).State == SignalState.STOP || Intersection.GetTrafficLight(laneId).State == SignalState.CLEAR_CROSSING) && roadUser.BoundingBox.IntersectsWith(Intersection.GetSensor(laneId).BoundingBox) && !roadUser.RedLight && !roadUser.CrossRedLight ); }
/// <summary> /// Bepalen om een bocht te maken met een kans van 1 op 4 /// </summary> /// <param name="roadUser">de roaduser waar het om gaat</param> /// <param name="P"> het punt waar de roaduser zich bevind</param> /// <param name="turnLocationX"> de X van het punt waar de bocht moet plaatsvinden</param> /// <param name="turnLocationY"> de Y van het punt waar de bocht moet plaatsvinden</param> /// <param name="faceX"> de X van het punt waar de auto naar toe moet wijzen indien hij een bocht maakt</param> /// <param name="faceY"> de Y van het punt waar de auto naar toe moet wijzen indien hij een bocht maakt</param> protected void Type3Turn(RoadUser roadUser, Point P, int turnLocationX, int turnLocationY, int faceX, int faceY) { if (P.X == turnLocationX && P.Y == turnLocationY) { if (random.Next(0, 3) == 1) { roadUser.FaceTo(new Point(faceX, faceY)); } } }
private void CheckCollision(RoadUser roadUser1, Rectangle boundBox) { foreach (RoadUser roadUser2 in Intersection.RoadUsers) { if (roadUser1 != roadUser2 && boundBox.IntersectsWith(roadUser2.BoundingBox)) { if (!(roadUser1 is Pedestrian && roadUser2 is Pedestrian)) { roadUser1.Speed = 0; } } } }
/// <summary> /// methode die gebruikt wordt om roadUsers van het ene kruispunt naar het andere over te zetten /// </summary> /// <param name="roadUser">de roadUser waar het om gaat</param> /// <param name="leaveX">de X positie waar wordt gedetecteerd of een auto het kruispunt gaat verlaten</param> /// <param name="leaveY">de Y positie waar wordt gedetecteerd of een auto het kruispunt gaat verlaten</param> /// <param name="spawnX">de X positie waar de nieuwe roadUser wordt gemaakt</param> /// <param name="spawnY">de Y positie waar de nieuwe roadUser wordt gemaakt</param> /// <param name="faceToX">de X positie waarde nieuwe roadUser naar toe moet wijzen</param> /// <param name="faceToY">de Y positie waarde nieuwe roadUser naar toe moet wijzen</param> private void TransferCarMethod(RoadUser roadUser, int leaveX, int leaveY, int spawnX, int spawnY, int faceToX, int faceToY) { Point P = roadUser.Location; if (IntersectionBottom != null && P.X == leaveX && P.Y == 382) { RoadUser newRoadUser = pickCorrectRoadUser(roadUser, spawnX, spawnY); if (newRoadUser != null) { newRoadUser.FaceTo(new Point(faceToX, faceToY)); IntersectionBottom.AddRoadUser(newRoadUser); } } else if (IntersectionLeft != null && P.X == 18 && P.Y == leaveY) { RoadUser newRoadUser = pickCorrectRoadUser(roadUser, spawnX, spawnY); if (newRoadUser != null) { newRoadUser.FaceTo(new Point(faceToX, faceToY)); IntersectionLeft.AddRoadUser(newRoadUser); } } else if (IntersectionRight != null && P.X == 382 && P.Y == leaveY) { RoadUser newRoadUser = pickCorrectRoadUser(roadUser, spawnX, spawnY); if (newRoadUser != null) { newRoadUser.FaceTo(new Point(faceToX, faceToY)); IntersectionRight.AddRoadUser(newRoadUser); } } else if (IntersectionTop != null && P.X == leaveX && P.Y == 18) { RoadUser newRoadUser = pickCorrectRoadUser(roadUser, spawnX, spawnY); if (newRoadUser != null) { newRoadUser.FaceTo(new Point(faceToX, faceToY)); IntersectionTop.AddRoadUser(newRoadUser); } } }
/// <summary> /// Statistieken bijwerken. /// </summary> /// <param name="roadUser"></param> /// <param name="intersection"></param> private void UpdateStats(RoadUser roadUser, IntersectionControl intersection) { lastRoadUser = roadUser; lastIC = intersection; StatsTotal++; if (roadUser.MaxSpeed >= 4) { StatsTotalSpeed++; } if (roadUser.RedLight) { StatsTotalRedLight++; } if (intersection.IntersectionType == IntersectionType.TYPE_1) { StatsTotalPerIntersection[0]++; } else if (intersection.IntersectionType == IntersectionType.TYPE_2) { StatsTotalPerIntersection[1]++; } else if (intersection.IntersectionType == IntersectionType.TYPE_3) { StatsTotalPerIntersection[2]++; } else if (intersection.IntersectionType == IntersectionType.TYPE_4) { StatsTotalPerIntersection[3]++; } else if (intersection.IntersectionType == IntersectionType.TYPE_5) { StatsTotalPerIntersection[4]++; } else if (intersection.IntersectionType == IntersectionType.TYPE_RAILWAY) { StatsTotalPerIntersection[5]++; } }
protected void HandleTrafficLightLane(RoadUser roadUser, LaneId lane) { if (Intersection.GetTrafficLight(lane).State == SignalState.GO && roadUser.BoundingBox.IntersectsWith(Intersection.GetSensor(lane).BoundingBox)) { roadUser.CrossRedLight = true; } if (Intersection.GetTrafficLight(lane).State == SignalState.STOP && roadUser.RedLight && roadUser.BoundingBox.IntersectsWith(Intersection.GetSensor(lane).BoundingBox)) { redLightList.Add(roadUser); } if (AddToTrafficLightQueue(lane, roadUser)) { roadUser.Speed = 0; if (!Queue.Contains(lane)) { Queue.Add(lane); } } }
public DriverToPedestrianDecorator(RoadUser roadUser) : base(roadUser) { }
public CyclistToPedestrianDecorator(RoadUser roadUser) : base(roadUser) { }
public PedestrainToCyclistDecorator(RoadUser roadUser) : base(roadUser) { }
public RoadUserDecorator(RoadUser roadUser) { _roadUser = roadUser; XPosition = roadUser.XPosition; YPosition = roadUser.YPosition; }
/// <summary> /// Nieuwe roaduser spawnen /// </summary> public void SpawnRoadUser() { RoadUser newRoadUser = null; int totalRoadusers = 0; foreach (IntersectionControl ic in Intersections) { totalRoadusers += ic.RoadUsers.Count; } IntersectionControl intersection = Intersections[random.Next(6)]; if (totalRoadusers < 300) { if (intersection.IntersectionType == IntersectionType.TYPE_1) { switch (random.Next(4)) { case 0: //south inbound left pedestrian newRoadUser = new Pedestrian(new Point(130, 405), 1); newRoadUser.FaceTo(new Point(130, 0)); break; case 1: //south inbound left road newRoadUser = pickRandomCar(216, 418); newRoadUser.FaceTo(new Point(216, 0)); break; case 2: //south inbound right road newRoadUser = pickRandomCar(244, 418); newRoadUser.FaceTo(new Point(244, 0)); break; case 3: //south inbound right pedestrian newRoadUser = new Pedestrian(new Point(266, 405), 1); newRoadUser.FaceTo(new Point(266, 0)); break; } } else if (intersection.IntersectionType == IntersectionType.TYPE_2) { switch (random.Next(8)) { case 0: //north inbound left pedestrian newRoadUser = new Pedestrian(new Point(266, -5), 1); newRoadUser.FaceTo(new Point(266, 400)); break; case 1: //north inbound right lane newRoadUser = pickRandomCar(156, -18); newRoadUser.FaceTo(new Point(156, 400)); break; case 2: //north inbound left lane newRoadUser = pickRandomCar(186, -18); newRoadUser.FaceTo(new Point(186, 400)); break; case 3: //north inbound right pedestrian newRoadUser = new Pedestrian(new Point(130, -5), 1); newRoadUser.FaceTo(new Point(130, 400)); break; case 4: //west inbound left pedestrian newRoadUser = new Pedestrian(new Point(-5, 130), 1); newRoadUser.FaceTo(new Point(400, 130)); break; case 5: //west inbound left lane newRoadUser = pickRandomCar(-18, 216); newRoadUser.FaceTo(new Point(400, 216)); break; case 6: //west inbound right lane newRoadUser = pickRandomCar(-18, 244); newRoadUser.FaceTo(new Point(400, 244)); break; case 7: //west inbound right pedestrian newRoadUser = new Pedestrian(new Point(-5, 266), 1); newRoadUser.FaceTo(new Point(400, 266)); break; } } else if (intersection.IntersectionType == IntersectionType.TYPE_3) { switch (random.Next(8)) { case 0: //south inbound left pedestrian newRoadUser = new Pedestrian(new Point(130, 405), 1); newRoadUser.FaceTo(new Point(130, 0)); break; case 1: //south inbound left lane newRoadUser = pickRandomCar(216, 418); newRoadUser.FaceTo(new Point(216, 0)); break; case 2: //south inbound right lane newRoadUser = pickRandomCar(244, 418); newRoadUser.FaceTo(new Point(244, 0)); break; case 3: //south inbound right pedestrian newRoadUser = new Pedestrian(new Point(266, 405), 1); newRoadUser.FaceTo(new Point(266, 0)); break; case 4: //west inbound left pedestrian newRoadUser = new Pedestrian(new Point(-5, 130), 1); newRoadUser.FaceTo(new Point(400, 130)); break; case 5: //west inbound left lane newRoadUser = pickRandomCar(-18, 216); newRoadUser.FaceTo(new Point(400, 216)); break; case 6: //west inbound right lane newRoadUser = pickRandomCar(-18, 244); newRoadUser.FaceTo(new Point(400, 244)); break; case 7: //west inbound right pedestrian newRoadUser = new Pedestrian(new Point(-5, 266), 1); newRoadUser.FaceTo(new Point(400, 266)); break; } } else if (intersection.IntersectionType == IntersectionType.TYPE_4) { switch (random.Next(8)) { case 0: //south inbound left pedestrian newRoadUser = new Pedestrian(new Point(130, 405), 1); newRoadUser.FaceTo(new Point(130, 0)); break; case 1: //south inbound left lane newRoadUser = pickRandomCar(216, 418); newRoadUser.FaceTo(new Point(216, 0)); break; case 2: //south inbound right lane newRoadUser = pickRandomCar(244, 418); newRoadUser.FaceTo(new Point(244, 0)); break; case 3: //south inbound right pedestrian newRoadUser = new Pedestrian(new Point(266, 405), 1); newRoadUser.FaceTo(new Point(266, 0)); break; case 4: //east inbound left pedestrian newRoadUser = new Pedestrian(new Point(405, 130), 1); newRoadUser.FaceTo(new Point(0, 130)); break; case 5: //east inbound right lane newRoadUser = pickRandomCar(418, 156); newRoadUser.FaceTo(new Point(0, 156)); break; case 6: //east inbound left lane newRoadUser = pickRandomCar(418, 186); newRoadUser.FaceTo(new Point(0, 186)); break; case 7: //east inbound right pedestrian newRoadUser = new Pedestrian(new Point(405, 266), 1); newRoadUser.FaceTo(new Point(0, 266)); break; } } else if (intersection.IntersectionType == IntersectionType.TYPE_5) { switch (random.Next(4)) { case 0: //north inbound left pedestrian newRoadUser = new Pedestrian(new Point(130, -5), 1); newRoadUser.FaceTo(new Point(130, 400)); break; case 1: //north inbound right lane newRoadUser = pickRandomCar(156, -18); newRoadUser.FaceTo(new Point(156, 400)); break; case 2: //north inbound left lane newRoadUser = pickRandomCar(186, -18); newRoadUser.FaceTo(new Point(186, 400)); break; case 3: //north inbound right pedestrian newRoadUser = new Pedestrian(new Point(266, -5), 1); newRoadUser.FaceTo(new Point(266, 400)); break; } } else if (intersection.IntersectionType == IntersectionType.TYPE_RAILWAY) { switch (random.Next(4)) { case 0: //east inbound left pedestrian newRoadUser = new Pedestrian(new Point(405, 130), 1); newRoadUser.FaceTo(new Point(0, 130)); break; case 1: //east inbound right lane newRoadUser = pickRandomCar(418, 156); newRoadUser.FaceTo(new Point(0, 156)); break; case 2: //east inbound left lane newRoadUser = pickRandomCar(418, 186); newRoadUser.FaceTo(new Point(0, 186)); break; case 3: //east inbound right pedestrian newRoadUser = new Pedestrian(new Point(405, 266), 1); newRoadUser.FaceTo(new Point(0, 266)); break; } } if (newRoadUser != null) { intersection.AddRoadUser(newRoadUser); UpdateStats(newRoadUser, intersection); } } }
public RoadSimulator() { Console.WindowHeight = 88; Console.WindowWidth = 80; var roadUsers = new List <RoadUser>(new RoadUser[] { new Pedestrian(), new Pedestrian(), new Pedestrian(), new Driver(), new Cyclist(), new Cyclist() }); bool collision = false; Random r = new Random(); foreach (var user in roadUsers) { do { user.XPosition = r.Next(Console.WindowWidth); user.YPosition = r.Next(Console.WindowHeight); } while (roadUsers.Where(u => u.XPosition == user.XPosition && u.YPosition == user.YPosition).ToList().Count > 1); } do { Console.Clear(); Console.CursorTop = 0; Console.CursorLeft = 0; for (int i = 0; i < Console.WindowHeight; i++) { for (int j = 0; j < Console.WindowWidth; j++) { List <RoadUser> users = roadUsers.Where(u => u.XPosition == i && u.YPosition == j).ToList(); if (users.Count > 1) { collision = true; Console.Write("X"); } else { RoadUser user = users.FirstOrDefault(); Console.Write(user?.Symbol().ToString() ?? " "); } } } for (int i = 0; i < roadUsers.Count; i++) { RoadUser user = roadUsers[i]; user.Move(); if (user.Symbol() == 'P') { var chance = r.Next(101); if (chance >= 90) { roadUsers[i] = new PedestrainToCyclistDecorator(user); Debug.WriteLine($"Pedestrian became cyclist"); } if (chance <= 10) { roadUsers[i] = new PedestrianToDriverDecorator(user); Debug.WriteLine($"Pedestrian became driver"); } } else if (user.Symbol() == 'C') { var chance = r.Next(101); if (chance >= 85) { roadUsers[i] = new CyclistToPedestrianDecorator(user); Debug.WriteLine($"Cyclist became pedestrian"); } } else if (user.Symbol() == 'D') { var chance = r.Next(101); if (chance >= 95) { roadUsers[i] = new DriverToPedestrianDecorator(user); Debug.WriteLine($"Driver became pedestrian"); } } } Console.ReadLine(); if (collision) { Console.WriteLine("Kolizja! (X - miejsce kolizji)"); } } while (!collision); }
public PedestrianToDriverDecorator(RoadUser roadUser) : base(roadUser) { }