/// <summary> /// Converts all the tileIDs into Tiles and plots /// them into the layer. /// </summary> /// <param name="tileIDs"> The tileIDs to convert. </param> /// <param name="layer"> The final layer. </param> private void LoadIntoLayer(int[] tileIDs, MapLayer layer) { int currentID = 0; // Loops through all tiles in tileIDs, converts them to object of the type // Tile and loads them into the layer. for (int i = 0; i < layer.Height; i++) { for (int j = 0; j < layer.Width; j++) { int id = tileIDs[currentID] - 1 < 0 ? 0 : tileIDs[currentID] - 1; layer.SetTile(j, i, id, this.FindTileset(id)); currentID++; } } }
/// <summary> /// Makes a layer for the map from the given input. /// </summary> /// <param name="lines"> The lines that holds the data for the map. </param> /// <param name="start"> The line to start reading from. </param> /// <param name="height"> The height of the map. </param> /// <param name="width"> The width of the map. </param> /// <param name="gameRef"> A snapshot of the game. </param> private void MakeLayer(string[] lines, int start, int height, int width, Game1 gameRef) { // Finds the name of the tileset used to make this specific layer and loads it. var tilesetName = lines[start + 2].Substring(lines[start + 2].IndexOf('=') + 1); var tilesetTexture = gameRef.Content.Load<Texture2D>(@"Textures\TileSets\" + tilesetName); var backgroundTileset = new Tileset( tilesetTexture, tilesetTexture.Width / 32, tilesetTexture.Height / 32, 32, 32); this.tilesets.Add(backgroundTileset); var index = lines[start + 1].IndexOf('=') + 1; var layerName = lines[start + 1].Substring(index, lines[start + 1].Length - index); var background = new MapLayer(layerName, width, height); // Goes through all the lines that hold tileIDs for (int i = 0; i < height; i++) { // Loads the current line and splits it into a string array. string[] stringTileIDs = lines[i + start + 4].Split(','); var tileIDs = new int[width]; // Converts each ID from a string to an int. for (int j = 0; j < width; j++) { tileIDs[j] = Convert.ToInt32(stringTileIDs[j]); } // Loops through the list of TileIDs and places new tiles in the layer, corrosponding // to the ID and position. for (int j = 0; j < width; j++) { background.SetTile(i, j, new Tile(tileIDs[j] - 1 < 0 ? 0 : tileIDs[j] - 1, this.tilesets.Count - 1)); } } this.layers.Add(background); }