public TileMap LoadTileMap(string imageFileName, string settingsFileName) { Texture2D layer = ScreenManager.Instance.Content.Load<Texture2D>(imageFileName); Color[] allPixels = new Color[(layer.Width * layer.Height)]; layer.GetData<Color>(allPixels); Dictionary<Color, SubTexture> lookupTable = LoadSettingsFile(settingsFileName); TileMap tm = new TileMap(layer.Width, layer.Height, 20, 20, 1, 1); // For now just 1 layer and 1 sublayer. (1 total texture per tile) for (int i = 0; i < allPixels.Length; i++) { int x = i % layer.Width; int y = i / layer.Width; SubTexture[,] textures = new SubTexture[1,1]; textures[0, 0] = lookupTable[allPixels[i]]; Tile t; t = textures[0,0] != null ? new Tile(textures) : null; tm.SetTile(t, x, y); } return tm; }
/// <summary> /// Given a list of tile coordinates, create a new TileMap from this /// TileMap that contains copies of each Tile that corresponds to every /// coordinate in the list. /// </summary> /// <param name="tileCoordinates"> /// A List of 2D coordinate sets, each represented as a 2-element array of integers. /// Format: /// int[0] => Tile X coordinate. /// int[1] => Tile Y coordinate. /// This List represents each Tile you want to copy into the new TileMap that will be /// returned from this function. /// </param> /// <returns> /// The resulting TileMap will be just big enough to fit all Tiles /// specified by the tileCoordinates List, and all in-between Tiles /// that aren't specified in the list will be null. /// </returns> public TileMap SubTileMapFromCoordList(List<int[]> tileCoordinates) { TileMap subTileMap; if (tileCoordinates.Count > 0) { // First, compute the minimum required width and height: int minX, minY, maxX, maxY, w, h; minX = minY = 999999999; maxX = maxY = 0; foreach (int[] tileCoord in tileCoordinates) { minX = Math.Min(tileCoord[0], minX); minY = Math.Min(tileCoord[1], minY); maxX = Math.Max(tileCoord[0], maxX); maxY = Math.Max(tileCoord[1], maxY); } w = maxX - minX + 1; h = maxY - minY + 1; subTileMap = new TileMap(w, h, TileWidth, TileHeight, LayerCount, SubLayerCount); foreach (int[] tileCoord in tileCoordinates) { Tile tile = this[tileCoord[0], tileCoord[1]]; DestructableTile destructable = this[tileCoord[0], tileCoord[1]] as DestructableTile; if (destructable != null) { subTileMap.SetTile(destructable, tileCoord[0] - minX, tileCoord[1] - minY); } else if (tile != null) { subTileMap.SetTile(tile, tileCoord[0] - minX, tileCoord[1] - minY); } } } else { subTileMap = new TileMap(0, 0, TileWidth, TileHeight, LayerCount, SubLayerCount); } return subTileMap; }