private TileMatrix ReadTileMatrix(ContentReader input, int layerWidth, int layerHeight) { List <TileMapTile> tileCache = new List <TileMapTile>(); TileMatrix matrix = new TileMatrix(layerWidth, layerHeight); for (int i = 0; i < layerWidth * layerHeight; i++) { int tileId = input.ReadInt32(); var tile = tileCache.FirstOrDefault(t => t.Id == tileId); if (tile == null) { tile = new TileMapTile(tileId); tileCache.Add(tile); } matrix.SetTile(i % layerWidth, i / layerWidth, tile); } return(matrix); }