Esempio n. 1
0
        public override void LoadFromFile( ContentManager content, string fileName )
        {
            string rawInput;
            string[] processBuffer;
            int numRows = 0;
            int numCols = 0;
            char[] whiteSpace = new char[] { ' ', '\r', '\t', '\n' };

            // This will create the reader just for this scope
            // So it will get garbage collected as soon as the scope collapses
            using ( XmlReader reader = XmlReader.Create( fileName ) )
            {
                // Read in the map's name
                reader.ReadToFollowing( "Name" );
                Name = reader.ReadElementString( "Name" ).Trim();

                // Read in the map size (in tiles)
                rawInput = reader.ReadElementString( "Size" ).Trim();
                processBuffer = rawInput.Split( whiteSpace );
                numRows = int.Parse( processBuffer[0] );
                numCols = int.Parse( processBuffer[1] );

                // Read in the tile size (in pixels)
                rawInput = reader.ReadElementString( "TileSize" ).Trim();
                processBuffer = rawInput.Split( whiteSpace );
                TileHeight = int.Parse( processBuffer[0] );
                TileWidth = int.Parse( processBuffer[1] );

                // Read in and load the list of textures used by this map
                rawInput = reader.ReadElementString( "TextureList" ).Trim();
                processBuffer = rawInput.Split( whiteSpace, StringSplitOptions.RemoveEmptyEntries );
                LoadTileTextures( content, processBuffer );

                // Read in the layers
                while ( reader.ReadToFollowing( "Layer" ) )
                {
                    TileLayer newLayer = new TileLayer( numCols, numRows );

                    // Read the tile texture indices
                    reader.ReadToFollowing( "Textures" );
                    rawInput = reader.ReadElementString( "Textures" ).Trim();
                    processBuffer = rawInput.Split( whiteSpace, StringSplitOptions.RemoveEmptyEntries );
                    for ( int row = 0; row < numRows; row++ )
                    {
                        for ( int col = 0; col < numCols; col++ )
                        {
                            newLayer.SetTileTextureIndex(
                                row,
                                col,
                                int.Parse( processBuffer[row * numCols + col] )
                                );
                        }
                    }

                    AddLayer( newLayer );
                }
            }
        }
Esempio n. 2
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. 3
0
 /// <summary>
 /// Adds a given layer on top the of tile layers present in the map.
 /// </summary>
 /// <param name="layer">The tile layer to add.</param>
 public void AddLayer( TileLayer layer )
 {
     layer.ParentMap = this;
     layer.LayerIndex = layers.Count;
     layers.Add( layer );
 }
Esempio n. 4
0
 public void AddNewLayer()
 {
     var tileLayer = new TileLayer(_tileSize, _defaultTileTexture);
     _layers.Add(tileLayer);
 }
Esempio n. 5
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. 6
0
 /// <summary>
 /// Wipes a layer completely. Used for animations etc.
 /// </summary>
 /// <param name="layerIndex"></param>
 public void ClearLayer(int layerIndex)
 {
     layers[layerIndex] = new TileLayer(numRows, numColumns);
 }
Esempio n. 7
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. 8
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. 9
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);
        }
Esempio n. 10
0
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            tileLayer = TileLayer.FromFile(Content, "Content/Layers/Layer1.layer");
            tileLayer2.LoadTileTextures(Content, "tiles/grass19");
            tileLayer3.LoadTileTextures(Content, "tiles/grass02");
            tileLayer3.Alpha = .5f;
            shipLayer.LoadTileTextures(Content, "tiles/hull");
        }
Esempio n. 11
0
 /// <summary>
 /// Wipes a layer completely. Used for animations etc.
 /// </summary>
 /// <param name="layerIndex"></param>
 public void ClearLayer(int layerIndex)
 {
     layers[layerIndex] = new TileLayer(numRows, numColumns);
 }