/// <summary>
        /// Adds a vehicle to the world
        /// </summary>
        /// <param name="screenCenter"></param>
        /// <returns></returns>
        public SimVehicle AddVehicle(Coordinates screenCenter)
        {
            // deafualt id is # of elts
            int idNumber = this.WorldService.Obstacles.Count + this.Vehicles.Count;

            // create array to hold used vehicle and obstacle id's
            bool[] usedIds = new bool[this.WorldService.Obstacles.Count + this.Vehicles.Count];

            // loop over obstacles and get ids
            foreach (SimObstacle ism in this.WorldService.Obstacles.Values)
            {
                // make sure num within # of obstacles and vehicles
                if (ism.ObstacleId.Number < this.WorldService.Obstacles.Count + this.Vehicles.Count)
                {
                    // check off
                    usedIds[ism.ObstacleId.Number] = true;
                }
            }

            // loop over vehicles and get ids
            foreach (SimVehicle ism in this.Vehicles.Values)
            {
                // make sure num within # of obstacles and vehicles
                if (ism.VehicleId.Number < this.WorldService.Obstacles.Count + this.Vehicles.Count)
                {
                    // check off
                    usedIds[ism.VehicleId.Number] = true;
                }
            }

            // loop over checked off id's
            for (int i = usedIds.Length - 1; i >= 0; i--)
            {
                // if find a false one set that id
                if (usedIds[i] == false)
                {
                    // set id num
                    idNumber = i;
                }
            }

            // create vehicle id
            SimVehicleId svi = new SimVehicleId(idNumber);

            // get position (center of screen)
            Coordinates position = screenCenter;

            // create state
            SimVehicleState svs = new SimVehicleState();
            svs.canMove = true;
            svs.Heading = new Coordinates(1,0);
            svs.IsBound = false;
            svs.Position = position;
            svs.Speed = 0;
            svs.VehicleID = svi;

            // create vehicle
            SimVehicle stv = new SimVehicle(svs, TahoeParams.VL, TahoeParams.T);

            // add to vehicles
            this.Vehicles.Add(stv.VehicleId, stv);

            // return
            return stv;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Adds a vehicle to the world
        /// </summary>
        /// <param name="screenCenter"></param>
        /// <returns></returns>
        public SimVehicle AddVehicle(Coordinates screenCenter)
        {
            // deafualt id is # of elts
            int idNumber = this.WorldService.Obstacles.Count + this.Vehicles.Count;

            // create array to hold used vehicle and obstacle id's
            bool[] usedIds = new bool[this.WorldService.Obstacles.Count + this.Vehicles.Count];

            // loop over obstacles and get ids
            foreach (SimObstacle ism in this.WorldService.Obstacles.Values)
            {
                // make sure num within # of obstacles and vehicles
                if (ism.ObstacleId.Number < this.WorldService.Obstacles.Count + this.Vehicles.Count)
                {
                    // check off
                    usedIds[ism.ObstacleId.Number] = true;
                }
            }

            // loop over vehicles and get ids
            foreach (SimVehicle ism in this.Vehicles.Values)
            {
                // make sure num within # of obstacles and vehicles
                if (ism.VehicleId.Number < this.WorldService.Obstacles.Count + this.Vehicles.Count)
                {
                    // check off
                    usedIds[ism.VehicleId.Number] = true;
                }
            }

            // loop over checked off id's
            for (int i = usedIds.Length - 1; i >= 0; i--)
            {
                // if find a false one set that id
                if (usedIds[i] == false)
                {
                    // set id num
                    idNumber = i;
                }
            }

            // create vehicle id
            SimVehicleId svi = new SimVehicleId(idNumber);

            // get position (center of screen)
            Coordinates position = screenCenter;

            // create state
            SimVehicleState svs = new SimVehicleState();

            svs.canMove   = true;
            svs.Heading   = new Coordinates(1, 0);
            svs.IsBound   = false;
            svs.Position  = position;
            svs.Speed     = 0;
            svs.VehicleID = svi;

            // create vehicle
            SimVehicle stv = new SimVehicle(svs, TahoeParams.VL, TahoeParams.T);

            // add to vehicles
            this.Vehicles.Add(stv.VehicleId, stv);

            // return
            return(stv);
        }
        private void unbindToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SimVehicle isv = (SimVehicle)this.selected;
            if (this.tracked != null && this.tracked.VehicleId.Equals(isv.VehicleId))
                this.tracked = null;

            ((SimVehicle)this.selected).SimVehicleState.IsBound = false;
            this.Invalidate();
        }
 private void trackToolStripMenuItem_Click(object sender, EventArgs e)
 {
     this.tracked = (SimVehicle)this.selected;
     this.Invalidate();
 }
 private void stopTrackToolStripMenuItem_Click(object sender, EventArgs e)
 {
     this.tracked = null;
     this.Invalidate();
 }
        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.selected is SimVehicle)
            {
                // vehicle
                SimVehicle isv = (SimVehicle)this.selected;

                // notfy
                SimulatorOutput.WriteLine("Removed Vehicle: " + isv.SimVehicleState.VehicleID.ToString());

                // remove
                this.displayObjects.Remove(this.selected);
                this.Simulation.simEngine.Vehicles.Remove(isv.VehicleId);
                this.Simulation.clientHandler.Remove(isv.VehicleId);
                this.Simulation.OnClientsChanged();

                if (this.tracked != null && this.tracked.VehicleId.Equals(isv.VehicleId))
                    this.tracked = null;

                // remove selecation
                this.selected = null;

                // properties
                this.Simulation.simEngine.SetPropertyGridDefault();

                // redraw
                this.Invalidate();
            }
        }
        /// <summary>
        /// What to do when mouse moves
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseMove(MouseEventArgs e)
        {
            #region Left

            if (e.Button == MouseButtons.Left)
            {
                // dragging vehicle
                if (this.selected != null && this.selected is CarDisplayObject && isDragging && this.temporaryCoordinate.HasValue)
                {
                    // Get the offset.
                    Point point = e.Location;

                    // new coord
                    Coordinates newCoord = this.transform.GetWorldPoint(new PointF(point.X, point.Y));

                    // calc offse
                    Coordinates offset = newCoord - this.temporaryCoordinate.Value;

                    // moving object
                    CarDisplayObject cdo = (CarDisplayObject)this.selected;

                    // check we are not tracking
                    if (this.tracked != null && cdo.Equals(this.tracked))
                    {
                        this.tracked = null;
                    }

                    // move
                    cdo.InMove(this.temporaryCoordinate.Value, offset, this.transform);

                    // check snap pos or heading
                    if (cdo.SnapHeading || cdo.SnapPosition)
                    {
                        // filter for vehicles
                        DisplayObjectFilter partitionDof = delegate(IDisplayObject target)
                        {
                            // check if target is network object
                            if (target is ArbiterLanePartition)
                                return true;
                            else
                                return false;
                        };

                        // check to see if selected a partition
                        HitTestResult vhcHtr = this.HitTest(transform.GetWorldPoint(new PointF(e.X, e.Y)), partitionDof);

                        // check hit
                        if (vhcHtr.Hit)
                        {
                            // get partition
                            ArbiterLanePartition alp = (ArbiterLanePartition)vhcHtr.DisplayObject;

                            // heading
                            Coordinates heading = alp.Vector();

                            // position
                            Coordinates closest = alp.PartitionPath.GetPoint(alp.PartitionPath.GetClosestPoint(transform.GetWorldPoint(new PointF(e.X, e.Y))));

                            if (cdo.SnapPosition)
                                cdo.Position = closest;

                            if (cdo.SnapHeading)
                                cdo.Heading = heading;
                        }
                    }
                }
                else if (this.selected != null && this.selected is SimObstacle && isDragging && this.temporaryCoordinate.HasValue)
                {
                    // Get the offset.
                    Point point = e.Location;

                    // new coord
                    Coordinates newCoord = this.transform.GetWorldPoint(new PointF(point.X, point.Y));

                    // calc offse
                    Coordinates offset = newCoord - this.temporaryCoordinate.Value;

                    // moving object
                    SimObstacle so = (SimObstacle)this.selected;

                    // move
                    so.InMove(this.temporaryCoordinate.Value, offset, this.transform);
                }
                // check if user is dragging
                else if (isDragging)
                {
                    // Get the offset.
                    Point point = (Point)controlTag;

                    // Calculate change in position
                    double deltaX = e.X - point.X;
                    double deltaY = e.Y - point.Y;

                    // Update the world
                    Coordinates tempCenter = WorldTransform.CenterPoint;
                    tempCenter.X -= deltaX / WorldTransform.Scale;
                    tempCenter.Y += deltaY / WorldTransform.Scale;
                    WorldTransform.CenterPoint = tempCenter;

                    // update control
                    controlTag = new Point(e.X, e.Y);
                }

                // redraw
                this.Invalidate();
            }

            #endregion

            #region Right

            else if (e.Button == MouseButtons.Right)
            {
                if (this.selected != null && this.selected is SimObstacle && isDragging && this.temporaryCoordinate.HasValue)
                {
                    // Get the offset.
                    Point point = e.Location;

                    // new coord
                    Coordinates newCoord = this.transform.GetWorldPoint(new PointF(point.X, point.Y));

                    // moving object
                    SimObstacle so = (SimObstacle)this.selected;

                    // calc new rel heading
                    Coordinates offset = newCoord - so.Position;

                    // calc degree diff
                    //double rotDiff = offset.ToDegrees() - this.temporaryCoordinate.Value.ToDegrees();

                    // new head
                    //Coordinates newHead = so.Heading.Rotate(rotDiff);

                    // set
                    so.Heading = offset;

                    this.Invalidate();
                }
            }

            #endregion

            base.OnMouseMove(e);
        }