Esempio n. 1
0
        public static TileLayer FromFile(ContentManager content, string filename)
        {
            TileLayer tileLayer;
            bool readingTextures = false;
            bool readingLayout = false;
            List<string> textureNames = new List<string>();
            List<List<int>> tempLayout = new List<List<int>>(); // inner list = single row, list of rows is grid
            using (StreamReader reader = new StreamReader(filename))
            {
                while (!reader.EndOfStream)
                {
                    string line = reader.ReadLine().Trim();

                    if (string.IsNullOrEmpty(line))
                        continue;
                    if (line.Contains("[Textures]"))
                    {
                        readingTextures = true;
                        readingLayout = false;
                    }
                    else if (line.Contains("[Layout]"))
                    {
                        readingLayout = true;
                        readingTextures = false;
                    }
                    else if (readingTextures)
                    {
                        textureNames.Add(line);
                    }
                    else if (readingLayout)
                    {
                        List<int> row = new List<int>();

                        string[] cells = line.Split(' ');

                        foreach (string c in cells)
                        {
                            if (!string.IsNullOrEmpty(c))
                                row.Add(int.Parse(c));
                        }

                        tempLayout.Add(row);
                    }
                }
            }

            int width = tempLayout[0].Count;
            int height = tempLayout.Count; // # of rows

            tileLayer = new TileLayer(width, height);

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    tileLayer.SetCellIndex(x,y,tempLayout[y][x]);
                }
            }
            tileLayer.LoadTileTextures(content, textureNames.ToArray());

                return tileLayer;
        }
Esempio n. 2
0
        private void adjustSizeOfMapToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            sizeForm form = new sizeForm();
            form.ShowDialog();
            TileMap tempTileMap = new TileMap();
            TileLayer templayer;
            CollisionLayer tempcolllayer;

            if (form.OKPressed)
            {
                tempTileMap = new TileMap();
                if (int.Parse(form.leftNegTextBox.Text) > 0 || int.Parse(form.leftPosTextBox.Text) > 0 ||
                    int.Parse(form.upNegTextBox.Text) > 0 || int.Parse(form.upPosTextBox.Text) > 0)
                {
                    foreach (TileLayer layer in tileMap.Layers)
                    {
                        templayer = new TileLayer(layer.Width + int.Parse(form.leftPosTextBox.Text) -
                            int.Parse(form.leftNegTextBox.Text), layer.Height + int.Parse(form.upPosTextBox.Text) -
                            int.Parse(form.upNegTextBox.Text));

                        for (int x = 0; x < templayer.Width; x++)
                        {
                            for (int y = 0; y < templayer.Height; y++)
                            {
                                if (y < int.Parse(form.upPosTextBox.Text) - int.Parse(form.upNegTextBox.Text) || x < int.Parse(form.leftPosTextBox.Text) - int.Parse(form.leftNegTextBox.Text))
                                    templayer.SetCellIndex(x, y, -1);
                                else
                                    templayer.SetCellIndex(x, y, layer.GetCellIndex(x - int.Parse(form.leftPosTextBox.Text) + int.Parse(form.leftNegTextBox.Text),
                                        y - int.Parse(form.upPosTextBox.Text) + int.Parse(form.upNegTextBox.Text)));
                            }
                        }
                        foreach (Texture2D textures in layer.ListofTextures)
                        {
                            templayer.AddTexture(textures);
                        }

                        foreach (TreasureChest chest in layer.ListofChests)
                        {
                            chest.Postition.X += int.Parse(form.leftPosTextBox.Text) - int.Parse(form.leftNegTextBox.Text);
                            chest.Postition.Y += int.Parse(form.upPosTextBox.Text) - int.Parse(form.upNegTextBox.Text);
                            if (chest.Postition.X > templayer.Width || chest.Postition.Y > templayer.Height)
                                continue;
                            templayer.AddChest(chest);
                        }

                        foreach (Door door in layer.ListofDoors)
                        {
                            door.Postition.X += int.Parse(form.leftPosTextBox.Text) - int.Parse(form.leftNegTextBox.Text);
                            door.Postition.Y += int.Parse(form.upPosTextBox.Text) - int.Parse(form.upNegTextBox.Text);
                            if (door.Postition.X > templayer.Width || door.Postition.Y > templayer.Height)
                                continue;
                            templayer.AddDoor(door);
                        }
                        tempTileMap.Layers.Add(templayer);
                    }
                    tempcolllayer = new CollisionLayer(tileMap.CollisionLayer.Width + int.Parse(form.leftPosTextBox.Text) -
                          int.Parse(form.leftNegTextBox.Text), tileMap.CollisionLayer.Height + int.Parse(form.upPosTextBox.Text) -
                          int.Parse(form.upNegTextBox.Text));
                    for (int x = 0; x < tempcolllayer.Width; x++)
                    {
                        for (int y = 0; y < tempcolllayer.Height; y++)
                        {
                            if (y < int.Parse(form.upPosTextBox.Text) - int.Parse(form.upNegTextBox.Text) || x < int.Parse(form.leftPosTextBox.Text) - int.Parse(form.leftNegTextBox.Text))
                                tempcolllayer.SetCellIndex(x, y, -1);
                            else
                                tempcolllayer.SetCellIndex(x, y, tileMap.CollisionLayer.GetCellIndex(x - int.Parse(form.leftPosTextBox.Text) + int.Parse(form.leftNegTextBox.Text),
                                    y - int.Parse(form.upPosTextBox.Text) + int.Parse(form.upNegTextBox.Text)));
                        }
                    }
                    tempTileMap.CollisionLayer = tempcolllayer;
                    tileMap = tempTileMap;
                    Dictionary<string, TileLayer> templayerdict = new Dictionary<string, TileLayer>();
                    int count = 0;
                    foreach (string layer in layerDict.Keys)
                    {
                        templayerdict.Add(layer, tileMap.Layers[count]);
                        count++;
                    }
                    layerDict = templayerdict;
                    currentLayer = null;
                    layerListBox.SelectedItem = null;
                }

                if (int.Parse(form.rightNegTextBox.Text) > 0 || int.Parse(form.rightPosTextBox.Text) > 0 ||
                    int.Parse(form.downNegTextBox.Text) > 0 || int.Parse(form.downPosTextBox.Text) > 0)
                {
                    tempTileMap = new TileMap();
                    foreach (TileLayer layer in tileMap.Layers)
                    {
                        templayer = new TileLayer(layer.Width + int.Parse(form.rightPosTextBox.Text) -
                            int.Parse(form.rightNegTextBox.Text), layer.Height + int.Parse(form.downPosTextBox.Text) -
                            int.Parse(form.downNegTextBox.Text));

                        for (int x = 0; x < templayer.Width; x++)
                        {
                            for (int y = 0; y < templayer.Height; y++)
                            {
                                templayer.SetCellIndex(x, y, layer.GetCellIndex(x, y));
                            }
                        }

                        foreach (Texture2D textures in layer.ListofTextures)
                        {
                            templayer.AddTexture(textures);
                        }

                        foreach (TreasureChest chest in layer.ListofChests)
                        {
                            if (chest.Postition.X > templayer.Width || chest.Postition.Y > templayer.Height)
                                continue;
                            templayer.AddChest(chest);
                        }

                        foreach (Door door in layer.ListofDoors)
                        {
                            if (door.Postition.X > templayer.Width || door.Postition.Y > templayer.Height)
                                continue;
                            templayer.AddDoor(door);
                        }
                        tempTileMap.Layers.Add(templayer);
                    }

                    tempcolllayer = new CollisionLayer(tileMap.CollisionLayer.Width + int.Parse(form.rightPosTextBox.Text) -
                            int.Parse(form.rightNegTextBox.Text), tileMap.CollisionLayer.Height + int.Parse(form.downPosTextBox.Text) -
                            int.Parse(form.downNegTextBox.Text));
                    for (int y = 0; y < tempcolllayer.Height; y++)
                    {
                        for (int x = 0; x < tempcolllayer.Width; x++)
                        {
                            tempcolllayer.SetCellIndex(x, y, tileMap.CollisionLayer.GetCellIndex(x, y));
                        }
                    }
                    tempTileMap.CollisionLayer = tempcolllayer;
                    tileMap = tempTileMap;
                    Dictionary<string, TileLayer> templayerdict = new Dictionary<string, TileLayer>();
                    int count = 0;
                    foreach (string layer in layerDict.Keys)
                    {
                        templayerdict.Add(layer, tileMap.Layers[count]);
                        count++;
                    }
                    layerDict = templayerdict;
                    currentLayer = null;
                    layerListBox.SelectedItem = null;
                }
            }
        }
Esempio n. 3
0
        private static TileLayer ProcessFile(string filename, List<string> textureNames)
        {
            TileLayer tileLayer;
            List<TreasureChest> treasurech = new List<TreasureChest>();
            List<Door> dooor = new List<Door>();
            List<int> tempLayout = new List<int>();
            Dictionary<string, string> properties = new Dictionary<string, string>();
            int width = 0;
            int height = 0;

            XmlTextReader reader = new XmlTextReader(filename);
            reader.WhitespaceHandling = WhitespaceHandling.None;

            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    if (reader.Name == "Texture")
                    {
                        string file = reader["File"];
                        textureNames.Add(file);
                    }

                    if (reader.Name == "Layout")
                    {
                        List<int> row = new List<int>();
                        width = int.Parse(reader["Width"]);
                        height = int.Parse(reader["Height"]);

                        reader.Read();

                        string[] cells = reader.Value.Split(' ');

                        foreach (string c in cells)
                                {
                                    if (!string.IsNullOrEmpty(c))
                                    {
                                        if (c.Contains("\r\n"))
                                            continue;

                                        tempLayout.Add(int.Parse(c));
                                    }
                                }
                    }

                    if (reader.Name == "Properties")
                    {
                        reader.Read();
                        string key = reader.Name;
                        string value = reader.ReadInnerXml();

                        properties.Add(key, value);
                    }

                    if (reader.Name == "TreasureChest")
                    {
                        TreasureChest tc = new TreasureChest();
                        tc.Postition.X = float.Parse(reader["LocationX"]);
                        tc.Postition.Y = float.Parse(reader["LocationY"]);
                        reader.Read();
                        tc.ItemType = reader["ItemType"];
                        tc.Item = reader["Item"];
                        tc.Quantity = int.Parse(reader["Quantity"]);
                        tc.Animations.Add("Closed", new FrameAnimation(1, 32, 48, 0, 0));
                        tc.CurrentAnimationName = "Closed";
                        treasurech.Add(tc);
                    }

                    if (reader.Name == "Door")
                    {
                        Door d = new Door();
                        d.Postition.X = float.Parse(reader["LocationX"]);
                        d.Postition.Y = float.Parse(reader["LocationY"]);
                        d.Animations.Add("Closed", new FrameAnimation(1, 96, 64, 0, 0));
                        d.CurrentAnimationName = "Closed";
                        dooor.Add(d);
                    }
                }
            }
            reader.Close();

            tileLayer = new TileLayer(width, height);
            int next = 0;

            for (int y = 0; y < height; y++)
                for (int x = 0; x < width; x++)
                {
                    tileLayer.SetCellIndex(x, y, tempLayout[next]);
                    next++;
                }

            foreach (KeyValuePair<string, string> property in properties)
            {
               switch (property.Key)
               {
                   case "Alpha":
                       tileLayer.Alpha = float.Parse(property.Value);
                       break;
               }
            }

            foreach (TreasureChest trech in treasurech)
            {
                tileLayer.chests.Add(trech);
            }

            foreach (Door d in dooor)
            {
                tileLayer.doors.Add(d);
            }

            return tileLayer;
        }
Esempio n. 4
0
        private static TileLayer ProcessFile(string filename, List<string> textureNames)
        {
            TileLayer tileLayer;
            List<List<int>> tempLayout = new List<List<int>>();
            Dictionary<string, string> properties = new Dictionary<string, string>();

            //using statement disposes of everything after the brackets, always use with file IO
            using (StreamReader reader = new StreamReader(filename))
            {
                bool readingTextures = false;
                bool readingLayout = false;
                bool readingProperties = false;

                while (!reader.EndOfStream)
                {
                    string line = reader.ReadLine().Trim();

                    if (string.IsNullOrEmpty(line))
                        continue;

                    if (line.Contains("[Textures]"))
                    {
                        readingTextures = true;
                        readingLayout = false;
                        readingProperties = false;
                    }
                    else if (line.Contains("[Layout]"))
                    {
                        readingTextures = false;
                        readingLayout = true;
                        readingProperties = false;
                    }
                    else if (line.Contains("[Properties]"))
                    {
                        readingProperties = true;
                        readingLayout = false;
                        readingTextures = false;
                    }
                    else if (readingTextures)
                    {
                        textureNames.Add(line);
                    }
                    else if (readingLayout)
                    {
                        List<int> row = new List<int>();

                        string[] cells = line.Split(' ');

                        foreach (string c in cells)
                        {
                            if (!string.IsNullOrEmpty(c))
                                row.Add(int.Parse(c));
                        }

                        tempLayout.Add(row);
                    }
                    else if (readingProperties)
                    {
                        string[] pair = line.Split('=');
                        string key = pair[0].Trim();
                        string value = pair[1].Trim();

                        properties.Add(key, value);
                    }
                }
            }

            //width = Cells in first row
            int width = tempLayout[0].Count;
            //number of rows
            int height = tempLayout.Count;

            tileLayer = new TileLayer(width, height);

            foreach (KeyValuePair<string, string> property in properties)
            {
                switch (property.Key)
                {
                    case "Alpha":
                        tileLayer.Alpha = float.Parse(property.Value);
                        break;
                }
            }

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    tileLayer.SetCellIndex(x, y, tempLayout[y][x]);
                }
            }
            return tileLayer;
        }
Esempio n. 5
0
        public static TileLayer FromFile(string filename, out string [] textures)
        {
            TileLayer tileLayer;
            ParseState parseState = ParseState.None;

            List<string> textureNames = new List<string>();
            List<List<int>> tempLayout = new List<List<int>>();

            // TODO: move into options structure
            float alpha = 1f;

            using (StreamReader reader = new StreamReader(filename))
            {
                while (!reader.EndOfStream)
                {
                    string line = reader.ReadLine().Trim();

                    if (!string.IsNullOrEmpty(line))
                    {
                        if (line.Contains("[Textures]"))
                        {
                            parseState = ParseState.Texture;
                        }
                        else if (line.Contains("[Layout]"))
                        {
                            parseState = ParseState.Layout;
                        }
                        else if (line.Contains("[Options]"))
                        {
                            // TODO: Move into [Options] section
                            // parse options like this:  OptionName = OptionValue
                            parseState = ParseState.Options;
                        }
                        else
                        {
                            switch (parseState)
                            {
                                case ParseState.Layout:
                                    List<int> row = new List<int>();

                                    string[] cells = line.Split(' ');
                                    foreach (string c in cells)
                                    {
                                        if (!string.IsNullOrEmpty(c))
                                        {
                                            row.Add(int.Parse(c));
                                        }
                                    }

                                    tempLayout.Add(row);
                                    break;

                                case ParseState.Texture:
                                    textureNames.Add(line);
                                    break;

                                case ParseState.Options:
                                    string[] nameValuePair = line.Split('=');

                                    // hopefully we have two
                                    if (nameValuePair.Length == 2)
                                    {
                                        if (nameValuePair[0].Trim().Equals("Alpha", StringComparison.InvariantCultureIgnoreCase))
                                        {
                                            alpha = float.Parse(nameValuePair[1]);
                                        }
                                    }

                                    break;

                            }
                        }
                    }

                }
            }

            int width = tempLayout[0].Count;
            int height = tempLayout.Count;

            tileLayer = new TileLayer(width, height);
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    tileLayer.SetCellIndex(x, y, tempLayout[y][x]);
                }
            }

            //tileLayer.LoadTileTextures(
            //    content,
            //    textureNames.ToArray());

            tileLayer.Alpha = alpha;

            textures = textureNames.ToArray();

            return tileLayer;
        }
Esempio n. 6
0
        public static TileLayer FromFile(ContentManager content, string filename)
        {
            TileLayer          tileLayer;
            bool               readingTextures = false;
            bool               readingLayout   = false;
            List <string>      textureNames    = new List <string>();
            List <List <int> > tempLayout      = new List <List <int> >();

            using (StreamReader reader = new StreamReader(filename))
            {
                while (!reader.EndOfStream)
                {
                    string line = reader.ReadLine().Trim();

                    if (string.IsNullOrEmpty(line))
                    {
                        continue;
                    }

                    if (line.Contains("[Textures]"))
                    {
                        readingTextures = true;
                        readingLayout   = false;
                    }
                    else if (line.Contains("[Layout]"))
                    {
                        readingTextures = false;
                        readingLayout   = true;
                    }
                    else if (readingTextures)
                    {
                        textureNames.Add(line);
                    }
                    else if (readingLayout)
                    {
                        List <int> row   = new List <int>();
                        string[]   cells = line.Split(' ');

                        foreach (string cell in cells)
                        {
                            if (!string.IsNullOrEmpty(cell))
                            {
                                row.Add(int.Parse(cell));
                            }
                        }
                        tempLayout.Add(row);
                    }
                }
            }

            int width  = tempLayout[0].Count;
            int height = tempLayout.Count;

            tileLayer = new TileLayer(width, height);

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    tileLayer.SetCellIndex(x, y, tempLayout[y][x]);
                }
            }

            tileLayer.LoadTileTextures(content, textureNames.ToArray());

            return(tileLayer);
        }