private void LoadFromFile(string path) { int width; List<string> lines = new List<string>(); using (StreamReader reader = new StreamReader(Application.GetResourceStream(new Uri(path, UriKind.Relative)).Stream)) { string line = reader.ReadLine(); width = line.Length; while (line != null) { lines.Add(line); if (line.Length != width) throw new Exception(String.Format("The length of line {0} is different from all preceeding lines.", lines.Count)); line = reader.ReadLine(); } } // Allocate the tile grid. Grid = new Tile[width + 2, lines.Count + 2]; numX = width; numY = lines.Count; Tile.Width /= numX; //Set width of tile based,based on size of the canvas divaded by number of tiles. Tile.Height /= numY; Tile.Size = new Vector2(Tile.Width, Tile.Height); // Loop over every tile position, for (int y = 0; y < numY; ++y) { for (int x = 0; x < numX; ++x) { try { char type = lines[y][x]; Grid[x, y] = Load(type, x, y); } catch (Exception e) { Grid[x, y] = Load('1', x, y); } } } Player.CreatInstance(this.GetTile(13, 11), content); Enemies = RandomPositionOfEnemies(); }
private Tile Load(char type, int x, int y) { Vector2 gridPosition = new Vector2(x, y); Vector2 position = gridPosition * Tile.Size; Rectangle bounds = new Rectangle((int)position.X, (int)position.Y, (int)Tile.Size.X, (int)Tile.Size.Y); if (type == '0') { TileCollision tileType = TileCollision.Passable; Tile tile = new Tile(space, tileType, bounds, gridPosition); tile.coin = new Coin(coin, tile.position * Tile.Size, Tile.Size); numCoins++; return tile; } else { TileCollision tileType = TileCollision.Impassable; return new Tile(wall, tileType, bounds, gridPosition); } }