Пример #1
0
        /// Author:	Guy Spronck
        /// Date:	05-06-2015
        /// <summary>
        /// Reads the file from using xml reader
        /// </summary>
        /// <returns>spawn, lvl, mapVariables</returns>
        /// <param name="stream">Stream.</param>
        private static Tuple<Spawn, Tile[, ], MapVariables> ReadFileXml(Stream stream)
        {
            // Initialize variables
            string[,] stringLvl = null, stringGraphicLvl = null;

            MapVariables mapVariables = new MapVariables ();

            // Setup xmlreader, throw error if problem with file.
            XmlReader xmlDoc;
            try{
                xmlDoc = XmlReader.Create(stream);
            }catch(ArgumentException){
                throw new MapParseException ("No file.");
            }catch(DirectoryNotFoundException){
                throw new MapParseException ("Directory not found");
            }

            // Decode the file and get the required values
            try{
                while (xmlDoc.Read ()) {
                    if (xmlDoc.NodeType == XmlNodeType.Element) {
                        if (xmlDoc.Name == "map") {
                            mapVariables.width = Convert.ToInt32 (xmlDoc.GetAttribute ("width"));
                            mapVariables.height = Convert.ToInt32 (xmlDoc.GetAttribute ("height"));
                        } else if (xmlDoc.Name == "tileset") {
                            if (xmlDoc.GetAttribute ("name").ToLower() == "roadmap") {
                                mapVariables.firstGidRoadmap = Convert.ToInt32 (xmlDoc.GetAttribute ("firstgid"));
                            } else if (xmlDoc.GetAttribute ("name").ToLower() == "tilesroad") {
                                mapVariables.firstGidGraphic = Convert.ToInt32 (xmlDoc.GetAttribute ("firstgid"));
                            }
                        }else if(xmlDoc.Name == "property"){
                            if (xmlDoc.GetAttribute ("name") == "minMoves") {
                                mapVariables.minMoves = Convert.ToInt32(xmlDoc.GetAttribute ("value"));
                            }else if (xmlDoc.GetAttribute ("name") == "minLines"){
                                mapVariables.minLines = Convert.ToInt32(xmlDoc.GetAttribute ("value"));
                            }else if (xmlDoc.GetAttribute ("name") == "CabbagesToRetreive"){
                                mapVariables.cabbagesToRetreive = Convert.ToInt32(xmlDoc.GetAttribute ("value"));
                            }else if (xmlDoc.GetAttribute ("name") == "SausagesToRetreive"){
                                mapVariables.sausagesToRetreive = Convert.ToInt32(xmlDoc.GetAttribute ("value"));
                            }else if (xmlDoc.GetAttribute ("name") == "Sausage" || xmlDoc.GetAttribute ("name") == "Cabbage" || xmlDoc.GetAttribute ("name") == "Ham"){
                                // Populate shop
                                AddItemToShops(mapVariables.mapInventory, xmlDoc.GetAttribute ("name"), Convert.ToInt32(xmlDoc.GetAttribute ("value")));
                            }
                        } else if (xmlDoc.Name == "layer") {
                            if (xmlDoc.GetAttribute ("name") == "Graphic") {
                                xmlDoc.ReadToDescendant ("data");
                                mapVariables.graphic = xmlDoc.ReadElementContentAsString ();
                            } else if (xmlDoc.GetAttribute ("name") == "Roadmap") {
                                xmlDoc.ReadToDescendant ("data");
                                mapVariables.roadmap = xmlDoc.ReadElementContentAsString ();
                            }
                        }
                    }
                }
            }catch(FormatException){
                throw new MapParseException("Invalid map format. (Contains string where int was expected.)");
            }finally{
                xmlDoc.Dispose ();
                stream.Close ();
            }

            stringLvl = ConvertStringMapToArray (mapVariables.roadmap, mapVariables.width, mapVariables.height);
            stringGraphicLvl = ConvertStringMapToArray (mapVariables.graphic, mapVariables.width, mapVariables.height);

            var tupleTileMap = GenerateTileMap (stringLvl, stringGraphicLvl, mapVariables);
            return Tuple.Create(tupleTileMap.Item1, tupleTileMap.Item2, mapVariables);
        }
Пример #2
0
        /// Author:	Guy Spronck
        /// Date:	04-06-2015
        /// <summary>
        /// Generates the map with the Tile object.
        /// </summary>
        /// <returns>Tuple containing Spawn, Tilemap</returns>
        /// <param name="stringLvl">String array for roadmap</param>
        /// <param name="stringGraphicLvl">String array for graphic layer</param>
        /// <param name="mapVariables">Variables of the map.</param>
        private static Tuple<Spawn, Tile[, ]> GenerateTileMap(string[,] stringLvl, string[,] stringGraphicLvl, MapVariables mapVariables)
        {
            int spawnX = 0, spawnY = 0;
            var lvl = new Tile[mapVariables.height, mapVariables.width];
            for (int iX = 0; iX < mapVariables.height; iX++) {
                for (int iY = 0; iY < mapVariables.width; iY++) {

                    if (stringLvl [iX, iY].Contains ((mapVariables.firstGidRoadmap+5).ToString())) {
                        spawnX = iY;
                        spawnY = iX;
                    }

                    lvl [iX, iY] = new Tile (checkNorthTile(iX,iY,stringLvl),
                        checkEastTile(iX,iY,stringLvl,mapVariables.width),
                        checkSouthTile(iX,iY,stringLvl,mapVariables.height),
                        checkWestTile(iX,iY,stringLvl),
                        checkShop(iX,iY,stringLvl,mapVariables.firstGidRoadmap),
                        Convert.ToInt32(stringGraphicLvl[iX,iY]) - mapVariables.firstGidGraphic);
                }
            }
            return Tuple.Create (new Spawn(spawnX,spawnY), lvl);
        }