public static Tile GetTileFor(TileData data) { Tile result = getHitbox(data.TileID); result.SetTexture(getSpriteRect(data.TextureID)); result.UseExtraData(data.ExtraData); return result; }
const int tileByteCount = 2; //The amount of bytes used to represent a single Tile #endregion Fields #region Methods //Creates a 2D array containing all the level's Tiles public static Tile[][] loadMap(string mapPath) { byte[] fileData = File.ReadAllBytes(mapPath); int metadataOffset = 0; //Tracks the amount of bytes from the start occupied by metadata //Get metadata int widthByteCount = fileData[0]; //Width and height bytecount specify the amount of bytes required to represent the width and height of the level respectively int heightByteCount = fileData[1]; metadataOffset += 2; int width = BytesToInt(SubArray(fileData, metadataOffset, widthByteCount)); metadataOffset += widthByteCount; int height = BytesToInt(SubArray(fileData, metadataOffset, heightByteCount)); metadataOffset += heightByteCount; Tile[][] map = new Tile[height][]; byte[] mapData = SubArray(fileData, metadataOffset, fileData.Length - metadataOffset); //When the metadata has been read a subarray is created to make the data of the Tile's start at index 0 to make the following code less complex for (int y = 0; y < height; y++) { Tile[] mapRow = new Tile[width]; for (int x = 0; x < width; x++) { int currentTile = y * width + x; byte[] tileByteData = SubArray(mapData, currentTile * tileByteCount, tileByteCount); TileData tileData = new TileData(tileByteData); mapRow[x] = TileFactory.GetTileFor(tileData); } map[y] = mapRow; } return map; }