C# 如何从txt文件中拖动2d级别数组

C# 如何从txt文件中拖动2d级别数组,c#,xna,streamreader,C#,Xna,Streamreader,我正在努力改进我在本教程中完成的这个塔防gtame。我现在希望从文本文件中加载这些级别,但不太确定如何使用streamreader实现这一点,有什么提示吗?这是我的level类的源代码 public class Level { protected int temperature; protected int levelNo; private Queue<Vector2> waypoints = new Queue<Vector2>(); p

我正在努力改进我在本教程中完成的这个塔防gtame。我现在希望从文本文件中加载这些级别,但不太确定如何使用streamreader实现这一点,有什么提示吗?这是我的level类的源代码

public class Level
{
    protected int temperature;
    protected int levelNo;
    private Queue<Vector2> waypoints = new Queue<Vector2>();
    public Queue<Vector2> Waypoints
    {
        get
        {
            return waypoints;
        }
    }

    int[,] map = new int[,]                    
    {



    {0,0,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,},
    {0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,},
    {0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0,},
    {0,0,0,0,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,},
    {0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,},
    {0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,},
    {0,0,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0,},
    {0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,},
    {0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,},
    {0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,},
    {0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0,},
    {0,0,0,0,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,},
    {0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,},
    {0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,},
    {0,0,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,},
    {0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,},

    };
    public int Temperature                                                                                    // get the width of the level (how many numbers are in a row)
    {
        get
        {
            return temperature;
        }
    }
    public int LevelNo                                                                                    // get the width of the level (how many numbers are in a row)
    {
        get
        {
            return levelNo;
        }
          set
        {
            levelNo = value;
        }
    }
    public int Width                                                                                    // get the width of the level (how many numbers are in a row)
    {
        get
        {
            return map.GetLength(1);
        }
    }
    public int Height                                                                                   // get the height of our level (how many numbers are there in a column)
    {
        get
        {
            return map.GetLength(0);
        }
    }
    public int GetIndex(int cellX, int cellY)                                                           // return the index of the requested cell.
    {
        if (cellX < 0 || cellX > Width - 1 || cellY < 0 || cellY > Height - 1)
        {
            return 0;
        }
        else
        {
            return map[cellY, cellX];
        }
    }
    public List<Texture2D> tileTextures = new List<Texture2D>();                                        // list to contain all the textures
    /// <summary>
    /// CONSTRUCTOR NEW LEVEL        
    /// </summary>
    public Level()
    {
        SetWayPoints(map);
        this.temperature = 1000;
        this.levelNo = 2;
    }
    private void SetWayPoints(int[,] map)
    {
        int currentPosVal = map[0, 0];
        int lPos = 0;
        int rPos = 0;
        int uPos = 0;
        int dPos = 0;
        int storedXPos = 99;
        int storedYPos = 99;
        int endstoredXPos = 99;
        int endstoredYPos = 99;
        int lastY = 0;
        int lastX = 0;
        //Search top ROW for start
        for (int i = 0; i < Width; i++)
        {
            currentPosVal = map[0, i];
            if (currentPosVal == 1)
            {
                storedXPos = i;
                storedYPos = 0;
                waypoints.Enqueue(new Vector2(storedXPos, storedYPos) * 32);
                lastX = storedXPos;
                lastY = storedYPos;
                break;
            }
        }
        //if start not set
        if (storedXPos == 99 && storedXPos == 99)
        {
            //look in 1st coloum for start
            for (int i = 0; i < Height; i++)
            {
                currentPosVal = map[i, 0];
                if (currentPosVal == 1)
                {
                    storedXPos = 0;
                    storedYPos = i;
                    waypoints.Enqueue(new Vector2(storedXPos, storedYPos) * 32);
                    lastX = storedXPos;
                    lastY = storedYPos;
                    break;
                }
            }
        }
        //search end COLOUM for end
        for (int i = 0; i < Height; i++)
        {
            currentPosVal = map[i, Width - 1];
            if (currentPosVal == 1)
            {
                endstoredXPos = Width - 1;
                endstoredYPos = i;
            }
        }
        //If end not set look in bottom row for end
        if (endstoredXPos == 99 && endstoredYPos == 99)
        {
            for (int i = 0; i < Width; i++)
            {
                currentPosVal = map[7, i];
                if (currentPosVal == 1)
                {
                    endstoredXPos = i;
                    endstoredYPos = Height - 1;
                }
            }
            if (endstoredXPos == 99 && endstoredYPos == 99)
            {

            }
        }
        // start midlle loop
        while (true)
        {
            lPos = 0;
            rPos = 0;
            uPos = 0;
            dPos = 0;

            //If current pos is not down the left hand edge
            if (storedXPos > 0) { lPos = map[storedYPos, storedXPos - 1]; }

            //If current pos square is not down the right hand edge
            if (storedXPos < Width - 1) { rPos = map[storedYPos, storedXPos + 1]; }

            //If current pos square is not in the top row
            if (storedYPos > 0) { uPos = map[storedYPos - 1, storedXPos]; }

            //If current pos square is not in the bottom row
            if (storedYPos < Height - 1) { dPos = map[storedYPos + 1, storedXPos]; }

            if (lPos == 1 && (lastX != storedXPos - 1 || lastY != storedYPos))
            {
                lastX = storedXPos;
                lastY = storedYPos;
                storedXPos--;
                waypoints.Enqueue(new Vector2(storedXPos, storedYPos) * 32);

            }
            else if (rPos == 1 && (lastX != storedXPos + 1 || lastY != storedYPos))
            {
                lastX = storedXPos;
                lastY = storedYPos;
                storedXPos++;
                waypoints.Enqueue(new Vector2(storedXPos, storedYPos) * 32);
            }
            else if (dPos == 1 && (lastX != storedXPos || lastY != storedYPos + 1))
            {
                lastX = storedXPos;
                lastY = storedYPos;
                storedYPos++;
                waypoints.Enqueue(new Vector2(storedXPos, storedYPos) * 32);
            }
            else if (uPos == 1 && (lastX != storedXPos || lastY != storedYPos - 1))
            {
                lastX = storedXPos;
                lastY = storedYPos;
                storedYPos--;
                waypoints.Enqueue(new Vector2(storedXPos, storedYPos) * 32);

            }
            if (storedXPos == endstoredXPos && storedYPos == endstoredYPos)
            {
                break;
            }
        }
    }
    public void AddTexture(Texture2D texture)                                                           // method adds a texture to our texture list. 
    {
        tileTextures.Add(texture);
    }
    //Reads number from array, store its value in textureIndex, Use textureIndex to get the texture from tileTextures, 
    public void Draw(SpriteBatch batch)                                                                     //Draw appropiate texture, Repeat through all elements of the array
    {
        int textureIndex;
        Texture2D texture;
        for (int x = 0; x < Width; x++)
        {
            for (int y = 0; y < Height; y++)
            {
                if (levelNo == 0)
                {
                    textureIndex = map[y, x];
                    if (textureIndex == -1)
                        continue;
                    texture = tileTextures[textureIndex];
                    batch.Draw(texture, new Rectangle(x * 32, y * 32, 32, 32), Color.White);
                }
                if (levelNo > 0)
                {
                    textureIndex = map[y, x];

                    textureIndex += (levelNo * 2);
                   if (textureIndex == -1)
                    continue;
                texture = tileTextures[textureIndex];
                batch.Draw(texture, new Rectangle(x * 32, y * 32, 32, 32), Color.White);
                }
            }
        }
    }
}
公共类级别
{
保护温度;
受保护的int LEVENO;
专用队列航路点=新队列();
公共队列航路点
{
得到
{
返回航路点;
}
}
int[,]map=新int[,]
{
{0,0,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,},
{0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,},
{0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0,},
{0,0,0,0,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,},
{0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,},
{0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,},
{0,0,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0,},
{0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,},
{0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,},
{0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,},
{0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0,},
{0,0,0,0,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,},
{0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,},
{0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,},
{0,0,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,},
{0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,},
};
public int Temperature//获取级别的宽度(一行中有多少个数字)
{
得到
{
返回温度;
}
}
public int LevelNo//获取级别的宽度(一行中有多少个数字)
{
得到
{
返回级别No;
}
设置
{
levelNo=数值;
}
}
public int Width//获取级别的宽度(一行中有多少个数字)
{
得到
{
返回map.GetLength(1);
}
}
public int Height//获取我们级别的高度(一列中有多少个数字)
{
得到
{
返回map.GetLength(0);
}
}
public int GetIndex(int-cellX,int-cellY)//返回请求单元格的索引。
{
如果(cellX<0 | | cellX>宽度-1 | | cellY<0 | | cellY>高度-1)
{
返回0;
}
其他的
{
返回图[cellY,cellX];
}
}
public List tileTextures=new List();//包含所有纹理的列表
/// 
///新水平
/// 
公共级别()
{
设定航路点(地图);
该温度=1000;
这1.levelNo=2;
}
专用无效设置航路点(int[,]地图)
{
int currentPosVal=map[0,0];
int-lPos=0;
int rPos=0;
int-uPos=0;
int-dPos=0;
int storedXPos=99;
int storedYPos=99;
int-endstoredXPos=99;
int endstoredYPos=99;
int-lastY=0;
int lastX=0;
//搜索最上面一行的开始
对于(int i=0;i0){lPos=map[storedYPos,storedXPos-1];}
//如果当前pos方块不在右边缘下方
if(storedXPos0){uPos=map[storedYPos-1,storedXPos];}
//如果当前pos squ
0,0,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0
0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0
0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0
0,0,0,0,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0
0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0
0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0
0,0,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0
0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0
0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0
0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0
0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0
0,0,0,0,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0
0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0
0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0
0,0,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0
0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1