示例#1
0
文件: Car.cs 项目: eddiebrowne/cars
        public void Drive()
        {
            if (IsApproachingCar)
            {
                Deccelerate();
            }

            var distance = Speed / (double)Simulator.Precision;

            if (Position.Point + distance > CurrentRoad.Length)
            {
                distance = CurrentRoad.Length - Position.Point;
            }

            Position.Move(distance);

            if (Destination != null)
            {
                if (ShouldChangeLanes)
                {
                    Position.Lane = (Position.Lane > Destination.Lane) ? Position.Lane-- : Position.Lane++;
                }

                if (IsApproachingDestination)
                {
                    Deccelerate();
                    CurrentRoad = CurrentRoad.ChangeRoad(this, Destination);
                    CurrentRoad.AddCar(this);
                }
            }
        }
示例#2
0
 public static void RenderMap(object sender, PaintEventArgs e)
 {
     if (IsReady)
     {
         CurrentRoad.RenderRoad(UserPanel, e);
         CurrentRoadTransit.RenderRoadTransit(UserPanel, e);
         foreach (var light in TrafficLights)
         {
             TrafficLight.RenderLight(light, UserPanel, e);
         }
         foreach (var c in Cars)
         {
             e.Graphics.DrawImage(c.Sprite, new Point(c.X, c.Y));
         }
         foreach (var p in Peoples)
         {
             e.Graphics.DrawImage(p.Sprite, new Point(p.X, p.Y));
         }
     }
     else if (Clear)
     {
         e.Graphics.Clear(Color.AliceBlue);
         Clear = false;
     }
 }
        public override void Tick(TrafficSim sim)
        {
            //if the car has driven to its destination then remove the car
            //from the simulation
            if (NextRoad == null)
            {
                sim.RemoveVehicle(this);
                return;
            }

            Vector2 roadDir   = Vector2.Normalize(CurrentRoad.RoadEnd - CurrentRoad.RoadStart);
            Vector2 newCarPos = Pos + roadDir * MaxVelocity;

            //if car is still on the same road then just move the car
            if (CurrentRoad.PosWithinRoad(newCarPos))
            {
                Pos = newCarPos;
                return;
            }

            //car position puts it at the next road so ask the
            //intersection if the car can be moved to the next road
            if (!CurrentRoad.CanPassIntersection(NextRoad))
            {
                Pos = CurrentRoad.RoadEnd;
                return;
            }

            //move car to the next road
            CurrentRoad.RemoveVehicle(this);
            CurrentRoad = NextRoad;
            CurrentRoad.AddVehicle(this);

            //car moved to th next road so remove it from the path
            Path.RemoveAt(0);
        }