public void TestCarSendMessage()
        {
            int SignatureId = 5;
            Car TestCar     = new Car(SignatureId);

            //Send Brakewarning
            BrakeWarning brakeWarning1 = new BrakeWarning(DateTime.Now, new Point(0, 0));

            TestCar.SendMessage(brakeWarning1);
            //Send second brakewarning (wont send to avoid spam)
            BrakeWarning brakeWarning2 = new BrakeWarning(DateTime.Now, new Point(0, 0));

            TestCar.SendMessage(brakeWarning2);
            //Send emergency brake warning (will send)
            EmergencyBrakeWarning emergencyBrakeWarning1 = new EmergencyBrakeWarning(DateTime.Now, new Point(0, 0));

            TestCar.SendMessage(emergencyBrakeWarning1);
            //Send sencond emergency brake (wont send to avoid spam)
            EmergencyBrakeWarning emergencyBrakeWarning2 = new EmergencyBrakeWarning(DateTime.Now, new Point(0, 0));

            TestCar.SendMessage(emergencyBrakeWarning2);

            Assert.AreEqual(brakeWarning1, TestCar.GetMessages[0]);
            Assert.AreEqual(emergencyBrakeWarning1, TestCar.GetMessages[1]);
            Assert.AreEqual(2, TestCar.GetMessages.Count);
        }
        public void TestBrakeWarningConstructor()
        {
            DateTime TimeSend = DateTime.Now;
            Point    Location = new Point(1, 5);

            BrakeWarning warning = new BrakeWarning(TimeSend, Location);

            Assert.AreEqual(TimeSend, warning.TimeSend);
            Assert.AreEqual(Location, warning.Location);
        }
Exemplo n.º 3
0
        private void DetectCollision()
        {
            CarsBrake.Clear();
            CarsEmergencyBrake.Clear();
            int MinDistanceBrake          = 60;
            int MinDistanceEmergencyBrake = 30;

            foreach (Car Car1 in Cars)
            {
                foreach (Car Car2 in Cars)
                {
                    if (Car1 != Car2)
                    {
                        //------------------------------------------------
                        //Predict next car locations
                        //------------------------------------------------
                        Point NextPosCar1 = new Point(
                            Car1.Position.X + Car1.Path.Direction.X * 2,
                            Car1.Position.Y + Car1.Path.Direction.Y * 2
                            );
                        Point NextPosCar2 = new Point(
                            Car2.Position.X + Car2.Path.Direction.X * 2,
                            Car2.Position.Y + Car2.Path.Direction.Y * 2
                            );

                        //------------------------------------------------
                        //Check if distance is decreasing
                        //------------------------------------------------
                        //New Distance
                        //------------------------------------------------
                        int    dX          = NextPosCar1.X - NextPosCar2.X;
                        int    dY          = NextPosCar1.Y - NextPosCar2.Y;
                        double NewDistance = Math.Sqrt(Math.Pow(dX, 2) + Math.Pow(dY, 2));
                        //------------------------------------------------
                        //Old Distance
                        //------------------------------------------------
                        dX = Car1.Position.X - Car2.Position.X;
                        dY = Car1.Position.Y - Car2.Position.Y;
                        double OldDistance = Math.Sqrt(Math.Pow(dX, 2) + Math.Pow(dY, 2));
                        //------------------------------------------------
                        //Comparing
                        //------------------------------------------------
                        bool Decreasing = NewDistance < OldDistance;
                        bool Brake = NewDistance <MinDistanceBrake && NewDistance> MinDistanceEmergencyBrake && Decreasing;
                        bool EmergencyBrake = NewDistance < MinDistanceEmergencyBrake && Decreasing;

                        //------------------------------------------------
                        //Check if the cars are driving towards eachother
                        //------------------------------------------------
                        //Car1
                        //------------------------------------------------
                        dX = NextPosCar1.X - Car2.Position.X;
                        dY = NextPosCar1.Y - Car2.Position.Y;
                        double Distance_Car1NewPos_Car2OldPos = Math.Sqrt(Math.Pow(dX, 2) + Math.Pow(dY, 2));
                        bool   Car1DrivesTowardsCar2          = Distance_Car1NewPos_Car2OldPos < OldDistance;
                        //------------------------------------------------
                        //Car2
                        //------------------------------------------------
                        dX = NextPosCar2.X - Car1.Position.X;
                        dY = NextPosCar2.Y - Car1.Position.Y;
                        double Distance_Car2NewPos_Car1OldPos = Math.Sqrt(Math.Pow(dX, 2) + Math.Pow(dY, 2));
                        bool   Car2DrivesTowardsCar1          = Distance_Car2NewPos_Car1OldPos < OldDistance;

                        if (Brake)
                        {
                            if (!CarsBrake.Contains(Car1) && Car1DrivesTowardsCar2)
                            {
                                CarsBrake.Add(Car1);
                            }
                            if (!CarsBrake.Contains(Car2) && Car2DrivesTowardsCar1)
                            {
                                CarsBrake.Add(Car2);
                            }
                        }
                        else if (EmergencyBrake)
                        {
                            if (!CarsEmergencyBrake.Contains(Car1) && Car1DrivesTowardsCar2)
                            {
                                CarsEmergencyBrake.Add(Car1);
                            }
                            if (!CarsEmergencyBrake.Contains(Car2) && Car2DrivesTowardsCar1)
                            {
                                CarsEmergencyBrake.Add(Car2);
                            }
                        }
                    }
                }
            }
            foreach (Car c in CarsBrake)
            {
                if (!c.IsLost)
                {
                    BrakeWarning bw = new BrakeWarning(DateTime.Now, c.Position);
                    c.SendMessage(bw);
                    //locClient.SendWarning((byte)c.SignatureId);
                }
            }
            foreach (Car c in CarsEmergencyBrake)
            {
                if (!c.IsLost)
                {
                    EmergencyBrakeWarning bw = new EmergencyBrakeWarning(DateTime.Now, c.Position);
                    c.SendMessage(bw);
                    //locClient.SendEmergencyWarning((byte)c.SignatureId);
                }
            }
        }