Example #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;
        }
Example #2
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;
        }