// Checks all surroundings of a given hex if there is a match and explode it public bool LookForMatchSingleHex(HexagonScript HexToCheck) { for (int StateRef = 0; StateRef <= 5; StateRef++) { try { bool bIsHighColumn = HexToCheck.Coordinate.x % 2 == 0; Vector2Int[] Possibilities = bIsHighColumn ? HighlightPossibilities_High : HighlightPossibilities_Low; HexagonScript FirstHexNearby = HexReferences[HexToCheck.Coordinate.x + Possibilities[StateRef].x, HexToCheck.Coordinate.y + Possibilities[StateRef].y].GetComponent <HexagonScript>(); HexagonScript SecondHexNearby = HexReferences[HexToCheck.Coordinate.x + Possibilities[StateRef + 1].x, HexToCheck.Coordinate.y + Possibilities[StateRef + 1].y].GetComponent <HexagonScript>(); if (HexToCheck.ColorID == FirstHexNearby.ColorID && HexToCheck.ColorID == SecondHexNearby.ColorID && FirstHexNearby.ColorID == SecondHexNearby.ColorID) { // Prevent checking already checked hexes if (FirstHexNearby == HexToCheck || SecondHexNearby == HexToCheck || FirstHexNearby == SecondHexNearby) { continue; } ExplodeMatch(HexToCheck, FirstHexNearby, SecondHexNearby); return(true); } } catch (System.IndexOutOfRangeException) { continue; } } return(false); }
public bool LookForMatch(HexagonScript SelectedScriptRef, bool bIsRight) { // Check all 6 blocks nearby of the incoming hex parameter for a match if (bIsRight) { return(FilterResult(SelectedScriptRef, 0, 1)); } else { return(FilterResult(SelectedScriptRef, -1, 0)); } }
// Creates a Hex and sets its values according to its position and coordinate public void SpawnHexagon(int c, int r) { GameObject SpawnedHexagon_GO = Instantiate(HexagonRef, HexGridPositions[c, r], Quaternion.identity); HexagonScript HexagonScriptRef = SpawnedHexagon_GO.GetComponent <HexagonScript>(); // Spawn a hex if its greater than the rate and reset it back to 0 if (PlayerRef.BombSpawnTarget >= BombSpawnRate) { PlayerRef.BombSpawnTarget = 0; HexagonScriptRef.Type = 1; } HexagonScriptRef.Coordinate = new Vector2Int(c, r); HexagonScriptRef.CalculatePosition(); HexagonScriptRef.ColorID = Random.Range(0, ColorChoices.Length); SpawnedHexagon_GO.GetComponent <SpriteRenderer>().material.color = ColorChoices[HexagonScriptRef.ColorID]; HexReferences[c, r] = SpawnedHexagon_GO; }
IEnumerator TurnSelectedHexes(float Delay, bool bIsLastTurn, bool bIsRight) { yield return(new WaitForSeconds(Delay)); Vector2Int[] CoordinatesToCheck = new Vector2Int[3]; Vector3[] SelectedHexesPositions = new Vector3[3]; // Getting Coordinates to check for every possible way, which is 3 times - // TODO Might Remove : Not necessary but easier to read for (int i = 0; i <= 2; i++) { try { SelectedHexesPositions[i] = SelectedHexes[i].transform.position; CoordinatesToCheck[i] = SelectedHexes[i].GetComponent <HexagonScript>().Coordinate; } catch (System.NullReferenceException) { } } // Switch Positions of the hexes visually if (bIsRight) { SelectedHexes[0].transform.position = SelectedHexesPositions[1]; SelectedHexes[1].transform.position = SelectedHexesPositions[2]; SelectedHexes[2].transform.position = SelectedHexesPositions[0]; } else { SelectedHexes[0].transform.position = SelectedHexesPositions[2]; SelectedHexes[1].transform.position = SelectedHexesPositions[0]; SelectedHexes[2].transform.position = SelectedHexesPositions[1]; } // Switch Positions of the hexes programmatically SwitchPositions(CoordinatesToCheck[0], CoordinatesToCheck[1], CoordinatesToCheck[2], bIsRight); // Checking every highlighted hex for a color match foreach (var HexToCheck in SelectedHexes) { HexagonScript SelectedScriptRef = HexToCheck.GetComponent <HexagonScript>(); // If a match is found stop the search for the next turn if (LookForMatch(SelectedScriptRef, bIsRight)) { break; } } if (bIsLastTurn) { bIsTurning = false; } }
// Explode the match and give points to player then UnHighlight everyhex public void ExplodeMatch(HexagonScript FirstHex, HexagonScript SecondHex, HexagonScript ThirdHex) { HexReferences[FirstHex.Coordinate.x, FirstHex.Coordinate.y] = null; HexReferences[SecondHex.Coordinate.x, SecondHex.Coordinate.y] = null; HexReferences[ThirdHex.Coordinate.x, ThirdHex.Coordinate.y] = null; Vector2Int[] CoordinateReferences = new Vector2Int[] { FirstHex.Coordinate, SecondHex.Coordinate, ThirdHex.Coordinate }; Destroy(FirstHex.gameObject, .1f); Destroy(SecondHex.gameObject, .2f); Destroy(ThirdHex.gameObject, .3f); PlayerRef.Points += PointsPerExplosion * 3; PlayerRef.BombSpawnTarget += PointsPerExplosion * 3; PlayerRef.UpdatePoint(); StopHighlightingAll(); StartCoroutine(RecalculateCoordinates(.4f)); StartCoroutine(SpawnNewHexagons(.7f)); }
// Used to make it better to read ^^ public bool FilterResult(HexagonScript SelectedScriptRef, int FirstHexDifference, int SecondHexDifference) { for (int StateRef = 0; StateRef <= 5; StateRef++) { try { // Set the Possiblity formula according to if a column is high or not bool bIsHighColumn = SelectedScriptRef.Coordinate.x % 2 == 0; Vector2Int[] Possibilities = bIsHighColumn ? HighlightPossibilities_High : HighlightPossibilities_Low; // Get the first and second hex reference nearby of the incoming hex parameter HexagonScript FirstHexNearby = HexReferences[SelectedScriptRef.Coordinate.x + Possibilities[StateRef + FirstHexDifference].x, SelectedScriptRef.Coordinate.y + Possibilities[StateRef + FirstHexDifference].y].GetComponent <HexagonScript>(); HexagonScript SecondHexNearby = HexReferences[SelectedScriptRef.Coordinate.x + Possibilities[StateRef + SecondHexDifference].x, SelectedScriptRef.Coordinate.y + Possibilities[StateRef + SecondHexDifference].y].GetComponent <HexagonScript>(); // If a match is made by 3 of the same colours if (SelectedScriptRef.ColorID == FirstHexNearby.ColorID && SelectedScriptRef.ColorID == SecondHexNearby.ColorID && FirstHexNearby.ColorID == SecondHexNearby.ColorID) { // Prevent checking already checked hexes if (FirstHexNearby == SelectedScriptRef || SecondHexNearby == SelectedScriptRef || FirstHexNearby == SecondHexNearby) { continue; } bIsTurning = true; PlayerRef.Turn++; PlayerRef.UpdateTurnCount(); DecrementAllBombTimers(); // Destroy the matched hexes and return true so it stops checking for more ExplodeMatch(SelectedScriptRef, FirstHexNearby, SecondHexNearby); return(true); } } // Catch if State is out of array catch (System.IndexOutOfRangeException) { continue; } } // If no match is made return false so it keeps on looping for the next hex return(false); }
// Switch Positions of 3 Selected hexes programmatically public void SwitchPositions(Vector2Int Coord1, Vector2Int Coord2, Vector2Int Coord3, bool bIsRight) { // Get the Script references of hexagons according to their coordinate on the HexReferences Grid HexagonScript Hex0 = HexReferences[Coord1.x, Coord1.y].GetComponent <HexagonScript>(); HexagonScript Hex1 = HexReferences[Coord2.x, Coord2.y].GetComponent <HexagonScript>(); HexagonScript Hex2 = HexReferences[Coord3.x, Coord3.y].GetComponent <HexagonScript>(); if (bIsRight) { // Switch out local Coordinates var TempCoord0 = Hex0.Coordinate; Hex0.Coordinate = Hex1.Coordinate; Hex1.Coordinate = Hex2.Coordinate; Hex2.Coordinate = TempCoord0; // Switch out Positions so it wont be bugged out if nearby walls var TempPos0 = Hex0.Position; Hex0.Position = Hex1.Position; Hex1.Position = Hex2.Position; Hex2.Position = TempPos0; // Switch out States so it wont be buggy if a match is made and its still highlighting // TODO :: Might Remove it after automated switch system var TempState0 = Hex0.State; Hex0.State = Hex1.State; Hex1.State = Hex2.State; Hex2.State = TempState0; // Switch out the references of the objects on the HexReferences grid to match with the newly placed coordinate var TempHex0 = HexReferences[Coord1.x, Coord1.y]; var TempHex1 = HexReferences[Coord2.x, Coord2.y]; HexReferences[Coord1.x, Coord1.y] = HexReferences[Coord3.x, Coord3.y]; HexReferences[Coord2.x, Coord2.y] = TempHex0; HexReferences[Coord3.x, Coord3.y] = TempHex1; // Switch out the current Highlighted Hexes order so it remembers where it was last time // P.S [0] is the clicked hex and when it turns it stays same so we prevent it here SelectedHexes[0] = HexReferences[Coord1.x, Coord1.y]; SelectedHexes[1] = TempHex0; SelectedHexes[2] = TempHex1; } else { // Switch out local Coordinates var TempCoord0 = Hex0.Coordinate; Hex0.Coordinate = Hex2.Coordinate; Hex2.Coordinate = Hex1.Coordinate; Hex1.Coordinate = TempCoord0; // Switch out Positions so it wont be bugged out if nearby walls var TempPos0 = Hex0.Position; Hex0.Position = Hex2.Position; Hex2.Position = Hex1.Position; Hex1.Position = TempPos0; // Switch out States so it wont be buggy if a match is made and its still highlighting // TODO :: Might Remove it after automated switch system var TempState0 = Hex0.State; Hex0.State = Hex2.State; Hex2.State = Hex1.State; Hex1.State = TempState0; // Switch out the references of the objects on the HexReferences grid to match with the newly placed coordinate var TempHex1 = HexReferences[Coord1.x, Coord1.y]; var TempHex2 = HexReferences[Coord2.x, Coord2.y]; var TempHex3 = HexReferences[Coord3.x, Coord3.y]; HexReferences[Coord1.x, Coord1.y] = TempHex2; HexReferences[Coord3.x, Coord3.y] = TempHex1; HexReferences[Coord2.x, Coord2.y] = TempHex3; // Switch out the current Highlighted Hexes order so it remembers where it was last time // P.S [0] is the clicked hex and when it turns it stays same so we prevent it here SelectedHexes[0] = TempHex2; SelectedHexes[2] = TempHex1; SelectedHexes[1] = TempHex3; } for (int k = 0; k <= 2; k++) { SelectedHexes[k].GetComponent <HexagonScript>().ResetStates(); } }