俄罗斯经典宝石功能;
包括方块的移动,方块阴影显示,位置检测,唯一解检测,失败检测,新手引导等功能。
开发环境:Unity2019
阴影使用射线检测:
hit = Physics2D.Raycast(transform.position, -Vector3.forward,20,~(1<<8));
if (hit && hit.collider.tag == "Chunk")
{
if (chunk != null&&chunk != hit.collider.gameObject)
{
transform.parent.GetComponent<Move>().RemoveBlock(this);
chunk.GetComponent<Image>().sprite = Lib.instance.Ground;
chunk.GetComponent<Image>().color = Color.white;
}
chunk = hit.collider.gameObject;
transform.parent.GetComponent<Move>().AddBlock(this);
if (isChangeColor)
{
chunk.GetComponent<Image>().sprite = GetComponent<Image>().sprite;
chunk.GetComponent<Image>().color = Color.gray;
}
else
{
chunk.GetComponent<Image>().sprite = Lib.instance.Ground;
chunk.GetComponent<Image>().color = Color.white;
}
}
else {
if (chunk != null)
{
transform.parent.GetComponent<Move>().RemoveBlock(this);
chunk.GetComponent<Image>().sprite = Lib.instance.Ground;
chunk.GetComponent<Image>().color = Color.white;
}
}
唯一解检测:
排查map里面为空的位置,如果只有一个位置符合,即为唯一解。
public void OnlyHint(Transform shape)
{
Vector3 NPos;//记录map中的空位置
int count = 0;
Dictionary<int, Vector3> RecordPosition = new Dictionary<int, Vector3>();// 记录唯一解的位置
//shape.localScale = Vector3.one;
for (int i = 0; i < UIGame.instance.map.GetLength(1); i++)
{
for (int j = 0; j < UIGame.instance.map.GetLength(0); j++)
{
if (UIGame.instance.map[i, j] == null)
{
NPos = SaveGround.instance.Groundmap[i, j].position;
Dictionary<int, Vector3> temporary = new Dictionary<int, Vector3>();//用一个临时位置储存shape孩子位置
for (int a = 0; a < shape.childCount; a++)
{
temporary[a] = shape.GetChild(a).position;
}
float Xdistance, Ydistance;//shape孩子和空位置的距离
Xdistance = shape.GetChild(0).position.x - NPos.x;
Ydistance = shape.GetChild(0).position.y - NPos.y;
for (int a = 0; a < shape.childCount; a++)//改变所有shape孩子的临时位置
{
temporary[a] = new Vector3(temporary[a].x - Xdistance, temporary[a].y - Ydistance, 0f);
}
if (PosLegal(temporary))
{
count++;
for (int a = 0; a < shape.childCount; a++)//记录唯一解的位置
{
RecordPosition[a] = temporary[a];
}
}
}
}
}
// shape.localScale = Vector3.one * 0.4f;
if (count == 1)
{
ShowPosition(RecordPosition);
}
}
方块位置保存;
public void SaveShape(Transform shape)
{
for (int i = 0; i < shape.childCount; i++)
{
Vector3 childPos = shape.GetChild(i).position;
xIndex = (int)Math.Round((childPos.x + xPosx.x) / spacing);
yIndex = (int)Math.Round((childPos.y + (-MPosy.y)) / spacing);
if (xIndex >= 7) { xIndex = 7; }
if (yIndex >= 7) { yIndex = 7; }
if (xIndex <= 0) { xIndex = 0; }
if (yIndex <= 0) { yIndex = 0; }
map[xIndex, yIndex] = shape.GetChild(i);
}
CheckClear();
}
方块消除检测:
/// <summary>
/// 方块消除检测
/// </summary>
public void CheckClear()
{
List<int> rowIndex = new List<int>();
List<int> colIndex = new List<int>();
//消除行
for (int i = 0; i < map.GetLength(1); i++)
{
bool isClearRow = true;
for (int j = 0; j < map.GetLength(0); j++)
{
if (map[j, i] == null)
{
isClearRow = false;
break;
}
}
if (isClearRow)
{
rowIndex.Add(i);
}
}
//消除列
for (int i = 0; i < map.GetLength(0); i++)
{
bool isClearCol = true;
for (int j = 0; j < map.GetLength(1); j++)
{
if (map[i, j] == null)
{
isClearCol = false;
break;
}
}
if (isClearCol)
{
colIndex.Add(i);
}
}
AssignMap02();
if (rowIndex.Count >= 1 && colIndex.Count >= 1)
{
ClearShapeRow(rowIndex);
ClearShapeCol(colIndex);
CountScore(rowIndex.Count+colIndex.Count);
PlayEliminateMusic(rowIndex.Count + colIndex.Count);
DoUIGame.instance.ShowNum(rowIndex.Count + colIndex.Count);
}
else if (colIndex.Count >= 1)
{
ClearShapeCol(colIndex);
CountScore(colIndex.Count);
PlayEliminateMusic( colIndex.Count);
DoUIGame.instance.ShowNum(colIndex.Count);
}
else if (rowIndex.Count >= 1)
{
ClearShapeRow(rowIndex);
CountScore(rowIndex.Count);
PlayEliminateMusic(rowIndex.Count);
DoUIGame.instance.ShowNum(rowIndex.Count);
}
}
Apk下载地址(有些机型不支持):https://github.com/fiveoneyan/Block-Apk