// 遍历该元素周围的元素 private void findMatchItem(GameObject curObj) { if (curObj == null) { return; } if (!curObj.GetComponent <GameSp>().canMove()) { return; } if (!curMatchObjArr.Contains(curObj)) { curMatchObjArr.Add(curObj); } else { return; } GameSp curSp = curObj.GetComponent <GameSp>(); int rowCount = _model.m_RowCount; int colCount = _model.m_ColCount; // top if (curSp.Row + 1 < rowCount && spArr[curSp.Row + 1, curSp.Col] != null && spArr[curSp.Row + 1, curSp.Col].GetComponent <GameSp>().StyleType == curSp.StyleType) { findMatchItem(spArr[curSp.Row + 1, curSp.Col]); } // right if (curSp.Col + 1 < colCount && spArr[curSp.Row, curSp.Col + 1] != null && spArr[curSp.Row, curSp.Col + 1].GetComponent <GameSp>().StyleType == curSp.StyleType) { findMatchItem(spArr[curSp.Row, curSp.Col + 1]); } // left if (curSp.Col - 1 >= 0 && spArr[curSp.Row, curSp.Col - 1] != null && spArr[curSp.Row, curSp.Col - 1].GetComponent <GameSp>().StyleType == curSp.StyleType) { findMatchItem(spArr[curSp.Row, curSp.Col - 1]); } // down if (curSp.Row - 1 >= 0 && spArr[curSp.Row - 1, curSp.Col] != null && spArr[curSp.Row - 1, curSp.Col].GetComponent <GameSp>().StyleType == curSp.StyleType) { findMatchItem(spArr[curSp.Row - 1, curSp.Col]); } }
// 判断other是否相邻 public bool isFriend(GameObject other) { GameSp otherSp = other.GetComponent <GameSp>(); if (otherSp == null) { return(false); } return((otherSp.Row == Row && Mathf.Abs(otherSp.Col - Col) == 1) || (otherSp.Col == Col && Mathf.Abs(otherSp.Row - Row) == 1)); }
// 两个元素移动交换 private void exchangeMove(GameObject currItem, GameObject otherItem) { GameSp currSp = currItem.GetComponent <GameSp>(); GameSp otherSp = otherItem.GetComponent <GameSp>(); int tempRow = currSp.Row; int tempCol = currSp.Col; currSp.GetComponent <SpMove>().move(otherSp.Row, otherSp.Col, dropTime); otherSp.GetComponent <SpMove>().move(tempRow, tempCol, dropTime); spArr[currSp.Row, currSp.Col] = currItem; spArr[otherSp.Row, otherSp.Col] = otherItem; }
/// <summary> /// 匹配当前元素周围的销毁项 /// </summary> /// <param name="curObj">当前元素</param> /// <returns>是否有销毁项</returns> private bool destoryMatchItems(GameObject curObj) { // 匹配 bool isDestory = false; List <GameObject> destoryItems = stepMath(curObj); curMatchObjArr.Clear(); foreach (GameObject obj in destoryItems) { GameSp objSp = obj.GetComponent <GameSp>(); objSp.clear(); spArr[objSp.Row, objSp.Col] = null; isDestory = true; } return(isDestory); }
// 交换两个元素 private IEnumerator exchangeItem(GameObject currItem, GameObject otherItem) { GameSp currSp = currItem.GetComponent <GameSp>(); GameSp otherSp = otherItem.GetComponent <GameSp>(); if (currSp.isFriend(otherItem) && currSp.canMove() && otherSp.canMove()) { exchangeMove(currItem, otherItem); if (!destoryMatchItems(currItem) && !destoryMatchItems(otherItem)) { yield return(new WaitForSeconds(dropTime)); exchangeMove(currItem, otherItem); } else { yield return(new WaitForSeconds(0.3f)); StartCoroutine(AllDrop()); } } }
private void Awake() { gameSp = GetComponent <GameSp>(); }