public bool GemsMatch(Gem gem1, Gem gem2, out BackToBackCount gem1Count, out BackToBackCount gem2Count) { gem1Count = backToBackCountOnIndex(gem1.GetIndex(), gem1.gemTypeSO.GemColor); gem2Count = backToBackCountOnIndex(gem2.GetIndex(), gem2.gemTypeSO.GemColor); //Debug.Log("CountG1H: " + gem1Count.horizontalGems.Count + " V: " + gem1Count.verticalGems.Count); //Debug.Log("CountG2H: " + gem2Count.horizontalGems.Count + " V: " + gem2Count.verticalGems.Count); return(gem1Count.HasEnoughToMatch() || gem2Count.HasEnoughToMatch()); }
public void ExplodeGems(BackToBackCount gemCount) { if (gemCount.HasEnoughHorizontalToMatch()) { // TODO - collect type of gems to use as power foreach (Gem gem in gemCount.horizontalGems) { if (gem.gemTypeSO.GemType != GemType.Empty) { Instantiate(gemExplosion, gem.transform.position, gem.transform.rotation); count++; if (color != gem.gemTypeSO.GemColor) { color = gem.gemTypeSO.GemColor; } } GemTypeSO gemTypeSO = new GemTypeSO(); gemTypeSO.GemType = GemType.Empty; gemTypeSO.GemColor = GemColor.Empty; gem.SetType(gemTypeSO); } } if (gemCount.HasEnoughVerticalToMatch()) { // TODO - collect type of gems to use as power foreach (Gem gem in gemCount.verticalGems) { if (gem.gemTypeSO.GemType != GemType.Empty) { Instantiate(gemExplosion, gem.transform.position, gem.transform.rotation); count++; if (color != gem.gemTypeSO.GemColor) { color = gem.gemTypeSO.GemColor; } } GemTypeSO gemTypeSO = new GemTypeSO(); gemTypeSO.GemType = GemType.Empty; gemTypeSO.GemColor = GemColor.Empty; gem.SetType(gemTypeSO); } } if (count > 0) { PlayerPowerups(count, color); count = 0; } }
void FixInitialBoard() { for (int x = 0, y = 0; x < boardNumCells.x; x++, y = 0) { for (; y < boardNumCells.y; y++) { Gem currentGem = gemsTable[x, y]; BackToBackCount cellCount = backToBackCountOnIndex(currentGem.GetIndex(), currentGem.gemTypeSO.GemColor); if (cellCount.HasEnoughToMatch()) { int currentIndex = 0; GemColor[] neighboursColours = new GemColor[4]; if (x != boardNumCells.x - 1) { Vector2Int newIndex = currentGem.GetIndex() + Vector2Int.right; neighboursColours[currentIndex++] = gemsTable[newIndex.x, newIndex.y].gemTypeSO.GemColor; } if (x != 0) { Vector2Int newIndex = currentGem.GetIndex() + Vector2Int.left; neighboursColours[currentIndex++] = gemsTable[newIndex.x, newIndex.y].gemTypeSO.GemColor; } if (y != boardNumCells.y - 1) { Vector2Int newIndex = currentGem.GetIndex() + Vector2Int.up; neighboursColours[currentIndex++] = gemsTable[newIndex.x, newIndex.y].gemTypeSO.GemColor; } if (y != 0) { Vector2Int newIndex = currentGem.GetIndex() + Vector2Int.down; neighboursColours[currentIndex++] = gemsTable[newIndex.x, newIndex.y].gemTypeSO.GemColor; } GemColor gemColor = GemColor.Blue; while (Array.Exists(neighboursColours, color => color == gemColor)) { gemColor++; } currentGem.SetType(gemsInfo.Info[(int)gemColor - 1]); } } } }
public void RecheckBoard() { bool exploded = false; for (int x = 0; x < boardNumCells.x; x++) { for (int y = 0; y < boardNumCells.y; y++) { BackToBackCount count = backToBackCountOnIndex(new Vector2Int(x, y), gemsTable[x, y].gemTypeSO.GemColor); if (count.HasEnoughToMatch()) { ExplodeGems(count); exploded = true; } } } if (exploded) { DropGems(); } }
public BackToBackCount backToBackCountOnIndex(Vector2Int index, GemColor gemColor) { BackToBackCount count = new BackToBackCount(); Gem rootGem = gemsTable[index.x, index.y]; if (rootGem != null) { count.horizontalGems.Add(rootGem); count.verticalGems.Add(rootGem); } // Count left for (int x = index.x - 1; x >= 0; x--) { Gem gem = gemsTable[x, index.y]; if (gem == null || !gem.CanBeMatchedWith(gemColor)) { break; } else { count.horizontalGems.Add(gem); } } // Count right for (int x = index.x + 1; x < boardNumCells.x; x++) { Gem gem = gemsTable[x, index.y]; if (gem == null || !gem.CanBeMatchedWith(gemColor)) { break; } else { count.horizontalGems.Add(gem); } } // Count up for (int y = index.y + 1; y < boardNumCells.y; y++) { Gem gem = gemsTable[index.x, y]; if (gem == null || !gem.CanBeMatchedWith(gemColor)) { break; } else { count.verticalGems.Add(gem); } } // Count down for (int y = index.y - 1; y >= 0; y--) { Gem gem = gemsTable[index.x, y]; if (gem == null || !gem.CanBeMatchedWith(gemColor)) { break; } else { count.verticalGems.Add(gem); } } return(count); }