// Do Empty Tile Move Down after matching 3 private void DoEmptyDown() { for (var x = 0; x < Data.tileWidth; x++) { for (var y = 0; y < Data.tileHeight; y++) { var thiscell = cells[x, y]; if (!thiscell.IsEmpty) { continue; } int y1; for (y1 = y; y1 > 0; y1--) { DoSwapTile(FindTile(new TilePoint(x, y1)), FindTile(new TilePoint(x, y1 - 1))); } } } for (var x = 0; x < Data.tileWidth; x++) { int y; for (y = Data.tileHeight - 1; y >= 0; y--) { var thiscell = cells[x, y]; if (thiscell.IsEmpty) { break; } } if (y < 0) { continue; } var y1 = y; for (y = 0; y <= y1; y++) { MatchItem tile = FindTile(new TilePoint(x, y)); tile.transform.localPosition = new Vector3(x * cellWidth, (y - (y1 + 1)) * -cellHeight, 0f); tile.cell.SetRandomTile(6); int spriteIndex = (int)tile.cell.cellType - 1; tile.spriteIndex = spriteIndex; Sprite sprite = sprites[spriteIndex]; SpriteRenderer renderer = tile.GetComponent <SpriteRenderer>(); renderer.sprite = sprite; renderer.enabled = true; } } foreach (MatchItem tile in tiles) { Vector3 pos = new Vector3(tile.point.x * cellWidth, tile.point.y * -cellHeight); float dist = Vector3.Distance(tile.transform.localPosition, pos) * 0.01f; dist = 1f; TweenParms parms = new TweenParms().Prop("localPosition", pos).Ease(easeType); HOTween.To(tile.transform, 0.5f * dist, parms); } StartCoroutine(CheckMatch3TileOnly(0.5f)); }
//Make animation fade out after matching tiles and before creating new tiles to drop down IEnumerator animateDelete(MatchItem item, int index) { SpriteRenderer sr = item.GetComponent <SpriteRenderer>(); for (int i = 0; i < 4; i++) { sr.sprite = deleteSprites[index + i]; yield return(new WaitForSeconds(0.1f)); } GameObject instance = Instantiate(explosionPrefab) as GameObject; instance.transform.parent = effectArea; instance.transform.localPosition = new Vector3(item.point.x * cellWidth, item.point.y * -cellHeight, -5f); item.cell.cellType = Data.TileTypes.Empty; sr.enabled = false; }
// Check Match-3 Tile void CheckMatch3(Dictionary <TilePoint, Data.TileTypes> stack, MatchItem a, MatchItem b) { List <MatchItem> destroyListBomb = new List <MatchItem>(); List <MatchItem> destroyList = new List <MatchItem>(); List <MatchItem> bombList = new List <MatchItem>(); if (a != null) { switch (a.cell.cellType) { case Data.TileTypes.AtBomb: case Data.TileTypes.ShBomb: case Data.TileTypes.HBomb: case Data.TileTypes.ScBomb: case Data.TileTypes.GBomb: if (FindTile(new TilePoint(a.point.x, a.point.y + 1)) != null) { destroyListBomb.Add(FindTile(new TilePoint(a.point.x, a.point.y + 1))); } if (FindTile(new TilePoint(a.point.x, a.point.y - 1)) != null) { destroyListBomb.Add(FindTile(new TilePoint(a.point.x, a.point.y - 1))); } if (FindTile(new TilePoint(a.point.x - 1, a.point.y)) != null) { destroyListBomb.Add(FindTile(new TilePoint(a.point.x - 1, a.point.y))); } if (FindTile(new TilePoint(a.point.x + 1, a.point.y)) != null) { destroyListBomb.Add(FindTile(new TilePoint(a.point.x + 1, a.point.y))); } stack.Add(a.point, a.cell.cellType); break; default: break; } switch (b.cell.cellType) { case Data.TileTypes.AtBomb: case Data.TileTypes.ShBomb: case Data.TileTypes.HBomb: case Data.TileTypes.ScBomb: case Data.TileTypes.GBomb: if (FindTile(new TilePoint(b.point.x, b.point.y + 1)) != null) { destroyListBomb.Add(FindTile(new TilePoint(b.point.x, b.point.y + 1))); } if (FindTile(new TilePoint(b.point.x, b.point.y - 1)) != null) { destroyListBomb.Add(FindTile(new TilePoint(b.point.x, b.point.y - 1))); } if (FindTile(new TilePoint(b.point.x - 1, b.point.y)) != null) { destroyListBomb.Add(FindTile(new TilePoint(b.point.x - 1, b.point.y))); } if (FindTile(new TilePoint(b.point.x + 1, b.point.y)) != null) { destroyListBomb.Add(FindTile(new TilePoint(b.point.x + 1, b.point.y))); } stack.Add(b.point, b.cell.cellType); break; default: break; } } while (findBomb(destroyListBomb) != null) { MatchItem item = findBomb(destroyListBomb); switch (item.cell.cellType) { case Data.TileTypes.AtBomb: case Data.TileTypes.ShBomb: case Data.TileTypes.HBomb: case Data.TileTypes.ScBomb: case Data.TileTypes.GBomb: stack.Add(item.point, item.cell.cellType); destroyListBomb.Remove(item); if (FindTile(new TilePoint(item.point.x, item.point.y + 1)) != null) { if (!stack.ContainsKey(new TilePoint(item.point.x, item.point.y + 1))) { destroyListBomb.Add(FindTile(new TilePoint(item.point.x, item.point.y + 1))); } } if (FindTile(new TilePoint(item.point.x, item.point.y - 1)) != null) { if (!stack.ContainsKey(new TilePoint(item.point.x, item.point.y - 1))) { destroyListBomb.Add(FindTile(new TilePoint(item.point.x, item.point.y - 1))); } } if (FindTile(new TilePoint(item.point.x - 1, item.point.y)) != null) { if (!stack.ContainsKey(new TilePoint(item.point.x - 1, item.point.y))) { destroyListBomb.Add(FindTile(new TilePoint(item.point.x - 1, item.point.y))); } } if (FindTile(new TilePoint(item.point.x + 1, item.point.y)) != null) { if (!stack.ContainsKey(new TilePoint(item.point.x + 1, item.point.y))) { destroyListBomb.Add(FindTile(new TilePoint(item.point.x + 1, item.point.y))); } } break; default: break; } } foreach (MatchItem item in destroyListBomb.ToArray()) { if (stack.ContainsKey(item.point)) { destroyListBomb.Remove(item); } } foreach (KeyValuePair <TilePoint, Data.TileTypes> item in stack) { destroyList.Add(FindTile(item.Key)); } if (3 < destroyList.Count) { byte matchCount = 0; byte matchGroup = 0; int inc = 0; for (int baseindex = 0; baseindex < destroyList.Count; baseindex++) { matchCount = 1; if (baseindex != 0) { if (destroyList[baseindex].matchGroup != destroyList[baseindex - 1].matchGroup) { matchGroup++; destroyList[baseindex].matchGroup = matchGroup; } } else { matchGroup++; destroyList[0].matchGroup = matchGroup; } //右方向 inc = 1; bool isMatch = false; for (int x = 0; x < destroyList.Count; x++) { for (int x1 = 0; x1 < destroyList.Count; x1++) { if ((destroyList[baseindex].point.x + inc == destroyList[x1].point.x) && (destroyList[baseindex].point.y == destroyList[x1].point.y) && (destroyList[baseindex].cell.cellType == destroyList[x1].cell.cellType)) { matchCount++; destroyList[x1].matchGroup = matchGroup; isMatch = true; } } if (isMatch) { inc++; isMatch = false; } else { break; } } //左方向 inc = 1; isMatch = false; for (int x = 0; x < destroyList.Count; x++) { for (int x1 = 0; x1 < destroyList.Count; x1++) { if ((destroyList[baseindex].point.x - inc == destroyList[x1].point.x) && (destroyList[baseindex].point.y == destroyList[x1].point.y) && (destroyList[baseindex].cell.cellType == destroyList[x1].cell.cellType)) { matchCount++; destroyList[x1].matchGroup = matchGroup; isMatch = true; } } if (isMatch) { inc++; isMatch = false; } else { break; } } //下方向 inc = 1; isMatch = false; for (int x = 0; x < destroyList.Count; x++) { for (int x1 = 0; x1 < destroyList.Count; x1++) { if ((destroyList[baseindex].point.x == destroyList[x1].point.x) && (destroyList[baseindex].point.y + inc == destroyList[x1].point.y) && (destroyList[baseindex].cell.cellType == destroyList[x1].cell.cellType)) { matchCount++; destroyList[x1].matchGroup = matchGroup; isMatch = true; } } if (isMatch) { inc++; isMatch = false; } else { break; } } //上方向 inc = 1; isMatch = false; for (int x = 0; x < destroyList.Count; x++) { for (int x1 = 0; x1 < destroyList.Count; x1++) { if ((destroyList[baseindex].point.x == destroyList[x1].point.x) && (destroyList[baseindex].point.y - inc == destroyList[x1].point.y) && (destroyList[baseindex].cell.cellType == destroyList[x1].cell.cellType)) { matchCount++; destroyList[x1].matchGroup = matchGroup; isMatch = true; } } if (isMatch) { inc++; isMatch = false; } else { break; } } destroyList[baseindex].matchCount = matchCount; } if (a != null) { foreach (MatchItem item in destroyList) { if (4 <= item.matchCount) { if (item.Equals(a) || item.Equals(b)) { switch (item.matchCount) { case 4: case 5: case 6: case 7: default: if ((int)item.cell.cellType < 6) { item.cell.cellType = item.cell.cellType + 5; bombList.Add(item); } break; } } } } } else { int matchGroupCount = 0; foreach (MatchItem item in destroyList) { if (matchGroupCount <= item.matchGroup) { matchGroupCount = item.matchGroup; } } List <MatchItem>[] matchGroupList = new List <MatchItem> [matchGroupCount]; foreach (MatchItem item in destroyList) { //Debug.Log(item != null); if (matchGroupList[item.matchGroup - 1] == null) { matchGroupList[item.matchGroup - 1] = new List <MatchItem>(); } matchGroupList[item.matchGroup - 1].Add(item); } MatchItem bombTile; foreach (List <MatchItem> matchList in matchGroupList) { bombTile = matchList[0]; foreach (MatchItem item in matchList) { if (bombTile.matchCount < item.matchCount) { bombTile = item; } } if (4 <= bombTile.matchCount) { bombTile.cell.cellType = bombTile.cell.cellType + 5; bombList.Add(bombTile); destroyList.Remove(bombTile); } } } } foreach (MatchItem itembomb in bombList) { foreach (MatchItem item in destroyList.ToArray()) { if (itembomb.Equals(item)) { destroyList.Remove(itembomb); } } } foreach (MatchItem item in destroyListBomb) { int type = (int)item.cell.cellType; item.cell.cellType = Data.TileTypes.Empty; item.GetComponent <SpriteRenderer>().enabled = false; GameObject instance = Instantiate(explosionPrefab) as GameObject; instance.transform.parent = effectArea; instance.transform.localPosition = new Vector3(item.point.x * cellWidth, item.point.y * -cellHeight, -5f); DoStarEffect(instance.transform.localPosition, type); } foreach (MatchItem item in bombList) { Sprite sprite = sprites[(int)item.cell.cellType - 1]; SpriteRenderer renderer = item.GetComponent <SpriteRenderer>(); renderer.sprite = sprite; renderer.enabled = true; } foreach (MatchItem item in destroyList) { int type = (int)item.cell.cellType; audioMatchSource[(int)(item.cell.cellType) - 1].Play(); // if (type <= 5) { item.cell.cellType = Data.TileTypes.Empty; item.GetComponent <SpriteRenderer>().enabled = false; // } GameObject instance = Instantiate(explosionPrefab) as GameObject; instance.transform.parent = effectArea; instance.transform.localPosition = new Vector3(item.point.x * cellWidth, item.point.y * -cellHeight, -5f); DoStarEffect(instance.transform.localPosition, type); // if(6 <= type) { // Sprite sprite = sprites[(int)item.cell.cellType - 1]; // SpriteRenderer renderer = item.GetComponent<SpriteRenderer>(); // renderer.sprite = sprite; // renderer.enabled = true; // } } // StartCoroutine( AttackMonster(0.7f) ); StartCoroutine(FillEmpty(0.5f)); }