/// <summary> /// Creates a tile layer by reading the contentManager we got from the SharpTiles library. This will create our tile layers of type "Floor" and "Objects". /// </summary> /// <param name="layerContent"></param> /// <param name="tileSets"></param> /// <returns></returns> private TileLayer CreateTileLayer(LayerContent layerContent, IEnumerable <TileSetContent> tileSets) { if (layerContent == null) { throw new ArgumentNullException("layerContent"); } if (tileSets == null) { throw new ArgumentNullException("tileSets"); } TileLayerContent tileLayerContent = layerContent as TileLayerContent; TileLayerType tileLayerType = GetTileLayerType(layerContent); TileLayer tileLayer = new TileLayer(layerContent.Name, tileLayerContent.Width, tileLayerContent.Height, tileLayerType); foreach (uint tileID in tileLayerContent.Data) { uint flippedHorizontallyFlag = 0x80000000; uint flippedVerticallyFlag = 0x40000000; int tileIndex = (int)(tileID & ~(flippedVerticallyFlag | flippedHorizontallyFlag)); Tile tile = CreateTile(tileIndex, tileSets, tileLayerType); tileLayer.AddTile(tile); } return(tileLayer); }
public TileLayer(string name, int width, int height, TileLayerType type) { Type = type; Name = name; Width = width; Height = height; }
/// <summary> /// Based on the tile layer type passed, this method returns a tile type for all tiles that would be contained in a layer of that type. /// </summary> /// <param name="tileLayerType"></param> /// <returns></returns> private static TileType GetTileType(TileLayerType tileLayerType) { TileType tileType = TileType.None; if (tileLayerType == TileLayerType.Floor) { tileType = TileType.Floor; } if (tileLayerType == TileLayerType.Objects) { tileType = TileType.Object; } return(tileType); }
/// <summary> /// Returns the type of the tile layer according to the layer's name as read from the Tiled map editor. /// </summary> /// <param name="layerContent"></param> /// <returns></returns> private static TileLayerType GetTileLayerType(LayerContent layerContent) { if (layerContent == null) { throw new ArgumentNullException("layerContent"); } TileLayerType tileLayerType = TileLayerType.None; if (layerContent.Name.Contains("Floor")) { tileLayerType = TileLayerType.Floor; } else if (layerContent.Name.Contains("Objects")) { tileLayerType = TileLayerType.Objects; } return(tileLayerType); }
/// <summary> /// Based on a passed tile index, create a Tile by looking up which TileSet it belongs to, assign the proper TilSet texture, /// and find the bounds of the rectangle that encompasses the correct tile texture within the total tileset texture. /// </summary> /// <param name="tileIndex">Index of the tile (GID) within the map file</param> /// <param name="tileSets">Enumerable list of tilesets used to find out which tileset a tile belongs to</param> /// <param name="tileLayerType"></param> /// <returns></returns> private Tile CreateTile(int tileIndex, IEnumerable <TileSetContent> tileSets, TileLayerType tileLayerType) { if (tileSets == null) { throw new ArgumentNullException("tileSets"); } Tile tile = new Tile(); // we don't want to look up tiles with ID 0 in tile sets because Tiled Map Editor treats ID 0 as an empty tile if (tileIndex > Tile.EmptyTileID) { Texture tileSetTexture = null; Rectangle source = new Rectangle(); foreach (TileSetContent tileSet in tileSets) { if (tileIndex - tileSet.FirstGID < tileSet.Tiles.Count) { tileSetTexture = tileSet.Texture; source = tileSet.Tiles[tileIndex - tileSet.FirstGID].SourceTextureBounds; break; } } TileType tileType = GetTileType(tileLayerType); tile = new Tile(tileSetTexture, source, TileWidth, TileHeight, tileType); } return(tile); }