Unity中的中文Checker C#。连接邻居瓷砖

时间:2018-07-06 16:24:27

标签: c# unity3d

让一名目前正忙于作业的学生创建游戏中文跳棋。我现在想做的就是让每个 tile 知道周围的邻居以及该邻居方向。在代码方面,这就是我所得到的:

    public class BoardSpawn : MonoBehaviour
    {

    [SerializeField]
    GameObject tiles, pawn;

    [SerializeField]
    float offset;
    //Skapar en jagged array av spelobjekt.
    GameObject[][] board = new GameObject[17][];
    TileScript[][] virtualBoard = new TileScript[17][];

    int[] upperOffset = new int[] { 1, 1, 1, 5, 0, 0, 0, 0, 1, 1, 1, 1, -4, 0, 0, 0 };

    int[] lowerOffset = new int[] { 0, 0, 0, -4, 1, 1, 1, 1, 0, 0, 0, 0, 5, 1, 1, 1 };


    //Längden på raden. 
    int[] rowLengths = new int[] { 1, 2, 3, 4, 13, 12, 11, 10, 9, 10, 11, 12, 13, 4, 3, 2, 1 };

    void Start()
    {
        SpawnThisBoard();
        SetNeighbours();

    }

    // spawning the board with for in for loop 
    void SpawnThisBoard()
    {// loops through the row
        for (int i = 0; i < board.Length; i++)
        {
            board[i] = new GameObject[rowLengths[i]];
            virtualBoard[i] = new TileScript[rowLengths[i]];
            //loops through the column
            for (int j = 0; j < board[i].Length; j++)
            {
     // Creates a tile for each index position for the current row
                GameObject tile = Instantiate(tiles, new Vector3((-(rowLengths[i] - 1) + (offset * j)), 0f, offset * (8 - i)), Quaternion.identity);
                virtualBoard[i][j] = tile.GetComponent<TileScript>();

                virtualBoard[i][j].AssignIndex(j, i);
                // Just a switch to give certian positions a color
                switch (i)
                {
                    case 0:
                    case 1:
                    case 2:
                    case 3:
                        virtualBoard[i][j].SetColor(yellow);
                        break;
                    case 4:
                    case 5:
                    case 6:
                    case 7:
                        if (j < board[i].Length - 9)
                        {
                            virtualBoard[i][j].SetColor(blue);

                        }
                        else if (j > 8)
                        {
                            virtualBoard[i][j].SetColor(red);
                        }
                        break;
                    case 8:
                        break;
                    case 9:
                    case 10:
                    case 11:
                    case 12:
                        if (j < board[i].Length - 9)
                        {
                            virtualBoard[i][j].SetColor(green);

                        }
                        else if (j > 8)
                        {
                            virtualBoard[i][j].SetColor(black);
                        }

                        break;

                    case 13:
                    case 14:
                    case 15:
                    case 16:
                        virtualBoard[i][j].SetColor(white);
                        break;



                    default:
                        break;
                }
            }
        }

    }
    // gives position and direction to each tile 
    void SetNeighbours()
    {
        int test = 0;
        for (int i = 0; i < virtualBoard.Length; i++)
        {
            int min = 0, max = 0, rowOffset = 0;
            switch (i)
            {
                case 1:
                case 2:
                case 3:
                    max = virtualBoard[i].Length - 1;
                    break;
            }
            for (int j = 0; j < virtualBoard[i].Length; j++)
            {
//If its not the tile on the top corner then we have a neighbour above the tile

                if (i > 0)
                {
                    if (j < max)
                    {
                        virtualBoard[i][j].CreateNeighbour(virtualBoard[i - 1][j + rowOffset], Direction.UpperLeft);
                    }
                    if (j >= min && j + rowOffset < virtualBoard[i - 1].Length)
                    {
                       virtualBoard[i][j].CreateNeighbour(virtualBoard[i - 1][j + rowOffset], Direction.UpperRight);
                    }

                    if (j - lowerOffset[i] == 0)
                    {                      
                            virtualBoard[i][j].CreateNeighbour(virtualBoard[i - 1][(j + lowerOffset[i])], Direction.UpperLeft);
                    }
                    if (j - lowerOffset[i] == 0)
                    {                    
                        virtualBoard[i][j].CreateNeighbour(virtualBoard[i - 1][(j - lowerOffset[i])], Direction.UpperRight);
                    }

                }
//if its not the last tile on the bottom corner then the tile has a neighbour below

                if (i < virtualBoard.Length - 1)
                {

                    if (j - lowerOffset[i] == 1)
                    {
                        virtualBoard[i][j].CreateNeighbour(virtualBoard[i + 1][j - (upperOffset[i] - 1)], Direction.LowerLeft);
                    }
                    if (j - lowerOffset[i] == 0)
                    {
                        virtualBoard[i][j].CreateNeighbour(virtualBoard[i + 1][j - (upperOffset[i] - 1)], Direction.LowerRight);
                    }

                }
//if its not the last tile on the left side, the tile has a neighbour on its left side.


                if (j > 0)
                {
                    virtualBoard[i][j].CreateNeighbour(virtualBoard[i][j - 1], Direction.Left);
                }

//if its not the last tile on the right ride, the tile has a neighbour on its right side.


                if (j < virtualBoard[i].Length - 1)
                {
                    virtualBoard[i][j].CreateNeighbour(virtualBoard[i][j + 1], Direction.Right);
                }
            }
        }
    }
}

我遇到的问题是数组索引超出范围,老实说,我不知道如何解决这个问题...

希望它不会太凌乱以至于无法理解,已经尝试到处寻找类似的东西并且什么也没找到...

0 个答案:

没有答案