/// <summary> /// Loads the world file (the IDs of the tiles) into a collection and into their corresponding 'Blocks' /// </summary> private void LoadWorldIDsIntoCache() { using (var sr = new BinaryReader(File.Open(m_levelFile, FileMode.Open))) { //Read in the width of the world in Blocks WorldWidth = sr.ReadInt32(); //Rad in the height of the world in Blocks WorldHeight = sr.ReadInt32(); int totalNumberOfBlocks = WorldWidth * WorldHeight; //Read the width of a single Block TilesPerBlockWidth = sr.ReadInt32(); //Read the height of a single Block TilesPerBlockHeight = sr.ReadInt32(); //Size per tile SizePerTile = sr.ReadInt32(); TilesPerBlock = TilesPerBlockWidth * TilesPerBlockHeight; //Time to read in every single block for (int block = 0; block < totalNumberOfBlocks; block++) { //Create a new spot in the cache for the block m_levelBlockCache.Add(new List<CachedTileInformation>()); //Read the BlockID (Not currently used) int blockID = sr.ReadInt32(); //Read in the number of tiles in the block int numOfTileInBlock = sr.ReadInt32(); //Read in the for (int tile = 0; tile < numOfTileInBlock; tile++) { CachedTileInformation cacheTileInfo = new CachedTileInformation(); //Read all the info about the Tile. cacheTileInfo.TileID = sr.ReadInt32(); cacheTileInfo.TilePosX = sr.ReadInt32(); cacheTileInfo.TilePosY = sr.ReadInt32(); m_levelBlockCache[block].Add(cacheTileInfo); } } } }
/// <summary> /// Saves the test world to a binary file. /// </summary> public void saveTestWorld() { using (var sw = new BinaryWriter(File.Open("Content/Levels/TestLevel.level", FileMode.Create))) { int worldWidth = 20; int worldHeight = 20; int tilesPerBlockWidth = 20; int tilesPerBlockHeight = 20; int sizeOfTile = 10; sw.Write(worldWidth); //Width of world in Blocks sw.Write(worldHeight); //Height of world in Blocks sw.Write(tilesPerBlockWidth); //Block Width sw.Write(tilesPerBlockHeight); //Block Height sw.Write(sizeOfTile); //The size of a tile. int requiredBlocks = worldWidth * worldHeight; var listOfBlocks = new List<List<CachedTileInformation>>(); for (int block = 0; block < requiredBlocks; block++) { listOfBlocks.Add(new List<CachedTileInformation>()); if (block > worldHeight * 2 - 1) { for (int tileY = 0; tileY < tilesPerBlockHeight; tileY++) { for (int tileX = 0; tileX < tilesPerBlockWidth; tileX++) { CachedTileInformation cacheTileInfo = new CachedTileInformation(); cacheTileInfo.TileID = 1; cacheTileInfo.TilePosX = tileX; cacheTileInfo.TilePosY = tileY; listOfBlocks[block].Add(cacheTileInfo); } } } } int blockID = 0; //Write the blocks to file listOfBlocks.ForEach(list => { sw.Write(blockID++); sw.Write(list.Count); //Write number of tiles in the block //Write tiles to the block list.ForEach(entry => { sw.Write(entry.TileID); sw.Write(entry.TilePosX); sw.Write(entry.TilePosY); }); }); } }