public void CalculateBlockLocation(BlockParentManager manager, Vector2 pos, int row, int col) { //这个代表的是在第几列 int indexX = Mathf.RoundToInt((pos.x) / minSize); if (indexX == -1 && pos.x > cellOri.x - minSize / 2f) { indexX = 0; } if (indexX == wordCol && pos.x < cellOri.x + minSize * wordCol + 2 * (wordCol - 1) - 10) { indexX = wordCol - 1; } // Debug.Log ("移动的横坐标是 : " + indexX); if (indexX < 0 || indexX >= wordCol || indexX + col >= wordCol) { manager.Back(); return; } //这个代表的是在第几行 int indexY = Mathf.RoundToInt((-pos.y) / minSize); if (indexY == -1 && pos.y < cellOri.y + minSize - 10f) { indexY = 0; } // Debug.Log ("移动的纵坐标是 : " + indexY); if (indexY < 0 || indexY >= wordRow || indexY + row >= wordRow) { manager.Back(); return; } int m = indexX; int k = indexY; List <PuzzleLetterCell> cells = new List <PuzzleLetterCell> (); for (int i = 0; i < manager.pos.GetLength(0); i++) { for (int j = 0; j < manager.pos.GetLength(1); j++) { PuzzleLetterCell cell = wordGroup.GetChild(k * wordCol + m).GetComponent <PuzzleLetterCell> (); if (manager.pos[i, j] == 1) { if (cell.isShowing) { manager.Back(); return; } else { cells.Add(cell); } } m++; } m = indexX; k++; } PlaceBlock(manager, true); manager.SetBlockPosition(CalculateWordPos(indexX, indexY), cells); }
//点击格子显示Tips public void ShowHint(PuzzleLetterCell letter) { string rowHint = ""; string colHint = ""; Position pos = letter.pos; List <PuzzleLetterCell> rowCell = new List <PuzzleLetterCell> (); List <PuzzleLetterCell> colCell = new List <PuzzleLetterCell> (); foreach (PuzzleLetterCell cell in cellGroup) { cell.ClearHintBg(); } //row先向左遍历 然后再向右row的hint 都在最左边 if (pos.y == 0) { rowHint = data.hintInfo[pos.x][pos.y]; } else { for (int i = pos.y - 1; i >= 0; i--) { if (!string.IsNullOrEmpty(wordInfo[pos.x][i])) { rowCell.Add(cellGroup[pos.x * wordCol + i]); rowHint = data.hintInfo[pos.x][i]; } else { break; } } } for (int j = pos.y + 1; j < wordCol; j++) { if (!string.IsNullOrEmpty(wordInfo[pos.x][j])) { rowCell.Add(cellGroup[pos.x * wordCol + j]); } else { break; } } //向下的遍历 先向上 if (pos.x == 0) { colHint = data.hintInfo[pos.x][pos.y]; } else { for (int m = pos.x - 1; m >= 0; m--) { if (!string.IsNullOrEmpty(wordInfo[m][pos.y])) { colCell.Add(cellGroup[m * wordCol + pos.y]); colHint = data.hintInfo[m][pos.y]; } else { break; } } } for (int n = pos.x + 1; n < wordRow; n++) { if (!string.IsNullOrEmpty(wordInfo[n][pos.y])) { colCell.Add(cellGroup[n * wordCol + pos.y]); } else { break; } } //设置相关区域背景 letter.SetHintBg(2); foreach (PuzzleLetterCell cell in rowCell) { cell.SetHintBg(0); } foreach (PuzzleLetterCell cell in colCell) { cell.SetHintBg(1); } foreach (Transform item in rowContent) { if (item.GetComponent <TipItem> ().tipIndex == rowHint) { item.GetComponent <TipItem> ().ClickButtonOn(); } else { item.GetComponent <TipItem> ().ClickButtonOff(); } } foreach (Transform item in colContent) { if (item.GetComponent <TipItem> ().tipIndex == colHint) { item.GetComponent <TipItem> ().ClickButtonOn(); } else { item.GetComponent <TipItem> ().ClickButtonOff(); } } }
private void SetWordGrid() { wordRow = wordInfo.Count; wordCol = wordInfo[0].Count; Calculate(wordRow, wordCol); for (int i = 0; i < wordInfo.Count; i++) { List <string> letters = wordInfo[i]; for (int j = 0; j < letters.Count; j++) { if (i == 0 && j == 0) { letterCell.pos.x = i; letterCell.pos.y = j; letterCell.SetSize(minSize); letterCell.GetComponent <RectTransform> ().anchoredPosition = CalculateWordPos(j, i); if (!string.IsNullOrEmpty(wordInfo[i][j])) { letterCell.gameObject.SetActive(true); if (data.hintInfo.Count != 0 && !string.IsNullOrEmpty(data.hintInfo[i][j])) { hintText.GetComponent <RectTransform> ().sizeDelta = new Vector2(hintMin, hintMin); hintText.GetComponentInChildren <Text> ().text = data.hintInfo[i][j]; } if (saveData != null) { letterCell.isShowing = saveData.letterShow[i][j]; } } else { letterCell.isShowing = true; } cellGroup.Add(letterCell); } else { PuzzleLetterCell cell = Instantiate(letterCell, wordGroup); cell.pos.x = i; cell.pos.y = j; cell.SetSize(minSize); cell.isShowing = false; cell.gameObject.SetActive(false); cell.GetComponent <RectTransform> ().anchoredPosition = CalculateWordPos(j, i); cell.name = "letterCell" + i.ToString() + j.ToString(); cellGroup.Add(cell); if (!string.IsNullOrEmpty(wordInfo[i][j])) { cell.gameObject.SetActive(true); if (saveData != null) { cell.isShowing = saveData.letterShow[i][j]; } if (data.hintInfo.Count != 0 && !string.IsNullOrEmpty(data.hintInfo[i][j])) { Vector2 cellSize = cell.GetComponent <RectTransform> ().sizeDelta; GameObject hintClone = Instantiate(hintText, hintGroup); hintClone.GetComponentInChildren <Text> ().text = data.hintInfo[i][j]; hintClone.GetComponent <RectTransform> ().sizeDelta = new Vector2(hintMin, hintMin); hintClone.GetComponent <RectTransform> ().anchoredPosition = new Vector2((minSize + 2) * j, (-minSize - 2) * i) + hintText.GetComponent <RectTransform> ().anchoredPosition; } } else { cell.isShowing = true; } } } } }