功能包括:
失败检测,标记炸弹,点击标记自动排除周围安全区域,限制棋盘拖动范围等功能。
开发环境:unity2019
点击正确排除周围炸弹;
void Circulation(Block block) {
inCheckBlocks.Add(block);
if (block.GetBoomCount(blocks) == 0)
{
for (float x = block._blockIndex.x - 1; x <= block._blockIndex.x + 1; x++)
{
for (float y = block._blockIndex.y - 1; y <= block._blockIndex.y + 1; y++)
{
foreach (var v in blocks)
{
if (v._blockIndex == new Vector2(x, y) && !v._hasChecked && !inCheckBlocks.Contains(v))
{
if (v.GetBoomCount(blocks) == 0)
{
Check(v);
}
}
}
}
}
}
}
限制棋盘移动:
// 设置最大、最小坐标
void SetDragRange()
{
minX = -((GetComponent<RectTransform>().rect.width / 2) * transform.localScale.x - container.rect.width / 2);
//Debug.Log(minX);
maxX =Mathf.Abs( (GetComponent<RectTransform>().rect.width / 2) * transform.localScale.x - container.rect.width / 2);
// Debug.Log(maxX);
maxY = Mathf.Abs(container.rect.height / 2 - ((GetComponent<RectTransform>().rect.height / 2) * transform.localScale.x));
// Debug.Log(maxY);
}
void Magnet() {
if (transform.localPosition.y <= -maxY)
{
transform.localPosition = new Vector3(transform.localPosition.x, -maxY- 25, 0);
// Debug.Log("a上");
}
//下
if (transform.localPosition.y >= maxY)
{
transform.localPosition = new Vector3(transform.localPosition.x, maxY + 25, 0);
// Debug.Log("下");
}
// 左
if (transform.localPosition.x >= maxX)
{
transform.localPosition = new Vector3(maxX + 25, transform.localPosition.y, 0);
// Debug.Log("左");
}
//右
if (transform.localPosition.x <= minX)
{
transform.localPosition = new Vector3(minX - 25, transform.localPosition.y, 0);
// Debug.Log("右");
}
}
点击数字排除炸弹(这段有点乱):
void ChangeBlock(Block block) {
int CirleNum = 0;
int WorseNum = 0;
for (float x = block._blockIndex.x - 1; x <= block._blockIndex.x + 1; x++)
{
for (float y = block._blockIndex.y - 1; y <= block._blockIndex.y + 1; y++)
{
foreach (var v in blocks)
{
if (v._blockIndex == new Vector2(x, y) && v._blockType == BlockType.RedFlag && v.OriginalType == BlockType.Boom)
{
CirleNum++;
}
else if (v._blockIndex == new Vector2(x, y) && v._blockType == BlockType.RedFlag && v.OriginalType == BlockType.Normal)
{
WorseNum++;
}
}
}
}
//标记正确时
if (CirleNum == block.GetBoomCount(blocks) && WorseNum==0)
{
if (PlayerPrefs.GetInt("Shake", 1) == 1)
{
KomalUtil.Instance.TapEngineSelection();
}
for (float x = block._blockIndex.x - 1; x <= block._blockIndex.x + 1; x++)
{
for (float y = block._blockIndex.y - 1; y <= block._blockIndex.y + 1; y++)
{
foreach (var v in blocks)
{
if (v._blockIndex == new Vector2(x, y) && v._blockType == BlockType.Normal)
{
Circulation02(v);
v.imgClicked.gameObject.SetActive(true);
}
}
}
}
}
else if(CirleNum != block.GetBoomCount(blocks) && WorseNum >0)
{
if (PlayerPrefs.GetInt("Shake", 1) == 1)
{
KomalUtil.Instance.TapEngineSelection();
}
for (float x = block._blockIndex.x - 1; x <= block._blockIndex.x + 1; x++)
{
for (float y = block._blockIndex.y - 1; y <= block._blockIndex.y + 1; y++)
{
foreach (var v in blocks)
{
if (v._blockIndex == new Vector2(x, y) && v._blockType == BlockType.RedFlag && v.OriginalType == BlockType.Normal)
{
v.transform.GetComponent<Image>().sprite = Lib.instance.FlagB[6];
facade.SendNotification("MSG_GameOver");
v.imgBoom.gameObject.SetActive(false);
v.imgRedBoom.gameObject.SetActive(true);
v.imgRedBoom.sprite = Lib.instance.FlagB[1];
btnDead.gameObject.SetActive(true);
btnSmile.gameObject.SetActive(false);
Resmall();
}
else if (v._blockIndex == new Vector2(x, y) && v.OriginalType == BlockType.Boom && v._blockType == BlockType.RedFlag)
{
v.transform.GetComponent<Image>().sprite = Lib.instance.FlagB[6];
facade.SendNotification("MSG_GameOver");
v.imgBoom.gameObject.SetActive(false);
v.imgRedBoom.gameObject.SetActive(true);
v.imgRedBoom.sprite = Lib.instance.FlagB[2];
btnDead.gameObject.SetActive(true);
btnSmile.gameObject.SetActive(false);
Resmall();
}
else if (v._blockIndex == new Vector2(x, y) && v._blockType == BlockType.Boom || v._blockType == BlockType.QueFlag)
{
v.transform.GetComponent<Image>().sprite = Lib.instance.FlagB[6];
facade.SendNotification("MSG_GameOver");
v.imgBoom.gameObject.SetActive(false);
v.imgRedBoom.gameObject.SetActive(true);
v.imgRedBoom.sprite = Lib.instance.FlagB[7];
btnDead.gameObject.SetActive(true);
btnSmile.gameObject.SetActive(false);
Resmall();
}
}
}
}
}
}