// Handle egg pop and fall, once hit grid, we are finding all nearby eggs with same color, and trigger pop and fall if >= 3 are the same public List <Egg> SpreadToAllNearbyEgg(Egg egg, bool isOnlyCheckSame = true) { Stack <Egg> uncheckedEgg = new Stack <Egg>(); // contains all potential eggs that has not been further check SortedSet <Egg> checkedEgg = new SortedSet <Egg>(); // contains all eggs that are checked List <Egg> crackedEgg = new List <Egg>(); // list of eggs that can be cracked (same color with hit) uncheckedEgg.Push(egg); crackedEgg.Add(egg); while (uncheckedEgg.Count > 0) { Egg currentEgg = uncheckedEgg.Pop(); int curRow, curCol; for (int i = 1; i <= 6; i++) // Check 6 eggs next to it { GetPositionFromKey(currentEgg, i, out curRow, out curCol); if (curRow < 0 || curRow > ROWS || curCol < 0 || curCol > COLUMNS) { continue; } Egg eggKey = GetEggInGrid(curRow, curCol); if (eggKey != null) { if (!checkedEgg.Contains(eggKey)) { if (eggKey.GetStringFromColor() != "None") { if (isOnlyCheckSame && eggKey.GetColor() != currentEgg.GetColor()) { continue; } if (!uncheckedEgg.Contains(eggKey)) { uncheckedEgg.Push(eggKey); } if (!crackedEgg.Contains(eggKey)) { crackedEgg.Add(eggKey); } } } } } checkedEgg.Add(currentEgg); } return(crackedEgg); }
// Find closest egg, which takes bullet egg and eggOnGrid to process; Egg GetNewEggPosition(GameObject eggOnGrid, GameObject refBullet) { Vector3 direction = refBullet.transform.position - eggOnGrid.transform.position; // this is a vector from hit egg to bullet Egg eggData = eggOnGrid.GetComponent <Egg>(); float AngleOfVector = Vector3.Angle(Vector3.up, direction); if (direction.x < 0) { AngleOfVector = -AngleOfVector; } int offsetRow = (eggData.GetRow() % 2 == 1) ? 0 : 1; int newCol = -1; int newRow = -1; //Debug.Log(AngleOfVector); int positionKey = GetPositionFromKey(eggData, GetKeyFromAngle(AngleOfVector), out newRow, out newCol); //Debug.Log(eggData.GetRow() +":"+ eggData.GetCol()+ " ;; New egg: "+newRow+":"+newCol); Egg firstCandidate = GetEggInGrid(newRow, newCol); //Debug.Log(firstCandidate.GetStringFromColor()); if (firstCandidate.GetStringFromColor() != "None") // We have to fetch second candidate, as this one is there { //Debug.Log("Failed 1"); int newKey = -1; float minAngleDiff = 181f; for (int key = 1; key <= 6; key++) { if (key != positionKey) { //Debug.Log("Key "+key+" : "+Mathf.Abs(GetAngleCenterByKey(key) - AngleOfVector)); if (Mathf.Abs(GetAngleCenterByKey(key) - AngleOfVector) < minAngleDiff) { newKey = key; minAngleDiff = Mathf.Abs(GetAngleCenterByKey(key) - AngleOfVector); } } } GetPositionFromKey(eggData, newKey, out newRow, out newCol); //Debug.Log("C2: "+eggData.GetRow() +":"+ eggData.GetCol()+ " ;; New egg: "+newRow+":"+newCol); return(GetEggInGrid(newRow, newCol)); } return(firstCandidate); }