public TileMap(Tileset tileset, MapLayer layer) { tilesets = new List<Tileset>(); tilesets.Add(tileset); mapLayers = new List<MapLayer>(); mapLayers.Add(layer); mapWidth = mapLayers[0].Width; mapHeight = mapLayers[0].Height; }
/// <summary> /// Loads the level from files created in an external tile editor. /// </summary> /// <param name="fileStream"></param> /// <param name="levelIndex"></param> public virtual void LoadLevel(int numLayers) { List<MapLayer> mapLayers = new List<MapLayer>(); // Holds the layers for the level. List<string> tilelines; // The lines of text that hold tile information. List<string> collisionlines; // The lines of text that hold collision information. List<string> spritelines; // The lines of text that hold sprite information. // Load the text file that has the assigned collisions. string collisionPath = string.Format("Content/Levels/{0}/collision.txt", levelIndex); using (Stream fileStream = TitleContainer.OpenStream(collisionPath)) { collisionlines = new List<string>(); collisionlines = readFile(fileStream); fileStream.Close(); } // Load the text file that has the assigned sprite positions. string spritePath = string.Format("Content/Levels/{0}/sprite.txt", levelIndex); using (Stream fileStream = TitleContainer.OpenStream(spritePath)) { spritelines = new List<string>(); spritelines = readFile(fileStream); fileStream.Close(); } // Loop through every single layer. for (int i = 0; i < numLayers; i++) { // Load the text file that has the assigned tile positions. string levelPath = string.Format("Content/Levels/{0}/{1}.txt", levelIndex, i); using (Stream fileStream = TitleContainer.OpenStream(levelPath)) { tilelines = new List<string>(); tilelines = readFile(fileStream); fileStream.Close(); } // The level is as wide as the number of elements in the line, or the number of commas, and as high as the number of lines. levelHeight = tilelines.Count; // Create a new layer. MapLayer layer = new MapLayer(levelWidth, levelHeight); // Loop over every tile position in the file and set each tile to the layer. for (int y = 0; y < layer.Height; y++) { string[] tileNumbers = tilelines[y].Split(','); string[] collisionNumbers = collisionlines[y].Split(','); string[] spriteNumbers = spritelines[y].Split(','); for (int x = 0; x < layer.Width; x++) { // Use the numbers from the file by converting them to integers. int tileIndex = Convert.ToInt32(tileNumbers[x]); Tile tile = new Tile(tileIndex - 1, i, x, y); layer.SetTile(x, y, tile); // Set the tile collisions and sprites only once. if (i == 0) { // Get the collision number from the file by converting it to an integer and load the collision. int collisionNum = Convert.ToInt32(collisionNumbers[x]); LoadCollisions(layer, x, y, collisionNum); // Get the sprite number from the file by converting it to an integer and load the sprite. int spriteNum = Convert.ToInt32(spriteNumbers[x]); LoadSprites(x, y, spriteNum); levelLayer = layer; } } } mapLayers.Add(layer); } // Create the tilemap with the tilesets and the layers. map = new TileMap(tilesets, mapLayers); }
/// <summary> /// Sets each tile collision by using the information in a file provided by an external tile editor. /// </summary> /// <param name="fileStream"></param> /// <param name="levelIndex"></param> public virtual void LoadCollisions(MapLayer layer, int x, int y, int collisionNum) { CollisionType collisionType; if (collisionNum == 2) collisionType = CollisionType.Impassable; else collisionType = CollisionType.Passable; layer.SetTileCollision(x, y, collisionType); }
public void AddLayer(MapLayer layer) { if (layer.Width != mapWidth && layer.Height != mapHeight) throw new Exception("Map layer size exception"); mapLayers.Add(layer); }