Exemplo n.º 1
0
        private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            lblStatusText.Text = "Loading...";

            fileDialog.Filter = "Data Files (.dat)|*.dat|All Files (*.*)|*.*";

            fileDialog.InitialDirectory = Application.StartupPath + "\\Maps\\";
            DialogResult result = fileDialog.ShowDialog();

            if (result == DialogResult.OK) // Test result.
            {
                string file = fileDialog.FileName;

                string mapname = "";
                int mapwidth = 0;
                int mapheight = 0;

                bool readingHeader = false;
                bool readingTiles = false;
                bool readingObjects = false;
                bool readingLights = false;

                MapTile[,] mapArray = new MapTile[0, 0];
                List<MapObject> mapObjects = new List<MapObject>();
                List<MapLight> mapLights = new List<MapLight>();
                //init maparray here

                try
                {

                    StreamReader reader = new StreamReader(file);
                    while (!reader.EndOfStream)
                    {

                        string line = reader.ReadLine();
                        Console.WriteLine(line);

                        if (line == "[Header]")
                        {
                            readingHeader = true;
                        }

                        if (readingHeader)
                        {
                            if (line != "[Header]" && line != "[/Header]") //ignore start and end
                            {

                                String[] unparsedLine = line.Split(':');
                                mapname = unparsedLine[0];
                                mapwidth = int.Parse(unparsedLine[1]);
                                mapheight = int.Parse(unparsedLine[2]);

                            }

                            if (line == "[/Header]")
                            {
                                readingHeader = false;
                                break;

                            }

                        }
                    }
                    mapArray = new MapTile[mapwidth, mapheight];
                    reader.Close();

                }
                catch (IOException ex)
                {
                    Console.WriteLine(ex.Message);
                }

                try
                {
                    StreamReader reader = new StreamReader(file);
                    while (!reader.EndOfStream)
                    {

                        string line = reader.ReadLine();
                        Console.WriteLine(line);
                        if (line == "[Tiles]")
                        {
                            readingTiles = true;
                        }

                        if (readingTiles)
                        {
                            if (line != "[Tiles]" && line != "[/Tiles]") //ignore start and end
                            {
                                //parse em
                                //3 parts x, ,y id
                                int X;
                                int Y;
                                int ID;

                                string[] unparsedLine = line.Split(':');
                                X = int.Parse(unparsedLine[0]);
                                Y = int.Parse(unparsedLine[1]);
                                ID = int.Parse(unparsedLine[2]);

                                MapTile tempTile = new MapTile(X, Y, ID);
                                mapArray[X, Y] = tempTile;
                            }
                        }

                        if (line == "[/Tiles]")
                        {
                            readingTiles = false;
                        }

                        if (line == "[Objects]")
                        {
                            readingObjects = true;
                        }

                        if (readingObjects)
                        {
                            if (line != "[Objects]" && line != "[/Objects]") //ignore start and end
                            {
                                //3 parts x, ,y id
                                int X;
                                int Y;
                                int ID;
                                //Console.WriteLine(line);
                                string[] unparsedLine = line.Split(':');
                                X = int.Parse(unparsedLine[0]);
                                Y = int.Parse(unparsedLine[1]);
                                ID = int.Parse(unparsedLine[2]);

                                MapObject tempObject = new MapObject(X, Y, ID);
                                mapObjects.Add(tempObject);
                            }
                        }

                        if (line == "[/Objects]")
                        {
                            readingObjects = false;
                        }

                        if (line == "[Lights]")
                        {
                            readingLights = true;

                        }

                        if (readingLights)
                        {
                            if (line != "[Lights]" && line != "[/Lights]")
                            {
                                string[] unparsedLine = line.Split(':');
                                int X = int.Parse(unparsedLine[0]);
                                int Y = int.Parse(unparsedLine[1]);
                            }
                        }

                        if (line == "[/Lights]")
                        {
                            readingLights = false;
                        }

                        this.mapControl.map = null;
                        this.mapControl.map = new Map(mapControl, mapwidth, mapheight, mapname);
                        this.mapControl.map.setMapArray(mapArray);
                        this.mapControl.map.setMapObjects(mapObjects);
                        this.mapControl.map.setMapLights(mapLights);
                    }
                    reader.Close();
                }
                catch (IOException exc)
                {
                }
            }
            //Console.WriteLine(result); // <-- For debugging use only.
            lblStatusText.Text = "Done.";
        }
Exemplo n.º 2
0
 public void addObject(MapObject i)
 {
     this.objectArray.Add(i);
 }