您可以将构造函数用作条件而不调用它们吗

时间:2018-11-04 17:39:45

标签: c# constructor conditional-statements

我知道这个标题可能有点含糊,但是我不确定如何尝试解释我要做什么。

我要尝试将大量向量分类为细胞。我使用构造函数将向量按4组进行分类。即使我从不使用变量“ cell”,但使用“ Cells”,此代码是否也可以工作?

public class assignCells
{

    public static List<List<Vector3>> Cells = new List<List<Vector3>>();

    public assignCells(Vector3 bottom, Vector3 top, Vector3 right, Vector3 left)
    {
        if (top.x == bottom.x + 20.0 && right.z == left.z + 50.0)
        {
            Cells.Add(new List<Vector3> { bottom, top, right, left });
        }
    }
}  

public void gC()
{
    List<Vector3> old = new List<Vector3>();

    List<int> rnL = new List<int>();

    for (int i = 0; i < xsize * zsize; i++)
    {

        List<Vector3> tC = assignCells.Cells[rn];
        List<int> rCg = new List<int>
        {
            rn + 1,
            rn + 201,
            rn - 1,
            rn - 201
        };
        int rCp = rc.Next(1, 5);
        int oC = rCg[rCp];
        List<Vector3> nC = assignCells.Cells[oC];

        if (nC == null)
        {
            if(old.Contains(tC[1]) || old.Contains(tC[2]) || old.Contains(tC[3]) || old.Contains(tC[4])){
                rnL[i--] = rn;
                continue;
            }
            else
            {
                i--;
            }

        }
        if(tC[1] == nC[2])
        {
            tC.RemoveAt(1);
            Instantiate(Wall, tC[2], Quaternion.Euler(0, 0, 0));
            Instantiate(Wall, tC[3], Quaternion.Euler(90, 0, 0));
            Instantiate(Wall, tC[4], Quaternion.Euler(90, 0, 0));
            assignCells.Cells.Insert(rn, old);
            assignCells.Cells.RemoveAt(rn);
        }
        if (tC[3] == nC[4])
        {
            tC.RemoveAt(3);
            Instantiate(Wall, tC[1], Quaternion.Euler(0, 0, 0));
            Instantiate(Wall, tC[2], Quaternion.Euler(0, 0, 0));
            Instantiate(Wall, tC[4], Quaternion.Euler(90, 0, 0));
            assignCells.Cells.Insert(rn, old);
            assignCells.Cells.RemoveAt(rn);
        }
        if (tC[2] == nC[1])
        {
            tC.RemoveAt(2);
            Instantiate(Wall, tC[1], Quaternion.Euler(0, 0, 0));
            Instantiate(Wall, tC[3], Quaternion.Euler(90, 0, 0));
            Instantiate(Wall, tC[4], Quaternion.Euler(90, 0, 0));
            assignCells.Cells.Insert(rn, old);
            assignCells.Cells.RemoveAt(rn);
        }
        if (tC[4] == nC[3])
        {
            tC.RemoveAt(4);
            Instantiate(Wall, tC[1], Quaternion.Euler(0, 0, 0));
            Instantiate(Wall, tC[2], Quaternion.Euler(0, 0, 0));
            Instantiate(Wall, tC[3], Quaternion.Euler(90, 0, 0));
            assignCells.Cells.Insert(rn, old);
            assignCells.Cells.RemoveAt(rn);
        }
        rnL.Add(rn);
        rn = oC;

    }
}


void CreateW()
{
    for (int i = 0; i < zsize * xsize; i++)
    {
        int n = i;
        int nn = 50 * n;
        int nn2 = 20 * n;
        mazeNumH.Add(new Vector3(-5000 + nn, 0, -5000 + nn2));
        mazeNumV.Add(new Vector3(-5000 + nn2, 0, -5000 + nn));
    }
    for (int i = 0; i < zsize * xsize; i++)
    {
        assignCells cell = new assignCells(mazeNumH[i], mazeNumH[i + zsize], mazeNumV[i], mazeNumV[i + 1]);
    }
    gC();
}

0 个答案:

没有答案