中国棋盘格C#in Unity。连接相邻瓷砖
我是一名学生,目前正忙于一项任务,制作中国跳棋游戏。我现在想做的是让每个瓷砖都知道它周围的邻居以及邻居的方向。代码方面,这是我得到的:中国棋盘格C#in Unity。连接相邻瓷砖,c#,unity3d,C#,Unity3d,我是一名学生,目前正忙于一项任务,制作中国跳棋游戏。我现在想做的是让每个瓷砖都知道它周围的邻居以及邻居的方向。代码方面,这是我得到的: public class BoardSpawn : MonoBehaviour { [SerializeField] GameObject tiles, pawn; [SerializeField] float offset; //Skapar en jagged array av spelobjekt
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);
}
}
}
}
}
公共类BoardSpawn:monobhavior
{
[序列化字段]
游戏对象瓷砖、棋子;
[序列化字段]
浮动偏移量;
//Skapar en锯齿阵列av spelobjekt。
GameObject[][]板=新GameObject[17][];
TileScript[][]虚拟板=新TileScript[17][];
int[]upperOffset=newint[]{1,1,1,5,0,0,0,0,0,1,1,1,1,-4,0,0,0,0};
int[]Lowerofset=newint[]{0,0,0,-4,1,1,1,1,0,0,0,5,1,1,1,1};
//朗登·帕登。
int[]rowLength=新的int[]{1,2,3,4,13,12,11,10,9,10,11,12,13,4,3,2,1};
void Start()
{
创建此板();
设置邻居();
}
//使用for-in-for循环生成板
void thisboard()
{//在行中循环
对于(int i=0;i8)
{
虚拟板[i][j].SetColor(红色);
}
打破
案例8:
打破
案例9:
案例10:
案例11:
案例12:
if(j8)
{
虚拟板[i][j].SetColor(黑色);
}
打破
案例13:
案例14:
案例15:
案例16:
虚拟板[i][j].SetColor(白色);
打破
违约:
打破
}
}
}
}
//为每个瓷砖提供位置和方向
作废
{
int检验=0;
for(int i=0;i0)
{
如果(j=min&&j+rowOffset0)
{
虚拟板[i][j].createNeighbor(虚拟板[i][j-1],方向.左);
}
//如果它不是右边的最后一块瓷砖,那么瓷砖的右边有一个邻居。
if(j