数据检测数组
#region 数据检测数组-NumIndexArray-NumAssemblyArray
private int [][] NumIndexArray = new int [4 ][]{
new int [4 ]{0 ,0 ,0 ,0 },
new int [4 ]{0 ,0 ,0 ,0 },
new int [4 ]{0 ,0 ,0 ,0 },
new int [4 ]{0 ,0 ,0 ,0 }
};
private Num[][] NumAssemblyArray = new Num[4 ][]{
new Num[4 ]{null ,null ,null ,null },
new Num[4 ]{null ,null ,null ,null },
new Num[4 ]{null ,null ,null ,null },
new Num[4 ]{null ,null ,null ,null }
};
#endregion
检测鼠标按下并通过鼠标抬起检测方法进行移动
#region 检测鼠标按下并通过鼠标抬起检测方法进行移动-CheckAndMoveNum
private void CheckAndMoveNum () {
if (Input.GetMouseButtonDown(0 ))
{
mouseDownPosition = Input.mousePosition;
}
if (Input.GetMouseButtonUp(0 ) == false ) return ;
TouchDir touchDir=GetTouchDir();
ToMove(touchDir);
}
#endregion
判断鼠标滑动方向
#region 判断鼠标滑动方向- GetTouchDir
private TouchDir GetTouchDir() {
if (Input.GetMouseButtonUp (0 )) {
Vector3 TouchOffset = Input.mousePosition - mouseDownPosition
//Mathf.Abs (TouchOffset.x ) >= Mathf.Abs (TouchOffset.y )取正负号
if (Mathf.Abs (TouchOffset.x ) >= Mathf.Abs (TouchOffset.y ) && Mathf.Abs (TouchOffset.x ) > 80 )
{
if (TouchOffset.x > 0 )
{
return TouchDir.Right
}
else if (TouchOffset.x < 0 )
{
return TouchDir.Left
}
}
if (Mathf.Abs (TouchOffset.x ) < Mathf.Abs (TouchOffset.y ) && Mathf.Abs (TouchOffset.y ) > 80 )
{
if (TouchOffset.y > 0 )
{
return TouchDir.Top
}
else if (TouchOffset.y < 0 )
{
return TouchDir.Bottom
}
}
}
return TouchDir.None
}
#endregion
生成Num-CreateNum
#region 生成Num-CreateNum(TouchDir touchDir)-(int x,int y,int Index)
private void CreateNum (TouchDir touchDir) {
Vector2 newPos = SelectEmptyPos(touchDir);
if (newPos.x != 9 || newPos.y != 9 )
{
GameObject item = Instantiate(NumPrefab) as GameObject;
Num numItem = item.GetComponent<Num>();
numItem.NumIndex = 2 ;
numItem.InitX = (int )newPos.x;
numItem.InitY = (int )newPos.y;
item.transform.parent = this .transform;
NumIndexArray[(int )newPos.x][(int )newPos.y] = 2 ;
NumAssemblyArray[(int )newPos.x][(int )newPos.y] = numItem;
StatisticsScore();
}
else {
gameState = GameState.End;
bool isEnd1 = true ;
bool isEnd2 = true ;
int index=0 ;
#region 循环判断是否有相邻的相同数字
for (int x = 0 ; x < 4 ; x++)
{
for (int y = 0 ; y < 4 ; y++)
{
if (index == 0 )
{
index = NumIndexArray[x][y];
}
else
{
if (index != NumIndexArray[x][y])
{
index = NumIndexArray[x][y];
}
else
{
isEnd1 = false ;
break ;
}
}
}
}
index = 0 ;
for (int y = 0 ; y < 4 ; y++)
{
for (int x = 0 ; x < 4 ; x++)
{
if (index == 0 )
{
index = NumIndexArray[x][y];
}
else
{
if (index != NumIndexArray[x][y])
{
index = NumIndexArray[x][y];
}
else
{
isEnd2 = false ;
break ;
}
}
}
}
#endregion
if (!isEnd1 || !isEnd2) {
gameState = GameState.Playing;
}
StatisticsScore();
}
}
private void CreateNum (int x,int y,int Index)
{
GameObject item = Instantiate(NumPrefab) as GameObject;
Num numItem = item.GetComponent<Num>();
numItem.NumIndex = Index;
numItem.InitX = x;
numItem.InitY = y;
item.transform.parent = this .transform;
NumIndexArray[x][y] = Index;
NumAssemblyArray[x][y] = numItem;
StatisticsScore();
}
#endregion
源码下载