/// Checks if a shape is next to another one /// either horizontally or vertically public static bool AreVerticalOrHorizontalNeighbors(Shape_4 s1, Shape_4 s2) { return((s1.Column == s2.Column || s1.Row == s2.Row) && Mathf.Abs(s1.Column - s2.Column) <= 1 && Mathf.Abs(s1.Row - s2.Row) <= 1); }
private void CreateBigBonus(Shape_4 hitGoCache) { GameObject BigBonus = Instantiate(Bigbonusprefabs, BottomRight + new Vector2(hitGoCache.Column * CandySize.x, hitGoCache.Row * CandySize.y), Quaternion.identity) as GameObject; shapes[hitGoCache.Row, hitGoCache.Column] = BigBonus; var BigBonusShape = BigBonus.GetComponent <Shape_4>(); BigBonusShape.Assign("BigBomb", hitGoCache.Row, hitGoCache.Column); BigBonusShape.Bigbonus |= BonusType_4.DestroyBothWholeRowColumn; }
/// Swaps properties of the two shapes /// We could do a shallow copy/exchange here, but anyway... public static void SwapColumnRow(Shape_4 a, Shape_4 b) { int temp = a.Row; a.Row = b.Row; b.Row = temp; temp = a.Column; a.Column = b.Column; b.Column = temp; }
/// Checks if the current shape is of the same type as the par bameter public bool IsSameType(Shape_4 otherShape) { if (otherShape == null || !(otherShape is Shape_4)) { throw new ArgumentException("otherShape"); } return(string.Compare(this.Type, (otherShape as Shape_4).Type) == 0); }
/// Creates a new Bonus based on the shape parameter private void CreateBonus(Shape_4 hitGoCache) { GameObject Bonus = Instantiate(GetBonusFromType(hitGoCache.Type), BottomRight + new Vector2(hitGoCache.Column * CandySize.x, hitGoCache.Row * CandySize.y), Quaternion.identity) as GameObject; shapes[hitGoCache.Row, hitGoCache.Column] = Bonus; var BonusShape = Bonus.GetComponent <Shape_4>(); //will have the same type as the "normal" candy BonusShape.Assign(hitGoCache.Type, hitGoCache.Row, hitGoCache.Column); //add the proper Bonus type BonusShape.Bonus |= BonusType_4.DestroyWholeRowColumn; }
/// Swaps the position of two items, also keeping a backup public void Swap(GameObject g1, GameObject g2) { //hold a backup in case no match is produced backupG1 = g1; backupG2 = g2; var g1Shape = g1.GetComponent <Shape_4>(); var g2Shape = g2.GetComponent <Shape_4>(); //get array indexes int g1Row = g1Shape.Row; int g1Column = g1Shape.Column; int g2Row = g2Shape.Row; int g2Column = g2Shape.Column; //swap them in the array var temp = shapes[g1Row, g1Column]; shapes[g1Row, g1Column] = shapes[g2Row, g2Column]; shapes[g2Row, g2Column] = temp; //swap their respective properties Shape_4.SwapColumnRow(g1Shape, g2Shape); }
private IEnumerator FindMatchesAndCollapse(RaycastHit2D hit2) { //get the second item that was part of the swipe var hitGo2 = hit2.collider.gameObject; shapes.Swap(hitGo, hitGo2); //move the swapped ones hitGo.transform.positionTo(ConstantsVariable_4.AnimationDuration, hitGo2.transform.position); hitGo2.transform.positionTo(ConstantsVariable_4.AnimationDuration, hitGo.transform.position); yield return(new WaitForSeconds(ConstantsVariable_4.AnimationDuration)); //get the matches via the helper methods var hitGoMatchesSet = shapes.GetMatches(hitGo); var hitGo2MatchesSet = shapes.GetMatches(hitGo2); var totalMatches = hitGoMatchesSet.MatchedCandy .Union(hitGo2MatchesSet.MatchedCandy).Distinct(); //if user's swap didn't create at least a 3-match, undo their swap if (totalMatches.Count() < ConstantsVariable_4.MinimumMatches) { Audio_Source.PlayOneShot(Sounds[0]); hitGo.transform.positionTo(ConstantsVariable_4.AnimationDuration, hitGo2.transform.position); hitGo2.transform.positionTo(ConstantsVariable_4.AnimationDuration, hitGo.transform.position); yield return(new WaitForSeconds(ConstantsVariable_4.AnimationDuration)); shapes.UndoSwap(); } else { Audio_Source.PlayOneShot(Sounds[1]); } if (totalMatches.Count() > ConstantsVariable_4.MinimumMatches) { Audio_Source.PlayOneShot(Sounds[4]); } //if more than 3 matches and no Bonus is contained in the line, we will award a new Bonus bool addBonus = totalMatches.Count() == ConstantsVariable_4.MinimumMatchesForBonus && !BonusTypeUtilities_4.ContainsDestroyWholeRowColumn(hitGoMatchesSet.BonusesContained) && !BonusTypeUtilities_4.ContainsDestroyWholeRowColumn(hitGo2MatchesSet.BonusesContained); bool addbigbonus = totalMatches.Count() >= ConstantsVariable_4.MinimumMatchesForBigBonus && !BonusTypeUtilities_4.ContainsDestroyWholeRowColumn(hitGoMatchesSet.BonusesContained) && !BonusTypeUtilities_4.ContainsDestroyWholeRowColumn(hitGo2MatchesSet.BonusesContained) && !BonusTypeUtilities_4.ContainsDestroyBothWholeRowColumn(hitGoMatchesSet.BonusesContained) && !BonusTypeUtilities_4.ContainsDestroyBothWholeRowColumn(hitGo2MatchesSet.BonusesContained); Shape_4 hitGoCache = null; if (addBonus) { //get the game object that was of the same type var sameTypeGo = hitGoMatchesSet.MatchedCandy.Count() > 0 ? hitGo : hitGo2; hitGoCache = sameTypeGo.GetComponent <Shape_4>(); } if (addbigbonus) { var allTypeGo = hitGoMatchesSet.MatchedCandy.Count() > 0?hitGo : hitGo2; hitGoCache = allTypeGo.GetComponent <Shape_4>(); } int timesRun = 1; while (totalMatches.Count() >= ConstantsVariable_4.MinimumMatches) { //increase score IncreaseScore((totalMatches.Count() - 2) * ConstantsVariable_4.Match3Score); if (timesRun >= 2) { IncreaseScore(ConstantsVariable_4.SubsequentMatchScore); } foreach (var item in totalMatches) { shapes.Remove(item); RemoveFromScene(item); } //check and instantiate Bonus if needed if (addBonus) { CreateBonus(hitGoCache); } if (addbigbonus) { Debug.Log("Bigbonus"); CreateBigBonus(hitGoCache); } addbigbonus = false; addBonus = false; //get the columns that we had a collapse var columns = totalMatches.Select(go => go.GetComponent <Shape_4>().Column).Distinct(); //the order the 2 methods below get called is important!!! //collapse the ones gone var collapsedCandyInfo = shapes.Collapse(columns); //create new ones var newCandyInfo = CreateNewCandyInSpecificColumns(columns); int maxDistance = Mathf.Max(collapsedCandyInfo.MaxDistance, newCandyInfo.MaxDistance); MoveAndAnimate(newCandyInfo.AlteredCandy, maxDistance); MoveAndAnimate(collapsedCandyInfo.AlteredCandy, maxDistance); //will wait for both of the above animations yield return(new WaitForSeconds(ConstantsVariable_4.MoveAnimationMinDuration * maxDistance)); //search if there are matches with the new/collapsed items totalMatches = shapes.GetMatches(collapsedCandyInfo.AlteredCandy). Union(shapes.GetMatches(newCandyInfo.AlteredCandy)).Distinct(); timesRun++; } state = GameState_4.None; StartCheckForPotentialMatches(); }