//methode maakt een kopie van de huidige tile die net getekend is, zodat dezelfde tile nog een keer getekend kan worden. public static Tile CopyCurrentTile(SimControl s,Tile startTile) { Tile tile; string tileName = startTile.name; switch (tileName) { case "Spawner": Spawner currentSpawnerTile = (Spawner)startTile; tile = new Spawner(s, currentSpawnerTile.Direction); break; case "Crossroad": tile = new Crossroad(s); break; case "Road": Road currentRoadTile = (Road)startTile; tile = new Road(currentRoadTile.StartDirection, currentRoadTile.EndDirection); break; case "Fork": Fork currentForkTile = (Fork)startTile; tile = new Fork(s, currentForkTile.NotDirection); break; default: tile = new Crossroad(s); break; } return tile; }
private void RotateTile(string LeftOrRight) { int difference; if (LeftOrRight == "Right") difference = 0; else difference = +2; Tile originalTile = windowselect.simwindow.simcontrol.selectedTile; SimControl simcontrol = windowselect.simwindow.simcontrol; Tile rotatedTile; switch (originalTile.name) { case "Fork": rotatedTile = new Fork(simcontrol, (originalTile.NotDirection+difference) % 4 + 1); break; case "Crossroad": rotatedTile = new Crossroad(simcontrol); break; case "Road": rotatedTile = new Road((originalTile.StartDirection+difference )% 4 + 1, (originalTile.EndDirection +difference)% 4 + 1); break; case "Spawner": rotatedTile = new Spawner(windowselect.simwindow.simcontrol, (originalTile.Direction + difference) % 4 + 1); break; default: rotatedTile = new Crossroad(simcontrol); break; } simcontrol.DrawTile(originalTile.position, rotatedTile); windowselect.simwindow.simcontrol.DrawSelectLine(originalTile.position); }
/// <summary> /// Click Open in homescreen /// Loads previous saved files /// </summary> public void Open_Click() { Tile[] tempTileList = new Tile[(windowselect.simwindow.simcontrol.Size.Width / 100) * (windowselect.simwindow.simcontrol.Size.Height / 100)]; /// Does this work? SimControl simcontrol; simcontrol = windowselect.simwindow.simcontrol; simcontrol.oldselectedTile = null; /// Make new stream Stream myStream1 = null; Stream myStream2 = null; Stream dummyStream = null; int addLines = 0; int totalLines; /// New open dialog OpenFileDialog openDialog = new OpenFileDialog(); /// Few basic settings for the opendialog //openFileDialog1.InitialDirectory = "c:\\"; openDialog.Filter = "TrafficSimulation files (*.trs)|*.trs"; openDialog.FilterIndex = 1; openDialog.RestoreDirectory = true; if (openDialog.ShowDialog() == DialogResult.OK) { /// Change cursor to wait cursor this.Cursor = Cursors.WaitCursor; /// Make new loadwindow with the startposition in the middle LoadWindow LoadWin = new LoadWindow(); LoadWin.StartPosition = FormStartPosition.CenterScreen; LoadWin.FormBorderStyle = FormBorderStyle.FixedDialog; LoadWin.ControlBox = false; /// Try to open the file try { if (openDialog.OpenFile() != null) { /// Make 3 streams, 1 for the road and spawners, 2 for the forks and crossroads and the dummy for counting myStream1 = openDialog.OpenFile(); myStream2 = openDialog.OpenFile(); dummyStream = openDialog.OpenFile(); /// Streamreaders for each stream StreamReader dummyReader = new StreamReader(dummyStream); StreamReader r1, r2; /// Set the number of lines of code that has to be deleted and added addLines = 0; /// Count the numbers of lines in the file string line; while ((line = dummyReader.ReadLine()) != null) { addLines++; } /// Total amount of lines that has to be run through while loading totalLines = addLines * 2; /// Set maximums of the progressbar LoadWin.progressBar1.Maximum = totalLines; /// Show the LoadWindow LoadWin.Show(); /// Clear the map simcontrol.backgroundBC.ClearBitmap(); simcontrol.backgroundBC.bitmap.MakeTransparent(Color.Green); simcontrol.trafficlightBC.ClearBitmap(); simcontrol.trafficlightBC.bitmap.MakeTransparent(Color.Green); simcontrol.Invalidate(); /// Clear the list simcontrol.simulationMap.ClearTileList(); #region Add roads and spawners to the map /// Add al the roads to the map using (myStream1) { // Streamreader for stream 1 r1 = new StreamReader(myStream1); // Set value of progressBar1 back to 0 and change maximum LoadWin.progressBar1.Value = 0; LoadWin.progressBar1.Maximum = (addLines * 2); /// Read the file line for line while (r1.Peek() >= 0) { string t = r1.ReadLine(); // Char that splits the data char[] splitChar = { '_' }; // Array of strings with info about tile string[] information = t.Split(splitChar); Bitmap tileImage; Tile currentBuildTile; int roadX; int roadY; int tilesHorizontal = Size.Width / 100; /// You need different information from different tiles /// So you need multiple cases, one for each tile /// /// Basic information /// 0: Tile /// 1: X position /// 2: Y position /// Specific information /// 3: Trafficlight strat /// 4: Maxspeed for a tile /// 5: Begin direction (notDirection for Fork, direction for Spawner) /// 6: End direction (only for Road) /// 7: laneshightolow, not for Crossroad and Fork /// 8: laneslowtohigh, not for Crossroad and Fork /// 9: Cars per second in spawner switch (information[0]) { case "Road": /// Make new tile currentBuildTile = new Road(Convert.ToInt32(information[5]), Convert.ToInt32(information[6])); /// Get the location roadX = Convert.ToInt32(information[1]) / 100; roadY = Convert.ToInt32(information[2]) / 100; /// Set some values currentBuildTile.SetValues(simcontrol, new Point((roadX * 100), roadY * 100)); currentBuildTile.LanesHighToLow = Convert.ToInt32(information[7]); currentBuildTile.LanesLowToHigh = Convert.ToInt32(information[8]); currentBuildTile.maxSpeed = Convert.ToInt32(information[4]); /// Add to list tempTileList[(roadX + roadY * 20)] = currentBuildTile; /// Draw the tile tileImage = currentBuildTile.DrawImage(); simcontrol.backgroundBC.AddObject(tileImage, new Point(roadX * 100, roadY * 100)); /// Add 1 to progressBar1/2 LoadWin.progressBar1.PerformStep(); break; /// Load a spawner to the list case "Spawner": /// Make new tile currentBuildTile = new Spawner(windowselect.simwindow.simcontrol, Convert.ToInt32(information[5])); Spawner spawner = (Spawner)currentBuildTile; /// Get location roadX = Convert.ToInt32(information[1]) / 100; roadY = Convert.ToInt32(information[2]) / 100; /// Set some values spawner.maxSpeed = Convert.ToInt32(information[4]); spawner.UpdateLanes(simcontrol, Convert.ToInt32(information[5]), Convert.ToInt32(information[7]), Convert.ToInt32(information[8])); spawner.SetValues(simcontrol, new Point((roadX * 100), roadY * 100)); spawner.CarsSpawnChance = Convert.ToInt32(information[9]); /// Add to list tempTileList[(roadX + roadY * 20)] = currentBuildTile; /// Draw the tile tileImage = currentBuildTile.DrawImage(); simcontrol.backgroundBC.AddObject(tileImage, new Point(roadX * 100, roadY * 100)); simcontrol.Invalidate(); /// Add 1 to progressBar1/2 LoadWin.progressBar1.PerformStep(); break; default: break; } } /// Copy the tempTileList to tileList foreach (Tile tile in tempTileList) { if (tile != null) { windowselect.simwindow.simcontrol.simulationMap.AddTile(tile); } } } #endregion #region Add the rest to the map using (myStream2) { r2 = new StreamReader(myStream2); tempTileList = new Tile[(windowselect.simwindow.simcontrol.Size.Width / 100) * (windowselect.simwindow.simcontrol.Size.Height / 100)]; /// Read the file line for line while (r2.Peek() >= 0) { string t = r2.ReadLine(); // Char that splits the string char[] splitChar = { '_' }; // Array of strings with info about tile string[] information = t.Split(splitChar); Bitmap tileImage; Tile currentBuildTile; int roadX; int roadY; int tilesHorizontal = Size.Width / 100; /// You need different information from different tiles /// So you need multiple cases, one for each tile /// /// Basic information /// 0: Tile /// 1: X position /// 2: Y position /// Specific information /// 3: Trafficlight strat /// 4: Maxspeed for a tile /// 5: Begin direction (notDirection for Fork, direction for Spawner) /// 6: End direction (only for Road) /// 7: laneshightolow, not for Crossroad and Fork /// 8: laneslowtohigh, not for Crossroad and Fork /// 9: Cars per second in spawner switch (information[0]) { /// Load a fork into the list case "Fork": /// Make new tile currentBuildTile = new Fork(simcontrol, Convert.ToInt32(information[5])); /// Get the location roadX = Convert.ToInt32(information[1]) / 100; roadY = Convert.ToInt32(information[2]) / 100; /// Set some values currentBuildTile.SetValues(simcontrol, new Point((roadX * 100), roadY * 100)); currentBuildTile.maxSpeed = Convert.ToInt32(information[4]); currentBuildTile.GetControl().strat = Convert.ToInt32(information[3]); /// Add to list tempTileList[(roadX + roadY * 20)] = currentBuildTile; /// Draw the tile tileImage = currentBuildTile.DrawImage(); simcontrol.backgroundBC.AddObject(tileImage, new Point(roadX * 100, roadY * 100)); simcontrol.Invalidate(); /// Add 1 to progressBar1/2 LoadWin.progressBar1.PerformStep(); break; /// Load a crossroad to the list case "Crossroad": /// Make new tile currentBuildTile = new Crossroad(simcontrol); /// Get location roadX = Convert.ToInt32(information[1]) / 100; roadY = Convert.ToInt32(information[2]) / 100; /// Set some values currentBuildTile.SetValues(simcontrol, new Point((roadX * 100), roadY * 100)); currentBuildTile.maxSpeed = Convert.ToInt32(information[4]); currentBuildTile.GetControl().strat = Convert.ToInt32(information[3]); /// Add to list tempTileList[(roadX + roadY * 20)] = currentBuildTile; /// Draw the tile tileImage = currentBuildTile.DrawImage(); simcontrol.backgroundBC.AddObject(tileImage, new Point(roadX * 100, roadY * 100)); simcontrol.Invalidate(); /// Add 1 to progressBar1/2 LoadWin.progressBar1.PerformStep(); break; default: break; } } /// Copy the tempTileList to tileList foreach (Tile tile in tempTileList) { if (tile != null) { windowselect.simwindow.simcontrol.simulationMap.AddTile(tile); } } } #endregion } /// Set the current building tile to a straight road simcontrol.currentBuildTile = new Road(1, 3); ///Set the state to selected simcontrol.state = "selected"; LoadWin.Close(); /// Set cursor back to an arrow this.Cursor = Cursors.Arrow; /// New screen windowselect.New(); } /// Throw exception when something is wrong catch (Exception ex) { LoadWin.Close(); this.Cursor = Cursors.Arrow; MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message); } } }