Exemple #1
0
        public Car(SolidColorBrush c, Road rd, Car frontCar = null)
        {
            _speed = 2;
            _height = 10;
            _length = 25;
            PosX = rd.CarX + Road.Height / (float)2;
            PosY = rd.CarY + Road.Height / (float)2;
            _color = c ?? Brushes.OrangeRed;

            _park = null;

            _road = rd;
            _angle = rd.SensAngle;

            this.FrontCar = frontCar;

            ++CPT;
            Id = CPT;

            MainRect = new ShapeRectangle
            {
                Height = this.Height,
                Width = this.Length,
                Margin = new Thickness(PosX, PosY, 0, 0),
                Stroke = BorderColor,
                Fill = Color
            };

            rd.Cars.Add(this);

            //((MainWindow)((App)Application.Current).MainWindow).doDrawEvent += Draw;
        }
Exemple #2
0
 private void WinOnRemoveCarEvent(Car car)
 {
     Cars.Remove(car);
 }
Exemple #3
0
        //private bool IsOverRoad(Road r)
        //{
        //    bool ret = false;
        //    Rect roadRectangle = new Rect(r.MainRectangle.RenderSize);
        //    Rect thisRectangle = new Rect(MainRect.RenderSize);
        //    if (roadRectangle.Contains(new Point(PosX, PosY)))
        //        ret = true;
        //    //Rect roadRectangle = new Rect()
        //    return ret;
        //}
        private void ChangeRoad(Road road)
        {
            road.Cars.Add(this);
            _road.Cars.Remove(this);

            _road = road;

            if (_angle.Equals(Math.PI/-2))
                PosY -= 15;
            else if (_angle.Equals(Math.PI / 2))
                PosY += 15;

            _angle = road.SensAngle;

            // changer la voiture de devant
            float dist = 1000;
            double tmpDist;
            Car[] carz = road.Cars.Where(c => c.PosX > PosX).ToArray();
            Car closest = null;

            // get closest car
            foreach (Car car in carz)
            {
                if (car.Equals(this))
                    continue;

                tmpDist = DistanceTo(car);
                if (tmpDist < dist)
                {
                    dist = (float) tmpDist;
                    closest = car;
                }
            }

            Car prevCar = _road.Cars.FirstOrDefault(c => c.FrontCar == closest);
            if(prevCar != null)
                prevCar.FrontCar = this;
            FrontCar = closest;

            ChangedRoad = true;
        }
Exemple #4
0
 protected bool Equals(Car other)
 {
     return PosX.Equals(other.PosX) && PosY.Equals(other.PosY) && this._angle.Equals(other._angle) &&
            this._length == other._length && this._speed.Equals(other._speed) && this._height == other._height &&
            Equals(this._color, other._color);
 }
Exemple #5
0
        public void Advance(float step = (float) 0.8)
        {
            //if (IsOutOfMap())
            //{

            //}
            Road closeRoad = null;

            if((_angle.Equals(Math.PI / 2) || _angle.Equals(Math.PI / -2)) && !ChangedRoad)
                closeRoad = NearXRoad();

            if (closeRoad != null)
            {
                Debug.WriteLine("close to road : " + closeRoad);
                ChangeRoad(closeRoad);
            }

            PosX += (float)Math.Cos(_angle)*_speed*step;
            PosY += (float)Math.Sin(_angle)*_speed*step;

            IEnumerable<Car> list = this.CollidingCars();

            BorderColor = Color;
            if (list.Any())
            {
                BorderColor = Brushes.Red;

                foreach (Car car in list)
                {
                    if (!(car.PosX > PosX)) continue;

                    FrontCar = car;
                    break;
                }
            }

            double distanceToLight = HandleLight();

            double distanceToCar = 1000;

            if (FrontCar != null)
                distanceToCar = DistanceTo(FrontCar);

            if (distanceToLight <= DistToLight)
                DistToLight = distanceToLight;
            else
                LightPassed = true;

            AdaptSpeed(LightPassed || distanceToCar < distanceToLight ? distanceToCar : distanceToLight );
        }
Exemple #6
0
        private static void CreateCar()
        {
            if (Map.TotalCars >= Map.MAXCAR)
                return;

            Road road;
            double carX = 0;
            int nbRoad = Map.Roads.Count;
            int rand = Map.GetRandomInt(0, nbRoad);
            var carColor = RandomBrush();

            road = Map.Roads[rand];
            //road = Map.Roads[3];

            Map.TotalCars ++;

            // do road.AddCar
            Car frontCar = road.LastCar,
                car = new Car(carColor, road, frontCar);

            //Debug.WriteLine("------------Car " + car.Id + " created behind " + frontCar?.Id + " on road having PosY=" + road.PosY);
        }